| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- <?php
- namespace App\Services;
- use App\OwnerBillReport;
- use App\OwnerBillReportArchive;
- use App\OwnerFeeExpress;
- use App\OwnerLogisticFeeDetail;
- use App\Traits\ServiceAppAop;
- use App\OwnerLogisticFeeReport;
- use Carbon\Carbon;
- use Illuminate\Contracts\Pagination\LengthAwarePaginator;
- use Illuminate\Database\Eloquent\Builder;
- use Illuminate\Database\Eloquent\Collection;
- class OwnerLogisticFeeReportService implements \App\Interfaces\SettlementBillReportInterface
- {
- const TYPE = '快递费-合计';
- use ServiceAppAop;
- use \App\Traits\SettlementBillServiceTrait;
- protected $modelClass = OwnerLogisticFeeReport::class;
- private $reportDate;
- /** @var $archiveService OwnerBillReportArchiveService */
- private $archiveService;
- public function switchType($type)
- {
- // TODO: Implement switchType() method.
- }
- public function buildExport($details): array
- {
- // TODO: Implement buildExport() method.
- }
- public function confirmBill($counting_month, $owner_id)
- {
- $billReport = OwnerBillReport::query()
- ->select('storage_fee', 'id')
- ->where('owner_id', $owner_id)
- ->where('counting_month', $counting_month)
- ->firstOr(function () {
- return new OwnerBillReport();
- });
- $reports = $this->getRecords($owner_id, $counting_month);
- $recordTotal = $this->getRecordTotal($owner_id, $counting_month);
- OwnerBillReportArchive::query()->create([
- 'owner_bill_report_id' => $billReport->id ?? null,
- 'owner_id' => $owner_id,
- 'counting_month' => $counting_month,
- 'type' => $this::TYPE,
- 'archiver_id' => auth()->id(),
- 'archived_at' => now(),
- 'information' => [
- 'reports' => $reports,
- 'recordTotal' => $recordTotal,
- ],
- ]);
- $this->confirmBillFeeTotal($counting_month, $owner_id);
- }
- public function recordReport($counting_month = null,array $ownerIds = [])
- {
- if (is_null($counting_month)) {
- //默认统计上个月的数据
- $counting_month = now()->subMonth()->startOfMonth()->toDateTimeString();
- }
- list($start, $end) = $this->getStartAndEnd($counting_month);
- $builder = OwnerFeeExpress::query()
- ->selectRaw("
- province_id,
- logistic_id,
- DATE_FORMAT(created_at,'%Y-%m') as counted_date,
- initial_weight,
- initial_weight_price,
- count(1) as initial_amount,
- additional_weight_price,
- additional_weight,
- sum(additional_weight_amount) as additional_amount,
- sum(total_fee*tax_rate/100) as tax_fee,
- sum(total_fee) as fee,
- sum(total_fee) as fee,
- created_at,
- owner_id")
- ->whereBetween('created_at', [$start, $end]);
- if (!empty($ownerIds)) {
- $builder->whereIn('owner_id', $ownerIds);
- }
- $ownerLogisticFeeDetails = $builder
- ->groupBy(
- 'initial_weight',
- 'initial_weight_price',
- 'additional_weight_price',
- 'additional_weight',
- 'logistic_id',
- 'province_id',
- 'counted_date',
- 'owner_id')
- ->get();
- $ownerLogisticFeeReportArray = [];
- foreach ($ownerLogisticFeeDetails as $ownerLogisticFeeDetail) {
- $ownerLogisticFeeReportArray[] = [
- 'logistic_id' => $ownerLogisticFeeDetail->logistic_id,
- 'province_id' => $ownerLogisticFeeDetail->province_id,
- '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_weight_price,
- 'additional_amount' => $ownerLogisticFeeDetail->additional_amount,
- 'fee' => $ownerLogisticFeeDetail->fee,
- 'tax_fee' => $ownerLogisticFeeDetail->tax_fee,
- 'owner_id' => $ownerLogisticFeeDetail->owner_id,
- ];
- }
- //保证接口幂等性 删除统计月的数据
- OwnerLogisticFeeReport::query()->where('counted_date', $counting_month)->delete();
- OwnerLogisticFeeReport::query()->insertOrIgnore($ownerLogisticFeeReportArray);
- }
- /**
- * 订单统计分页查询
- * @param $owner_id
- * @param $counting_month string 查询的年月 2021-05-01
- * @param $paginateParams
- * @return LengthAwarePaginator
- */
- private function getPagination($owner_id, string $counting_month, $paginateParams): LengthAwarePaginator
- {
- return $this->getSql($owner_id, $counting_month)
- ->paginate($paginateParams['paginate'] ?? 50);
- }
- public function get(array $kvPairs): array
- {
- $this->archiveService = app('OwnerBillReportArchiveService');
- if ($this->archiveService->isArchived($kvPairs['counting_month'], $kvPairs['owner_id'], $kvPairs['type']) == 1) {
- //查询存档数据
- $archived = $this->archiveService->get($kvPairs);
- $reports = collect($archived->information['reports']);
- $recordTotal = $archived->information['recordTotal'];
- } else {
- $recordTotal = $this->getRecordTotal($kvPairs['owner_id'], $kvPairs['counting_month']);
- $reports = $this->getPagination($kvPairs['owner_id'], $kvPairs['counting_month'], $kvPairs['paginateParams']);
- }
- return array($reports, $recordTotal);
- }
- /**
- * 订单统计查询
- * @param $owner_id
- * @param $counting_month string 查询的年月 2021-05-01
- * @return Builder[]|Collection
- */
- public function getRecords($owner_id, string $counting_month)
- {
- return $this->getSql($owner_id, $counting_month)->get();
- }
- /**
- * 订单总计查询
- * @param $owner_id
- * @param $counting_month string 查询的年月 2021-05-01
- * @return array
- */
- public function getRecordTotal($owner_id, string $counting_month): array
- {
- $logistic_fee = OwnerLogisticFeeReport::query()
- ->where('owner_id', $owner_id)
- ->where('counted_date', $counting_month)
- ->sum('fee');
- $order_count = (int)OwnerLogisticFeeReport::query()
- ->where('owner_id', $owner_id)
- ->where('counted_date', $counting_month)
- ->sum('initial_amount');
- return [
- 'logistic_fee' => $logistic_fee,
- 'order_count' => $order_count,
- ];
- }
- /**
- * @param $owner_id
- * @param $counting_month
- * @return Builder
- */
- public function getSql($owner_id, $counting_month): Builder
- {
- return OwnerLogisticFeeReport::query()
- ->with(['logistic:id,name','province'])
- ->where('owner_id', $owner_id)
- ->where('counted_date', $counting_month)
- ->orderByDesc('logistic_id')
- ->orderByDesc('province_id');
- }
- /**
- *获取总金额和税费
- */
- public function getTotalFee($owner_id, $counting_month)
- {
- return OwnerLogisticFeeReport::query()
- ->selectRaw("sum(fee) as fee,
- sum(tax_fee) as tax_fee")
- ->where('owner_id', $owner_id)
- ->where('counted_date', $counting_month)
- ->first();
- }
- }
|