You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
3.8KB

  1. <?php
  2. namespace App\Logic;
  3. use App\Exceptions\SkipException;
  4. use App\Kintone\Models\BankCheckResult;
  5. use App\Kintone\Models\DropDown\PoolTransferHistory\TransferMethod;
  6. use App\Kintone\Models\DropDown\PoolTransferHistory\TransgerType;
  7. use App\Kintone\Models\Pool;
  8. use App\Kintone\Models\PoolTransferHistory;
  9. use App\Kintone\Models\SmbcAccountTransferResult;
  10. use App\Kintone\Models\SmbcPayment;
  11. use App\Kintone\Models\YuchoPaymentResult;
  12. use App\Util\DateUtil;
  13. /**
  14. * 各種支払いデータから入金プールへ異動する
  15. */
  16. class PoolTransferManager
  17. {
  18. private SmbcPayment|SmbcAccountTransferResult|YuchoPaymentResult|BankCheckResult|null $payment = null;
  19. private Pool|null $pool = null;
  20. private PoolTransferHistory|null $history = null;
  21. public function moveToPool(SmbcPayment|SmbcAccountTransferResult|YuchoPaymentResult|BankCheckResult $payment): Pool
  22. {
  23. $this->clear();
  24. if (!$payment->customerCode) {
  25. throw new SkipException(sprintf("顧客コード未割当 レコード番号:%d", $payment->getRecordId()));
  26. }
  27. $this->payment = $payment;
  28. $this->getPool();
  29. $this->setPoolDone();
  30. $this->makeHistory();
  31. $this->setPoolAmount();
  32. $this->save();
  33. return $this->pool;
  34. }
  35. private function getPool()
  36. {
  37. $access = Pool::getAccess();
  38. $query = Pool::getQuery()->where(Pool::FIELD_CUSTOMER_CODE, $this->payment->customerCode);
  39. $list = $access->some($query);
  40. if ($list->isNotEmpty()) {
  41. if ($list->count() !== 1) {
  42. throw new SkipException(sprintf("データ不正 入金プール一意制約違反 顧客コード%d", $this->payment->customerCode));
  43. }
  44. $this->pool = $list->first();
  45. return;
  46. }
  47. $pool = new Pool();
  48. $pool->customerCode = $this->payment->customerCode;
  49. $pool->poolAmount = 0;
  50. $pool->save();
  51. $this->pool = $pool;
  52. }
  53. private function makeHistory()
  54. {
  55. $history = new PoolTransferHistory();
  56. $history->poolRecordNo = $this->pool->getRecordId();
  57. $history->customerCode = $this->payment->customerCode;
  58. $history->transferDatetime = DateUtil::now();
  59. $history->transferType = TransgerType::INCOME;
  60. $history->transferAmount = $this->payment->paymentAmount;
  61. $history->poolAmountBefore = $this->pool->poolAmount;
  62. $history->poolAmountAfter = $this->pool->poolAmount + $this->payment->paymentAmount;
  63. if ($this->payment instanceof SmbcPayment) {
  64. $history->incomeMethod = TransferMethod::CVS;
  65. $history->incomeCvsPaymentRecordNo = $this->payment->getRecordId();
  66. }
  67. if ($this->payment instanceof SmbcAccountTransferResult) {
  68. $history->incomeMethod = TransferMethod::ACCOUNT_TRANSFER;
  69. $history->incomeAccountTransferResultRecordNo = $this->payment->getRecordId();
  70. }
  71. if ($this->payment instanceof YuchoPaymentResult) {
  72. $history->incomeMethod = TransferMethod::YUCHO;
  73. $history->incomeYuchoTransferRecordNo = $this->payment->getRecordId();
  74. }
  75. if ($this->payment instanceof BankCheckResult) {
  76. $history->incomeMethod = TransferMethod::BANK_CHECK;
  77. $history->incomeBankCheckPaymentRecordNo = $this->payment->getRecordId();
  78. }
  79. $this->history = $history;
  80. }
  81. private function setPoolDone()
  82. {
  83. $this->payment->poolDone = ["済"];
  84. }
  85. private function setPoolAmount()
  86. {
  87. $this->pool->poolAmount += $this->payment->paymentAmount;
  88. }
  89. private function save()
  90. {
  91. $this->history->save();
  92. $this->pool->save();
  93. $this->payment->save();
  94. }
  95. private function clear()
  96. {
  97. $this->payment = null;
  98. $this->pool = null;
  99. $this->history = null;
  100. }
  101. }