repzo-sap-absjo 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (157) hide show
  1. package/README.md +3 -0
  2. package/changelog.md +17 -0
  3. package/lib/actions/create_invoice.d.ts +26 -0
  4. package/lib/actions/create_invoice.js +311 -0
  5. package/lib/actions/create_payment.d.ts +5 -0
  6. package/lib/actions/create_payment.js +177 -0
  7. package/lib/actions/create_proforma.d.ts +5 -0
  8. package/lib/actions/create_proforma.js +241 -0
  9. package/lib/actions/create_return_invoice.d.ts +5 -0
  10. package/lib/actions/create_return_invoice.js +268 -0
  11. package/lib/actions/create_transfer.d.ts +5 -0
  12. package/lib/actions/create_transfer.js +171 -0
  13. package/lib/actions/index.d.ts +3 -0
  14. package/lib/actions/index.js +31 -0
  15. package/lib/commands/adjust_inventory.d.ts +6 -0
  16. package/lib/commands/adjust_inventory.js +303 -0
  17. package/lib/commands/bank.d.ts +7 -0
  18. package/lib/commands/bank.js +301 -0
  19. package/lib/commands/basic.d.ts +2 -0
  20. package/lib/commands/basic.js +73 -0
  21. package/lib/commands/category.d.ts +4 -0
  22. package/lib/commands/category.js +193 -0
  23. package/lib/commands/channel.d.ts +4 -0
  24. package/lib/commands/channel.js +213 -0
  25. package/lib/commands/client.d.ts +39 -0
  26. package/lib/commands/client.js +410 -0
  27. package/lib/commands/client_disabled.d.ts +4 -0
  28. package/lib/commands/client_disabled.js +183 -0
  29. package/lib/commands/index.d.ts +23 -0
  30. package/lib/commands/index.js +82 -0
  31. package/lib/commands/join.d.ts +2 -0
  32. package/lib/commands/join.js +134 -0
  33. package/lib/commands/measureunit.d.ts +23 -0
  34. package/lib/commands/measureunit.js +314 -0
  35. package/lib/commands/measureunit_family.d.ts +12 -0
  36. package/lib/commands/measureunit_family.js +263 -0
  37. package/lib/commands/payment_term.d.ts +4 -0
  38. package/lib/commands/payment_term.js +217 -0
  39. package/lib/commands/price_list.d.ts +18 -0
  40. package/lib/commands/price_list.js +608 -0
  41. package/lib/commands/product.d.ts +39 -0
  42. package/lib/commands/product.js +454 -0
  43. package/lib/commands/product_disabled.d.ts +4 -0
  44. package/lib/commands/product_disabled.js +178 -0
  45. package/lib/commands/rep.d.ts +2 -0
  46. package/lib/commands/rep.js +246 -0
  47. package/lib/commands/tag.d.ts +2 -0
  48. package/lib/commands/tag.js +195 -0
  49. package/lib/commands/tax.d.ts +2 -0
  50. package/lib/commands/tax.js +198 -0
  51. package/lib/commands/warehouse.d.ts +4 -0
  52. package/lib/commands/warehouse.js +221 -0
  53. package/lib/index.d.ts +29 -0
  54. package/lib/index.js +6 -0
  55. package/lib/test/actions/create_invoice.d.ts +1 -0
  56. package/lib/test/actions/create_invoice.js +214 -0
  57. package/lib/test/actions/create_payment.d.ts +1 -0
  58. package/lib/test/actions/create_payment.js +131 -0
  59. package/lib/test/actions/create_proforma.d.ts +1 -0
  60. package/lib/test/actions/create_proforma.js +242 -0
  61. package/lib/test/actions/create_return_invoice.d.ts +1 -0
  62. package/lib/test/actions/create_return_invoice.js +226 -0
  63. package/lib/test/actions/create_transfer.d.ts +1 -0
  64. package/lib/test/actions/create_transfer.js +142 -0
  65. package/lib/test/commands/adjust_inventory.d.ts +1 -0
  66. package/lib/test/commands/adjust_inventory.js +97 -0
  67. package/lib/test/commands/bank.d.ts +1 -0
  68. package/lib/test/commands/bank.js +97 -0
  69. package/lib/test/commands/basic.d.ts +1 -0
  70. package/lib/test/commands/basic.js +339 -0
  71. package/lib/test/commands/category.d.ts +1 -0
  72. package/lib/test/commands/category.js +97 -0
  73. package/lib/test/commands/channel.d.ts +1 -0
  74. package/lib/test/commands/channel.js +97 -0
  75. package/lib/test/commands/client.d.ts +1 -0
  76. package/lib/test/commands/client.js +97 -0
  77. package/lib/test/commands/client_disabled.d.ts +1 -0
  78. package/lib/test/commands/client_disabled.js +97 -0
  79. package/lib/test/commands/join.d.ts +1 -0
  80. package/lib/test/commands/join.js +339 -0
  81. package/lib/test/commands/measureunit.d.ts +1 -0
  82. package/lib/test/commands/measureunit.js +97 -0
  83. package/lib/test/commands/measureunit_family.d.ts +1 -0
  84. package/lib/test/commands/measureunit_family.js +97 -0
  85. package/lib/test/commands/payment_term.d.ts +1 -0
  86. package/lib/test/commands/payment_term.js +97 -0
  87. package/lib/test/commands/price_list.d.ts +1 -0
  88. package/lib/test/commands/price_list.js +97 -0
  89. package/lib/test/commands/product.d.ts +1 -0
  90. package/lib/test/commands/product.js +97 -0
  91. package/lib/test/commands/product_disabled.d.ts +1 -0
  92. package/lib/test/commands/product_disabled.js +97 -0
  93. package/lib/test/commands/rep.d.ts +1 -0
  94. package/lib/test/commands/rep.js +97 -0
  95. package/lib/test/commands/tag.d.ts +1 -0
  96. package/lib/test/commands/tag.js +97 -0
  97. package/lib/test/commands/tax.d.ts +1 -0
  98. package/lib/test/commands/tax.js +97 -0
  99. package/lib/test/commands/warehouse.d.ts +1 -0
  100. package/lib/test/commands/warehouse.js +97 -0
  101. package/lib/types.d.ts +77 -0
  102. package/lib/types.js +1 -0
  103. package/lib/util.d.ts +70 -0
  104. package/lib/util.js +164 -0
  105. package/package.json +51 -0
  106. package/src/actions/create_invoice.ts +275 -0
  107. package/src/actions/create_payment.ts +138 -0
  108. package/src/actions/create_proforma.ts +187 -0
  109. package/src/actions/create_return_invoice.ts +217 -0
  110. package/src/actions/create_transfer.ts +160 -0
  111. package/src/actions/index.ts +27 -0
  112. package/src/commands/adjust_inventory.ts +250 -0
  113. package/src/commands/bank.ts +243 -0
  114. package/src/commands/basic.ts +64 -0
  115. package/src/commands/category.ts +176 -0
  116. package/src/commands/channel.ts +219 -0
  117. package/src/commands/client.ts +368 -0
  118. package/src/commands/client_disabled.ts +158 -0
  119. package/src/commands/index.ts +87 -0
  120. package/src/commands/join.ts +80 -0
  121. package/src/commands/measureunit.ts +276 -0
  122. package/src/commands/measureunit_family.ts +209 -0
  123. package/src/commands/payment_term.ts +224 -0
  124. package/src/commands/price_list.ts +434 -0
  125. package/src/commands/product.ts +359 -0
  126. package/src/commands/product_disabled.ts +152 -0
  127. package/src/commands/rep.ts +209 -0
  128. package/src/commands/tag.ts +173 -0
  129. package/src/commands/tax.ts +178 -0
  130. package/src/commands/warehouse.ts +204 -0
  131. package/src/index.ts +7 -0
  132. package/src/test/actions/create_invoice.ts +214 -0
  133. package/src/test/actions/create_payment.ts +131 -0
  134. package/src/test/actions/create_proforma.ts +242 -0
  135. package/src/test/actions/create_return_invoice.ts +226 -0
  136. package/src/test/actions/create_transfer.ts +142 -0
  137. package/src/test/commands/adjust_inventory.ts +100 -0
  138. package/src/test/commands/bank.ts +100 -0
  139. package/src/test/commands/basic.ts +342 -0
  140. package/src/test/commands/category.ts +100 -0
  141. package/src/test/commands/channel.ts +100 -0
  142. package/src/test/commands/client.ts +100 -0
  143. package/src/test/commands/client_disabled.ts +100 -0
  144. package/src/test/commands/join.ts +342 -0
  145. package/src/test/commands/measureunit.ts +100 -0
  146. package/src/test/commands/measureunit_family.ts +100 -0
  147. package/src/test/commands/payment_term.ts +100 -0
  148. package/src/test/commands/price_list.ts +100 -0
  149. package/src/test/commands/product.ts +100 -0
  150. package/src/test/commands/product_disabled.ts +100 -0
  151. package/src/test/commands/rep.ts +100 -0
  152. package/src/test/commands/tag.ts +100 -0
  153. package/src/test/commands/tax.ts +100 -0
  154. package/src/test/commands/warehouse.ts +100 -0
  155. package/src/types.ts +76 -0
  156. package/src/util.ts +205 -0
  157. package/tsconfig.json +15 -0
@@ -0,0 +1,276 @@
1
+ import Repzo from "repzo";
2
+ import { Service } from "repzo/src/types";
3
+ import DataSet from "data-set-query";
4
+ import { CommandEvent, Result, FailedDocsReport } from "../types";
5
+ import {
6
+ _fetch,
7
+ _create,
8
+ _update,
9
+ _delete,
10
+ update_bench_time,
11
+ date_formatting,
12
+ set_error,
13
+ } from "../util.js";
14
+
15
+ export interface SAPUoM {
16
+ ITEMCODE: string; // "010-BIC-RA0001",
17
+ UOMGROUPENTRY: number; // 133;
18
+ UOMGROUPCODE: string; // "BOXn",
19
+ UOMGROUPNAME: string; // "BOXn",
20
+ BASEUOMID: number; // 67;
21
+ BASEUOMCODE: string; // "BOX",
22
+ BASEQTY: number; // 1.0;
23
+ ALTUOMID: number; // 67;
24
+ ALTUOMCODE: string; // "BOX",
25
+ ALTQTY: number; // 1.0;
26
+ repzo_factor?: number;
27
+ }
28
+
29
+ interface SAPUoMs {
30
+ result: "Success";
31
+ UoM: SAPUoM[];
32
+ }
33
+
34
+ export const sync_measureunit = async (commandEvent: CommandEvent) => {
35
+ const repzo = new Repzo(commandEvent.app.formData?.repzoApiKey, {
36
+ env: commandEvent.env,
37
+ });
38
+
39
+ const commandLog = new Repzo.CommandLog(
40
+ repzo,
41
+ commandEvent.app,
42
+ commandEvent.command
43
+ );
44
+ try {
45
+ console.log("sync_measureunit");
46
+
47
+ const new_bench_time = new Date().toISOString();
48
+ const bench_time_key = "bench_time_measureunit";
49
+
50
+ await commandLog.load(commandEvent.sync_id);
51
+ await commandLog
52
+ .addDetail("Repzo SAP: Started Syncing Measure units")
53
+ .commit();
54
+
55
+ const nameSpace: string = commandEvent.nameSpace.join("_");
56
+ const result: Result = {
57
+ sap_total: 0,
58
+ repzo_total: 0,
59
+ created: 0,
60
+ updated: 0,
61
+ failed: 0,
62
+ };
63
+ const failed_docs_report: FailedDocsReport = [];
64
+
65
+ const sap_UoMs: SAPUoM[] = await get_sap_UoMs(
66
+ commandEvent.app.formData.sapHostUrl,
67
+ {}
68
+ );
69
+ result.sap_total = sap_UoMs?.length;
70
+
71
+ await commandLog
72
+ .addDetail(`${result.sap_total} Unit of Measures in SAP`)
73
+ .commit();
74
+
75
+ // Get Repzo default measureunit
76
+ const repzo_UoM_parent = await repzo.measureunit.find({
77
+ parent: "nil",
78
+ disabled: false,
79
+ });
80
+
81
+ if (!repzo_UoM_parent?.data || repzo_UoM_parent?.data?.length != 1)
82
+ throw "the parent of measure unit is not found or the nameSpace has more than one";
83
+ const repzo_parent_id = repzo_UoM_parent.data[0]._id;
84
+
85
+ const repzo_UoMs = await repzo.measureunit.find({ per_page: 50000 });
86
+ result.repzo_total = repzo_UoMs?.data?.length;
87
+ await commandLog
88
+ .addDetail(`${repzo_UoMs?.data?.length} Measure Units in Repzo`)
89
+ .commit();
90
+
91
+ const byProduct: { [ket: string]: SAPUoM[] } = {};
92
+ sap_UoMs.forEach((unit) => {
93
+ if (!byProduct[unit.ITEMCODE]) byProduct[unit.ITEMCODE] = [];
94
+ byProduct[unit.ITEMCODE].push(unit);
95
+ });
96
+
97
+ Object.values(byProduct).forEach((units: SAPUoM[]) => {
98
+ const max_unit: {
99
+ value: number | null;
100
+ sap_product_UoMs: SAPUoM[];
101
+ default_unit?: SAPUoM | null;
102
+ } = {
103
+ sap_product_UoMs: [],
104
+ value: null,
105
+ default_unit: null,
106
+ };
107
+ units.forEach((unit) => {
108
+ if (max_unit.value == null || unit.BASEQTY > max_unit.value) {
109
+ max_unit.value = unit.BASEQTY;
110
+ max_unit.sap_product_UoMs.push(unit);
111
+ } else if (unit.BASEQTY == max_unit.value) {
112
+ max_unit.sap_product_UoMs.push(unit);
113
+ }
114
+ });
115
+
116
+ if (max_unit.sap_product_UoMs.length > 1) {
117
+ max_unit.default_unit = max_unit.sap_product_UoMs.find(
118
+ (u) => u.ALTUOMCODE == "PC"
119
+ );
120
+ if (!max_unit.default_unit) {
121
+ console.log(
122
+ "Create/Update Measure Unit Failed >> ",
123
+ `${max_unit?.sap_product_UoMs[0]?.ITEMCODE} Could not found the base_unit`,
124
+ units
125
+ );
126
+ failed_docs_report.push({
127
+ method: "create",
128
+ doc_id:
129
+ max_unit?.sap_product_UoMs[0]?.ITEMCODE || units[0]?.ITEMCODE,
130
+ doc: units,
131
+ error_message: set_error(
132
+ `Create/Update Measure Unit Failed >> ${max_unit?.sap_product_UoMs[0]?.ITEMCODE} Could not found the base_unit`
133
+ ),
134
+ });
135
+ result.failed++;
136
+ return;
137
+ }
138
+ } else {
139
+ max_unit.default_unit = max_unit.sap_product_UoMs[0];
140
+ }
141
+
142
+ units.forEach((unit) => {
143
+ if (max_unit?.default_unit)
144
+ unit.repzo_factor =
145
+ (unit.ALTQTY / unit.BASEQTY) * max_unit?.default_unit?.BASEQTY;
146
+ });
147
+ });
148
+
149
+ let unique_UoMs: { [key: string]: SAPUoM } | SAPUoM[] = {};
150
+
151
+ for (let i = 0; i < sap_UoMs?.length; i++) {
152
+ const Uom = sap_UoMs[i];
153
+ const key = `${Uom.UOMGROUPENTRY}_${Uom.ALTUOMID}`;
154
+ if (!unique_UoMs[key]) unique_UoMs[key] = Uom;
155
+ }
156
+
157
+ unique_UoMs = Object.values(unique_UoMs);
158
+
159
+ const db = new DataSet([], { autoIndex: false });
160
+ db.createIndex({
161
+ ALTUOMCODE: true,
162
+ repzo_factor: true,
163
+ UOMGROUPENTRY: true,
164
+ ALTUOMID: true,
165
+ });
166
+ db.load(unique_UoMs);
167
+
168
+ for (let i = 0; i < unique_UoMs?.length; i++) {
169
+ const sap_UoM: SAPUoM = unique_UoMs[i];
170
+ const repzo_UoM = repzo_UoMs.data.find(
171
+ (r_UoM) =>
172
+ r_UoM.integration_meta?.id ==
173
+ `${nameSpace}_${sap_UoM.UOMGROUPENTRY}_${sap_UoM.ALTUOMID}`
174
+ );
175
+
176
+ const body:
177
+ | Service.MeasureUnit.Create.Body
178
+ | Service.MeasureUnit.Update.Body = {
179
+ parent: repzo_parent_id,
180
+ name: sap_UoM.ALTUOMCODE,
181
+ factor: sap_UoM.repzo_factor || 0, // ??????
182
+ disabled: false,
183
+ integration_meta: {
184
+ id: `${nameSpace}_${sap_UoM.UOMGROUPENTRY}_${sap_UoM.ALTUOMID}`,
185
+ UOMGROUPENTRY: sap_UoM.UOMGROUPENTRY,
186
+ ALTUOMID: sap_UoM.ALTUOMID,
187
+ },
188
+ company_namespace: [nameSpace],
189
+ };
190
+
191
+ if (!repzo_UoM) {
192
+ // Create
193
+ try {
194
+ const created_UoM = await repzo.measureunit.create(
195
+ body as Service.MeasureUnit.Create.Body
196
+ );
197
+ result.created++;
198
+ } catch (e: any) {
199
+ console.log("Create Measure Unit Failed >> ", e?.response, body);
200
+ failed_docs_report.push({
201
+ method: "create",
202
+ doc: body,
203
+ error_message: set_error(e),
204
+ });
205
+ result.failed++;
206
+ }
207
+ } else {
208
+ const found_identical_docs = db.search({
209
+ ALTUOMCODE: repzo_UoM.name,
210
+ repzo_factor: repzo_UoM.factor,
211
+ UOMGROUPENTRY: repzo_UoM.integration_meta?.UOMGROUPENTRY,
212
+ ALTUOMID: repzo_UoM.integration_meta?.ALTUOMID,
213
+ });
214
+ if (found_identical_docs.length) continue;
215
+ // Update
216
+ try {
217
+ const updated_UoM = await repzo.measureunit.update(
218
+ repzo_UoM._id,
219
+ body as Service.MeasureUnit.Update.Body
220
+ );
221
+ result.updated++;
222
+ } catch (e: any) {
223
+ console.log(
224
+ "Update Measure Unit Failed >> ",
225
+ e?.response?.data,
226
+ body
227
+ );
228
+ failed_docs_report.push({
229
+ method: "update",
230
+ doc_id: repzo_UoM?._id,
231
+ doc: body,
232
+ error_message: set_error(e),
233
+ });
234
+ result.failed++;
235
+ }
236
+ }
237
+ }
238
+
239
+ // console.log(result);
240
+
241
+ await update_bench_time(
242
+ repzo,
243
+ commandEvent.app._id,
244
+ bench_time_key,
245
+ new_bench_time
246
+ );
247
+ await commandLog
248
+ .setStatus(
249
+ "success",
250
+ failed_docs_report.length ? failed_docs_report : null
251
+ )
252
+ .setBody(result)
253
+ .commit();
254
+ return result;
255
+ } catch (e: any) {
256
+ //@ts-ignore
257
+ console.error(e?.response?.data || e);
258
+ await commandLog.setStatus("fail", e).commit();
259
+ throw e;
260
+ }
261
+ };
262
+
263
+ export const get_sap_UoMs = async (
264
+ serviceEndPoint: string,
265
+ query?: { updateAt?: string }
266
+ ): Promise<SAPUoM[]> => {
267
+ try {
268
+ const sap_UoMs: SAPUoMs = await _create(serviceEndPoint, "/Uom", {
269
+ Inactive: "N",
270
+ Locked: "N",
271
+ });
272
+ return sap_UoMs?.UoM;
273
+ } catch (e: any) {
274
+ throw e;
275
+ }
276
+ };
@@ -0,0 +1,209 @@
1
+ import Repzo from "repzo";
2
+ import { Service } from "repzo/src/types";
3
+ import DataSet from "data-set-query";
4
+ import _ from "lodash";
5
+ import { CommandEvent, Result, FailedDocsReport } from "../types";
6
+ import {
7
+ _fetch,
8
+ _create,
9
+ _update,
10
+ _delete,
11
+ update_bench_time,
12
+ date_formatting,
13
+ set_error,
14
+ } from "../util.js";
15
+
16
+ import { get_sap_UoMs, SAPUoM } from "./measureunit.js";
17
+
18
+ export const sync_measureunit_family = async (commandEvent: CommandEvent) => {
19
+ const repzo = new Repzo(commandEvent.app.formData?.repzoApiKey, {
20
+ env: commandEvent.env,
21
+ });
22
+
23
+ const commandLog = new Repzo.CommandLog(
24
+ repzo,
25
+ commandEvent.app,
26
+ commandEvent.command
27
+ );
28
+ try {
29
+ console.log("sync_measureunit_family");
30
+
31
+ const new_bench_time = new Date().toISOString();
32
+ const bench_time_key = "bench_time_measureunit_family";
33
+
34
+ await commandLog.load(commandEvent.sync_id);
35
+ await commandLog
36
+ .addDetail("Repzo SAP: Started Syncing Measure Units Family")
37
+ .commit();
38
+
39
+ const nameSpace: string = commandEvent.nameSpace.join("_");
40
+ const result = {
41
+ sap_total: 0,
42
+ repzo_total: 0,
43
+ sap_UoM_total: 0,
44
+ repzo_UoM_total: 0,
45
+ created: 0,
46
+ updated: 0,
47
+ failed: 0,
48
+ };
49
+ const failed_docs_report: FailedDocsReport = [];
50
+
51
+ const sap_UoMs: SAPUoM[] = await get_sap_UoMs(
52
+ commandEvent.app.formData.sapHostUrl,
53
+ {}
54
+ );
55
+ result.sap_UoM_total = sap_UoMs?.length;
56
+
57
+ await commandLog
58
+ .addDetail(`${result.sap_UoM_total} Unit of Measures in SAP`)
59
+ .commit();
60
+
61
+ let repzo_UoMs = await repzo.measureunit.find({
62
+ disabled: false,
63
+ per_page: 50000,
64
+ });
65
+ result.repzo_UoM_total = repzo_UoMs?.data?.length;
66
+ await commandLog
67
+ .addDetail(`${repzo_UoMs?.data?.length} Measure Units in Repzo`)
68
+ .commit();
69
+
70
+ if (!repzo_UoMs?.data?.length) {
71
+ throw "measure units are not found or the nameSpace has more than one";
72
+ }
73
+
74
+ repzo_UoMs.data = repzo_UoMs.data?.filter((UoM) => UoM.integration_meta);
75
+
76
+ const repzo_UoMs_family = await repzo.measureunitFamily.find({
77
+ disabled: false,
78
+ per_page: 50000,
79
+ });
80
+ result.repzo_total = repzo_UoMs_family?.data?.length;
81
+ await commandLog
82
+ .addDetail(
83
+ `${repzo_UoMs_family?.data?.length} Measure Units Family in Repzo`
84
+ )
85
+ .commit();
86
+
87
+ const sap_unique_family: {
88
+ [key: string]: { [integration_id: string]: 1 };
89
+ } = {};
90
+
91
+ for (let i = 0; i < sap_UoMs?.length; i++) {
92
+ const sap_UoM = sap_UoMs[i];
93
+ const key = sap_UoM.ITEMCODE;
94
+ if (!sap_unique_family[key]) sap_unique_family[key] = {};
95
+ const uom_key = `${nameSpace}_${sap_UoM.UOMGROUPENTRY}_${sap_UoM.ALTUOMID}`;
96
+ sap_unique_family[key][uom_key] = 1;
97
+ }
98
+
99
+ result.sap_total = Object.keys(sap_unique_family)?.length;
100
+ await commandLog
101
+ .addDetail(`${result.sap_total} Measure Units Family in SAP`)
102
+ .commit();
103
+
104
+ for (let key in sap_unique_family) {
105
+ const sap_family = sap_unique_family[key];
106
+ const repzo_family = repzo_UoMs_family.data.find(
107
+ (r_family) => r_family.integration_meta?.id == `${nameSpace}_${key}`
108
+ );
109
+
110
+ let measureunits: string[] = [];
111
+ Object.keys(sap_family).forEach((unit) => {
112
+ {
113
+ const UoM = repzo_UoMs?.data?.find(
114
+ (repzo_uom) => repzo_uom?.integration_meta?.id == unit
115
+ );
116
+ if (UoM) {
117
+ measureunits.push(UoM._id.toString());
118
+ }
119
+ }
120
+ });
121
+
122
+ const body:
123
+ | Service.MeasureUnitFamily.Create.Body
124
+ | Service.MeasureUnitFamily.Update.Body = {
125
+ name: key,
126
+ disabled: false,
127
+ integration_meta: { id: `${nameSpace}_${key}` },
128
+ measureunits: measureunits || [],
129
+ company_namespace: [nameSpace],
130
+ };
131
+
132
+ if (!repzo_family) {
133
+ // Create
134
+ try {
135
+ const created_UoM = await repzo.measureunitFamily.create(
136
+ body as Service.MeasureUnit.Create.Body
137
+ );
138
+ result.created++;
139
+ } catch (e: any) {
140
+ console.log(
141
+ "Create Measure Unit Family Failed >> ",
142
+ e?.response,
143
+ body
144
+ );
145
+ failed_docs_report.push({
146
+ method: "create",
147
+ doc: body,
148
+ error_message: set_error(e),
149
+ });
150
+ result.failed++;
151
+ }
152
+ } else {
153
+ if (
154
+ repzo_family.name == body.name &&
155
+ !_.difference(
156
+ repzo_family?.measureunits?.map((m) => m?.toString()) || [],
157
+ body?.measureunits || []
158
+ )?.length
159
+ ) {
160
+ continue;
161
+ }
162
+
163
+ // Update
164
+ try {
165
+ const updated_UoM = await repzo.measureunitFamily.update(
166
+ repzo_family._id,
167
+ body as Service.MeasureUnit.Update.Body
168
+ );
169
+ result.updated++;
170
+ } catch (e: any) {
171
+ console.log(
172
+ "Update Measure Unit Family Failed >> ",
173
+ e?.response?.data,
174
+ body
175
+ );
176
+ failed_docs_report.push({
177
+ method: "update",
178
+ doc_id: repzo_family?._id,
179
+ doc: body,
180
+ error_message: set_error(e),
181
+ });
182
+ result.failed++;
183
+ }
184
+ }
185
+ }
186
+
187
+ // console.log(result);
188
+
189
+ await update_bench_time(
190
+ repzo,
191
+ commandEvent.app._id,
192
+ bench_time_key,
193
+ new_bench_time
194
+ );
195
+ await commandLog
196
+ .setStatus(
197
+ "success",
198
+ failed_docs_report.length ? failed_docs_report : null
199
+ )
200
+ .setBody(result)
201
+ .commit();
202
+ return result;
203
+ } catch (e: any) {
204
+ //@ts-ignore
205
+ console.error(e?.response?.data || e);
206
+ await commandLog.setStatus("fail", e).commit();
207
+ throw e;
208
+ }
209
+ };
@@ -0,0 +1,224 @@
1
+ import Repzo from "repzo";
2
+ import { Service } from "repzo/src/types";
3
+ import DataSet from "data-set-query";
4
+ import { CommandEvent, Result, FailedDocsReport } from "../types";
5
+ import {
6
+ _fetch,
7
+ _create,
8
+ _update,
9
+ _delete,
10
+ update_bench_time,
11
+ date_formatting,
12
+ set_error,
13
+ } from "../util.js";
14
+
15
+ interface SAPClient {
16
+ CLIENTID: string; // "C01077";
17
+ CLIENTDESC: string; // "س م عايش";
18
+ CLIENTADDRESS?: string; // null;
19
+ CLIENTCITY?: string; // null;
20
+ CLIENTCOUNTRY?: string; // null;
21
+ CLIENTCOUNTY?: string; // null;
22
+ CLIENTGROUPCODE: number; // 123;
23
+ CLIENTGROUP: string; // "Cash Van";
24
+ PAYMENTTERM: number; // 0;
25
+ CLIENTCONTACTPERSON?: string; // null;
26
+ CLIENTPHONE1?: string; // null;
27
+ CLIENTPHONE2?: string; // null;
28
+ CLIENTNOTE?: string; // null;
29
+ CLIENTSTATUS: "N" | "Y";
30
+ CLIENTCREDITCONSUMED: number; // 0.0;
31
+ CLIENTMAXCHEQUEVALUE: number; // 0.0;
32
+ CLIENTCREDITLIMIT: number; // 0.0;
33
+ CLIENTPRICELISTID: number; // 2;
34
+ CLIENTADDRESSID?: string; // null;
35
+ CLIENTDESCF?: string; // null;
36
+ SALESPERSONCODE: number; // -1;
37
+ DISCOUNTPERCENT: number; // 0.0;
38
+ ACTIVE: "N" | "Y";
39
+ FROZEN: "N" | "Y";
40
+ TERRITORYID?: string; // null;
41
+ TERRITORYNAME?: string; // null;
42
+ CREATEDATE: string; // "2021-12-06T21:00:00Z";
43
+ UPDATEDATE: string; // "2022-11-29T21:00:00Z";
44
+ PARENTCODE?: string; // null;
45
+ }
46
+
47
+ interface SAPClients {
48
+ result: "Success";
49
+ Customers: SAPClient[];
50
+ }
51
+
52
+ export const sync_payment_term = async (commandEvent: CommandEvent) => {
53
+ const repzo = new Repzo(commandEvent.app.formData?.repzoApiKey, {
54
+ env: commandEvent.env,
55
+ });
56
+
57
+ const commandLog = new Repzo.CommandLog(
58
+ repzo,
59
+ commandEvent.app,
60
+ commandEvent.command
61
+ );
62
+ try {
63
+ console.log("sync_payment_term");
64
+
65
+ const new_bench_time = new Date().toISOString();
66
+ const bench_time_key = "bench_time_payment_term";
67
+
68
+ await commandLog.load(commandEvent.sync_id);
69
+ await commandLog
70
+ .addDetail("Repzo SAP: Started Syncing Payments Term")
71
+ .commit();
72
+
73
+ const nameSpace: string = commandEvent.nameSpace.join("_");
74
+ const result: Result = {
75
+ sap_total: 0,
76
+ repzo_total: 0,
77
+ created: 0,
78
+ updated: 0,
79
+ failed: 0,
80
+ };
81
+ const failed_docs_report: FailedDocsReport = [];
82
+
83
+ const sap_clients: SAPClient[] = await get_sap_clients(
84
+ commandEvent.app.formData.sapHostUrl,
85
+ {
86
+ updateAt: commandEvent.app.options_formData[bench_time_key],
87
+ GroupCode: commandEvent.app.formData.GroupCode,
88
+ }
89
+ );
90
+
91
+ const sap_unique_payment_terms: { [key: number]: true } = {};
92
+ sap_clients?.forEach((client: SAPClient) => {
93
+ const paymentTerm = client.PAYMENTTERM;
94
+ sap_unique_payment_terms[paymentTerm] = true;
95
+ });
96
+
97
+ const sap_payment_terms = Object.keys(sap_unique_payment_terms);
98
+
99
+ result.sap_total = sap_payment_terms?.length;
100
+ await commandLog
101
+ .addDetail(
102
+ `${result.sap_total} Payments Term in SAP changed since ${
103
+ commandEvent.app.options_formData[bench_time_key] || "ever"
104
+ }`
105
+ )
106
+ .commit();
107
+
108
+ const repzo_payment_terms = await repzo.paymentTerm.find({
109
+ per_page: 50000,
110
+ });
111
+ result.repzo_total = repzo_payment_terms?.data?.length;
112
+ await commandLog
113
+ .addDetail(`${repzo_payment_terms?.data?.length} Payments Term in Repzo`)
114
+ .commit();
115
+
116
+ for (let i = 0; i < sap_payment_terms?.length; i++) {
117
+ const sap_payment_term = sap_payment_terms[i];
118
+ const repzo_payment_term = repzo_payment_terms.data.find(
119
+ (r_payment_term) =>
120
+ r_payment_term.integration_meta?.id ==
121
+ `${nameSpace}_${sap_payment_term}`
122
+ );
123
+
124
+ const body:
125
+ | Service.PaymentTerm.Create.Body
126
+ | Service.PaymentTerm.Update.Body = {
127
+ name: sap_payment_term.toString(),
128
+ due_days: Number(sap_payment_term),
129
+ integration_meta: {
130
+ id: `${nameSpace}_${sap_payment_term}`,
131
+ },
132
+ company_namespace: [nameSpace],
133
+ };
134
+
135
+ if (!repzo_payment_term) {
136
+ // Create
137
+ try {
138
+ const created_payment_term = await repzo.paymentTerm.create(
139
+ body as Service.PaymentTerm.Create.Body
140
+ );
141
+ result.created++;
142
+ } catch (e: any) {
143
+ console.log("Create Payment Term Failed >> ", e?.response, body);
144
+ failed_docs_report.push({
145
+ method: "create",
146
+ doc: body,
147
+ error_message: set_error(e),
148
+ });
149
+ result.failed++;
150
+ }
151
+ } else {
152
+ if (
153
+ repzo_payment_term?.name == body?.name &&
154
+ repzo_payment_term?.due_days == body?.due_days
155
+ )
156
+ continue;
157
+ // Update
158
+ try {
159
+ const updated_payment_term = await repzo.paymentTerm.update(
160
+ repzo_payment_term._id,
161
+ body as Service.PaymentTerm.Update.Body
162
+ );
163
+ result.updated++;
164
+ } catch (e: any) {
165
+ console.log(
166
+ "Update Payment Term Failed >> ",
167
+ e?.response?.data,
168
+ body
169
+ );
170
+ failed_docs_report.push({
171
+ method: "update",
172
+ doc_id: repzo_payment_term?._id,
173
+ doc: body,
174
+ error_message: set_error(e),
175
+ });
176
+ result.failed++;
177
+ }
178
+ }
179
+ }
180
+
181
+ // console.log(result);
182
+
183
+ await update_bench_time(
184
+ repzo,
185
+ commandEvent.app._id,
186
+ bench_time_key,
187
+ new_bench_time
188
+ );
189
+ await commandLog
190
+ .setStatus(
191
+ "success",
192
+ failed_docs_report.length ? failed_docs_report : null
193
+ )
194
+ .setBody(result)
195
+ .commit();
196
+ return result;
197
+ } catch (e: any) {
198
+ //@ts-ignore
199
+ console.error(e?.response?.data || e);
200
+ await commandLog.setStatus("fail", e).commit();
201
+ throw e;
202
+ }
203
+ };
204
+
205
+ const get_sap_clients = async (
206
+ serviceEndPoint: string,
207
+ query?: { updateAt?: string; GroupCode?: string }
208
+ ): Promise<SAPClient[]> => {
209
+ try {
210
+ const sap_clients: SAPClients = await _create(
211
+ serviceEndPoint,
212
+ "/Customers",
213
+ {
214
+ Active: "Y",
215
+ Frozen: "N",
216
+ UpdateAt: date_formatting(query?.updateAt, "YYYYMMDD:HHmmss"),
217
+ GroupCode: query?.GroupCode || "",
218
+ }
219
+ );
220
+ return sap_clients.Customers;
221
+ } catch (e: any) {
222
+ throw e;
223
+ }
224
+ };