repzo-sap-absjo 1.0.19 → 1.0.20

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 (76) hide show
  1. package/lib/actions/create_invoice.d.ts +13 -19
  2. package/lib/actions/create_invoice.js +207 -377
  3. package/lib/actions/create_payment.d.ts +1 -4
  4. package/lib/actions/create_payment.js +107 -195
  5. package/lib/actions/create_proforma.d.ts +1 -4
  6. package/lib/actions/create_proforma.js +146 -285
  7. package/lib/actions/create_return_invoice.d.ts +1 -4
  8. package/lib/actions/create_return_invoice.js +158 -312
  9. package/lib/actions/create_transfer.d.ts +1 -4
  10. package/lib/actions/create_transfer.js +126 -223
  11. package/lib/actions/index.js +15 -23
  12. package/lib/commands/adjust_inventory.d.ts +2 -4
  13. package/lib/commands/adjust_inventory.js +160 -290
  14. package/lib/commands/bank.d.ts +2 -4
  15. package/lib/commands/bank.js +157 -279
  16. package/lib/commands/basic.js +48 -67
  17. package/lib/commands/brand.d.ts +1 -3
  18. package/lib/commands/brand.js +100 -174
  19. package/lib/commands/category.d.ts +1 -3
  20. package/lib/commands/category.js +104 -178
  21. package/lib/commands/channel.d.ts +1 -3
  22. package/lib/commands/channel.js +117 -198
  23. package/lib/commands/client.d.ts +32 -34
  24. package/lib/commands/client.js +244 -404
  25. package/lib/commands/client_disabled.d.ts +1 -3
  26. package/lib/commands/client_disabled.js +94 -163
  27. package/lib/commands/index.d.ts +11 -15
  28. package/lib/commands/index.js +62 -62
  29. package/lib/commands/join.js +66 -192
  30. package/lib/commands/measureunit.d.ts +14 -19
  31. package/lib/commands/measureunit.js +191 -289
  32. package/lib/commands/measureunit_family.d.ts +8 -10
  33. package/lib/commands/measureunit_family.js +138 -250
  34. package/lib/commands/payment_term.d.ts +1 -3
  35. package/lib/commands/payment_term.js +123 -202
  36. package/lib/commands/price_list.d.ts +15 -15
  37. package/lib/commands/price_list.js +326 -612
  38. package/lib/commands/product.d.ts +30 -32
  39. package/lib/commands/product.js +263 -485
  40. package/lib/commands/product_disabled.d.ts +1 -3
  41. package/lib/commands/product_disabled.js +94 -160
  42. package/lib/commands/rep.js +141 -221
  43. package/lib/commands/tag.js +109 -174
  44. package/lib/commands/tax.js +112 -177
  45. package/lib/commands/warehouse.d.ts +1 -3
  46. package/lib/commands/warehouse.js +119 -207
  47. package/lib/index.d.ts +12 -21
  48. package/lib/test/actions/create_invoice.js +188 -193
  49. package/lib/test/actions/create_payment.js +107 -112
  50. package/lib/test/actions/create_proforma.js +216 -220
  51. package/lib/test/actions/create_return_invoice.js +200 -205
  52. package/lib/test/actions/create_transfer.js +115 -120
  53. package/lib/test/commands/adjust_inventory.js +90 -90
  54. package/lib/test/commands/bank.js +90 -90
  55. package/lib/test/commands/basic.js +327 -328
  56. package/lib/test/commands/brand.js +90 -90
  57. package/lib/test/commands/category.js +90 -90
  58. package/lib/test/commands/channel.js +90 -90
  59. package/lib/test/commands/client.js +353 -355
  60. package/lib/test/commands/client_disabled.js +90 -90
  61. package/lib/test/commands/join.js +327 -328
  62. package/lib/test/commands/measureunit.js +90 -90
  63. package/lib/test/commands/measureunit_family.js +90 -90
  64. package/lib/test/commands/payment_term.js +90 -90
  65. package/lib/test/commands/price_list.js +334 -337
  66. package/lib/test/commands/product.js +90 -90
  67. package/lib/test/commands/product_disabled.js +90 -90
  68. package/lib/test/commands/rep.js +344 -346
  69. package/lib/test/commands/tag.js +90 -90
  70. package/lib/test/commands/tax.js +90 -90
  71. package/lib/test/commands/warehouse.js +90 -90
  72. package/lib/types.d.ts +53 -77
  73. package/lib/util.d.ts +19 -70
  74. package/lib/util.js +144 -170
  75. package/package.json +1 -1
  76. package/src/actions/create_invoice.ts +5 -25
@@ -1,626 +1,340 @@
1
1
  import Repzo from "repzo";
2
- import { _create, update_bench_time, set_error } from "../util.js";
2
+ import { _create, update_bench_time, set_error, } from "../util.js";
3
3
  import { get_sap_UoMs } from "./measureunit.js";
4
4
  export const sync_price_list = async (commandEvent) => {
5
- var _a,
6
- _b,
7
- _c,
8
- _d,
9
- _e,
10
- _f,
11
- _g,
12
- _h,
13
- _j,
14
- _k,
15
- _l,
16
- _m,
17
- _o,
18
- _p,
19
- _q,
20
- _r,
21
- _s,
22
- _t,
23
- _u;
24
- const repzo = new Repzo(
25
- (_a = commandEvent.app.formData) === null || _a === void 0
26
- ? void 0
27
- : _a.repzoApiKey,
28
- {
29
- env: commandEvent.env,
30
- }
31
- );
32
- const commandLog = new Repzo.CommandLog(
33
- repzo,
34
- commandEvent.app,
35
- commandEvent.command
36
- );
37
- try {
38
- // console.log("sync_price_list");
39
- const new_bench_time = new Date().toISOString();
40
- const bench_time_key = "bench_time_price_list";
41
- await commandLog.load(commandEvent.sync_id);
42
- await commandLog
43
- .addDetail("Repzo SAP: Started Syncing Price Lists")
44
- .commit();
45
- const nameSpace = commandEvent.nameSpace.join("_");
46
- const result = {
47
- PL: { created: 0, updated: 0, failed: 0 },
48
- PL_items: { created: 0, updated: 0, failed: 0 },
49
- sap_total: 0,
50
- repzo_total: 0,
51
- repzo_PL_items: 0,
52
- sap_UoMs_total: 0,
53
- repzo_products_total: 0,
54
- };
55
- const failed_docs_report = [];
56
- // Get SAP Price Lists
57
- const sap_price_lists = await get_sap_price_list(
58
- commandEvent.app.formData.sapHostUrl,
59
- { updateAt: commandEvent.app.options_formData[bench_time_key] }
60
- );
61
- result.sap_total =
62
- sap_price_lists === null || sap_price_lists === void 0
63
- ? void 0
64
- : sap_price_lists.length;
65
- await commandLog
66
- .addDetail(
67
- `${result.sap_total} Price Lists in SAP changed since ${
68
- commandEvent.app.options_formData[bench_time_key] || "ever"
69
- }`
70
- )
71
- .commit();
72
- // Get SAP UoMs
73
- const sap_UoMs = await get_sap_UoMs(
74
- commandEvent.app.formData.sapHostUrl,
75
- {}
76
- );
77
- result.sap_UoMs_total =
78
- sap_UoMs === null || sap_UoMs === void 0 ? void 0 : sap_UoMs.length;
79
- await commandLog
80
- .addDetail(`${result.sap_total} Unit of Measures in SAP`)
81
- .commit();
82
- // Get Repzo Price Lists
83
- const repzo_price_lists = await repzo.priceList.find({ per_page: 50000 });
84
- result.repzo_total =
85
- (_b =
86
- repzo_price_lists === null || repzo_price_lists === void 0
87
- ? void 0
88
- : repzo_price_lists.data) === null || _b === void 0
89
- ? void 0
90
- : _b.length;
91
- await commandLog
92
- .addDetail(
93
- `${
94
- (_c =
95
- repzo_price_lists === null || repzo_price_lists === void 0
96
- ? void 0
97
- : repzo_price_lists.data) === null || _c === void 0
98
- ? void 0
99
- : _c.length
100
- } Price Lists in Repzo`
101
- )
102
- .commit();
103
- // Get Repzo Products
104
- const repzo_products = await repzo.product.find({
105
- active: true,
106
- withVariants: true,
107
- per_page: 50000,
108
- });
109
- result.repzo_products_total =
110
- (_d =
111
- repzo_products === null || repzo_products === void 0
112
- ? void 0
113
- : repzo_products.data) === null || _d === void 0
114
- ? void 0
115
- : _d.length;
116
- await commandLog
117
- .addDetail(
118
- `${
119
- (_e =
120
- repzo_products === null || repzo_products === void 0
121
- ? void 0
122
- : repzo_products.data) === null || _e === void 0
123
- ? void 0
124
- : _e.length
125
- } Products in Repzo`
126
- )
127
- .commit();
128
- // Get Repzo MeasureUnits
129
- const repzo_UoMs = await repzo.measureunit.find({
130
- disabled: false,
131
- per_page: 50000,
132
- });
133
- result.repzo_total =
134
- (_f =
135
- repzo_UoMs === null || repzo_UoMs === void 0
136
- ? void 0
137
- : repzo_UoMs.data) === null || _f === void 0
138
- ? void 0
139
- : _f.length;
140
- await commandLog
141
- .addDetail(
142
- `${
143
- (_g =
144
- repzo_UoMs === null || repzo_UoMs === void 0
145
- ? void 0
146
- : repzo_UoMs.data) === null || _g === void 0
147
- ? void 0
148
- : _g.length
149
- } Measure Units in Repzo`
150
- )
151
- .commit();
152
- if (
153
- !((_h =
154
- repzo_UoMs === null || repzo_UoMs === void 0
155
- ? void 0
156
- : repzo_UoMs.data) === null || _h === void 0
157
- ? void 0
158
- : _h.length)
159
- )
160
- throw "MeasureUnits in Repzo was not found";
161
- const sap_unique_UoMs = {};
162
- sap_UoMs.forEach((doc) => {
163
- const key = `${doc.ITEMCODE}__${doc.ALTUOMCODE}`;
164
- sap_unique_UoMs[key] = doc.ALTQTY;
165
- });
166
- // Get Repzo {Product_sku : product_default_measureunit_name}
167
- const repzo_product_default_measureunit = {};
168
- repzo_products.data.forEach((product) => {
169
- if (!product.sku) return;
170
- if (!product.sv_measureUnit) {
171
- repzo_product_default_measureunit[product.sku] = null;
172
- } else {
173
- const default_measureunit = repzo_UoMs.data.find(
174
- (m) => m._id == product.sv_measureUnit
175
- );
176
- if (default_measureunit) {
177
- repzo_product_default_measureunit[product.sku] =
178
- default_measureunit.name;
179
- } else {
180
- repzo_product_default_measureunit[product.sku] = null;
181
- }
182
- }
5
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u;
6
+ const repzo = new Repzo((_a = commandEvent.app.formData) === null || _a === void 0 ? void 0 : _a.repzoApiKey, {
7
+ env: commandEvent.env,
183
8
  });
184
- // sort the data
185
- const priceLists_withItems = {};
186
- sap_price_lists.forEach((doc) => {
187
- if (!priceLists_withItems[doc.PLDID])
188
- priceLists_withItems[doc.PLDID] = [];
189
- priceLists_withItems[doc.PLDID].push(doc);
190
- });
191
- // create priceLists
192
- const priceLists_names = Object.keys(priceLists_withItems);
193
- for (let i = 0; i < priceLists_names.length; i++) {
194
- const price_list_name = priceLists_names[i];
195
- const body = {
196
- name: `PL_${price_list_name}`,
197
- integration_meta: { id: `${nameSpace}_${price_list_name}` },
198
- };
199
- const repzo_price_list =
200
- (_j =
201
- repzo_price_lists === null || repzo_price_lists === void 0
202
- ? void 0
203
- : repzo_price_lists.data) === null || _j === void 0
204
- ? void 0
205
- : _j.find((pl) => {
206
- var _a, _b;
207
- return (
208
- ((_a =
209
- pl === null || pl === void 0
210
- ? void 0
211
- : pl.integration_meta) === null || _a === void 0
212
- ? void 0
213
- : _a.id) ==
214
- ((_b =
215
- body === null || body === void 0
216
- ? void 0
217
- : body.integration_meta) === null || _b === void 0
218
- ? void 0
219
- : _b.id)
220
- );
221
- });
222
- if (!repzo_price_list) {
223
- // Create
224
- try {
225
- const created_price_list = await repzo.priceList.create(body);
226
- result.PL.created++;
227
- } catch (e) {
228
- // console.log("Create Price List Failed >> ", e?.response, body);
229
- failed_docs_report.push({
230
- method: "create",
231
- doc: body,
232
- error_message: set_error(e),
233
- });
234
- result.PL.failed++;
235
- }
236
- } else {
237
- if (
238
- (repzo_price_list === null || repzo_price_list === void 0
239
- ? void 0
240
- : repzo_price_list.name) ==
241
- (body === null || body === void 0 ? void 0 : body.name)
242
- )
243
- continue;
244
- // Update
245
- try {
246
- const updated_price_list = await repzo.priceList.update(
247
- repzo_price_list._id,
248
- body
249
- );
250
- result.PL.updated++;
251
- } catch (e) {
252
- // console.log("Update Price List Failed >> ", e?.response?.data, body);
253
- failed_docs_report.push({
254
- method: "update",
255
- doc_id:
256
- repzo_price_list === null || repzo_price_list === void 0
257
- ? void 0
258
- : repzo_price_list._id,
259
- doc: body,
260
- error_message: set_error(e),
261
- });
262
- result.PL.failed++;
263
- }
264
- }
265
- }
266
- // Price List Items ***************************************
267
- await commandLog
268
- .addDetail(`Start Sync Price List Items From SAP to Repzo`)
269
- .commit();
270
- const repzo_all_priceLists = await repzo.priceList.find({
271
- disabled: false,
272
- per_page: 50000,
273
- });
274
- await commandLog
275
- .addDetail(
276
- `${
277
- (_k =
278
- repzo_all_priceLists === null || repzo_all_priceLists === void 0
279
- ? void 0
280
- : repzo_all_priceLists.data) === null || _k === void 0
281
- ? void 0
282
- : _k.length
283
- } Price Lists in Repzo`
284
- )
285
- .commit();
286
- if (
287
- !((_l =
288
- repzo_all_priceLists === null || repzo_all_priceLists === void 0
289
- ? void 0
290
- : repzo_all_priceLists.data) === null || _l === void 0
291
- ? void 0
292
- : _l.length)
293
- )
294
- throw `No Price Lists was found On Repzo`;
295
- for (let priceList_name in priceLists_withItems) {
296
- const repzo_PriceList =
297
- (_m =
298
- repzo_all_priceLists === null || repzo_all_priceLists === void 0
299
- ? void 0
300
- : repzo_all_priceLists.data) === null || _m === void 0
301
- ? void 0
302
- : _m.find((pl) => {
303
- var _a;
304
- return (
305
- ((_a = pl.integration_meta) === null || _a === void 0
306
- ? void 0
307
- : _a.id) == `${nameSpace}_${priceList_name}`
308
- );
309
- });
310
- if (!repzo_PriceList) {
311
- // console.log(
312
- // `Price list with PLDID: ${priceList_name} was not created or disabled`
313
- // );
314
- failed_docs_report.push({
315
- method: "create",
316
- // doc: priceLists_withItems[priceList_name],
317
- error_message: set_error(
318
- `Failed Create PriceList Items: number of PL items: ${priceLists_withItems[priceList_name].length} => Because Price list with PLDID: ${priceList_name} was not created or disabled`
319
- ),
9
+ const commandLog = new Repzo.CommandLog(repzo, commandEvent.app, commandEvent.command);
10
+ try {
11
+ // console.log("sync_price_list");
12
+ const new_bench_time = new Date().toISOString();
13
+ const bench_time_key = "bench_time_price_list";
14
+ await commandLog.load(commandEvent.sync_id);
15
+ await commandLog
16
+ .addDetail("Repzo SAP: Started Syncing Price Lists")
17
+ .commit();
18
+ const nameSpace = commandEvent.nameSpace.join("_");
19
+ const result = {
20
+ PL: { created: 0, updated: 0, failed: 0 },
21
+ PL_items: { created: 0, updated: 0, failed: 0 },
22
+ sap_total: 0,
23
+ repzo_total: 0,
24
+ repzo_PL_items: 0,
25
+ sap_UoMs_total: 0,
26
+ repzo_products_total: 0,
27
+ };
28
+ const failed_docs_report = [];
29
+ // Get SAP Price Lists
30
+ const sap_price_lists = await get_sap_price_list(commandEvent.app.formData.sapHostUrl, { updateAt: commandEvent.app.options_formData[bench_time_key] });
31
+ result.sap_total = sap_price_lists === null || sap_price_lists === void 0 ? void 0 : sap_price_lists.length;
32
+ await commandLog
33
+ .addDetail(`${result.sap_total} Price Lists in SAP changed since ${commandEvent.app.options_formData[bench_time_key] || "ever"}`)
34
+ .commit();
35
+ // Get SAP UoMs
36
+ const sap_UoMs = await get_sap_UoMs(commandEvent.app.formData.sapHostUrl, {});
37
+ result.sap_UoMs_total = sap_UoMs === null || sap_UoMs === void 0 ? void 0 : sap_UoMs.length;
38
+ await commandLog
39
+ .addDetail(`${result.sap_total} Unit of Measures in SAP`)
40
+ .commit();
41
+ // Get Repzo Price Lists
42
+ const repzo_price_lists = await repzo.priceList.find({ per_page: 50000 });
43
+ result.repzo_total = (_b = repzo_price_lists === null || repzo_price_lists === void 0 ? void 0 : repzo_price_lists.data) === null || _b === void 0 ? void 0 : _b.length;
44
+ await commandLog
45
+ .addDetail(`${(_c = repzo_price_lists === null || repzo_price_lists === void 0 ? void 0 : repzo_price_lists.data) === null || _c === void 0 ? void 0 : _c.length} Price Lists in Repzo`)
46
+ .commit();
47
+ // Get Repzo Products
48
+ const repzo_products = await repzo.product.find({
49
+ active: true,
50
+ withVariants: true,
51
+ per_page: 50000,
320
52
  });
321
- result.PL_items.failed +=
322
- ((_o = priceLists_withItems[priceList_name]) === null || _o === void 0
323
- ? void 0
324
- : _o.length) || 0;
325
- continue;
326
- }
327
- const repzo_price_list_items = await repzo.priceListItem.find({
328
- disabled: false,
329
- pricelist_id:
330
- repzo_PriceList === null || repzo_PriceList === void 0
331
- ? void 0
332
- : repzo_PriceList._id,
333
- per_page: 50000,
334
- });
335
- // Create Price list items
336
- const priceList_items = {};
337
- priceLists_withItems[priceList_name].forEach((doc) => {
338
- if (!sap_unique_UoMs[`${doc.PLITEMID}__${doc.PLITEMUNIT}`]) {
339
- // console.log(
340
- // `error => ${doc.PLITEMID}__${doc.PLITEMUNIT} was not found on the Uom`
341
- // );
342
- failed_docs_report.push({
343
- method: "create",
344
- // doc: priceLists_withItems[priceList_name],
345
- error_message: set_error(
346
- `Create PL items => PLITEMID: ${doc.PLITEMID}, PLITEMUNIT:${doc.PLITEMUNIT} was not found on the Uom`
347
- ),
348
- });
349
- result.PL_items.failed++;
350
- return;
351
- }
352
- doc.factor = sap_unique_UoMs[`${doc.PLITEMID}__${doc.PLITEMUNIT}`];
353
- const key = `${doc.PLITEMID}`;
354
- if (!priceList_items[key]) {
355
- priceList_items[key] = doc;
356
- } else {
357
- const current_doc = priceList_items[key];
358
- if (
359
- current_doc.PLITEMUNIT !=
360
- repzo_product_default_measureunit[current_doc.PLITEMID]
361
- ) {
362
- if (
363
- doc.PLITEMUNIT ==
364
- repzo_product_default_measureunit[current_doc.PLITEMID]
365
- ) {
366
- priceList_items[key] = doc;
367
- } else if (
368
- (current_doc === null || current_doc === void 0
369
- ? void 0
370
- : current_doc.factor) >
371
- (doc === null || doc === void 0 ? void 0 : doc.factor)
372
- ) {
373
- priceList_items[key] = doc;
53
+ result.repzo_products_total = (_d = repzo_products === null || repzo_products === void 0 ? void 0 : repzo_products.data) === null || _d === void 0 ? void 0 : _d.length;
54
+ await commandLog
55
+ .addDetail(`${(_e = repzo_products === null || repzo_products === void 0 ? void 0 : repzo_products.data) === null || _e === void 0 ? void 0 : _e.length} Products in Repzo`)
56
+ .commit();
57
+ // Get Repzo MeasureUnits
58
+ const repzo_UoMs = await repzo.measureunit.find({
59
+ disabled: false,
60
+ per_page: 50000,
61
+ });
62
+ result.repzo_total = (_f = repzo_UoMs === null || repzo_UoMs === void 0 ? void 0 : repzo_UoMs.data) === null || _f === void 0 ? void 0 : _f.length;
63
+ await commandLog
64
+ .addDetail(`${(_g = repzo_UoMs === null || repzo_UoMs === void 0 ? void 0 : repzo_UoMs.data) === null || _g === void 0 ? void 0 : _g.length} Measure Units in Repzo`)
65
+ .commit();
66
+ if (!((_h = repzo_UoMs === null || repzo_UoMs === void 0 ? void 0 : repzo_UoMs.data) === null || _h === void 0 ? void 0 : _h.length))
67
+ throw "MeasureUnits in Repzo was not found";
68
+ const sap_unique_UoMs = {};
69
+ sap_UoMs.forEach((doc) => {
70
+ const key = `${doc.ITEMCODE}__${doc.ALTUOMCODE}`;
71
+ sap_unique_UoMs[key] = doc.ALTQTY;
72
+ });
73
+ // Get Repzo {Product_sku : product_default_measureunit_name}
74
+ const repzo_product_default_measureunit = {};
75
+ repzo_products.data.forEach((product) => {
76
+ if (!product.sku)
77
+ return;
78
+ if (!product.sv_measureUnit) {
79
+ repzo_product_default_measureunit[product.sku] = null;
80
+ }
81
+ else {
82
+ const default_measureunit = repzo_UoMs.data.find((m) => m._id == product.sv_measureUnit);
83
+ if (default_measureunit) {
84
+ repzo_product_default_measureunit[product.sku] =
85
+ default_measureunit.name;
86
+ }
87
+ else {
88
+ repzo_product_default_measureunit[product.sku] = null;
89
+ }
90
+ }
91
+ });
92
+ // sort the data
93
+ const priceLists_withItems = {};
94
+ sap_price_lists.forEach((doc) => {
95
+ if (!priceLists_withItems[doc.PLDID])
96
+ priceLists_withItems[doc.PLDID] = [];
97
+ priceLists_withItems[doc.PLDID].push(doc);
98
+ });
99
+ // create priceLists
100
+ const priceLists_names = Object.keys(priceLists_withItems);
101
+ for (let i = 0; i < priceLists_names.length; i++) {
102
+ const price_list_name = priceLists_names[i];
103
+ const body = {
104
+ name: `PL_${price_list_name}`,
105
+ integration_meta: { id: `${nameSpace}_${price_list_name}` },
106
+ };
107
+ const repzo_price_list = (_j = repzo_price_lists === null || repzo_price_lists === void 0 ? void 0 : repzo_price_lists.data) === null || _j === void 0 ? void 0 : _j.find((pl) => { var _a, _b; return ((_a = pl === null || pl === void 0 ? void 0 : pl.integration_meta) === null || _a === void 0 ? void 0 : _a.id) == ((_b = body === null || body === void 0 ? void 0 : body.integration_meta) === null || _b === void 0 ? void 0 : _b.id); });
108
+ if (!repzo_price_list) {
109
+ // Create
110
+ try {
111
+ const created_price_list = await repzo.priceList.create(body);
112
+ result.PL.created++;
113
+ }
114
+ catch (e) {
115
+ // console.log("Create Price List Failed >> ", e?.response, body);
116
+ failed_docs_report.push({
117
+ method: "create",
118
+ doc: body,
119
+ error_message: set_error(e),
120
+ });
121
+ result.PL.failed++;
122
+ }
123
+ }
124
+ else {
125
+ if ((repzo_price_list === null || repzo_price_list === void 0 ? void 0 : repzo_price_list.name) == (body === null || body === void 0 ? void 0 : body.name))
126
+ continue;
127
+ // Update
128
+ try {
129
+ const updated_price_list = await repzo.priceList.update(repzo_price_list._id, body);
130
+ result.PL.updated++;
131
+ }
132
+ catch (e) {
133
+ // console.log("Update Price List Failed >> ", e?.response?.data, body);
134
+ failed_docs_report.push({
135
+ method: "update",
136
+ doc_id: repzo_price_list === null || repzo_price_list === void 0 ? void 0 : repzo_price_list._id,
137
+ doc: body,
138
+ error_message: set_error(e),
139
+ });
140
+ result.PL.failed++;
141
+ }
374
142
  }
375
- }
376
- }
377
- });
378
- const priceListItems = Object.values(priceList_items);
379
- for (let j = 0; j < priceListItems.length; j++) {
380
- const item = priceListItems[j];
381
- if (!item.factor && item.factor !== 0) {
382
- failed_docs_report.push({
383
- method: "create",
384
- // doc: priceLists_withItems[priceList_name],
385
- error_message: set_error(
386
- `Price List: ${item.PLDID} of Product with PLITEMID: ${item.PLITEMID} does not have Uom`
387
- ),
388
- });
389
- result.PL_items.failed++;
390
- continue;
391
- }
392
- const repzo_product =
393
- (_p =
394
- repzo_products === null || repzo_products === void 0
395
- ? void 0
396
- : repzo_products.data) === null || _p === void 0
397
- ? void 0
398
- : _p.find((product) => {
399
- var _a;
400
- return (
401
- ((_a =
402
- product === null || product === void 0
403
- ? void 0
404
- : product.integration_meta) === null || _a === void 0
405
- ? void 0
406
- : _a.id) == `${nameSpace}_${item.PLITEMID}`
407
- );
408
- });
409
- if (!repzo_product) {
410
- failed_docs_report.push({
411
- method: "create",
412
- // doc: priceLists_withItems[priceList_name],
413
- error_message: set_error(
414
- `Price List: ${item.PLDID} of Product with PLITEMID: ${item.PLITEMID} was not found or disabled`
415
- ),
416
- });
417
- result.PL_items.failed++;
418
- continue;
419
- }
420
- const repzo_product_uoms =
421
- (_q =
422
- repzo_UoMs === null || repzo_UoMs === void 0
423
- ? void 0
424
- : repzo_UoMs.data) === null || _q === void 0
425
- ? void 0
426
- : _q.filter((uom) => {
427
- var _a, _b, _c, _d;
428
- return (
429
- ((_a = uom === null || uom === void 0 ? void 0 : uom._id) ===
430
- null || _a === void 0
431
- ? void 0
432
- : _a.toString()) ==
433
- ((_b =
434
- repzo_product === null || repzo_product === void 0
435
- ? void 0
436
- : repzo_product.sv_measureUnit) === null ||
437
- _b === void 0
438
- ? void 0
439
- : _b.toString()) ||
440
- ((_c = repzo_product.measureunit_family) === null ||
441
- _c === void 0
442
- ? void 0
443
- : _c.includes(
444
- (_d =
445
- uom === null || uom === void 0 ? void 0 : uom._id) ===
446
- null || _d === void 0
447
- ? void 0
448
- : _d.toString()
449
- ))
450
- );
451
- });
452
- const repzo_product_uom = repzo_product_uoms.find(
453
- (uom) => uom.name == item.PLITEMUNIT
454
- );
455
- if (!repzo_product_uom) {
456
- failed_docs_report.push({
457
- method: "create",
458
- // doc: priceLists_withItems[priceList_name],
459
- error_message: set_error(
460
- `Price List: ${item.PLDID} of MeasureUnit with _id: ${
461
- (_r =
462
- repzo_product === null || repzo_product === void 0
463
- ? void 0
464
- : repzo_product.sv_measureUnit) === null || _r === void 0
465
- ? void 0
466
- : _r.toString()
467
- } was not found or disabled`
468
- ),
469
- });
470
- result.PL_items.failed++;
471
- continue;
472
- }
473
- const price =
474
- repzo_product_uom &&
475
- (repzo_product_uom === null || repzo_product_uom === void 0
476
- ? void 0
477
- : repzo_product_uom.factor) == 1
478
- ? Math.round(item.PLITEMPRICEVALUE * 1000)
479
- : Math.round(
480
- (item.PLITEMPRICEVALUE * 1000) / repzo_product_uom.factor
481
- );
482
- const variant =
483
- (_s =
484
- repzo_product === null || repzo_product === void 0
485
- ? void 0
486
- : repzo_product.variants) === null || _s === void 0
487
- ? void 0
488
- : _s.find((variant) => {
489
- var _a;
490
- return (
491
- ((_a =
492
- variant === null || variant === void 0
493
- ? void 0
494
- : variant.integration_meta) === null || _a === void 0
495
- ? void 0
496
- : _a.id) == `${nameSpace}_${item.PLITEMID}`
497
- );
498
- });
499
- if (!variant) {
500
- // console.log(
501
- // `Price List: ${item.PLDID} of Variant with PLITEMID: ${item.PLITEMID} was not found`
502
- // );
503
- failed_docs_report.push({
504
- method: "create",
505
- // doc: priceLists_withItems[priceList_name],
506
- error_message: set_error(
507
- `Price List: ${item.PLDID} of Variant with PLITEMID: ${item.PLITEMID} was not found`
508
- ),
509
- });
510
- result.PL_items.failed++;
511
- continue;
512
143
  }
513
- const body = {
514
- integration_meta: {
515
- id: `${nameSpace}_${item.PLDID}_${item.PLITEMID}`,
516
- },
517
- product_id: repzo_product._id,
518
- variant_id: variant._id,
519
- pricelist_id: repzo_PriceList._id,
520
- price: price,
521
- };
522
- // console.log(data);
523
- const is_found_in_repzo_db =
524
- (_t =
525
- repzo_price_list_items === null || repzo_price_list_items === void 0
526
- ? void 0
527
- : repzo_price_list_items.data) === null || _t === void 0
528
- ? void 0
529
- : _t.find((item) => {
530
- var _a, _b;
531
- return (
532
- ((_a =
533
- item === null || item === void 0
534
- ? void 0
535
- : item.integration_meta) === null || _a === void 0
536
- ? void 0
537
- : _a.id) ==
538
- ((_b =
539
- body === null || body === void 0
540
- ? void 0
541
- : body.integration_meta) === null || _b === void 0
542
- ? void 0
543
- : _b.id)
544
- );
545
- });
546
- // console.log(`${data.integration_meta?.id} => ${is_found_in_repzo_db ? "create" : "update"}`)
547
- if (!is_found_in_repzo_db) {
548
- // Create
549
- try {
550
- const created_PL_item = await repzo.priceListItem.create(body);
551
- result.PL_items.created++;
552
- } catch (e) {
553
- // console.log("Create PL Item Failed >> ", e?.response?.data, body);
554
- failed_docs_report.push({
555
- method: "create",
556
- // doc: body,
557
- error_message: set_error(e),
144
+ // Price List Items ***************************************
145
+ await commandLog
146
+ .addDetail(`Start Sync Price List Items From SAP to Repzo`)
147
+ .commit();
148
+ const repzo_all_priceLists = await repzo.priceList.find({
149
+ disabled: false,
150
+ per_page: 50000,
151
+ });
152
+ await commandLog
153
+ .addDetail(`${(_k = repzo_all_priceLists === null || repzo_all_priceLists === void 0 ? void 0 : repzo_all_priceLists.data) === null || _k === void 0 ? void 0 : _k.length} Price Lists in Repzo`)
154
+ .commit();
155
+ if (!((_l = repzo_all_priceLists === null || repzo_all_priceLists === void 0 ? void 0 : repzo_all_priceLists.data) === null || _l === void 0 ? void 0 : _l.length))
156
+ throw `No Price Lists was found On Repzo`;
157
+ for (let priceList_name in priceLists_withItems) {
158
+ const repzo_PriceList = (_m = repzo_all_priceLists === null || repzo_all_priceLists === void 0 ? void 0 : repzo_all_priceLists.data) === null || _m === void 0 ? void 0 : _m.find((pl) => { var _a; return ((_a = pl.integration_meta) === null || _a === void 0 ? void 0 : _a.id) == `${nameSpace}_${priceList_name}`; });
159
+ if (!repzo_PriceList) {
160
+ // console.log(
161
+ // `Price list with PLDID: ${priceList_name} was not created or disabled`
162
+ // );
163
+ failed_docs_report.push({
164
+ method: "create",
165
+ // doc: priceLists_withItems[priceList_name],
166
+ error_message: set_error(`Failed Create PriceList Items: number of PL items: ${priceLists_withItems[priceList_name].length} => Because Price list with PLDID: ${priceList_name} was not created or disabled`),
167
+ });
168
+ result.PL_items.failed +=
169
+ ((_o = priceLists_withItems[priceList_name]) === null || _o === void 0 ? void 0 : _o.length) || 0;
170
+ continue;
171
+ }
172
+ const repzo_price_list_items = await repzo.priceListItem.find({
173
+ disabled: false,
174
+ pricelist_id: repzo_PriceList === null || repzo_PriceList === void 0 ? void 0 : repzo_PriceList._id,
175
+ per_page: 50000,
558
176
  });
559
- result.PL_items.failed++;
560
- }
561
- } else {
562
- if (is_found_in_repzo_db.price == body.price) continue;
563
- // Update
564
- try {
565
- const updated_PL_item = await repzo.priceListItem.update(
566
- is_found_in_repzo_db._id,
567
- body
568
- );
569
- result.PL_items.updated++;
570
- } catch (e) {
571
- // console.log(
572
- // "Update Price List Item Failed >> ",
573
- // e?.response?.data,
574
- // body
575
- // );
576
- failed_docs_report.push({
577
- method: "update",
578
- doc_id:
579
- is_found_in_repzo_db === null || is_found_in_repzo_db === void 0
580
- ? void 0
581
- : is_found_in_repzo_db._id,
582
- doc: body,
583
- error_message: set_error(e),
177
+ // Create Price list items
178
+ const priceList_items = {};
179
+ priceLists_withItems[priceList_name].forEach((doc) => {
180
+ if (!sap_unique_UoMs[`${doc.PLITEMID}__${doc.PLITEMUNIT}`]) {
181
+ // console.log(
182
+ // `error => ${doc.PLITEMID}__${doc.PLITEMUNIT} was not found on the Uom`
183
+ // );
184
+ failed_docs_report.push({
185
+ method: "create",
186
+ // doc: priceLists_withItems[priceList_name],
187
+ error_message: set_error(`Create PL items => PLITEMID: ${doc.PLITEMID}, PLITEMUNIT:${doc.PLITEMUNIT} was not found on the Uom`),
188
+ });
189
+ result.PL_items.failed++;
190
+ return;
191
+ }
192
+ doc.factor = sap_unique_UoMs[`${doc.PLITEMID}__${doc.PLITEMUNIT}`];
193
+ const key = `${doc.PLITEMID}`;
194
+ if (!priceList_items[key]) {
195
+ priceList_items[key] = doc;
196
+ }
197
+ else {
198
+ const current_doc = priceList_items[key];
199
+ if (current_doc.PLITEMUNIT !=
200
+ repzo_product_default_measureunit[current_doc.PLITEMID]) {
201
+ if (doc.PLITEMUNIT ==
202
+ repzo_product_default_measureunit[current_doc.PLITEMID]) {
203
+ priceList_items[key] = doc;
204
+ }
205
+ else if ((current_doc === null || current_doc === void 0 ? void 0 : current_doc.factor) > (doc === null || doc === void 0 ? void 0 : doc.factor)) {
206
+ priceList_items[key] = doc;
207
+ }
208
+ }
209
+ }
584
210
  });
585
- result.PL_items.failed++;
586
- }
211
+ const priceListItems = Object.values(priceList_items);
212
+ for (let j = 0; j < priceListItems.length; j++) {
213
+ const item = priceListItems[j];
214
+ if (!item.factor && item.factor !== 0) {
215
+ failed_docs_report.push({
216
+ method: "create",
217
+ // doc: priceLists_withItems[priceList_name],
218
+ error_message: set_error(`Price List: ${item.PLDID} of Product with PLITEMID: ${item.PLITEMID} does not have Uom`),
219
+ });
220
+ result.PL_items.failed++;
221
+ continue;
222
+ }
223
+ const repzo_product = (_p = repzo_products === null || repzo_products === void 0 ? void 0 : repzo_products.data) === null || _p === void 0 ? void 0 : _p.find((product) => { var _a; return ((_a = product === null || product === void 0 ? void 0 : product.integration_meta) === null || _a === void 0 ? void 0 : _a.id) == `${nameSpace}_${item.PLITEMID}`; });
224
+ if (!repzo_product) {
225
+ failed_docs_report.push({
226
+ method: "create",
227
+ // doc: priceLists_withItems[priceList_name],
228
+ error_message: set_error(`Price List: ${item.PLDID} of Product with PLITEMID: ${item.PLITEMID} was not found or disabled`),
229
+ });
230
+ result.PL_items.failed++;
231
+ continue;
232
+ }
233
+ const repzo_product_uoms = (_q = repzo_UoMs === null || repzo_UoMs === void 0 ? void 0 : repzo_UoMs.data) === null || _q === void 0 ? void 0 : _q.filter((uom) => {
234
+ var _a, _b, _c, _d;
235
+ return ((_a = uom === null || uom === void 0 ? void 0 : uom._id) === null || _a === void 0 ? void 0 : _a.toString()) == ((_b = repzo_product === null || repzo_product === void 0 ? void 0 : repzo_product.sv_measureUnit) === null || _b === void 0 ? void 0 : _b.toString()) ||
236
+ ((_c = repzo_product.measureunit_family) === null || _c === void 0 ? void 0 : _c.includes((_d = uom === null || uom === void 0 ? void 0 : uom._id) === null || _d === void 0 ? void 0 : _d.toString()));
237
+ });
238
+ const repzo_product_uom = repzo_product_uoms.find((uom) => uom.name == item.PLITEMUNIT);
239
+ if (!repzo_product_uom) {
240
+ failed_docs_report.push({
241
+ method: "create",
242
+ // doc: priceLists_withItems[priceList_name],
243
+ error_message: set_error(`Price List: ${item.PLDID} of MeasureUnit with _id: ${(_r = repzo_product === null || repzo_product === void 0 ? void 0 : repzo_product.sv_measureUnit) === null || _r === void 0 ? void 0 : _r.toString()} was not found or disabled`),
244
+ });
245
+ result.PL_items.failed++;
246
+ continue;
247
+ }
248
+ const price = repzo_product_uom && (repzo_product_uom === null || repzo_product_uom === void 0 ? void 0 : repzo_product_uom.factor) == 1
249
+ ? Math.round(item.PLITEMPRICEVALUE * 1000)
250
+ : Math.round((item.PLITEMPRICEVALUE * 1000) / repzo_product_uom.factor);
251
+ const variant = (_s = repzo_product === null || repzo_product === void 0 ? void 0 : repzo_product.variants) === null || _s === void 0 ? void 0 : _s.find((variant) => { var _a; return ((_a = variant === null || variant === void 0 ? void 0 : variant.integration_meta) === null || _a === void 0 ? void 0 : _a.id) == `${nameSpace}_${item.PLITEMID}`; });
252
+ if (!variant) {
253
+ // console.log(
254
+ // `Price List: ${item.PLDID} of Variant with PLITEMID: ${item.PLITEMID} was not found`
255
+ // );
256
+ failed_docs_report.push({
257
+ method: "create",
258
+ // doc: priceLists_withItems[priceList_name],
259
+ error_message: set_error(`Price List: ${item.PLDID} of Variant with PLITEMID: ${item.PLITEMID} was not found`),
260
+ });
261
+ result.PL_items.failed++;
262
+ continue;
263
+ }
264
+ const body = {
265
+ integration_meta: {
266
+ id: `${nameSpace}_${item.PLDID}_${item.PLITEMID}`,
267
+ },
268
+ product_id: repzo_product._id,
269
+ variant_id: variant._id,
270
+ pricelist_id: repzo_PriceList._id,
271
+ price: price,
272
+ };
273
+ // console.log(data);
274
+ const is_found_in_repzo_db = (_t = repzo_price_list_items === null || repzo_price_list_items === void 0 ? void 0 : repzo_price_list_items.data) === null || _t === void 0 ? void 0 : _t.find((item) => { var _a, _b; return ((_a = item === null || item === void 0 ? void 0 : item.integration_meta) === null || _a === void 0 ? void 0 : _a.id) == ((_b = body === null || body === void 0 ? void 0 : body.integration_meta) === null || _b === void 0 ? void 0 : _b.id); });
275
+ // console.log(`${data.integration_meta?.id} => ${is_found_in_repzo_db ? "create" : "update"}`)
276
+ if (!is_found_in_repzo_db) {
277
+ // Create
278
+ try {
279
+ const created_PL_item = await repzo.priceListItem.create(body);
280
+ result.PL_items.created++;
281
+ }
282
+ catch (e) {
283
+ // console.log("Create PL Item Failed >> ", e?.response?.data, body);
284
+ failed_docs_report.push({
285
+ method: "create",
286
+ // doc: body,
287
+ error_message: set_error(e),
288
+ });
289
+ result.PL_items.failed++;
290
+ }
291
+ }
292
+ else {
293
+ if (is_found_in_repzo_db.price == body.price)
294
+ continue;
295
+ // Update
296
+ try {
297
+ const updated_PL_item = await repzo.priceListItem.update(is_found_in_repzo_db._id, body);
298
+ result.PL_items.updated++;
299
+ }
300
+ catch (e) {
301
+ // console.log(
302
+ // "Update Price List Item Failed >> ",
303
+ // e?.response?.data,
304
+ // body
305
+ // );
306
+ failed_docs_report.push({
307
+ method: "update",
308
+ doc_id: is_found_in_repzo_db === null || is_found_in_repzo_db === void 0 ? void 0 : is_found_in_repzo_db._id,
309
+ doc: body,
310
+ error_message: set_error(e),
311
+ });
312
+ result.PL_items.failed++;
313
+ }
314
+ }
315
+ }
587
316
  }
588
- }
317
+ // console.log(result);
318
+ await update_bench_time(repzo, commandEvent.app._id, bench_time_key, new_bench_time);
319
+ await commandLog
320
+ .setStatus("success", failed_docs_report.length ? failed_docs_report : null)
321
+ .setBody(result)
322
+ .commit();
323
+ return result;
324
+ }
325
+ catch (e) {
326
+ //@ts-ignore
327
+ console.error(((_u = e === null || e === void 0 ? void 0 : e.response) === null || _u === void 0 ? void 0 : _u.data) || e);
328
+ await commandLog.setStatus("fail", e).commit();
329
+ throw e;
589
330
  }
590
- // console.log(result);
591
- await update_bench_time(
592
- repzo,
593
- commandEvent.app._id,
594
- bench_time_key,
595
- new_bench_time
596
- );
597
- await commandLog
598
- .setStatus(
599
- "success",
600
- failed_docs_report.length ? failed_docs_report : null
601
- )
602
- .setBody(result)
603
- .commit();
604
- return result;
605
- } catch (e) {
606
- //@ts-ignore
607
- console.error(
608
- ((_u = e === null || e === void 0 ? void 0 : e.response) === null ||
609
- _u === void 0
610
- ? void 0
611
- : _u.data) || e
612
- );
613
- await commandLog.setStatus("fail", e).commit();
614
- throw e;
615
- }
616
331
  };
617
332
  const get_sap_price_list = async (serviceEndPoint, query) => {
618
- try {
619
- const sap_price_lists = await _create(serviceEndPoint, "/PriceList", {
620
- UpdateAt: query === null || query === void 0 ? void 0 : query.updateAt,
621
- });
622
- return sap_price_lists.PriceList;
623
- } catch (e) {
624
- throw e;
625
- }
333
+ try {
334
+ const sap_price_lists = await _create(serviceEndPoint, "/PriceList", { UpdateAt: query === null || query === void 0 ? void 0 : query.updateAt });
335
+ return sap_price_lists.PriceList;
336
+ }
337
+ catch (e) {
338
+ throw e;
339
+ }
626
340
  };