OwnerStoreOutFeeReportService.php 6.7 KB

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