OwnerLogisticFeeReportService.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. class OwnerLogisticFeeReportService
  9. {
  10. use ServiceAppAop;
  11. protected $modelClass = OwnerLogisticFeeReport::class;
  12. private $reportDate;
  13. /**
  14. * 生成报表数据
  15. * 如果参数$date为空 统计上一个月的
  16. * 如果参数$date为2021-01-01 则统计2021-01-01 -- 2021-01-31之间的数据
  17. * @param null $date 统计月份,默认统计上个月的 2021-05-01
  18. */
  19. public function recordReport($date = null)
  20. {
  21. if (is_null($date)) {
  22. //默认统计上个月的数据
  23. $date = now()->subMonth()->startOfMonth()->toDateTimeString();
  24. }
  25. $this->reportDate = $date;
  26. $start = $this->reportDate;
  27. $end = Carbon::parse($this->reportDate)->endOfMonth()->toDateTimeString();
  28. $ownerLogisticFeeDetails = OwnerLogisticFeeDetail::query()
  29. ->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")
  30. ->whereBetween('created_at', [$start, $end])
  31. ->groupBy('initial_weight', 'initial_weight_price', 'additional_price', 'additional_weight', 'logistic_id', 'province', 'counted_date', 'owner_id')
  32. ->get();
  33. $ownerLogisticFeeReportArray = [];
  34. foreach ($ownerLogisticFeeDetails as $ownerLogisticFeeDetail) {
  35. $ownerLogisticFeeReportArray[] = [
  36. 'logistic_id' => $ownerLogisticFeeDetail->logistic_id,
  37. 'province' => $ownerLogisticFeeDetail->province,
  38. 'counted_date' => Carbon::parse($ownerLogisticFeeDetail->counted_date)->firstOfMonth()->toDateString(),
  39. 'initial_weight' => $ownerLogisticFeeDetail->initial_weight,
  40. 'initial_weight_price' => $ownerLogisticFeeDetail->initial_weight_price,
  41. 'initial_amount' => $ownerLogisticFeeDetail->initial_amount,
  42. 'additional_weight' => $ownerLogisticFeeDetail->additional_weight,
  43. 'additional_price' => $ownerLogisticFeeDetail->additional_price,
  44. 'additional_amount' => $ownerLogisticFeeDetail->additional_amount,
  45. 'fee' => ($ownerLogisticFeeDetail['initial_weight_price'] * $ownerLogisticFeeDetail['initial_amount']) + ($ownerLogisticFeeDetail['additional_amount'] * $ownerLogisticFeeDetail['additional_price']),
  46. 'owner_id' => $ownerLogisticFeeDetail->owner_id,
  47. ];
  48. }
  49. OwnerLogisticFeeReport::query()->insertOrIgnore($ownerLogisticFeeReportArray);
  50. }
  51. /**
  52. * 订单统计分页查询
  53. * @param $owner_id
  54. * @param $date string 查询的年月 2021-05-01
  55. * @param $paginateParams
  56. * @return LengthAwarePaginator
  57. */
  58. public function getRecordPagination($owner_id, string $date,$paginateParams): LengthAwarePaginator
  59. {
  60. return OwnerLogisticFeeReport::query()
  61. ->with('logistic:id,name')
  62. ->where('owner_id', $owner_id)
  63. ->where('counted_date', $date)
  64. ->orderByDesc('logistic_id')
  65. ->orderByDesc('province')
  66. ->paginate($paginateParams['paginate']??50);
  67. }
  68. /**
  69. * 订单总计查询
  70. * @param $owner_id
  71. * @param $date string 查询的年月 2021-05-01
  72. * @return array
  73. */
  74. public function getRecordTotal($owner_id, string $date): array
  75. {
  76. $logistic_fee = OwnerLogisticFeeReport::query()
  77. ->where('owner_id', $owner_id)
  78. ->where('counted_date', $date)
  79. ->sum('fee');
  80. $order_count = (int)OwnerLogisticFeeReport::query()
  81. ->where('owner_id', $owner_id)
  82. ->where('counted_date', $date)
  83. ->sum('initial_amount');
  84. return [
  85. 'logistic_fee' => $logistic_fee,
  86. 'order_count' => $order_count,
  87. ];
  88. }
  89. }