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

155 lines
3.8KB

  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. /**
  71. * 郵送投函完了
  72. *
  73. * @param array $attr
  74. * @return static
  75. */
  76. public function emailOrder(string $email, A4Receipt $file, array $attr = []): static
  77. {
  78. $this->fill([
  79. ReceiptIssuingOrder::COL_NAME_EMAIL => $email,
  80. ReceiptIssuingOrder::COL_NAME_STATUS_RECEIPT_EMAIL_SEND_ORDER_DATETIME => DateUtil::now(),
  81. ...$attr,
  82. ]);
  83. // メール送信
  84. $mail = new ReceiptA4($this->order);
  85. $mail->setEmail($this->order->email);
  86. $manager = new EmailManager($mail);
  87. $filename = sprintf("領収証_%s.pdf", $this->order->receipt_no);
  88. $manager->attach($file->getFullPath(), $filename, "application/pdf")
  89. ->confirm();
  90. $this->fill([
  91. ReceiptIssuingOrder::COL_NAME_RECEIPT_PDF_EMAIL_ID => $manager->getEmailId()
  92. ]);
  93. // イベント登録
  94. EmailOrderEvent::dispatch($this->order);
  95. PollEmailSendStatus::dispatch($this->order);
  96. return $this;
  97. }
  98. /**
  99. * 領収証確定
  100. *
  101. * @return static
  102. */
  103. public function setConfirm(): static
  104. {
  105. if ($this->order->receipt_no !== null) {
  106. throw new LogicException("領収証確定済み変更検知");
  107. }
  108. $this->order->receipt_no = $this->generateReceiptnNo();
  109. $this->order->status_receipt_confirm_datetime = DateUtil::now();
  110. // イベント登録
  111. ConfirmedEvent::dispatch($this->order);
  112. return $this;
  113. }
  114. public function fill(array $attr)
  115. {
  116. $this->order->fill($attr);
  117. return $this;
  118. }
  119. public function update()
  120. {
  121. // モデル更新
  122. $this->save();
  123. return [];
  124. }
  125. }