OwnerLogisticFeeReportService.php 7.7 KB

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