Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

130 lines
4.2KB

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