archiveService = app('OwnerBillReportArchiveService'); } public function getTotalFee($owner_id, $counting_month) { return $this->getSql($owner_id, $counting_month)->get()->sum('fee'); } /** * * @param array $kvPairs * @return array */ public function get(array $kvPairs): array { if ($this->archiveService->isArchived($kvPairs['counting_month'], $kvPairs['owner_id'], $kvPairs['type']) == 1) { $archived = $this->archiveService->get($kvPairs); $details = collect($archived['information']['details']); $total_fee = $archived['information']['total_fee']; } else { $details = $this->getSql($kvPairs['owner_id'], $kvPairs['counting_month'])->get(); $total_fee = $details->sum('fee'); } return array($details, $total_fee); } public function getSql($owner_id, $counting_month): Builder { $ownerMaterialQuery = OwnerMaterial::query()->select('id')->where('owner_id', $owner_id); list($start, $end) = $this->getStartAndEnd($counting_month); return Procurement::query() ->selectRaw("id,owner_material_id,quantity,unit_price,ROUND(unit_price*quantity,2) as fee,created_at") ->with('ownerMaterial:id,size,special,specification') ->whereIn('owner_material_id', $ownerMaterialQuery) ->whereBetween('created_at', [$start, $end]); } public function switchType($type) { // TODO: Implement switchType() method. } public function buildExport($details): array { $result = []; foreach ($details as $detail) { $result[] = [ $detail->created_at, $detail->ownerMaterial->special, $detail->ownerMaterial->specification, $detail->ownerMaterial->size, $detail->quantity, $detail->unit_price, $detail->fee, ]; } return $result; } public function add(array $model) { // TODO: Implement add() 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(); }); list($details, $total_fee) = $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' => [ 'details' => $details, 'total_fee' => $total_fee, ], ]); $this->confirmBillFeeTotal($counting_month, $owner_id); } }