領収証発行サービス
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

149 lines
3.6KB

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