*/ private Collection $histories; /** * @var Collection */ private Collection $plans; /** * @var Collection */ private Collection $completePlans; /** * @var Collection */ private Collection $firstPaymnetEntryRecordIds; public function attach(Pool $pool) { $this->clear(); if ($pool->poolAmount === 0) { throw new SkipException(sprintf("プール金なし 顧客コード:%d", $pool->customerCode)); } $this->pool = $pool; $this->plans = $this->getPaymentPlans(); foreach ($this->plans as $plan) { try { if (!$this->canAttach($plan)) { throw new AttachStop(sprintf("残高不足のため充当途中停止 顧客コード:%d", $this->pool->customerCode)); } $this->makeHistory($plan); $this->setPoolAmount($plan); $this->setPaymentPlanDone($plan); } catch (AttachStop $e) { logs()->warning($e->getMessage()); break; } } return $this; } public function getFirstPaymnetEntryRecordIds() { return $this->firstPaymnetEntryRecordIds->keys(); } private function getPaymentPlans() { $query = PaymentPlan::getQuery() ->where(PaymentPlan::FIELD_CUSTOMER_CODE, $this->pool->customerCode) ->whereNull(PaymentPlan::FIELD_APPROPRIATION_DATE) ->where(function (KintoneRecordQuery $q) { $q->whereNull(PaymentPlan::FIELD_APPROPRIATION_AMOUNT) ->orWhere(PaymentPlan::FIELD_APPROPRIATION_AMOUNT, 0); }) ->orderByAsc(PaymentPlan::FIELD_PAYMENT_PLAN_DATE); $plans = PaymentPlan::getAccess()->all($query); return $plans; } private function canAttach(PaymentPlan $plan) { $afterPool = $this->pool->poolAmount - $plan->paymentPlanAmount; return 0 <= $afterPool; } private function makeHistory(PaymentPlan $plan) { $history = new PoolTransferHistory(); $history->poolRecordNo = $this->pool->getRecordId(); $history->customerCode = $this->pool->customerCode; $history->transferDatetime = DateUtil::now(); $history->transferType = TransgerType::ATTACH; $history->transferAmount = $plan->paymentPlanAmount; $history->poolAmountBefore = $this->pool->poolAmount; $history->poolAmountAfter = $this->pool->poolAmount - $plan->paymentPlanAmount; $history->paymentPlanRecordNo = $plan->getRecordId(); $this->histories->push($history); } private function setPoolAmount(PaymentPlan $plan) { $afterPool = $this->pool->poolAmount - $plan->paymentPlanAmount; $this->pool->poolAmount = $afterPool; } private function setPaymentPlanDone(PaymentPlan $plan) { $plan->appropriationDate = DateUtil::now(); $plan->appropriationAmount = $plan->paymentPlanAmount; $plan->remainingAmount = 0; if (!!$plan->firstPaymentEntryRecordNo) { $this->firstPaymnetEntryRecordIds->put($plan->firstPaymentEntryRecordNo, true); } $this->completePlans->push($plan); } public function save() { foreach ($this->histories as $history) { $history->save(); } foreach ($this->completePlans as $plan) { $plan->save(); } $this->pool->save(); $this->makeReceipt(); return $this; } private function makeReceipt() { if ($this->completePlans->isEmpty()) { return; } // 支払い種別ごとにグルーピング $ids = []; foreach ($this->completePlans as $plan) { // 保証金は領収証を発行しない if ($plan->paymentType === PaymentType::A保証金) { continue; } $ids[$plan->paymentType][] = $plan; } // 領収証作成 foreach ($ids as $targets) { $manager = new ReceiptManager(); $manager->setPaymentPlans(collect($targets)) ->create($targets); } return $this; } private function clear() { $this->pool = null; $this->histories = collect(); $this->firstPaymnetEntryRecordIds = collect(); $this->plans = collect(); $this->completePlans = collect(); } } class AttachStop extends Exception { }