checkToken($token); if (!$ret) { throw new AppCommonException("トークン不正"); } $this->initialized = true; return $this; } public function initById(string $id) { $this->fetch($id); $this->initialized = true; return $this; } public function getOrder(): array { if (!$this->initialized) { throw new LogicException("初期化ミス"); } return $this->order->toArray(); } public function isConfirmed(): bool { if (!$this->initialized) { throw new LogicException("初期化ミス"); } return $this->order->status_receipt_confirm_datetime !== null; } public function checkTimestamp(Carbon $timestamp): static { if (!$this->initialized) { throw new LogicException("初期化ミス"); } if ($this->order->updated_at->notEqualTo($timestamp)) { throw new ExclusiveException(); } return $this; } protected function fetch(string $receiptIssuingOrderId) { $order = ReceiptIssuingOrder::findOrFail($receiptIssuingOrderId); if (!$this->loginUser()->checkAuthorization($order)) { throw new AppCommonException("認可不良"); } $this->order = $order; } protected function refreshToken() { $order = $this->order; $order->access_token = base64_encode(Str::uuid()); $order->access_token_expires_at = DateUtil::now()->adddays(7); return $this; } protected function checkToken(string $token): bool { $order = ReceiptIssuingOrder::whereAccessToken($token) ->first(); if (!($order instanceof ReceiptIssuingOrder)) { return false; } if ($order->access_token_expires_at === null) { return false; } if ($order->access_token !== $token) { logger("TOKEN 期限切れ"); return false; } $now = DateUtil::now(); $ret = $now->lt($this->order->access_token_expires_at); if (!$ret) { logger("TOKEN 期限切れ"); return false; } $this->order = $order; $this->initialized = true; return true; } protected function setStatus(): static { $order = $this->order; $order->status_done = false; // 郵送関連 if ($order->status_mail_post_date !== null) { $order->status_name = "投函済み"; $order->status_done = true; return $this; } if ($order->status_order_mail_datetime !== null) { $order->status_name = "郵送依頼中"; return $this; } // メール配信 if ($order->status_receipt_email_send_datetime !== null) { $order->status_name = "領収証メール配信済み"; $order->status_done = true; return $this; } if ($order->status_mail_post_date !== null) { $order->status_name = "領収証メール配信依頼中"; return $this; } // ダウンロード if ($order->status_receipt_download_datetime !== null) { $order->status_name = "ダウンロード済み"; $order->status_done = true; return $this; } // 申請段階 if ($order->status_receipt_confirm_datetime !== null) { $order->status_name = "確定済み"; return $this; } if ($order->status_first_access_datetime !== null) { $order->status_name = "アクセス済み"; return $this; } if ($order->status_sms_send_datetime !== null) { $order->status_name = "SMS配信済み"; return $this; } if ($order->sms_send_success === false) { $order->status_name = "SMS送信失敗"; return $this; } $order->status_name = "新規登録"; return $this; } protected function save(): static { $this->setStatus() ->updateCheck(); $this->order->save(); return $this; } public function setConfirm(): static { if ($this->order->receipt_no !== null) { throw new LogicException("領収証確定済み変更検知"); } $this->order->receipt_no = $this->generateReceiptnNo(); $this->order->status_receipt_confirm_datetime = DateUtil::now(); return $this; } private function generateReceiptnNo(): string { $count = 0; while (true) { $now = DateUtil::now(); $receiptNo = sprintf("%04d%02d%02d-%06d", $now->year, $now->month, $now->day, random_int(0, 999999)); if (!ReceiptIssuingOrder::whereReceiptNo($receiptNo)->exists()) { return $receiptNo; } $count++; if (1000 < $count) { throw new AppCommonException("領収証番号取得不正エラー"); } } } private function updateCheck(): static { // 確定済みデータの変更確認 if ($this->order->getOriginal(ReceiptIssuingOrder::COL_NAME_STATUS_RECEIPT_CONFIRM_DATETIME) !== null) { if ($this->order->isDirty([ ReceiptIssuingOrder::COL_NAME_RECEIPT_USE_DATE, ReceiptIssuingOrder::COL_NAME_RECEIPT_SHOP_NAME, ReceiptIssuingOrder::COL_NAME_RECEIPT_ISSUER, ReceiptIssuingOrder::COL_NAME_RECEIPT_PURPOSE, ReceiptIssuingOrder::COL_NAME_RECEIPT_NAME, ReceiptIssuingOrder::COL_NAME_RECEIPT_INVOICE_NO, ReceiptIssuingOrder::COL_NAME_RECEIPT_AMOUNT, ])) { throw new LogicException("確定済みデータの変更を検知"); } } return $this; } }