OwnerStoreOutFeeReportService.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace App\Services;
  3. use App\Interfaces\SettlementBillReportInterface;
  4. use App\OwnerBillReport;
  5. use App\OwnerBillReportArchive;
  6. use App\OwnerFeeDetail;
  7. use App\OwnerStoreFeeReport;
  8. use App\OwnerStoreOutFeeDetail;
  9. use App\Traits\ServiceAppAop;
  10. use App\OwnerStoreOutFeeReport;
  11. use Carbon\Carbon;
  12. use Illuminate\Database\Eloquent\Builder;
  13. use Illuminate\Support\Facades\DB;
  14. class OwnerStoreOutFeeReportService implements SettlementBillReportInterface
  15. {
  16. const TYPE = '出库费-合计';
  17. use ServiceAppAop;
  18. use \App\Traits\SettlementBillServiceTrait;
  19. protected $modelClass = OwnerStoreOutFeeReport::class;
  20. /** @var $detailService OwnerBillReportArchiveService */
  21. private $archiveService;
  22. /** @var $detailService OwnerStoreOutFeeDetailService */
  23. private $detailService;
  24. public function recordReport($counting_month = null)
  25. {
  26. $this->detailService = app('OwnerStoreOutFeeDetailService');
  27. if (is_null($counting_month)) {
  28. //默认统计上个月的数据
  29. $counting_month = now()->subMonth()->startOfMonth()->toDateString();
  30. }
  31. $this->reportDate = $counting_month;
  32. $start = $this->reportDate;
  33. $end = Carbon::parse($this->reportDate)->endOfMonth()->toDateString();
  34. $details = OwnerStoreOutFeeDetail::query()
  35. ->selectRaw("
  36. DATE_FORMAT(owner_store_out_fee_details.created_at,'%Y-%m') as counting_month,
  37. step,
  38. unit_id,
  39. unit_price,
  40. sum(amount) as amount ,
  41. sum(fee) as fee ,
  42. owner_id,
  43. work_name
  44. ")
  45. ->whereBetween('created_at', [$start, $end])
  46. ->groupBy(
  47. 'counting_month',
  48. 'step',
  49. 'unit_id',
  50. 'unit_price',
  51. 'owner_id',
  52. 'work_name'
  53. )->get();
  54. $reports = [];
  55. foreach ($details as $detail) {
  56. $counting_month = Carbon::parse($detail->counting_month)->startOfMonth()->toDateString();
  57. $ownerBillReport = OwnerBillReport::query()
  58. ->selectRaw("id")
  59. ->where('owner_id', $detail->owner_id)
  60. ->where('counting_month', $counting_month)->first();
  61. $reports[] = [
  62. 'owner_bill_report_id' => $ownerBillReport->id ?? null,
  63. 'owner_id' => $detail->owner_id,
  64. 'counting_month' => $counting_month,
  65. 'step' => $detail->step,
  66. 'unit_id' => $detail->unit_id,
  67. 'unit_price' => $detail->unit_price,
  68. 'amount' => $detail->amount,
  69. 'fee' => $detail->fee,
  70. 'work_name' => $detail->work_name,
  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. ->get();
  107. $work_name_fee_total = OwnerStoreOutFeeReport::query()
  108. ->selectRaw("
  109. sum(fee) as fee,
  110. work_name
  111. ")
  112. ->where('counting_month' , $kvPairs['counting_month'])
  113. ->where('owner_id', $kvPairs['owner_id'])
  114. ->groupBy('work_name')
  115. ->get();
  116. $fee_total = $work_name_fee_total->sum('fee');
  117. }
  118. return array($reports, $work_name_fee_total, $fee_total);
  119. }
  120. public function confirmBill($counting_month, $owner_id)
  121. {
  122. $billReport = OwnerBillReport::query()
  123. ->select('storage_fee', 'id')
  124. ->where('owner_id', $owner_id)
  125. ->where('counting_month', $counting_month)
  126. ->firstOr(function () {
  127. return new OwnerBillReport();
  128. });
  129. list($reports, $work_name_fee_total, $fee_total) = $this->get([
  130. 'owner_id' => $owner_id,
  131. 'counting_month' => $counting_month,
  132. 'type' => $this::TYPE,
  133. ]);
  134. OwnerBillReportArchive::query()->create([
  135. 'owner_bill_report_id' => $billReport->id ?? null,
  136. 'owner_id' => $owner_id,
  137. 'counting_month' => $counting_month,
  138. 'type' => $this::TYPE,
  139. 'archiver_id' => auth()->id(),
  140. 'archived_at' => now(),
  141. 'information' => [
  142. 'reports' => $reports,
  143. 'work_name_fee_total' => $work_name_fee_total,
  144. 'fee_total' => $fee_total,
  145. ],
  146. ]);
  147. $this->confirmBillFeeTotal($counting_month, $owner_id);
  148. }
  149. }