OwnerStoreOutFeeReportService.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. sum(owner_fee_details.work_fee) as work_fee,
  46. owner_price_operations.name
  47. ")
  48. ->whereBetween('owner_store_out_fee_details.created_at', [$start, $end])
  49. ->groupBy('counting_month',
  50. 'owner_store_out_fee_details.owner_id',
  51. 'owner_store_out_fee_details.step',
  52. 'owner_price_operations.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. 'fee' => $detail->work_fee,
  72. ];
  73. }
  74. foreach (array_chunk($reports, 1000) as $reports_chunked) {
  75. OwnerStoreOutFeeReport::query()->insertOrIgnore($reports_chunked);
  76. }
  77. }
  78. public function getSql($owner_id, $counting_month): Builder
  79. {
  80. // TODO: Implement getSql() method.
  81. }
  82. public function buildExport($details): array
  83. {
  84. // TODO: Implement buildExport() method.
  85. }
  86. public function switchType($type)
  87. {
  88. // TODO: Implement switchType() method.
  89. }
  90. function get(array $kvPairs)
  91. {
  92. $this->archiveService = app('OwnerBillReportArchiveService');
  93. if ($this->archiveService->isArchived($kvPairs['counting_month'], $kvPairs['owner_id'], $kvPairs['type']) == 1) {
  94. //查询存档数据
  95. $archived = $this->archiveService->get($kvPairs);
  96. $reports = collect($archived->information['reports']);
  97. $work_name_fee_total = collect($archived->information['work_name_fee_total']);
  98. $fee_total = $archived->information['fee_total'];
  99. } else {
  100. $reports = OwnerStoreOutFeeReport::query()
  101. ->with(['unit:id,name'])
  102. ->where('owner_id', $kvPairs['owner_id'])
  103. ->where('counting_month', $kvPairs['counting_month'])
  104. ->orderBy('owner_price_operation_id')
  105. ->get();
  106. $work_name_fee_total = OwnerStoreOutFeeReport::query()
  107. ->leftJoin('owner_price_operations', 'owner_store_out_fee_reports.owner_price_operation_id', '=', 'owner_price_operations.id')
  108. ->selectRaw("
  109. sum(owner_store_out_fee_reports.fee) as fee,
  110. owner_price_operations.name,
  111. owner_price_operations.id
  112. ")
  113. ->where('owner_id', $kvPairs['owner_id'])
  114. ->where('counting_month', $kvPairs['counting_month'])
  115. ->groupBy('owner_price_operations.name')
  116. ->get();
  117. $fee_total = OwnerStoreOutFeeReport::query()
  118. ->where('owner_id', $kvPairs['owner_id'])
  119. ->where('counting_month', $kvPairs['counting_month'])
  120. ->sum('fee');
  121. }
  122. return array($reports, $work_name_fee_total, $fee_total);
  123. }
  124. public function confirmBill($counting_month, $owner_id)
  125. {
  126. $billReport = OwnerBillReport::query()
  127. ->select('storage_fee', 'id')
  128. ->where('owner_id', $owner_id)
  129. ->where('counting_month', $counting_month)
  130. ->firstOr(function () {
  131. return new OwnerBillReport();
  132. });
  133. list($reports, $work_name_fee_total, $fee_total) = $this->get([
  134. 'owner_id' => $owner_id,
  135. 'counting_month' => $counting_month,
  136. 'type' => $this::TYPE,
  137. ]);
  138. OwnerBillReportArchive::query()->create([
  139. 'owner_bill_report_id' => $billReport->id ?? null,
  140. 'owner_id' => $owner_id,
  141. 'counting_month' => $counting_month,
  142. 'type' => $this::TYPE,
  143. 'archiver_id' => auth()->id(),
  144. 'archived_at' => now(),
  145. 'information' => [
  146. 'reports' => $reports,
  147. 'work_name_fee_total' => $work_name_fee_total,
  148. 'fee_total' => $fee_total,
  149. ],
  150. ]);
  151. $this->confirmBillFeeTotal($counting_month, $owner_id);
  152. }
  153. }