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.

132 line
3.8KB

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