Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

193 řádky
5.1KB

  1. <?php
  2. namespace App\Logic;
  3. use App\Exceptions\SkipException;
  4. use App\Kintone\KintoneRecordQuery;
  5. use App\Kintone\Models\DropDown\PaymentPlan\PaymentType;
  6. use App\Kintone\Models\DropDown\PoolTransferHistory\TransgerType;
  7. use App\Kintone\Models\PaymentPlan;
  8. use App\Kintone\Models\Pool;
  9. use App\Kintone\Models\PoolTransferHistory;
  10. use App\Util\DateUtil;
  11. use Exception;
  12. use Illuminate\Support\Collection;
  13. /**
  14. * 入金プールから入金予定結果へ異動する
  15. */
  16. class PoolAttachManager
  17. {
  18. private Pool|null $pool = null;
  19. /**
  20. * @var Collection<int, PoolTransferHistory>
  21. */
  22. private Collection $histories;
  23. /**
  24. * @var Collection<int, PaymentPlan>
  25. */
  26. private Collection $plans;
  27. /**
  28. * @var Collection<int, PaymentPlan>
  29. */
  30. private Collection $completePlans;
  31. /**
  32. * @var Collection<int, bool>
  33. */
  34. private Collection $firstPaymnetEntryRecordIds;
  35. public function attach(Pool $pool)
  36. {
  37. $this->clear();
  38. if ($pool->poolAmount === 0) {
  39. throw new SkipException(sprintf("プール金なし 顧客コード:%d", $pool->customerCode));
  40. }
  41. $this->pool = $pool;
  42. $this->plans = $this->getPaymentPlans();
  43. foreach ($this->plans as $plan) {
  44. try {
  45. if (!$this->canAttach($plan)) {
  46. throw new AttachStop(sprintf("残高不足のため充当途中停止 顧客コード:%d", $this->pool->customerCode));
  47. }
  48. $this->makeHistory($plan);
  49. $this->setPoolAmount($plan);
  50. $this->setPaymentPlanDone($plan);
  51. } catch (AttachStop $e) {
  52. logs()->warning($e->getMessage());
  53. break;
  54. }
  55. }
  56. return $this;
  57. }
  58. public function getFirstPaymnetEntryRecordIds()
  59. {
  60. return $this->firstPaymnetEntryRecordIds->keys();
  61. }
  62. private function getPaymentPlans()
  63. {
  64. $query = PaymentPlan::getQuery()
  65. ->where(PaymentPlan::FIELD_CUSTOMER_CODE, $this->pool->customerCode)
  66. ->whereNull(PaymentPlan::FIELD_APPROPRIATION_DATE)
  67. ->where(function (KintoneRecordQuery $q) {
  68. $q->whereNull(PaymentPlan::FIELD_APPROPRIATION_AMOUNT)
  69. ->orWhere(PaymentPlan::FIELD_APPROPRIATION_AMOUNT, 0);
  70. })
  71. ->orderByAsc(PaymentPlan::FIELD_PAYMENT_PLAN_DATE);
  72. $plans = PaymentPlan::getAccess()->all($query);
  73. return $plans;
  74. }
  75. private function canAttach(PaymentPlan $plan)
  76. {
  77. $afterPool = $this->pool->poolAmount - $plan->paymentPlanAmount;
  78. return 0 <= $afterPool;
  79. }
  80. private function makeHistory(PaymentPlan $plan)
  81. {
  82. $history = new PoolTransferHistory();
  83. $history->poolRecordNo = $this->pool->getRecordId();
  84. $history->customerCode = $this->pool->customerCode;
  85. $history->transferDatetime = DateUtil::now();
  86. $history->transferType = TransgerType::ATTACH;
  87. $history->transferAmount = $plan->paymentPlanAmount;
  88. $history->poolAmountBefore = $this->pool->poolAmount;
  89. $history->poolAmountAfter = $this->pool->poolAmount - $plan->paymentPlanAmount;
  90. $history->paymentPlanRecordNo = $plan->getRecordId();
  91. $this->histories->push($history);
  92. }
  93. private function setPoolAmount(PaymentPlan $plan)
  94. {
  95. $afterPool = $this->pool->poolAmount - $plan->paymentPlanAmount;
  96. $this->pool->poolAmount = $afterPool;
  97. }
  98. private function setPaymentPlanDone(PaymentPlan $plan)
  99. {
  100. $plan->appropriationDate = DateUtil::now();
  101. $plan->appropriationAmount = $plan->paymentPlanAmount;
  102. $plan->remainingAmount = 0;
  103. if (!!$plan->firstPaymentEntryRecordNo) {
  104. $this->firstPaymnetEntryRecordIds->put($plan->firstPaymentEntryRecordNo, true);
  105. }
  106. $this->completePlans->push($plan);
  107. }
  108. public function save()
  109. {
  110. foreach ($this->histories as $history) {
  111. $history->save();
  112. }
  113. foreach ($this->completePlans as $plan) {
  114. $plan->save();
  115. }
  116. $this->pool->save();
  117. $this->makeReceipt();
  118. return $this;
  119. }
  120. private function makeReceipt()
  121. {
  122. if ($this->completePlans->isEmpty()) {
  123. return;
  124. }
  125. // 支払い種別ごとにグルーピング
  126. $ids = [];
  127. foreach ($this->completePlans as $plan) {
  128. // 保証金は領収証を発行しない
  129. if ($plan->paymentType === PaymentType::A保証金) {
  130. continue;
  131. }
  132. $ids[$plan->paymentType][] = $plan;
  133. }
  134. // 領収証作成
  135. foreach ($ids as $targets) {
  136. $manager = new ReceiptManager();
  137. $manager->setPaymentPlans(collect($targets))
  138. ->create($targets);
  139. }
  140. return $this;
  141. }
  142. private function clear()
  143. {
  144. $this->pool = null;
  145. $this->histories = collect();
  146. $this->firstPaymnetEntryRecordIds = collect();
  147. $this->plans = collect();
  148. $this->completePlans = collect();
  149. }
  150. }
  151. class AttachStop extends Exception
  152. {
  153. }