OwnerStoreFeeReportService.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. ->selectRaw("
  39. DATE_FORMAT(owner_store_fee_details.created_at,'%Y-%m') as counting_month,
  40. unit_id,
  41. unit_price,
  42. work_name,
  43. sum(amount) as amounts ,
  44. owner_store_fee_details.owner_id,
  45. owner_fee_detail_id,
  46. sum(owner_store_fee_details.fee) as fee
  47. ")
  48. ->whereBetween('owner_store_fee_details.created_at', [$start, $end])
  49. ->groupBy(
  50. 'counting_month',
  51. 'owner_id',
  52. 'unit_id',
  53. 'unit_price',
  54. 'work_name'
  55. )
  56. ->get();
  57. $reports = [];
  58. foreach ($details as $detail) {
  59. $counting_month = Carbon::parse($detail->counting_month)->startOfMonth()->toDateString();
  60. $ownerBillReport = OwnerBillReport::query()
  61. ->selectRaw("id")
  62. ->where('owner_id', $detail->owner_id)
  63. ->where('counting_month', $counting_month)->first();
  64. $reports[] = [
  65. 'owner_bill_report_id' => $ownerBillReport->id ?? null,
  66. 'owner_id' => $detail->owner_id,
  67. 'counting_month' => $counting_month,
  68. 'unit_id' => $detail->unit_id,
  69. 'unit_price' => $detail->unit_price,
  70. 'amount' => $detail->amounts,
  71. 'fee' => $detail->fee,
  72. 'work_name' => $detail->work_name,
  73. ];
  74. }
  75. $reports_chunked = array_chunk($reports, 1000);
  76. //保证幂等性 插入前删除该月的统计数据
  77. OwnerStoreFeeReport::query()->where('counting_month', $counting_month)->delete();
  78. foreach ($reports_chunked as $items) {
  79. OwnerStoreFeeReport::query()->insertOrIgnore($items);
  80. }
  81. }
  82. public function get(array $kvPairs): array
  83. {
  84. $this->archiveService = app('OwnerBillReportArchiveService');
  85. $this->detailService = app('OwnerStoreFeeDetailService');
  86. if ($this->archiveService->isArchived($kvPairs['counting_month'], $kvPairs['owner_id'], $kvPairs['type']) == 1) {
  87. //查询存档数据
  88. $archived = $this->archiveService->get($kvPairs);
  89. $reports = collect($archived->information['reports']);
  90. $totalAmount = $archived->information['totalAmount'];
  91. $totalFee = $archived->information['totalFee'];
  92. $owner_price_operation_fees = collect($archived->information['owner_price_operation_fees']);
  93. } else {
  94. $reports = $this->getSql($kvPairs['owner_id'], $kvPairs['counting_month'])->get();
  95. $totalAmount = $reports->sum('amount');
  96. $totalFee = number_format($reports->sum('fee'), 2);
  97. $owner_price_operation_fees = OwnerStoreFeeReport::query()
  98. ->selectRaw("sum(fee) as fee,work_name")
  99. ->where('owner_id', $kvPairs['owner_id'])
  100. ->where('counting_month', $kvPairs['counting_month'])
  101. ->groupBy('work_name')->get();
  102. }
  103. return array($reports, $totalAmount, $totalFee, $owner_price_operation_fees);
  104. }
  105. /**
  106. * @param $owner_id
  107. * @param $counting_month
  108. * @return Builder
  109. */
  110. public function getSql($owner_id, $counting_month): Builder
  111. {
  112. return OwnerStoreFeeReport::query()
  113. ->with(['unit:id,name'])
  114. ->where('owner_id', $owner_id)
  115. ->where('counting_month', $counting_month);
  116. }
  117. public function switchType($type)
  118. {
  119. // TODO: Implement switchType() method.
  120. }
  121. public function buildExport($details): array
  122. {
  123. // TODO: Implement buildExport() method.
  124. }
  125. public function confirmBill($counting_month, $owner_id)
  126. {
  127. $billReport = OwnerBillReport::query()
  128. ->select('storage_fee', 'id')
  129. ->where('owner_id', $owner_id)
  130. ->where('counting_month', $counting_month)
  131. ->firstOr(function () {
  132. return new OwnerBillReport();
  133. });
  134. list($reports, $totalAmount, $totalFee, $owner_price_operation_fees) = $this->get([
  135. 'owner_id' => $owner_id,
  136. 'counting_month' => $counting_month,
  137. 'type' => $this::TYPE,
  138. ]);
  139. OwnerBillReportArchive::query()->create([
  140. 'owner_bill_report_id' => $billReport->id ?? null,
  141. 'owner_id' => $owner_id,
  142. 'counting_month' => $counting_month,
  143. 'type' => $this::TYPE,
  144. 'archiver_id' => auth()->id(),
  145. 'archived_at' => now(),
  146. 'information' => [
  147. 'reports' => $reports,
  148. 'totalAmount' => $totalAmount,
  149. 'totalFee' => $totalFee,
  150. 'owner_price_operation_fees' => $owner_price_operation_fees
  151. ],
  152. ]);
  153. $this->confirmBillFeeTotal($counting_month, $owner_id);
  154. }
  155. }