京都のkintone用javascript
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.

261 lines
8.9KB

  1. import { AppID } from "@/common/appids";
  2. import { makeRecordData } from "@/common/rest-api-client";
  3. import { 契約状況同期 } from "@/logic/契約状況同期";
  4. import { ShowConfirmDialog } from "@/middleware/swal";
  5. import { getDetailUrl } from "@/rest-api/url";
  6. import { get定期駐車場プランマスタ } from "@/rest-api/定期駐車場プランマスタ";
  7. import { get自動承認グループ } from "@/rest-api/自動承認グループ";
  8. import { get車室契約情報, get車室契約情報一覧 } from "@/rest-api/車室契約情報";
  9. import { get車室情報一覧 } from "@/rest-api/車室情報2";
  10. import {
  11. getNextSMBC番号,
  12. getNext顧客コード,
  13. get顧客マスタ,
  14. } from "@/rest-api/顧客マスタ";
  15. import {
  16. 定期申込予約,
  17. 定期申込予約フィールド名,
  18. 状態Dropdown,
  19. 自動承認ステータスDropdown,
  20. } from "@/types/定期申込予約";
  21. import { 定期駐車場プランマスタ } from "@/types/定期駐車場プランマスタ";
  22. import { 自動承認グループ } from "@/types/自動承認グループ";
  23. import { 車室契約情報, 車室契約情報フィールド名 } from "@/types/車室契約情報";
  24. import { 車室情報2 } from "@/types/車室情報2";
  25. import { 顧客マスタフィールド名 } from "@/types/顧客マスタ";
  26. import { sprintf } from "sprintf";
  27. import { CancelError, Message } from "@/exception";
  28. import { dateParse, now } from "@/common/datetime";
  29. import bulkRequest from "@/rest-api/bulk";
  30. export class 申込 {
  31. private 定期申込予約: 定期申込予約;
  32. private 初期化済み: boolean = false;
  33. private 顧客情報: {
  34. 顧客コード: number;
  35. } | null = null;
  36. private 車室一覧: 車室情報2[] = [];
  37. private 契約一覧: 車室契約情報[] = [];
  38. private 自動承認グループ: 自動承認グループ | null = null;
  39. private プラン: 定期駐車場プランマスタ | null = null;
  40. private 作成後契約: 車室契約情報 | null = null;
  41. constructor(定期申込予約: 定期申込予約) {
  42. this.定期申込予約 = 定期申込予約;
  43. }
  44. async 初期化() {
  45. // 車室情報の取得
  46. await this.契約対象車室取得();
  47. // 対象のプランデータ取得
  48. await this.契約情報取得();
  49. // 自動承認データの取得
  50. await this.自動承認グループ取得();
  51. // 定期駐車場プランマスタデータの取得
  52. await this.プラン取得();
  53. this.初期化済み = true;
  54. return this;
  55. }
  56. async 選定() {
  57. // 空き車室の特定
  58. const target = this.選定情報取得();
  59. // 顧客マスタの作成/取得
  60. await this.顧客マスタ取得();
  61. // 契約の作成
  62. await this.契約情報作成(target);
  63. // 申込データの更新
  64. await this.申込情報完了();
  65. // データ保存
  66. await this.save();
  67. await 契約状況同期(
  68. this.定期申込予約.駐車場.value,
  69. Number(target.車室番号.value)
  70. );
  71. }
  72. 選定情報取得() {
  73. return this.対象車室取得();
  74. }
  75. 作成後契約取得() {
  76. return this.作成後契約;
  77. }
  78. private async 契約対象車室取得() {
  79. if (!this.定期申込予約.駐車場.value) {
  80. throw new Message("駐車場名の設定をしてください");
  81. }
  82. this.車室一覧 = await get車室情報一覧({
  83. 駐車場名: this.定期申込予約.駐車場.value,
  84. });
  85. }
  86. private async 契約情報取得() {
  87. if (!this.定期申込予約.駐車場.value) {
  88. throw new Message("駐車場名の設定をしてください");
  89. }
  90. const 基準日 = dateParse(this.定期申込予約.利用開始希望日.value) ?? now();
  91. this.契約一覧 = await get車室契約情報一覧({
  92. 駐車場名: this.定期申込予約.駐車場.value,
  93. 契約中のみ: 基準日,
  94. });
  95. }
  96. private async 自動承認グループ取得() {
  97. if (!this.定期申込予約.定期駐車場プラン.value) {
  98. throw new Message("プラン名の設定をしてください");
  99. }
  100. this.自動承認グループ = await get自動承認グループ(
  101. this.定期申込予約.定期駐車場プラン.value
  102. );
  103. }
  104. private async プラン取得() {
  105. if (!this.定期申込予約.定期駐車場プラン.value) {
  106. throw new Message("プラン名の設定をしてください");
  107. }
  108. this.プラン = await get定期駐車場プランマスタ(
  109. this.定期申込予約.定期駐車場プラン.value
  110. );
  111. }
  112. private 対象車室取得() {
  113. if (!this.初期化済み) {
  114. throw new Error("実装エラー 未初期化");
  115. }
  116. const target = this.自動承認グループ?.対象車室番号.value
  117. .filter(({ value: 定義 }) => {
  118. // 自動承認車室でない場合は対象外とする
  119. if (定義.自動承認スキップ.value.length !== 0) return false;
  120. // 契約中の車室は対象外とする
  121. const 同一車室の契約中情報: 車室契約情報 | undefined =
  122. this.契約一覧.find((契約) => {
  123. return 契約.車室番号.value === 定義.車室番号.value;
  124. });
  125. if (!!同一車室の契約中情報) return false;
  126. return true;
  127. })
  128. .sort((a, b) => {
  129. return Number(a.value.割当順.value) < Number(b.value.割当順.value)
  130. ? -1
  131. : 1;
  132. })
  133. .find(() => true);
  134. if (!target) {
  135. throw new Message("空き車室がありません");
  136. }
  137. const 車室番号 = Number(target.value.車室番号.value);
  138. const 車室 = this.車室一覧.find((room) => {
  139. return Number(room.車室番号.value) === 車室番号;
  140. });
  141. if (!車室) {
  142. throw new Message("車室がありません");
  143. }
  144. return 車室;
  145. }
  146. private async 顧客マスタ取得() {
  147. const customer = await get顧客マスタ({
  148. メールアドレス: this.定期申込予約.メールアドレス.value,
  149. 必須: false,
  150. });
  151. if (customer) {
  152. const confirm = await ShowConfirmDialog({
  153. html: sprintf(
  154. "<p>既存顧客<a href='%s' target='_blank'>[%s]</a>様へ契約を追加しますか</p>",
  155. getDetailUrl(AppID.顧客マスタ, customer.$id.value),
  156. customer.CustomerName.value
  157. ),
  158. });
  159. if (!confirm.isConfirmed) throw new CancelError();
  160. this.顧客情報 = {
  161. 顧客コード: Number(customer[顧客マスタフィールド名.顧客コード].value),
  162. };
  163. } else {
  164. await this.顧客マスタ作成();
  165. }
  166. }
  167. private async 顧客マスタ作成() {
  168. const F = 顧客マスタフィールド名;
  169. const 顧客コード = await getNext顧客コード();
  170. bulkRequest.create({
  171. app: AppID.顧客マスタ,
  172. record: makeRecordData({
  173. [F.顧客コード]: String(顧客コード),
  174. [F.顧客名]: this.定期申込予約.氏名.value,
  175. [F.顧客名カナ]: this.定期申込予約.フリガナ.value,
  176. [F.電話番号]: this.定期申込予約.電話番号.value,
  177. [F.メールアドレス]: this.定期申込予約.メールアドレス.value,
  178. [F.SMBC契約番号]: String(await getNextSMBC番号()),
  179. [F.支払方法]: "その他",
  180. }),
  181. });
  182. this.顧客情報 = {
  183. 顧客コード,
  184. };
  185. }
  186. private async 契約情報作成(対象車室: 車室情報2) {
  187. if (!this.顧客情報) throw new Error("顧客情報不正");
  188. if (!this.定期申込予約) throw new Error("定期申込予約不正");
  189. const F = 車室契約情報フィールド名;
  190. bulkRequest.create(
  191. {
  192. app: AppID.車室契約情報,
  193. record: makeRecordData({
  194. [F.顧客コード]: String(this.顧客情報.顧客コード),
  195. [F.契約日]: this.get契約開始日(),
  196. [F.車両番号]: this.定期申込予約.車両番号.value,
  197. [F.車室番号]: 対象車室.車室番号.value,
  198. [F.プラン名]: this.定期申込予約.定期駐車場プラン.value,
  199. }),
  200. },
  201. async ({ id }) => {
  202. if (!id) throw new Error();
  203. this.作成後契約 = await get車室契約情報(Number(id));
  204. }
  205. );
  206. }
  207. private async 申込情報完了() {
  208. if (this.プラン === null) throw new Error();
  209. const F = 定期申込予約フィールド名;
  210. bulkRequest.update({
  211. app: AppID.定期申込予約,
  212. id: this.定期申込予約.$id.value,
  213. record: makeRecordData({
  214. [F.状態]: 状態Dropdown.承認_自動承認,
  215. [F.自動承認ステータス]: 自動承認ステータスDropdown.承認済,
  216. [F.定期駐車料金]: this.プラン.契約金額.value,
  217. }),
  218. });
  219. }
  220. private get契約開始日() {
  221. const 利用開始希望日 = this.定期申込予約.利用開始希望日.value;
  222. if (!利用開始希望日) throw new Message("利用開始希望日不正");
  223. return 利用開始希望日;
  224. }
  225. private async save() {
  226. bulkRequest.debug();
  227. await bulkRequest.save();
  228. }
  229. }