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

107 lines
2.4KB

  1. <?php
  2. namespace App\Logic\ReceiptIssuingOrder;
  3. use App\Events\ReceiptIssuingOrder\ChangeHandlerEvent;
  4. use App\Events\ReceiptIssuingOrder\ConfirmedEvent;
  5. use App\Events\ReceiptIssuingOrder\MailOrderEvent;
  6. use App\Events\ReceiptIssuingOrder\MailPostedEvent;
  7. use App\Exceptions\AppCommonException;
  8. use App\Logic\SMS\SMSManager;
  9. use App\Models\ReceiptIssuingOrder;
  10. use App\Models\User;
  11. use App\Util\DateUtil;
  12. use Illuminate\Support\Carbon;
  13. use LogicException;
  14. class UpdateManager extends ReceiptIssuingOrderManager
  15. {
  16. public function __construct(
  17. protected ReceiptIssuingOrder $order,
  18. protected SMSManager $smsManager
  19. ) {
  20. parent::__construct($order);
  21. }
  22. public function changeHandler(User $newHandler)
  23. {
  24. if (!$this->initialized) {
  25. throw new LogicException("初期化ミス");
  26. }
  27. if ($this->order->contract_id !== $newHandler->contract_id) {
  28. throw new AppCommonException('契約不正');
  29. }
  30. $this->order->handler_id = $newHandler->id;
  31. ChangeHandlerEvent::dispatch($this->order);
  32. return $this;
  33. }
  34. /**
  35. * 郵送依頼
  36. *
  37. * @param array $attr
  38. * @return static
  39. */
  40. public function mailOrder(array $attr): static
  41. {
  42. $this->fill($attr);
  43. // イベント登録
  44. MailOrderEvent::dispatch($this->order);
  45. return $this;
  46. }
  47. /**
  48. * 郵送投函完了
  49. *
  50. * @param array $attr
  51. * @return static
  52. */
  53. public function mailPosted(Carbon $postDate): static
  54. {
  55. $this->order->status_mail_post_date = $postDate;
  56. // イベント登録
  57. MailPostedEvent::dispatch($this->order);
  58. return $this;
  59. }
  60. /**
  61. * 領収証確定
  62. *
  63. * @return static
  64. */
  65. public function setConfirm(): static
  66. {
  67. if ($this->order->receipt_no !== null) {
  68. throw new LogicException("領収証確定済み変更検知");
  69. }
  70. $this->order->receipt_no = $this->generateReceiptnNo();
  71. $this->order->status_receipt_confirm_datetime = DateUtil::now();
  72. // イベント登録
  73. ConfirmedEvent::dispatch($this->order);
  74. return $this;
  75. }
  76. public function fill(array $attr)
  77. {
  78. $this->order->fill($attr);
  79. return $this;
  80. }
  81. public function update()
  82. {
  83. // モデル更新
  84. $this->save();
  85. return [];
  86. }
  87. }