subMonth()->startOfMonth()->toDateString(); } list($start, $end) = $this->getStartAndEnd($counting_month); $builder = DB::table('owner_fee_operations') ->leftJoin('owner_fee_operation_details', 'owner_fee_operations.id', '=', 'owner_fee_operation_details.owner_fee_operation_id') ->leftJoin('owner_price_operations', 'owner_fee_operations.model_id', '=', 'owner_price_operations.id') ->selectRaw(" DATE_FORMAT(owner_fee_operations.worked_at,'%Y-%m') as counting_month, owner_fee_operation_details.unit_id, owner_fee_operation_details.price, owner_price_operations.name, sum(owner_fee_operation_details.amount) as amounts , owner_fee_operations.owner_id, owner_fee_operations.model_id, sum(owner_fee_operation_details.price*owner_fee_operation_details.amount) as fee, sum(owner_fee_operation_details.price*owner_fee_operation_details.amount*owner_fee_operations.tax_rate) as tax_fee ") ->whereBetween('owner_fee_operations.worked_at', [$start, $end]) ->whereNotNull('owner_fee_operation_details.price') ->whereNotNull('owner_fee_operation_details.amount') ->whereIn('model_id', OwnerPriceOperation::query()->select('id')->where('operation_type', '出库')); if (!empty($ownerIds)) { $builder->whereIn('owner_fee_operations.owner_id', $ownerIds); } $details = $builder->groupBy( 'counting_month', 'owner_id', 'unit_id', 'price', 'model_id' ) ->get(); $reports = []; foreach ($details as $detail) { $counting_month = Carbon::parse($detail->counting_month)->startOfMonth()->toDateString(); $ownerBillReport = OwnerBillReport::query() ->selectRaw("id") ->where('owner_id', $detail->owner_id) ->where('counting_month', $counting_month)->first(); $reports[] = [ 'owner_bill_report_id' => $ownerBillReport->id ?? null, 'owner_id' => $detail->owner_id, 'counting_month' => $counting_month, 'unit_id' => $detail->unit_id, 'unit_price' => $detail->price, 'amount' => $detail->amounts, 'fee' => $detail->fee, 'work_name' => $detail->name, 'model_id' => $detail->model_id, 'tax_fee' => $detail->tax_fee, ]; } $reports_chunked = array_chunk($reports, 1000); //保证幂等性 插入前删除该月的统计数据 OwnerStoreOutFeeReport::query()->where('counting_month', $counting_month)->delete(); foreach ($reports_chunked as $items) { OwnerStoreOutFeeReport::query()->insertOrIgnore($items); } } public function getSql($owner_id, $counting_month): Builder { // TODO: Implement getSql() method. } public function buildExport($details): array { // TODO: Implement buildExport() method. } public function switchType($type) { // TODO: Implement switchType() method. } function get(array $kvPairs) { $this->archiveService = app('OwnerBillReportArchiveService'); list($start, $end) = $this->getStartAndEnd($kvPairs['counting_month']); if ($this->archiveService->isArchived($kvPairs['counting_month'], $kvPairs['owner_id'], $kvPairs['type']) == 1) { //查询存档数据 $archived = $this->archiveService->get($kvPairs); $reports = collect($archived->information['reports']); $work_name_fee_total = collect($archived->information['work_name_fee_total']); $fee_total = $archived->information['fee_total']; } else { $reports = OwnerStoreOutFeeReport::query() ->with(['unit:id,name']) ->where('owner_id', $kvPairs['owner_id']) ->where('counting_month', $kvPairs['counting_month']) ->get(); $work_name_fee_total = OwnerStoreOutFeeReport::query() ->selectRaw(" sum(fee) as fee, work_name ") ->where('counting_month' , $kvPairs['counting_month']) ->where('owner_id', $kvPairs['owner_id']) ->groupBy('work_name') ->get(); $fee_total = $work_name_fee_total->sum('fee'); } return array($reports, $work_name_fee_total, $fee_total); } 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(); }); list($reports, $work_name_fee_total, $fee_total) = $this->get([ 'owner_id' => $owner_id, 'counting_month' => $counting_month, 'type' => $this::TYPE, ]); 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, 'work_name_fee_total' => $work_name_fee_total, 'fee_total' => $fee_total, ], ]); $this->confirmBillFeeTotal($counting_month, $owner_id); } }