OwnerStoreFeeReportService.php 7.1 KB

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