OwnerLogisticFeeReportService.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. sum(fee) as fee,
  82. created_at,
  83. owner_id")
  84. ->whereBetween('created_at', [$start, $end])
  85. ->groupBy('initial_weight', 'initial_weight_price', 'additional_price', 'additional_weight', 'logistic_id', 'province', 'counted_date', 'owner_id')
  86. ->get();
  87. $ownerLogisticFeeReportArray = [];
  88. foreach ($ownerLogisticFeeDetails as $ownerLogisticFeeDetail) {
  89. $ownerLogisticFeeReportArray[] = [
  90. 'logistic_id' => $ownerLogisticFeeDetail->logistic_id,
  91. 'province' => $ownerLogisticFeeDetail->province,
  92. 'counted_date' => Carbon::parse($ownerLogisticFeeDetail->counted_date)->firstOfMonth()->toDateString(),
  93. 'initial_weight' => $ownerLogisticFeeDetail->initial_weight,
  94. 'initial_weight_price' => $ownerLogisticFeeDetail->initial_weight_price,
  95. 'initial_amount' => $ownerLogisticFeeDetail->initial_amount,
  96. 'additional_weight' => $ownerLogisticFeeDetail->additional_weight,
  97. 'additional_price' => $ownerLogisticFeeDetail->additional_price,
  98. 'additional_amount' => $ownerLogisticFeeDetail->additional_amount,
  99. 'fee' => $ownerLogisticFeeDetail->fee,
  100. 'tax_fee' => $ownerLogisticFeeDetail->tax_fee,
  101. 'owner_id' => $ownerLogisticFeeDetail->owner_id,
  102. ];
  103. }
  104. //保证接口幂等性 删除统计月的数据
  105. OwnerLogisticFeeReport::query()->where('counted_date', $counting_month)->delete();
  106. OwnerLogisticFeeReport::query()->insertOrIgnore($ownerLogisticFeeReportArray);
  107. }
  108. /**
  109. * 订单统计分页查询
  110. * @param $owner_id
  111. * @param $counting_month string 查询的年月 2021-05-01
  112. * @param $paginateParams
  113. * @return LengthAwarePaginator
  114. */
  115. private function getPagination($owner_id, string $counting_month, $paginateParams): LengthAwarePaginator
  116. {
  117. return $this->getSql($owner_id, $counting_month)
  118. ->paginate($paginateParams['paginate'] ?? 50);
  119. }
  120. public function get(array $kvPairs): array
  121. {
  122. $this->archiveService = app('OwnerBillReportArchiveService');
  123. if ($this->archiveService->isArchived($kvPairs['counting_month'], $kvPairs['owner_id'], $kvPairs['type']) == 1) {
  124. //查询存档数据
  125. $archived = $this->archiveService->get($kvPairs);
  126. $reports = collect($archived->information['reports']);
  127. $recordTotal = $archived->information['recordTotal'];
  128. } else {
  129. $recordTotal = $this->getRecordTotal($kvPairs['owner_id'], $kvPairs['counting_month']);
  130. $reports = $this->getPagination($kvPairs['owner_id'], $kvPairs['counting_month'], $kvPairs['paginateParams']);
  131. }
  132. return array($reports, $recordTotal);
  133. }
  134. /**
  135. * 订单统计查询
  136. * @param $owner_id
  137. * @param $counting_month string 查询的年月 2021-05-01
  138. * @return Builder[]|Collection
  139. */
  140. public function getRecords($owner_id, string $counting_month)
  141. {
  142. return $this->getSql($owner_id, $counting_month)->get();
  143. }
  144. /**
  145. * 订单总计查询
  146. * @param $owner_id
  147. * @param $counting_month string 查询的年月 2021-05-01
  148. * @return array
  149. */
  150. public function getRecordTotal($owner_id, string $counting_month): array
  151. {
  152. $logistic_fee = OwnerLogisticFeeReport::query()
  153. ->where('owner_id', $owner_id)
  154. ->where('counted_date', $counting_month)
  155. ->sum('fee');
  156. $order_count = (int)OwnerLogisticFeeReport::query()
  157. ->where('owner_id', $owner_id)
  158. ->where('counted_date', $counting_month)
  159. ->sum('initial_amount');
  160. return [
  161. 'logistic_fee' => $logistic_fee,
  162. 'order_count' => $order_count,
  163. ];
  164. }
  165. /**
  166. * @param $owner_id
  167. * @param $counting_month
  168. * @return Builder
  169. */
  170. public function getSql($owner_id, $counting_month): Builder
  171. {
  172. return OwnerLogisticFeeReport::query()
  173. ->with('logistic:id,name')
  174. ->where('owner_id', $owner_id)
  175. ->where('counted_date', $counting_month)
  176. ->orderByDesc('logistic_id')
  177. ->orderByDesc('province');
  178. }
  179. /**
  180. *获取总金额和税费
  181. */
  182. public function getTotalFee($owner_id, $counting_month)
  183. {
  184. return OwnerLogisticFeeReport::query()
  185. ->selectRaw("sum(fee) as fee,
  186. sum(tax_fee) as tax_fee")
  187. ->where('owner_id', $owner_id)
  188. ->where('counted_date', $counting_month)
  189. ->first();
  190. }
  191. }