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.

120 satır
3.6KB

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