領収証発行サービス
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.

173 lines
4.2KB

  1. <?php
  2. namespace App\Logic\ReceiptIssuingOrder;
  3. use App\Email\Guests\ReceiptA4;
  4. use App\Events\ReceiptIssuingOrder\ChangeHandlerEvent;
  5. use App\Events\ReceiptIssuingOrder\ConfirmedEvent;
  6. use App\Events\ReceiptIssuingOrder\EmailOrderEvent;
  7. use App\Events\ReceiptIssuingOrder\MailOrderEvent;
  8. use App\Events\ReceiptIssuingOrder\MailPostedEvent;
  9. use App\Exceptions\AppCommonException;
  10. use App\Features\InstanceAble;
  11. use App\Files\PDF\Receipt\A4Receipt;
  12. use App\Jobs\ReceiptIssuingOrder\PollEmailSendStatus;
  13. use App\Logic\EmailManager;
  14. use App\Logic\SMS\SMSManager;
  15. use App\Models\ReceiptIssuingOrder;
  16. use App\Models\User;
  17. use App\Util\DateUtil;
  18. use Illuminate\Support\Carbon;
  19. use LogicException;
  20. class UpdateManager extends ReceiptIssuingOrderManager
  21. {
  22. use InstanceAble;
  23. public function __construct(
  24. protected ReceiptIssuingOrder $order,
  25. protected SMSManager $smsManager
  26. ) {
  27. parent::__construct($order);
  28. }
  29. public function changeHandler(User $newHandler)
  30. {
  31. if (!$this->initialized) {
  32. throw new LogicException("初期化ミス");
  33. }
  34. if ($this->order->contract_id !== $newHandler->contract_id) {
  35. throw new AppCommonException('契約不正');
  36. }
  37. $this->order->handler_id = $newHandler->id;
  38. ChangeHandlerEvent::dispatch($this->order);
  39. return $this;
  40. }
  41. /**
  42. * 郵送依頼
  43. *
  44. * @param array $attr
  45. * @return static
  46. */
  47. public function mailOrder(array $attr): static
  48. {
  49. $this->fill($attr);
  50. // イベント登録
  51. MailOrderEvent::dispatch($this->order);
  52. return $this;
  53. }
  54. /**
  55. * 郵送投函完了
  56. *
  57. * @param array $attr
  58. * @return static
  59. */
  60. public function mailPosted(Carbon $postDate): static
  61. {
  62. if ($this->order->status_mail_post_date === null) {
  63. // ステータス更新
  64. $this->order->status_mail_post_date = $postDate;
  65. // イベント登録
  66. MailPostedEvent::dispatch($this->order);
  67. }
  68. return $this;
  69. }
  70. public function acceptPrivacyPolicy(): static
  71. {
  72. $this->order->accept_privacy_policy = true;
  73. return $this;
  74. }
  75. public function acceptCorrectEntry(): static
  76. {
  77. $this->order->accept_correct_entry = true;
  78. return $this;
  79. }
  80. public function acceptSitePolicy(): static
  81. {
  82. $this->order->accept_site_policy = true;
  83. return $this;
  84. }
  85. /**
  86. * 郵送投函完了
  87. *
  88. * @param array $attr
  89. * @return static
  90. */
  91. public function emailOrder(string $email, A4Receipt $file, array $attr = []): static
  92. {
  93. $this->fill([
  94. ReceiptIssuingOrder::COL_NAME_EMAIL => $email,
  95. ReceiptIssuingOrder::COL_NAME_STATUS_RECEIPT_EMAIL_SEND_ORDER_DATETIME => DateUtil::now(),
  96. ...$attr,
  97. ]);
  98. // メール送信
  99. $mail = new ReceiptA4($this->order);
  100. $mail->setEmail($this->order->email);
  101. $manager = new EmailManager($mail);
  102. $filename = sprintf("領収証_%s.pdf", $this->order->receipt_no);
  103. $manager->attach($file->getFullPath(), $filename, "application/pdf")
  104. ->confirm();
  105. $this->fill([
  106. ReceiptIssuingOrder::COL_NAME_RECEIPT_PDF_EMAIL_ID => $manager->getEmailId()
  107. ]);
  108. // イベント登録
  109. EmailOrderEvent::dispatch($this->order);
  110. // 送信完了ジョブ登録
  111. PollEmailSendStatus::dispatch($this->order)
  112. ->delay(DateUtil::now()->addMinute());
  113. return $this;
  114. }
  115. /**
  116. * 領収証確定
  117. *
  118. * @return static
  119. */
  120. public function setConfirm(): static
  121. {
  122. if ($this->order->status_receipt_confirm_datetime !== null) {
  123. throw new LogicException("領収証確定済み変更検知");
  124. }
  125. $this->order->status_receipt_confirm_datetime = DateUtil::now();
  126. // イベント登録
  127. ConfirmedEvent::dispatch($this->order);
  128. return $this;
  129. }
  130. public function fill(array $attr)
  131. {
  132. $this->order->fill($attr);
  133. return $this;
  134. }
  135. public function update()
  136. {
  137. // モデル更新
  138. $this->save();
  139. return [];
  140. }
  141. }