OwnerLogisticFeeReportService.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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,province,DATE_FORMAT(created_at,'%Y-%m-%d') as counted_date,initial_weight,initial_weight_price,count(1) as initial_amount,additional_price,additional_weight,sum(additional_weigh_weight) as additional_amount,created_at,owner_id")
  35. ->whereBetween('created_at', [$start, $end])
  36. ->groupBy('initial_weight', 'initial_weight_price', 'additional_price', 'additional_weight', 'logistic_id', 'province', 'counted_date', 'owner_id')
  37. ->get();
  38. $ownerLogisticFeeReportArray = [];
  39. foreach ($ownerLogisticFeeDetails as $ownerLogisticFeeDetail) {
  40. $ownerLogisticFeeReportArray[] = [
  41. 'logistic_id' => $ownerLogisticFeeDetail->logistic_id,
  42. 'province' => $ownerLogisticFeeDetail->province,
  43. 'counted_date' => Carbon::parse($ownerLogisticFeeDetail->counted_date)->firstOfMonth()->toDateString(),
  44. 'initial_weight' => $ownerLogisticFeeDetail->initial_weight,
  45. 'initial_weight_price' => $ownerLogisticFeeDetail->initial_weight_price,
  46. 'initial_amount' => $ownerLogisticFeeDetail->initial_amount,
  47. 'additional_weight' => $ownerLogisticFeeDetail->additional_weight,
  48. 'additional_price' => $ownerLogisticFeeDetail->additional_price,
  49. 'additional_amount' => $ownerLogisticFeeDetail->additional_amount,
  50. 'fee' => ($ownerLogisticFeeDetail['initial_weight_price'] * $ownerLogisticFeeDetail['initial_amount']) + ($ownerLogisticFeeDetail['additional_amount'] * $ownerLogisticFeeDetail['additional_price']),
  51. 'owner_id' => $ownerLogisticFeeDetail->owner_id,
  52. ];
  53. }
  54. OwnerLogisticFeeReport::query()->insertOrIgnore($ownerLogisticFeeReportArray);
  55. }
  56. /**
  57. * 订单统计分页查询
  58. * @param $owner_id
  59. * @param $counting_month string 查询的年月 2021-05-01
  60. * @param $paginateParams
  61. * @return LengthAwarePaginator
  62. */
  63. private function getRecordPagination($owner_id, string $counting_month, $paginateParams): LengthAwarePaginator
  64. {
  65. return $this->getSql($owner_id, $counting_month)
  66. ->paginate($paginateParams['paginate'] ?? 50);
  67. }
  68. public function get(array $kvPairs): array
  69. {
  70. $this->archiveService = app('OwnerBillReportArchiveService');
  71. if ($this->archiveService->isArchived($kvPairs['counting_month'], $kvPairs['owner_id'], $kvPairs['type']) == 1) {
  72. $archived = $this->archiveService->get($kvPairs);
  73. $reports =collect($archived->information['reports']);
  74. $recordTotal = $archived->information['recordTotal'];
  75. } else {
  76. $recordTotal = $this->getRecordTotal($kvPairs['owner_id'], $kvPairs['counting_month']);
  77. $reports = $this->getRecordPagination($kvPairs['owner_id'], $kvPairs['counting_month'], $kvPairs['paginateParams']);
  78. }
  79. return array($reports, $recordTotal);
  80. }
  81. /**
  82. * 订单统计查询
  83. * @param $owner_id
  84. * @param $counting_month string 查询的年月 2021-05-01
  85. * @return Builder[]|Collection
  86. */
  87. public function getRecords($owner_id, string $counting_month)
  88. {
  89. return $this->getSql($owner_id, $counting_month)->get();
  90. }
  91. /**
  92. * 订单总计查询
  93. * @param $owner_id
  94. * @param $counting_month string 查询的年月 2021-05-01
  95. * @return array
  96. */
  97. public function getRecordTotal($owner_id, string $counting_month): array
  98. {
  99. $logistic_fee = OwnerLogisticFeeReport::query()
  100. ->where('owner_id', $owner_id)
  101. ->where('counted_date', $counting_month)
  102. ->sum('fee');
  103. $order_count = (int)OwnerLogisticFeeReport::query()
  104. ->where('owner_id', $owner_id)
  105. ->where('counted_date', $counting_month)
  106. ->sum('initial_amount');
  107. return [
  108. 'logistic_fee' => $logistic_fee,
  109. 'order_count' => $order_count,
  110. ];
  111. }
  112. /**
  113. * @param $owner_id
  114. * @param string $counting_month
  115. * @return Builder
  116. */
  117. public function getSql($owner_id, string $counting_month): Builder
  118. {
  119. return OwnerLogisticFeeReport::query()
  120. ->with('logistic:id,name')
  121. ->where('owner_id', $owner_id)
  122. ->where('counted_date', $counting_month)
  123. ->orderByDesc('logistic_id')
  124. ->orderByDesc('province');
  125. }
  126. }