OwnerStoreOutFeeReportService.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace App\Services;
  3. use App\Interfaces\SettlementBillReportInterface;
  4. use App\OwnerBillReport;
  5. use App\OwnerStoreFeeReport;
  6. use App\Traits\ServiceAppAop;
  7. use App\OwnerStoreOutFeeReport;
  8. use Carbon\Carbon;
  9. use Illuminate\Database\Eloquent\Builder;
  10. use Illuminate\Support\Facades\DB;
  11. class OwnerStoreOutFeeReportService implements SettlementBillReportInterface
  12. {
  13. const TYPE = '出库费-合计';
  14. use ServiceAppAop;
  15. protected $modelClass = OwnerStoreOutFeeReport::class;
  16. /** @var $detailService OwnerBillReportArchiveService */
  17. private $archiveService;
  18. /** @var $detailService OwnerStoreOutFeeDetailService */
  19. private $detailService;
  20. public function recordReport($counting_month = null)
  21. {
  22. $this->detailService = app('OwnerStoreOutFeeDetailService');
  23. if (is_null($counting_month)) {
  24. //默认统计上个月的数据
  25. $counting_month = now()->subMonth()->startOfMonth()->toDateString();
  26. }
  27. $this->reportDate = $counting_month;
  28. $start = $this->reportDate;
  29. $end = Carbon::parse($this->reportDate)->endOfMonth()->toDateString();
  30. $details =
  31. DB::table('owner_store_out_fee_details')
  32. ->leftJoin('owner_fee_details', 'owner_store_out_fee_details.owner_fee_detail_id', '=', 'owner_fee_details.id')
  33. ->leftJoin('owner_price_operations', 'owner_store_out_fee_details.owner_price_operation_id', '=', 'owner_price_operations.id')
  34. ->selectRaw("
  35. DATE_FORMAT(owner_store_out_fee_details.created_at,'%Y-%m') as counting_month,
  36. owner_store_out_fee_details.unit_id,
  37. owner_store_out_fee_details.unit_price,
  38. sum(owner_store_out_fee_details.amount) as amounts ,
  39. owner_store_out_fee_details.owner_id,
  40. owner_store_out_fee_details.owner_fee_detail_id,
  41. owner_store_out_fee_details.owner_price_operation_id,
  42. owner_store_out_fee_details.step,
  43. sum(owner_fee_details.work_fee) as work_fee,
  44. owner_price_operations.name
  45. ")
  46. ->whereBetween('owner_store_out_fee_details.created_at', [$start, $end])
  47. ->groupBy('counting_month',
  48. 'owner_store_out_fee_details.owner_id',
  49. 'owner_store_out_fee_details.step',
  50. 'owner_price_operations.id'
  51. )
  52. ->get();
  53. $reports = [];
  54. foreach ($details as $detail) {
  55. $counting_month = Carbon::parse($detail->counting_month)->startOfMonth()->toDateString();
  56. $ownerBillReport = OwnerBillReport::query()
  57. ->selectRaw("id")
  58. ->where('owner_id', $detail->owner_id)
  59. ->where('counting_month', $counting_month)->first();
  60. $reports[] = [
  61. 'owner_bill_report_id' => $ownerBillReport->id ?? null,
  62. 'owner_price_operation_id' => $detail->owner_price_operation_id,
  63. 'owner_id' => $detail->owner_id,
  64. 'counting_month' => $counting_month,
  65. 'step' => $detail->step,
  66. 'unit_id' => $detail->unit_id,
  67. 'unit_price' => $detail->unit_price,
  68. 'amount' => $detail->amounts,
  69. 'fee' => $detail->work_fee,
  70. ];
  71. }
  72. foreach (array_chunk($reports, 1000) as $reports_chunked) {
  73. OwnerStoreOutFeeReport::query()->insertOrIgnore($reports_chunked);
  74. }
  75. }
  76. public function getSql($owner_id, $counting_month): Builder
  77. {
  78. // TODO: Implement getSql() method.
  79. }
  80. public function buildExport($details): array
  81. {
  82. // TODO: Implement buildExport() method.
  83. }
  84. public function switchType($type)
  85. {
  86. // TODO: Implement switchType() method.
  87. }
  88. function get(array $kvPairs)
  89. {
  90. $this->archiveService = app('OwnerBillReportArchiveService');
  91. if ($this->archiveService->isArchived($kvPairs['counting_month'], $kvPairs['owner_id'], $kvPairs['type']) == 1) {
  92. //查询存档数据
  93. $archived = $this->archiveService->get($kvPairs);
  94. $reports = collect($archived->information['reports']);
  95. $work_name_fee_total = collect($archived->information['work_name_fee_total']);
  96. $fee_total = $archived->information['fee_total'];
  97. } else {
  98. $reports = OwnerStoreOutFeeReport::query()
  99. ->with(['unit:id,name'])
  100. ->where('owner_id', $kvPairs['owner_id'])
  101. ->where('counting_month', $kvPairs['counting_month'])
  102. ->orderBy('owner_price_operation_id')
  103. ->get();
  104. $work_name_fee_total = OwnerStoreOutFeeReport::query()
  105. ->leftJoin('owner_price_operations', 'owner_store_out_fee_reports.owner_price_operation_id', '=', 'owner_price_operations.id')
  106. ->selectRaw("
  107. sum(owner_store_out_fee_reports.fee) as fee,
  108. owner_price_operations.name,
  109. owner_price_operations.id
  110. ")
  111. ->where('owner_id', $kvPairs['owner_id'])
  112. ->where('counting_month', $kvPairs['counting_month'])
  113. ->groupBy('owner_price_operations.name')
  114. ->get();
  115. $fee_total = OwnerStoreOutFeeReport::query()
  116. ->where('owner_id', $kvPairs['owner_id'])
  117. ->where('counting_month', $kvPairs['counting_month'])
  118. ->sum('fee');
  119. }
  120. return array($reports, $work_name_fee_total, $fee_total);
  121. }
  122. }