| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- <?php
- namespace App\Services;
- use App\OwnerLogisticFeeDetail;
- use App\Traits\ServiceAppAop;
- use App\OwnerLogisticFeeReport;
- use Carbon\Carbon;
- use Illuminate\Contracts\Pagination\LengthAwarePaginator;
- class OwnerLogisticFeeReportService
- {
- use ServiceAppAop;
- protected $modelClass = OwnerLogisticFeeReport::class;
- private $reportDate;
- /**
- * 生成报表数据
- * 如果参数$date为空 统计上一个月的
- * 如果参数$date为2021-01-01 则统计2021-01-01 -- 2021-01-31之间的数据
- * @param null $date 统计月份,默认统计上个月的 2021-05-01
- */
- public function recordReport($date = null)
- {
- if (is_null($date)) {
- //默认统计上个月的数据
- $date = now()->subMonth()->startOfMonth()->toDateTimeString();
- }
- $this->reportDate = $date;
- $start = $this->reportDate;
- $end = Carbon::parse($this->reportDate)->endOfMonth()->toDateTimeString();
- $ownerLogisticFeeDetails = OwnerLogisticFeeDetail::query()
- ->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")
- ->whereBetween('created_at', [$start, $end])
- ->groupBy('initial_weight', 'initial_weight_price', 'additional_price', 'additional_weight', 'logistic_id', 'province', 'counted_date', 'owner_id')
- ->get();
- $ownerLogisticFeeReportArray = [];
- foreach ($ownerLogisticFeeDetails as $ownerLogisticFeeDetail) {
- $ownerLogisticFeeReportArray[] = [
- 'logistic_id' => $ownerLogisticFeeDetail->logistic_id,
- 'province' => $ownerLogisticFeeDetail->province,
- 'counted_date' => Carbon::parse($ownerLogisticFeeDetail->counted_date)->firstOfMonth()->toDateString(),
- 'initial_weight' => $ownerLogisticFeeDetail->initial_weight,
- 'initial_weight_price' => $ownerLogisticFeeDetail->initial_weight_price,
- 'initial_amount' => $ownerLogisticFeeDetail->initial_amount,
- 'additional_weight' => $ownerLogisticFeeDetail->additional_weight,
- 'additional_price' => $ownerLogisticFeeDetail->additional_price,
- 'additional_amount' => $ownerLogisticFeeDetail->additional_amount,
- 'fee' => ($ownerLogisticFeeDetail['initial_weight_price'] * $ownerLogisticFeeDetail['initial_amount']) + ($ownerLogisticFeeDetail['additional_amount'] * $ownerLogisticFeeDetail['additional_price']),
- 'owner_id' => $ownerLogisticFeeDetail->owner_id,
- ];
- }
- OwnerLogisticFeeReport::query()->insertOrIgnore($ownerLogisticFeeReportArray);
- }
- /**
- * 订单统计分页查询
- * @param $owner_id
- * @param $date string 查询的年月 2021-05-01
- * @param $paginateParams
- * @return LengthAwarePaginator
- */
- public function getRecordPagination($owner_id, string $date,$paginateParams): LengthAwarePaginator
- {
- return OwnerLogisticFeeReport::query()
- ->with('logistic:id,name')
- ->where('owner_id', $owner_id)
- ->where('counted_date', $date)
- ->orderByDesc('logistic_id')
- ->orderByDesc('province')
- ->paginate($paginateParams['paginate']??50);
- }
- /**
- * 订单总计查询
- * @param $owner_id
- * @param $date string 查询的年月 2021-05-01
- * @return array
- */
- public function getRecordTotal($owner_id, string $date): array
- {
- $logistic_fee = OwnerLogisticFeeReport::query()
- ->where('owner_id', $owner_id)
- ->where('counted_date', $date)
- ->sum('fee');
- $order_count = (int)OwnerLogisticFeeReport::query()
- ->where('owner_id', $owner_id)
- ->where('counted_date', $date)
- ->sum('initial_amount');
- return [
- 'logistic_fee' => $logistic_fee,
- 'order_count' => $order_count,
- ];
- }
- }
|