OwnerLogisticFeeReportService.php 7.7 KB

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