京都のkintone用javascript
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

253 řádky
8.9KB

  1. import { eventHnalder } from "@/common/app-template";
  2. import { AppID } from "@/common/appids";
  3. import { formatDateStr, today } from "@/common/datetime";
  4. import { initMenuBox, setHeaderButton } from "@/common/header-button";
  5. import { KintoneEvent, 値設定 } from "@/common/kintone-event";
  6. import { makeRecordData } from "@/common/rest-api-client";
  7. import { Message } from "@/exception";
  8. import { EmailID, sendEmail } from "@/mypage/メール";
  9. import { makeReceipt } from "@/mypage/領収証発行";
  10. import bulkRequest from "@/rest-api/bulk";
  11. import { get定期申込予約 } from "@/rest-api/定期申込予約";
  12. import { get顧客マスタ } from "@/rest-api/顧客マスタ";
  13. import { 入金予定結果, 入金予定結果フィールド名 } from "@/types/入金予定結果";
  14. import { 定期申込予約フィールド名 } from "@/types/定期申込予約";
  15. import { 顧客マスタフィールド名 } from "@/types/顧客マスタ";
  16. import { KintoneRestAPIClient } from "@kintone/rest-api-client";
  17. import { addDays, format, setHours } from "date-fns";
  18. import Swal from "sweetalert2";
  19. const client = new KintoneRestAPIClient();
  20. const 残金設定 = (record: 入金予定結果) => {
  21. record[入金予定結果フィールド名.残金].value = (() => {
  22. const amount =
  23. Number(record[入金予定結果フィールド名.支払予定金額].value ?? 0) -
  24. Number(record[入金予定結果フィールド名.入金額].value ?? 0);
  25. return String(amount);
  26. })();
  27. };
  28. const 承認メール送信 = (record: 入金予定結果) => {
  29. return sendEmail(EmailID.申込承認, {
  30. season_ticket_contract_entry_record_no: Number(
  31. record[入金予定結果フィールド名.初回振り込み関連申込レコード番号].value
  32. ),
  33. });
  34. };
  35. const 領収証作成 = () => {
  36. return makeReceipt([Number(kintone.app.record.getId())]);
  37. };
  38. const テスト充当 = async (record: 入金予定結果) => {
  39. await client.record.updateRecord({
  40. app: AppID.入金予定結果,
  41. id: kintone.app.record.getId() ?? "",
  42. record: makeRecordData({
  43. [入金予定結果フィールド名.入金日]: format(new Date(), "yyyy-MM-dd"),
  44. [入金予定結果フィールド名.入金額]:
  45. record[入金予定結果フィールド名.支払予定金額].value,
  46. [入金予定結果フィールド名.残金]: "0",
  47. }),
  48. });
  49. await 領収証作成();
  50. if (
  51. !!record[入金予定結果フィールド名.初回振り込み関連申込レコード番号].value
  52. ) {
  53. await 承認メール送信(record);
  54. }
  55. Swal.fire({
  56. title: "テスト充当完了",
  57. icon: "success",
  58. timer: 1000,
  59. timerProgressBar: true,
  60. }).then((result) => {
  61. location.reload();
  62. });
  63. };
  64. const getCallbackFillAmount = (record: 入金予定結果) => {
  65. return async () => {
  66. Swal.fire({
  67. title: "テスト充当",
  68. text: "!注意!テスト用の機能",
  69. showCancelButton: true,
  70. confirmButtonText: "実行",
  71. cancelButtonText: "キャンセル",
  72. }).then((result) => {
  73. if (result.isConfirmed) {
  74. テスト充当(record);
  75. }
  76. });
  77. };
  78. };
  79. // クエリパラメータよりフィールドに値をセットする
  80. const setData = (event: any, targets: string[]) => {
  81. const param = new URLSearchParams(location.search.replace("?", ""));
  82. targets.forEach((target) => {
  83. const value = param.get(target);
  84. if (value === null) return;
  85. 値設定(event, target, value, true);
  86. });
  87. };
  88. (() => {
  89. console.info("script build at " + process.env.BUILD_TIME);
  90. kintone.events.on(
  91. [KintoneEvent.詳細.レコード詳細画面を表示した後],
  92. eventHnalder(async (event) => {
  93. const currentRecord = event.record as 入金予定結果;
  94. await initMenuBox();
  95. // 各種ボタンの設置
  96. if (currentRecord[入金予定結果フィールド名.残金].value !== "0") {
  97. if (currentRecord.parking_name.value.startsWith("TP")) {
  98. setHeaderButton("テスト充当", getCallbackFillAmount(currentRecord));
  99. }
  100. }
  101. })
  102. );
  103. const 初回請求金額確定チェック = (record: 入金予定結果) => {
  104. if (!record.first_payment_entry_record_no.value) {
  105. throw new Message("初回振り込み関連申込レコード番号が入力されていません");
  106. }
  107. // 申込の初回振り込み情報を入力する
  108. const F = 定期申込予約フィールド名;
  109. const 支払予定日 = record.payment_plan_date.value;
  110. if (!支払予定日) {
  111. throw new Message("支払予定日が入力されていません");
  112. }
  113. const 支払予定金額 = record.payment_plan_amount.value;
  114. if (!支払予定金額) {
  115. throw new Message("支払予定金額が入力されていません");
  116. }
  117. };
  118. const 初回請求金額確定 = async (record: 入金予定結果) => {
  119. 初回請求金額確定チェック(record);
  120. const 申込 = await get定期申込予約(
  121. record.first_payment_entry_record_no.value
  122. );
  123. // 申込の初回振り込み情報を入力する
  124. const F = 定期申込予約フィールド名;
  125. const 支払予定日 = record.payment_plan_date.value ?? "";
  126. const 支払予定金額 = record.payment_plan_amount.value ?? "";
  127. const 日割り金額 = Number(支払予定金額) - Number(申込.定期駐車料金.value);
  128. const 日割り月 = (() => {
  129. if (日割り金額 === 0) return "";
  130. let month = Number(record.target_month.value) - 1;
  131. if (month === 0) {
  132. month = 12;
  133. }
  134. return String(month);
  135. })();
  136. bulkRequest.update({
  137. app: AppID.定期申込予約,
  138. id: record.first_payment_entry_record_no.value,
  139. record: makeRecordData({
  140. [F.振込期日]: 支払予定日,
  141. [F.初回振り込み合計額]: 支払予定金額,
  142. [F.請求対象分_月]: record.target_month.value,
  143. [F.請求対象分_金額]: 申込.定期駐車料金.value,
  144. [F.日割り分_金額]: String(日割り金額),
  145. [F.日割り分_月]: 日割り月,
  146. }),
  147. });
  148. await bulkRequest.save();
  149. };
  150. kintone.events.on(
  151. KintoneEvent.追加.レコード追加画面を表示した後,
  152. eventHnalder(async (event) => {
  153. const record = event.record as 入金予定結果;
  154. setData(event, [
  155. 入金予定結果フィールド名.車室情報管理レコード番号,
  156. 入金予定結果フィールド名.初回振り込み関連申込レコード番号,
  157. 入金予定結果フィールド名.支払予定金額,
  158. 入金予定結果フィールド名.支払種別,
  159. 入金予定結果フィールド名.支払方法,
  160. ]);
  161. 残金設定(record);
  162. return event;
  163. })
  164. );
  165. kintone.events.on(
  166. KintoneEvent.追加.保存するとき,
  167. eventHnalder(async (event: any) => {
  168. const record = event.record as 入金予定結果;
  169. // 初回請求確定時にはメールを送信する
  170. if (
  171. record[入金予定結果フィールド名.初回振り込み関連申込レコード番号].value
  172. ) {
  173. await 初回請求金額確定(record);
  174. }
  175. })
  176. );
  177. kintone.events.on(
  178. KintoneEvent.追加.保存に成功した後,
  179. eventHnalder(async (event: any) => {
  180. const record = event.record as 入金予定結果;
  181. // 初回請求確定時にはメールを送信する
  182. if (
  183. record[入金予定結果フィールド名.初回振り込み関連申込レコード番号].value
  184. ) {
  185. // メール送信
  186. sendEmail(EmailID.申込受付, {
  187. season_ticket_contract_entry_record_no: Number(
  188. record[入金予定結果フィールド名.初回振り込み関連申込レコード番号]
  189. .value
  190. ),
  191. });
  192. // 口座振替登録催促日時を設定する
  193. const 口座登録催促日時 = (() => {
  194. return formatDateStr(setHours(addDays(today(), 5), 9));
  195. })();
  196. const 顧客 = await get顧客マスタ({
  197. 顧客コード: Number(record.customer_code.value),
  198. });
  199. bulkRequest.update({
  200. app: AppID.顧客マスタ,
  201. id: 顧客?.$id.value,
  202. record: makeRecordData({
  203. [顧客マスタフィールド名.口座登録催促予定日時]: 口座登録催促日時,
  204. }),
  205. });
  206. }
  207. })
  208. );
  209. kintone.events.on(
  210. [
  211. KintoneEvent.追加.フィールドの値を変更したとき(
  212. 入金予定結果フィールド名.支払予定金額
  213. ),
  214. KintoneEvent.追加.フィールドの値を変更したとき(
  215. 入金予定結果フィールド名.入金額
  216. ),
  217. KintoneEvent.編集.フィールドの値を変更したとき(
  218. 入金予定結果フィールド名.支払予定金額
  219. ),
  220. KintoneEvent.編集.フィールドの値を変更したとき(
  221. 入金予定結果フィールド名.入金額
  222. ),
  223. ],
  224. (event) => {
  225. const record = event.record as 入金予定結果;
  226. 残金設定(record);
  227. return event;
  228. }
  229. );
  230. })();