OwnerLogisticFeeReportService.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace App\Services;
  3. use App\OwnerLogisticFeeDetail;
  4. use App\Traits\ServiceAppAop;
  5. use App\OwnerLogisticFeeReport;
  6. use Carbon\Carbon;
  7. use Illuminate\Contracts\Pagination\LengthAwarePaginator;
  8. use Illuminate\Database\Eloquent\Builder;
  9. use Illuminate\Database\Eloquent\Collection;
  10. class OwnerLogisticFeeReportService
  11. {
  12. const TYPE = '快递费-合计';
  13. use ServiceAppAop;
  14. protected $modelClass = OwnerLogisticFeeReport::class;
  15. private $reportDate;
  16. /** @var $archiveService OwnerBillReportArchiveService */
  17. private $archiveService;
  18. /**
  19. * 生成报表数据
  20. * 如果参数$counting_month为空 统计上一个月的
  21. * 如果参数$counting_month为2021-01-01 则统计2021-01-01 -- 2021-01-31之间的数据
  22. * @param null $counting_month 统计月份,默认统计上个月的 2021-05-01
  23. */
  24. public function recordReport($counting_month = null)
  25. {
  26. if (is_null($counting_month)) {
  27. //默认统计上个月的数据
  28. $counting_month = now()->subMonth()->startOfMonth()->toDateTimeString();
  29. }
  30. $this->reportDate = $counting_month;
  31. $start = $this->reportDate;
  32. $end = Carbon::parse($this->reportDate)->endOfMonth()->toDateTimeString();
  33. $ownerLogisticFeeDetails = OwnerLogisticFeeDetail::query()
  34. ->selectRaw("logistic_id,
  35. province,
  36. DATE_FORMAT(created_at,
  37. '%Y-%m') as counted_date,
  38. initial_weight,
  39. initial_weight_price,
  40. count(1) as initial_amount,
  41. additional_price,
  42. additional_weight,
  43. sum(additional_weigh_weight) as additional_amount,
  44. sum(tax_fee) as tax_fee,
  45. created_at,
  46. owner_id")
  47. ->whereBetween('created_at', [$start, $end])
  48. ->groupBy('initial_weight', 'initial_weight_price', 'additional_price', 'additional_weight', 'logistic_id', 'province', 'counted_date', 'owner_id')
  49. ->get();
  50. $ownerLogisticFeeReportArray = [];
  51. foreach ($ownerLogisticFeeDetails as $ownerLogisticFeeDetail) {
  52. $ownerLogisticFeeReportArray[] = [
  53. 'logistic_id' => $ownerLogisticFeeDetail->logistic_id,
  54. 'province' => $ownerLogisticFeeDetail->province,
  55. 'counted_date' => Carbon::parse($ownerLogisticFeeDetail->counted_date)->firstOfMonth()->toDateString(),
  56. 'initial_weight' => $ownerLogisticFeeDetail->initial_weight,
  57. 'initial_weight_price' => $ownerLogisticFeeDetail->initial_weight_price,
  58. 'initial_amount' => $ownerLogisticFeeDetail->initial_amount,
  59. 'additional_weight' => $ownerLogisticFeeDetail->additional_weight,
  60. 'additional_price' => $ownerLogisticFeeDetail->additional_price,
  61. 'additional_amount' => $ownerLogisticFeeDetail->additional_amount,
  62. 'fee' => ($ownerLogisticFeeDetail['initial_weight_price'] * $ownerLogisticFeeDetail['initial_amount']) + ($ownerLogisticFeeDetail['additional_amount'] * $ownerLogisticFeeDetail['additional_price']),
  63. 'tax_fee' => $ownerLogisticFeeDetail->tax_fee,
  64. 'owner_id' => $ownerLogisticFeeDetail->owner_id,
  65. ];
  66. }
  67. OwnerLogisticFeeReport::query()->insertOrIgnore($ownerLogisticFeeReportArray);
  68. }
  69. /**
  70. * 订单统计分页查询
  71. * @param $owner_id
  72. * @param $counting_month string 查询的年月 2021-05-01
  73. * @param $paginateParams
  74. * @return LengthAwarePaginator
  75. */
  76. private function getPagination($owner_id, string $counting_month, $paginateParams): LengthAwarePaginator
  77. {
  78. return $this->getSql($owner_id, $counting_month)
  79. ->paginate($paginateParams['paginate'] ?? 50);
  80. }
  81. public function get(array $kvPairs): array
  82. {
  83. $this->archiveService = app('OwnerBillReportArchiveService');
  84. if ($this->archiveService->isArchived($kvPairs['counting_month'], $kvPairs['owner_id'], $kvPairs['type']) == 1) {
  85. //查询存档数据
  86. $archived = $this->archiveService->get($kvPairs);
  87. $reports = collect($archived->information['reports']);
  88. $recordTotal = $archived->information['recordTotal'];
  89. } else {
  90. $recordTotal = $this->getRecordTotal($kvPairs['owner_id'], $kvPairs['counting_month']);
  91. $reports = $this->getPagination($kvPairs['owner_id'], $kvPairs['counting_month'], $kvPairs['paginateParams']);
  92. }
  93. return array($reports, $recordTotal);
  94. }
  95. /**
  96. * 订单统计查询
  97. * @param $owner_id
  98. * @param $counting_month string 查询的年月 2021-05-01
  99. * @return Builder[]|Collection
  100. */
  101. public function getRecords($owner_id, string $counting_month)
  102. {
  103. return $this->getSql($owner_id, $counting_month)->get();
  104. }
  105. /**
  106. * 订单总计查询
  107. * @param $owner_id
  108. * @param $counting_month string 查询的年月 2021-05-01
  109. * @return array
  110. */
  111. public function getRecordTotal($owner_id, string $counting_month): array
  112. {
  113. $logistic_fee = OwnerLogisticFeeReport::query()
  114. ->where('owner_id', $owner_id)
  115. ->where('counted_date', $counting_month)
  116. ->sum('fee');
  117. $order_count = (int)OwnerLogisticFeeReport::query()
  118. ->where('owner_id', $owner_id)
  119. ->where('counted_date', $counting_month)
  120. ->sum('initial_amount');
  121. return [
  122. 'logistic_fee' => $logistic_fee,
  123. 'order_count' => $order_count,
  124. ];
  125. }
  126. /**
  127. * @param $owner_id
  128. * @param string $counting_month
  129. * @return Builder
  130. */
  131. public function getSql($owner_id, string $counting_month): Builder
  132. {
  133. return OwnerLogisticFeeReport::query()
  134. ->with('logistic:id,name')
  135. ->where('owner_id', $owner_id)
  136. ->where('counted_date', $counting_month)
  137. ->orderByDesc('logistic_id')
  138. ->orderByDesc('province');
  139. }
  140. /**
  141. *获取总金额和税费
  142. */
  143. public function getTotalFee($owner_id, $counting_month)
  144. {
  145. return OwnerLogisticFeeReport::query()
  146. ->selectRaw("sum(fee) as fee,
  147. sum(tax_fee) as tax_fee")
  148. ->where('owner_id', $owner_id)
  149. ->where('counted_date', $counting_month)
  150. ->first();
  151. }
  152. }