|
- <?php
-
- namespace App\Logic\ReceiptIssuingOrder;
-
- use App\Codes\EnvironmentName;
- use App\Codes\SMSSendPurpose;
- use App\Events\ReceiptIssuingOrder\CreatedEvent;
- use App\Logic\SMS\SMSManager;
- use App\Models\ReceiptIssuingOrder;
- use App\Models\ReceiptIssuingOrderTax;
- use App\Util\DateUtil;
- use Illuminate\Database\Eloquent\Collection;
- use Illuminate\Support\Facades\View;
- use Illuminate\Support\Str;
- use LogicException;
-
- class CreateManager extends ReceiptIssuingOrderManager
- {
-
- /**
- * @var Collection<ReceiptIssuingOrderTax>
- */
- protected Collection $taxes;
-
- public function __construct(
- protected ReceiptIssuingOrder $order,
- protected SMSManager $smsManager
- ) {
- parent::__construct($order);
- $this->taxes = new Collection();
- }
-
- public function init()
- {
- $this->initialized = true;
- return $this;
- }
-
- public function id(): string
- {
- if (!$this->order->id) {
- $this->order->setId();
- }
- return $this->order->id;
- }
-
- public function fill(array $attr)
- {
- $this->order->fill($attr);
- return $this;
- }
-
- public function setTax(int $rate, int $amount): static
- {
-
- $tax = new ReceiptIssuingOrderTax();
- $tax->tax_rate = $rate;
- $tax->tax_amount = $amount;
-
- $this->taxes->push($tax);
-
- return $this;
- }
-
- public function create(): array
- {
- $order = $this->order;
-
- // パラメータチェック
- $messages = $this->paramCheck();
- if (count($messages) !== 0) {
- return $messages;
- }
-
- // モデル更新
- $order->order_datetime = DateUtil::now();
- $this->refreshToken();
-
- $contractId = $this->loginUser()->getCurrentContractId();
- if ($contractId === null) {
- throw new LogicException("契約不良");
- }
- $order->setContract($contractId);
-
- $this->save();
-
-
- // 消費税保存
- foreach ($this->taxes as $tax) {
- $tax->setContract($order->contract_id)
- ->setReceiptIssuingOrder($this->order->id)
- ->save();
- }
-
-
- // SMS配信
- $smsSendOrder = $this->smsManager::makeSMSSendOrder($order, SMSSendPurpose::SEND_RECEIPT_ISSUING_ORDER_FORM, $this->makeSMSContents());
- $smsSendOrder->send();
-
- // イベント登録
- CreatedEvent::dispatch($this->order);
-
- return [];
- }
-
- private function paramCheck(): array
- {
- $ret = [];
- $order = $this->order;
-
- return $ret;
- }
-
- private function makeSMSContents(): string
- {
-
- $url = "";
- if (app()->environment(EnvironmentName::LOCAL->value)) {
- // localhostのURLを指定できないので、空とする
- } else {
- $url = implode('/', [config('app.url'), 'app/receipt-issuing-oreder', $this->order->access_token]);
- }
- return View::make('sms.announce_receipt_issusing_order_form', [
- 'url' => $url,
- ])->render();
- }
- }
|