OwnerStoreOutFeeReportService.php 7.4 KB

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