import { AppID } from "@/common/appids"; import { makeRecordData } from "@/common/rest-api-client"; import { 契約状況同期 } from "@/logic/契約状況同期"; import { ShowConfirmDialog } from "@/middleware/swal"; import { getDetailUrl } from "@/rest-api/url"; import { get定期駐車場プランマスタ } from "@/rest-api/定期駐車場プランマスタ"; import { get自動承認グループ } from "@/rest-api/自動承認グループ"; import { get車室契約情報, get車室契約情報一覧 } from "@/rest-api/車室契約情報"; import { get車室情報一覧 } from "@/rest-api/車室情報2"; import { getNextSMBC番号, getNext顧客コード, get顧客マスタ, } from "@/rest-api/顧客マスタ"; import { 定期申込予約, 定期申込予約フィールド名, 状態Dropdown, 自動承認ステータスDropdown, } from "@/types/定期申込予約"; import { 定期駐車場プランマスタ } from "@/types/定期駐車場プランマスタ"; import { 自動承認グループ } from "@/types/自動承認グループ"; import { 車室契約情報, 車室契約情報フィールド名 } from "@/types/車室契約情報"; import { 車室情報2 } from "@/types/車室情報2"; import { 顧客マスタフィールド名 } from "@/types/顧客マスタ"; import { sprintf } from "sprintf"; import { CancelError, Message } from "@/exception"; import { dateParse, now } from "@/common/datetime"; import bulkRequest from "@/rest-api/bulk"; export class 申込 { private 定期申込予約: 定期申込予約; private 初期化済み: boolean = false; private 顧客情報: { 顧客コード: number; } | null = null; private 車室一覧: 車室情報2[] = []; private 契約一覧: 車室契約情報[] = []; private 自動承認グループ: 自動承認グループ | null = null; private プラン: 定期駐車場プランマスタ | null = null; private 作成後契約: 車室契約情報 | null = null; constructor(定期申込予約: 定期申込予約) { this.定期申込予約 = 定期申込予約; } async 初期化() { // 車室情報の取得 await this.契約対象車室取得(); // 対象のプランデータ取得 await this.契約情報取得(); // 自動承認データの取得 await this.自動承認グループ取得(); // 定期駐車場プランマスタデータの取得 await this.プラン取得(); this.初期化済み = true; return this; } async 選定() { // 空き車室の特定 const target = this.選定情報取得(); // 顧客マスタの作成/取得 await this.顧客マスタ取得(); // 契約の作成 await this.契約情報作成(target); // 申込データの更新 await this.申込情報完了(); // データ保存 await this.save(); await 契約状況同期( this.定期申込予約.駐車場.value, Number(target.車室番号.value) ); } 選定情報取得() { return this.対象車室取得(); } 作成後契約取得() { return this.作成後契約; } private async 契約対象車室取得() { if (!this.定期申込予約.駐車場.value) { throw new Message("駐車場名の設定をしてください"); } this.車室一覧 = await get車室情報一覧({ 駐車場名: this.定期申込予約.駐車場.value, }); } private async 契約情報取得() { if (!this.定期申込予約.駐車場.value) { throw new Message("駐車場名の設定をしてください"); } const 基準日 = dateParse(this.定期申込予約.利用開始希望日.value) ?? now(); this.契約一覧 = await get車室契約情報一覧({ 駐車場名: this.定期申込予約.駐車場.value, 契約中のみ: 基準日, }); } private async 自動承認グループ取得() { if (!this.定期申込予約.定期駐車場プラン.value) { throw new Message("プラン名の設定をしてください"); } this.自動承認グループ = await get自動承認グループ( this.定期申込予約.定期駐車場プラン.value ); } private async プラン取得() { if (!this.定期申込予約.定期駐車場プラン.value) { throw new Message("プラン名の設定をしてください"); } this.プラン = await get定期駐車場プランマスタ( this.定期申込予約.定期駐車場プラン.value ); } private 対象車室取得() { if (!this.初期化済み) { throw new Error("実装エラー 未初期化"); } const target = this.自動承認グループ?.対象車室番号.value .filter(({ value: 定義 }) => { // 自動承認車室でない場合は対象外とする if (定義.自動承認スキップ.value.length !== 0) return false; // 契約中の車室は対象外とする const 同一車室の契約中情報: 車室契約情報 | undefined = this.契約一覧.find((契約) => { return 契約.車室番号.value === 定義.車室番号.value; }); if (!!同一車室の契約中情報) return false; return true; }) .sort((a, b) => { return Number(a.value.割当順.value) < Number(b.value.割当順.value) ? -1 : 1; }) .find(() => true); if (!target) { throw new Message("空き車室がありません"); } const 車室番号 = Number(target.value.車室番号.value); const 車室 = this.車室一覧.find((room) => { return Number(room.車室番号.value) === 車室番号; }); if (!車室) { throw new Message("車室がありません"); } return 車室; } private async 顧客マスタ取得() { const customer = await get顧客マスタ({ メールアドレス: this.定期申込予約.メールアドレス.value, 必須: false, }); if (customer) { const confirm = await ShowConfirmDialog({ html: sprintf( "
既存顧客[%s]様へ契約を追加しますか
", getDetailUrl(AppID.顧客マスタ, customer.$id.value), customer.CustomerName.value ), }); if (!confirm.isConfirmed) throw new CancelError(); this.顧客情報 = { 顧客コード: Number(customer[顧客マスタフィールド名.顧客コード].value), }; } else { await this.顧客マスタ作成(); } } private async 顧客マスタ作成() { const F = 顧客マスタフィールド名; const 顧客コード = await getNext顧客コード(); bulkRequest.create({ app: AppID.顧客マスタ, record: makeRecordData({ [F.顧客コード]: String(顧客コード), [F.顧客名]: this.定期申込予約.氏名.value, [F.顧客名カナ]: this.定期申込予約.フリガナ.value, [F.電話番号]: this.定期申込予約.電話番号.value, [F.メールアドレス]: this.定期申込予約.メールアドレス.value, [F.SMBC契約番号]: String(await getNextSMBC番号()), [F.支払方法]: "その他", }), }); this.顧客情報 = { 顧客コード, }; } private async 契約情報作成(対象車室: 車室情報2) { if (!this.顧客情報) throw new Error("顧客情報不正"); if (!this.定期申込予約) throw new Error("定期申込予約不正"); const F = 車室契約情報フィールド名; bulkRequest.create( { app: AppID.車室契約情報, record: makeRecordData({ [F.顧客コード]: String(this.顧客情報.顧客コード), [F.契約日]: this.get契約開始日(), [F.車両番号]: this.定期申込予約.車両番号.value, [F.車室番号]: 対象車室.車室番号.value, [F.プラン名]: this.定期申込予約.定期駐車場プラン.value, }), }, async ({ id }) => { if (!id) throw new Error(); this.作成後契約 = await get車室契約情報(Number(id)); } ); } private async 申込情報完了() { if (this.プラン === null) throw new Error(); const F = 定期申込予約フィールド名; bulkRequest.update({ app: AppID.定期申込予約, id: this.定期申込予約.$id.value, record: makeRecordData({ [F.状態]: 状態Dropdown.承認_自動承認, [F.自動承認ステータス]: 自動承認ステータスDropdown.承認済, [F.定期駐車料金]: this.プラン.契約金額.value, }), }); } private get契約開始日() { const 利用開始希望日 = this.定期申込予約.利用開始希望日.value; if (!利用開始希望日) throw new Message("利用開始希望日不正"); return 利用開始希望日; } private async save() { bulkRequest.debug(); await bulkRequest.save(); } }