OwnerStoreOutFeeReportService.php 6.8 KB

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