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

128 lines
3.0KB

  1. <?php
  2. namespace App\Logic\ReceiptIssuingOrder;
  3. use App\Codes\EnvironmentName;
  4. use App\Codes\SMSSendPurpose;
  5. use App\Events\ReceiptIssuingOrder\CreatedEvent;
  6. use App\Logic\SMS\SMSManager;
  7. use App\Models\ReceiptIssuingOrder;
  8. use App\Models\ReceiptIssuingOrderTax;
  9. use App\Util\DateUtil;
  10. use Illuminate\Database\Eloquent\Collection;
  11. use Illuminate\Support\Facades\View;
  12. use Illuminate\Support\Str;
  13. use LogicException;
  14. class CreateManager extends ReceiptIssuingOrderManager
  15. {
  16. /**
  17. * @var Collection<ReceiptIssuingOrderTax>
  18. */
  19. protected Collection $taxes;
  20. public function __construct(
  21. protected ReceiptIssuingOrder $order,
  22. protected SMSManager $smsManager
  23. ) {
  24. parent::__construct($order);
  25. $this->taxes = new Collection();
  26. }
  27. public function init()
  28. {
  29. $this->initialized = true;
  30. return $this;
  31. }
  32. public function id(): string
  33. {
  34. if (!$this->order->id) {
  35. $this->order->setId();
  36. }
  37. return $this->order->id;
  38. }
  39. public function fill(array $attr)
  40. {
  41. $this->order->fill($attr);
  42. return $this;
  43. }
  44. public function setTax(int $rate, int $amount): static
  45. {
  46. $tax = new ReceiptIssuingOrderTax();
  47. $tax->tax_rate = $rate;
  48. $tax->tax_amount = $amount;
  49. $this->taxes->push($tax);
  50. return $this;
  51. }
  52. public function create(): array
  53. {
  54. $order = $this->order;
  55. // パラメータチェック
  56. $messages = $this->paramCheck();
  57. if (count($messages) !== 0) {
  58. return $messages;
  59. }
  60. // モデル更新
  61. $order->order_datetime = DateUtil::now();
  62. $this->refreshToken();
  63. $contractId = $this->loginUser()->getCurrentContractId();
  64. if ($contractId === null) {
  65. throw new LogicException("契約不良");
  66. }
  67. $order->setContract($contractId);
  68. $this->save();
  69. // 消費税保存
  70. foreach ($this->taxes as $tax) {
  71. $tax->setContract($order->contract_id)
  72. ->setReceiptIssuingOrder($this->order->id)
  73. ->save();
  74. }
  75. // SMS配信
  76. $smsSendOrder = $this->smsManager::makeSMSSendOrder($order, SMSSendPurpose::SEND_RECEIPT_ISSUING_ORDER_FORM, $this->makeSMSContents());
  77. $smsSendOrder->send();
  78. // イベント登録
  79. CreatedEvent::dispatch($this->order);
  80. return [];
  81. }
  82. private function paramCheck(): array
  83. {
  84. $ret = [];
  85. $order = $this->order;
  86. return $ret;
  87. }
  88. private function makeSMSContents(): string
  89. {
  90. $url = "";
  91. if (app()->environment(EnvironmentName::LOCAL->value)) {
  92. // localhostのURLを指定できないので、空とする
  93. } else {
  94. $url = implode('/', [config('app.url'), 'app/receipt-issuing-oreder', $this->order->access_token]);
  95. }
  96. return View::make('sms.announce_receipt_issusing_order_form', [
  97. 'url' => $url,
  98. ])->render();
  99. }
  100. }