OwnerStoreFeeReportService.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace App\Services;
  3. use App\OwnerBillReport;
  4. use App\OwnerBillReportArchive;
  5. use App\Traits\ServiceAppAop;
  6. use App\OwnerStoreFeeReport;
  7. use Carbon\Carbon;
  8. use Illuminate\Database\Eloquent\Builder;
  9. use Illuminate\Support\Facades\DB;
  10. class OwnerStoreFeeReportService implements \App\Interfaces\SettlementBillReportInterface
  11. {
  12. const TYPE = '入库费-合计';
  13. use ServiceAppAop;
  14. use \App\Traits\SettlementBillServiceTrait;
  15. protected $modelClass = OwnerStoreFeeReport::class;
  16. /** @var $archiveService OwnerBillReportArchiveService */
  17. private $archiveService;
  18. /** @var $detailService OwnerStoreFeeDetailService */
  19. private $detailService;
  20. /**
  21. * 生成报表数据
  22. * 如果参数$counting_month为空 统计上一个月的
  23. * 如果参数$counting_month为2021-01-01 则统计2021-01-01 -- 2021-01-31之间的数据
  24. * @param null $counting_month 统计月份,默认统计上个月的 2021-05-01
  25. */
  26. public function recordReport($counting_month = null)
  27. {
  28. $this->detailService = app('OwnerStoreFeeDetailService');
  29. if (is_null($counting_month)) {
  30. //默认统计上个月的数据
  31. $counting_month = now()->subMonth()->startOfMonth()->toDateString();
  32. }
  33. $this->reportDate = $counting_month;
  34. $start = $this->reportDate;
  35. $end = Carbon::parse($this->reportDate)->endOfMonth()->toDateString();
  36. $details =
  37. DB::table('owner_store_fee_details')
  38. ->leftJoin('owner_fee_details', 'owner_fee_detail_id', '=', 'owner_fee_details.id')
  39. ->selectRaw("DATE_FORMAT(owner_store_fee_details.created_at,'%Y-%m') as counting_month,
  40. unit_id,
  41. unit_price,
  42. sum(amount) as amounts ,
  43. owner_store_fee_details.owner_id,
  44. owner_fee_detail_id,
  45. sum(owner_fee_details.work_fee) as work_fee,
  46. owner_store_fee_details.owner_price_operation_id")
  47. ->whereBetween('owner_store_fee_details.created_at', [$start, $end])
  48. ->groupBy('counting_month', 'owner_store_fee_details.owner_id', 'owner_store_fee_details.owner_price_operation_id', 'unit_id', 'unit_price')
  49. ->get();
  50. $reports = [];
  51. foreach ($details as $detail) {
  52. $counting_month = Carbon::parse($detail->counting_month)->startOfMonth()->toDateString();
  53. $ownerBillReport = OwnerBillReport::query()
  54. ->selectRaw("id")
  55. ->where('owner_id', $detail->owner_id)
  56. ->where('counting_month', $counting_month)->first();
  57. $reports[] = [
  58. 'owner_bill_report_id' => $ownerBillReport->id ?? null,
  59. 'owner_price_operation_id' => $detail->owner_price_operation_id,
  60. 'owner_id' => $detail->owner_id,
  61. 'counting_month' => $counting_month,
  62. 'unit_id' => $detail->unit_id,
  63. 'unit_price' => $detail->unit_price,
  64. 'amount' => $detail->amounts,
  65. 'fee' => $detail->work_fee,
  66. ];
  67. }
  68. $reports_chunked = array_chunk($reports,1000 );
  69. foreach ($reports_chunked as $items) {
  70. OwnerStoreFeeReport::query()->insertOrIgnore($items);
  71. }
  72. }
  73. public function get(array $kvPairs): array
  74. {
  75. $this->archiveService = app('OwnerBillReportArchiveService');
  76. $this->detailService = app('OwnerStoreFeeDetailService');
  77. if ($this->archiveService->isArchived($kvPairs['counting_month'], $kvPairs['owner_id'], $kvPairs['type']) == 1) {
  78. //查询存档数据
  79. $archived = $this->archiveService->get($kvPairs);
  80. $reports = collect($archived->information['reports']);
  81. $totalAmount = $archived->information['totalAmount'];
  82. $totalFee = $archived->information['totalFee'];
  83. $owner_price_operation_fees = collect($archived->information['owner_price_operation_fees']);
  84. } else {
  85. $reports = $this->getSql($kvPairs['owner_id'], $kvPairs['counting_month'])->orderByDesc('owner_price_operation_id')->get();
  86. $totalAmount = $reports->sum('amount');
  87. $totalFee = number_format($reports->sum('fee'),2);
  88. $owner_price_operation_fees = OwnerStoreFeeReport::query()
  89. ->with('ownerPriceOperation:id,name')
  90. ->selectRaw("sum(fee) as fee,owner_price_operation_id")
  91. ->where('owner_id', $kvPairs['owner_id'])
  92. ->where('counting_month', $kvPairs['counting_month'])
  93. ->groupBy('owner_price_operation_id')->get();
  94. }
  95. return array($reports, $totalAmount, $totalFee,$owner_price_operation_fees);
  96. }
  97. /**
  98. * @param $owner_id
  99. * @param $counting_month
  100. * @return Builder
  101. */
  102. public function getSql($owner_id, $counting_month): Builder
  103. {
  104. return OwnerStoreFeeReport::query()
  105. ->with(['unit:id,name'])
  106. ->where('owner_id', $owner_id)
  107. ->where('counting_month', $counting_month);
  108. }
  109. public function switchType($type)
  110. {
  111. // TODO: Implement switchType() method.
  112. }
  113. public function buildExport($details): array
  114. {
  115. // TODO: Implement buildExport() method.
  116. }
  117. public function confirmBill($counting_month, $owner_id)
  118. {
  119. $billReport = OwnerBillReport::query()
  120. ->select('storage_fee', 'id')
  121. ->where('owner_id', $owner_id)
  122. ->where('counting_month', $counting_month)
  123. ->firstOr(function () {
  124. return new OwnerBillReport();
  125. });
  126. list($reports, $totalAmount, $totalFee, $owner_price_operation_fees) = $this->get([
  127. 'owner_id' => $owner_id,
  128. 'counting_month' => $counting_month,
  129. 'type' => $this::TYPE,
  130. ]);
  131. OwnerBillReportArchive::query()->create([
  132. 'owner_bill_report_id' => $billReport->id ?? null,
  133. 'owner_id' => $owner_id,
  134. 'counting_month' => $counting_month,
  135. 'type' => $this::TYPE,
  136. 'archiver_id' => auth()->id(),
  137. 'archived_at' => now(),
  138. 'information' => [
  139. 'reports' => $reports,
  140. 'totalAmount' => $totalAmount,
  141. 'totalFee' => $totalFee,
  142. 'owner_price_operation_fees' => $owner_price_operation_fees
  143. ],
  144. ]);
  145. $this->confirmBillFeeTotal($counting_month, $owner_id);
  146. }
  147. }