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.

146 line
4.3KB

  1. <?php
  2. namespace App\Logic;
  3. use App\Exceptions\AppCommonException;
  4. use App\Kintone\Models\BankAccountUpdateApplication;
  5. use App\Kintone\Models\ChangePlanApplication;
  6. use App\Kintone\Models\Customer;
  7. use App\Kintone\Models\GeneralApplication;
  8. use App\Kintone\Models\Parking;
  9. use App\Kintone\Models\ParkingCertificateApplication;
  10. use App\Kintone\Models\SeasonTicketContract;
  11. use App\Kintone\Models\SeasonTicketReOrderApplication;
  12. use App\Kintone\Models\StickerReOrderApplication;
  13. use App\Kintone\Models\TerminateApplication;
  14. use App\Kintone\Models\UserInfoUpdateApplication;
  15. use App\Kintone\Models\VehicleInfoUpdateApplication;
  16. use App\Util\DateUtil;
  17. use LogicException;
  18. /**
  19. * @template T of GeneralApplication
  20. */
  21. class GeneralApplicationManager
  22. {
  23. private ?Customer $customer = null;
  24. private ?SeasonTicketContract $seasonTicketContract = null;
  25. private ?Parking $parking = null;
  26. /**
  27. * @param T $model
  28. */
  29. public function __construct(private GeneralApplication $model)
  30. {
  31. if ($model instanceof TerminateApplication) {
  32. $this->setType("解約申請");
  33. return;
  34. }
  35. if ($model instanceof ParkingCertificateApplication) {
  36. $this->setType("車庫証明発行申請");
  37. return;
  38. }
  39. if ($model instanceof StickerReOrderApplication) {
  40. $this->setType("シール再発行申請");
  41. return;
  42. }
  43. if ($model instanceof SeasonTicketReOrderApplication) {
  44. $this->setType("定期券再発行申請");
  45. return;
  46. }
  47. if ($model instanceof VehicleInfoUpdateApplication) {
  48. $this->setType("車両番号・防犯登録番号変更");
  49. return;
  50. }
  51. if ($model instanceof UserInfoUpdateApplication) {
  52. $this->setType("利用者情報変更");
  53. return;
  54. }
  55. if ($model instanceof UserInfoUpdateApplication) {
  56. $this->setType("振替頻度変更");
  57. return;
  58. }
  59. if ($model instanceof BankAccountUpdateApplication) {
  60. $this->setType("口座変更申請");
  61. return;
  62. }
  63. if ($model instanceof ChangePlanApplication) {
  64. $this->setType("プラン変更");
  65. return;
  66. }
  67. }
  68. public function setCustomer(Customer $customer): static
  69. {
  70. $this->customer = $customer;
  71. return $this;
  72. }
  73. public function setSeasonTicketContract(SeasonTicketContract $seasonTicketContract): static
  74. {
  75. $this->seasonTicketContract = $seasonTicketContract;
  76. return $this;
  77. }
  78. public function setParking(Parking $parking): static
  79. {
  80. $this->parking = $parking;
  81. return $this;
  82. }
  83. /**
  84. * @return T
  85. */
  86. public function makeApplication()
  87. {
  88. $this->model->applicationDatetime = DateUtil::now();
  89. $this->model->status = "新規登録";
  90. $this->model->applicationNo = $this->getApplicationNo();
  91. if ($this->customer === null) {
  92. throw new LogicException("顧客未設定のため失敗");
  93. }
  94. $this->model->customerCode = $this->customer->customerCode;
  95. if ($this->seasonTicketContract !== null) {
  96. $this->model->seasonTicketContractRecordNo = $this->seasonTicketContract->getRecordId();
  97. }
  98. if ($this->parking !== null) {
  99. $this->model->parkingName = $this->parking->parkingName;
  100. }
  101. if ($this->model instanceof BankAccountUpdateApplication) {
  102. $this->model->applicationCustomerCode = $this->customer->customerCode;
  103. }
  104. return $this->model;
  105. }
  106. private function setType(string $type): static
  107. {
  108. $this->model->applicationType = $type;
  109. return $this;
  110. }
  111. private function getApplicationNo(): string
  112. {
  113. /**
  114. * 申請番号を発番する。重複チェックを一定回数行う。
  115. */
  116. for ($i = 0; $i < 10; $i++) {
  117. $no = sprintf("%s-%06d", DateUtil::now()->format('Ymd'), rand(1, 999999));
  118. $check = GeneralApplication::getAccess()->some(GeneralApplication::getQuery()->where(GeneralApplication::FIELD_APPLICATION_NO, $no));
  119. if ($check->isEmpty()) {
  120. return $no;
  121. }
  122. }
  123. throw new AppCommonException('申請番号取得失敗');
  124. }
  125. }