|
- <?php
-
- namespace App\Logic;
-
- use App\Exceptions\SkipException;
- use App\Kintone\KintoneRecordQuery;
- use App\Kintone\Models\DropDown\PaymentPlan\PaymentType;
- use App\Kintone\Models\DropDown\PoolTransferHistory\TransgerType;
- use App\Kintone\Models\PaymentPlan;
- use App\Kintone\Models\Pool;
- use App\Kintone\Models\PoolTransferHistory;
- use App\Util\DateUtil;
- use Exception;
- use Illuminate\Support\Collection;
-
- /**
- * 入金プールから入金予定結果へ異動する
- */
- class PoolAttachManager
- {
-
- private Pool|null $pool = null;
-
- /**
- * @var Collection<int, PoolTransferHistory>
- */
- private Collection $histories;
- /**
- * @var Collection<int, PaymentPlan>
- */
- private Collection $plans;
- /**
- * @var Collection<int, PaymentPlan>
- */
- private Collection $completePlans;
-
-
- /**
- * @var Collection<int, bool>
- */
- 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
- {
- }
|