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

100 lines
2.4KB

  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\Util\DateUtil;
  9. use Illuminate\Support\Facades\View;
  10. use Illuminate\Support\Str;
  11. use LogicException;
  12. class CreateManager extends ReceiptIssuingOrderManager
  13. {
  14. public function __construct(
  15. protected ReceiptIssuingOrder $order,
  16. protected SMSManager $smsManager
  17. ) {
  18. parent::__construct($order);
  19. }
  20. public function init()
  21. {
  22. $this->initialized = true;
  23. return $this;
  24. }
  25. public function id(): string
  26. {
  27. if (!$this->order->id) {
  28. $this->order->setId();
  29. }
  30. return $this->order->id;
  31. }
  32. public function fill(array $attr)
  33. {
  34. $this->order->fill($attr);
  35. return $this;
  36. }
  37. public function create(): array
  38. {
  39. $order = $this->order;
  40. // パラメータチェック
  41. $messages = $this->paramCheck();
  42. if (count($messages) !== 0) {
  43. return $messages;
  44. }
  45. // モデル更新
  46. $order->order_datetime = DateUtil::now();
  47. $this->refreshToken();
  48. $contractId = $this->loginUser()->getCurrentContractId();
  49. if ($contractId === null) {
  50. throw new LogicException("契約不良");
  51. }
  52. $order->setContract($contractId);
  53. $this->save();
  54. // SMS配信
  55. $smsSendOrder = $this->smsManager::makeSMSSendOrder($order, SMSSendPurpose::SEND_RECEIPT_ISSUING_ORDER_FORM, $this->makeSMSContents());
  56. $smsSendOrder->send();
  57. // イベント登録
  58. CreatedEvent::dispatch($this->order);
  59. return [];
  60. }
  61. private function paramCheck(): array
  62. {
  63. $ret = [];
  64. $order = $this->order;
  65. return $ret;
  66. }
  67. private function makeSMSContents(): string
  68. {
  69. $url = "";
  70. if (app()->environment(EnvironmentName::LOCAL->value)) {
  71. // localhostのURLを指定できないので、空とする
  72. } else {
  73. $url = implode('/', [config('app.url'), 'app/receipt-issuing-oreder', $this->order->access_token]);
  74. }
  75. return View::make('sms.announce_receipt_issusing_order_form', [
  76. 'url' => $url,
  77. ])->render();
  78. }
  79. }