|
- <?php
-
- namespace App\Logic;
-
- use App\Exceptions\SkipException;
- use App\Kintone\Models\BankCheckResult;
- use App\Kintone\Models\DropDown\PoolTransferHistory\TransferMethod;
- use App\Kintone\Models\DropDown\PoolTransferHistory\TransgerType;
- use App\Kintone\Models\Pool;
- use App\Kintone\Models\PoolTransferHistory;
- use App\Kintone\Models\SmbcAccountTransferResult;
- use App\Kintone\Models\SmbcPayment;
- use App\Kintone\Models\YuchoPaymentResult;
- use App\Util\DateUtil;
-
- /**
- * 各種支払いデータから入金プールへ異動する
- */
- class PoolTransferManager
- {
-
- private SmbcPayment|SmbcAccountTransferResult|YuchoPaymentResult|BankCheckResult|null $payment = null;
- private Pool|null $pool = null;
- private PoolTransferHistory|null $history = null;
-
-
- public function moveToPool(SmbcPayment|SmbcAccountTransferResult|YuchoPaymentResult|BankCheckResult $payment): Pool
- {
- $this->clear();
-
- if (!$payment->customerCode) {
- throw new SkipException(sprintf("顧客コード未割当 レコード番号:%d", $payment->getRecordId()));
- }
- $this->payment = $payment;
-
- $this->getPool();
-
- $this->setPoolDone();
-
- $this->makeHistory();
-
- $this->setPoolAmount();
-
- $this->save();
-
- return $this->pool;
- }
-
-
- private function getPool()
- {
- $access = Pool::getAccess();
- $query = Pool::getQuery()->where(Pool::FIELD_CUSTOMER_CODE, $this->payment->customerCode);
- $list = $access->some($query);
- if ($list->isNotEmpty()) {
- if ($list->count() !== 1) {
- throw new SkipException(sprintf("データ不正 入金プール一意制約違反 顧客コード%d", $this->payment->customerCode));
- }
- $this->pool = $list->first();
- return;
- }
- $pool = new Pool();
- $pool->customerCode = $this->payment->customerCode;
- $pool->poolAmount = 0;
- $pool->save();
-
- $this->pool = $pool;
- }
-
- private function makeHistory()
- {
- $history = new PoolTransferHistory();
- $history->poolRecordNo = $this->pool->getRecordId();
- $history->customerCode = $this->payment->customerCode;
- $history->transferDatetime = DateUtil::now();
- $history->transferType = TransgerType::INCOME;
- $history->transferAmount = $this->payment->paymentAmount;
- $history->poolAmountBefore = $this->pool->poolAmount;
- $history->poolAmountAfter = $this->pool->poolAmount + $this->payment->paymentAmount;
-
- if ($this->payment instanceof SmbcPayment) {
- $history->incomeMethod = TransferMethod::CVS;
- $history->incomeCvsPaymentRecordNo = $this->payment->getRecordId();
- }
- if ($this->payment instanceof SmbcAccountTransferResult) {
- $history->incomeMethod = TransferMethod::ACCOUNT_TRANSFER;
- $history->incomeAccountTransferResultRecordNo = $this->payment->getRecordId();
- }
- if ($this->payment instanceof YuchoPaymentResult) {
- $history->incomeMethod = TransferMethod::YUCHO;
- $history->incomeYuchoTransferRecordNo = $this->payment->getRecordId();
- }
- if ($this->payment instanceof BankCheckResult) {
- $history->incomeMethod = TransferMethod::BANK_CHECK;
- $history->incomeBankCheckPaymentRecordNo = $this->payment->getRecordId();
- }
-
- $this->history = $history;
- }
-
- private function setPoolDone()
- {
- $this->payment->poolDone = ["済"];
- }
-
- private function setPoolAmount()
- {
- $this->pool->poolAmount += $this->payment->paymentAmount;
- }
-
- private function save()
- {
- $this->history->save();
- $this->pool->save();
- $this->payment->save();
- }
-
- private function clear()
- {
- $this->payment = null;
- $this->pool = null;
- $this->history = null;
- }
- }
|