repzo-sap-absjo 1.0.53 → 1.0.55

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 (39) hide show
  1. package/lib/actions/create_client.js +1 -0
  2. package/lib/actions/create_invoice.d.ts +1 -1
  3. package/lib/actions/create_invoice.js +55 -8
  4. package/lib/actions/create_payment.d.ts +1 -1
  5. package/lib/actions/create_proforma.d.ts +1 -1
  6. package/lib/actions/create_proforma.js +67 -40
  7. package/lib/actions/create_return_invoice.d.ts +1 -1
  8. package/lib/actions/create_return_invoice.js +46 -2
  9. package/lib/actions/create_transfer.d.ts +1 -1
  10. package/lib/actions/index.d.ts +1 -1
  11. package/lib/commands/adjust_inventory.js +221 -4
  12. package/lib/commands/rep.js +65 -29
  13. package/lib/commands/warehouse.js +149 -4
  14. package/lib/index.d.ts +1 -1
  15. package/lib/types.d.ts +4 -0
  16. package/lib/util.d.ts +4 -4
  17. package/package.json +1 -1
  18. package/src/actions/create_client.ts +3 -2
  19. package/src/actions/create_invoice.ts +13 -4
  20. package/src/actions/create_proforma.ts +12 -6
  21. package/src/actions/create_return_invoice.ts +10 -1
  22. package/src/commands/adjust_inventory.ts +169 -2
  23. package/src/commands/bank.ts +5 -1
  24. package/src/commands/brand.ts +2 -2
  25. package/src/commands/category.ts +2 -2
  26. package/src/commands/channel.ts +2 -2
  27. package/src/commands/client.ts +2 -2
  28. package/src/commands/client_disabled.ts +2 -2
  29. package/src/commands/measureunit.ts +2 -2
  30. package/src/commands/payment_term.ts +2 -2
  31. package/src/commands/price_list.ts +2 -2
  32. package/src/commands/price_list_disabled.ts +2 -2
  33. package/src/commands/product.ts +8 -4
  34. package/src/commands/product_disabled.ts +8 -4
  35. package/src/commands/rep.ts +26 -2
  36. package/src/commands/tag.ts +2 -2
  37. package/src/commands/tax.ts +2 -2
  38. package/src/commands/warehouse.ts +136 -3
  39. package/src/types.ts +4 -0
@@ -216,6 +216,173 @@ export const adjust_inventory = async (commandEvent: CommandEvent) => {
216
216
  }
217
217
  }
218
218
 
219
+ const consider_virtual_warehouse =
220
+ commandEvent.app.formData?.virtualWarehouses
221
+ ?.consider_virtual_warehouse || false;
222
+
223
+ if (consider_virtual_warehouse) {
224
+ const absolute_qty_for_virtual_warehouses_before_accumulation =
225
+ commandEvent.app.formData?.virtualWarehouses
226
+ ?.consider_virtual_warehouse || true;
227
+ const repzo_virtual_warehouses = repzo_warehouses?.data.filter(
228
+ (wh) =>
229
+ wh.integration_meta?.is_virtual_warehouse &&
230
+ wh.integration_meta?.main_warehouses_codes?.length > 0
231
+ );
232
+ await commandLog
233
+ .addDetail(
234
+ `${repzo_virtual_warehouses?.length} Virtual Warehouse(s) in Repzo`
235
+ )
236
+ .commit();
237
+
238
+ for (let i = 0; i < repzo_virtual_warehouses.length; i++) {
239
+ try {
240
+ const repzo_warehouse = repzo_virtual_warehouses[i];
241
+ const sap_related_inventories = sap_inventories.filter((inventory) =>
242
+ repzo_warehouse.integration_meta?.main_warehouses_codes?.includes(
243
+ inventory.STOREID
244
+ )
245
+ );
246
+
247
+ // Get Repzo Inventory
248
+ const repzo_inventory = await repzo.inventory.find({
249
+ warehouse_id: repzo_warehouse._id,
250
+ per_page: 50000,
251
+ });
252
+
253
+ const shared_inventory: { [key: string]: SAPStoresBalance } = {};
254
+ sap_related_inventories.forEach((sap_inventory) => {
255
+ sap_inventory.items.forEach((item) => {
256
+ const key = `${item.ITEMID}__${item.UNITID}__${item.UNITNAME}`;
257
+ const QTY = item.QTY;
258
+ if (!shared_inventory[key]) {
259
+ shared_inventory[key] = item;
260
+ shared_inventory[key].QTY = 0;
261
+ }
262
+ if (absolute_qty_for_virtual_warehouses_before_accumulation) {
263
+ shared_inventory[key].QTY += QTY > 0 ? QTY : 0;
264
+ } else {
265
+ shared_inventory[key].QTY += QTY;
266
+ }
267
+ });
268
+ });
269
+
270
+ let variants = Object.values(shared_inventory).map((sap_item) => {
271
+ try {
272
+ const variant = repzo_variants?.data?.find(
273
+ (variant) =>
274
+ variant.integration_meta?.ITEMCODE == sap_item.ITEMID
275
+ );
276
+ if (!variant) {
277
+ // console.log(
278
+ // `Variant with ITEMCODE: ${sap_item.ITEMID} was not found`
279
+ // );
280
+ throw `Variant with ITEMCODE: ${sap_item.ITEMID} was not found`;
281
+ }
282
+
283
+ const measureUnit = repzo_measureunits?.data?.find(
284
+ (measure_unit) =>
285
+ measure_unit._id.toString() ==
286
+ (
287
+ variant.product as Service.Product.ProductSchema
288
+ )?.sv_measureUnit?.toString()
289
+ );
290
+ if (!measureUnit) {
291
+ // console.log(
292
+ // `MeasureUnit with UNITNAME: ${sap_item.UNITNAME} & ALTUOMID: ${sap_item.UNITID} was not found`
293
+ // );
294
+ throw `MeasureUnit with UNITNAME: ${sap_item.UNITNAME} & ALTUOMID: ${sap_item.UNITID} was not found`;
295
+ }
296
+
297
+ const qty = measureUnit.factor * sap_item.QTY;
298
+
299
+ const match_item_in_repzo_inventory = repzo_inventory?.data?.find(
300
+ (repzo_item) =>
301
+ repzo_item.variant_id.toString() == variant._id.toString()
302
+ );
303
+
304
+ if (match_item_in_repzo_inventory) {
305
+ //@ts-ignore
306
+ match_item_in_repzo_inventory.has_match_in_SAP = true;
307
+ }
308
+
309
+ // const diff_qty = match_item_in_repzo_inventory
310
+ // ? qty - match_item_in_repzo_inventory.qty
311
+ // : qty;
312
+
313
+ return {
314
+ variant: variant._id,
315
+ qty: qty,
316
+ match_item_in_repzo_inventory,
317
+ };
318
+ } catch (e) {
319
+ // console.log(e);
320
+ failed_docs_report.push({
321
+ method: "fetchingData",
322
+ doc_id: sap_item.UNITNAME,
323
+ doc: {
324
+ ...sap_item,
325
+ virtual_repzo_warehouse: repzo_warehouse.code,
326
+ },
327
+ error_message: set_error(e),
328
+ });
329
+ result.items_failed++;
330
+ }
331
+ });
332
+
333
+ const unit_variants: { [variant_id: string]: any } = {};
334
+ variants
335
+ .filter((i) => i)
336
+ .forEach((item: any) => {
337
+ if (!unit_variants[item.variant]) {
338
+ unit_variants[item.variant] = item;
339
+ } else {
340
+ unit_variants[item.variant].qty += item.qty;
341
+ }
342
+ });
343
+
344
+ variants = Object.values(unit_variants)
345
+ .map((item) => {
346
+ item.qty = item.match_item_in_repzo_inventory
347
+ ? item.qty - item.match_item_in_repzo_inventory.qty
348
+ : item.qty;
349
+ return item;
350
+ })
351
+ .concat(
352
+ ...repzo_inventory?.data
353
+ //@ts-ignore
354
+ ?.filter((item) => !item.has_match_in_SAP)
355
+ .map((item) => {
356
+ return { variant: item.variant_id, qty: -1 * item.qty };
357
+ })
358
+ )
359
+ .filter((item) => item && item.qty);
360
+
361
+ const body: Service.AdjustInventory.Create.Body = {
362
+ time: Date.now(),
363
+ sync_id: uuid(),
364
+ to: repzo_warehouse._id,
365
+ //@ts-ignore
366
+ variants: variants,
367
+ };
368
+ // console.log(body);
369
+ if (!body.variants.length) continue;
370
+
371
+ const res = await repzo.adjustInventory.create(body);
372
+ result.created++;
373
+ } catch (e) {
374
+ // console.log(e);
375
+ failed_docs_report.push({
376
+ method: "fetchingData",
377
+ doc_id: repzo_virtual_warehouses[i]._id,
378
+ doc: { virtual_repzo_warehouse: repzo_virtual_warehouses[i].code },
379
+ error_message: set_error(e),
380
+ });
381
+ result.failed++;
382
+ }
383
+ }
384
+ }
385
+
219
386
  // console.log(result);
220
387
  await commandLog
221
388
  .setStatus(
@@ -238,11 +405,11 @@ const get_sap_inventories = async (
238
405
  query?: { updateAt?: string }
239
406
  ): Promise<SAPStoresBalance[]> => {
240
407
  try {
241
- const sap_inventories: SAPStoresBalances = await _create(
408
+ const sap_inventories: SAPStoresBalances = (await _create(
242
409
  serviceEndPoint,
243
410
  "/StoresBalance",
244
411
  {}
245
- );
412
+ )) as SAPStoresBalances;
246
413
  return sap_inventories.StoresBalance;
247
414
  } catch (e: any) {
248
415
  throw e;
@@ -235,7 +235,11 @@ const get_sap_banks = async (
235
235
  query?: { updateAt?: string }
236
236
  ): Promise<SAPBank[]> => {
237
237
  try {
238
- const sap_banks: SAPBanks = await _create(serviceEndPoint, "/Banks", {});
238
+ const sap_banks: SAPBanks = (await _create(
239
+ serviceEndPoint,
240
+ "/Banks",
241
+ {}
242
+ )) as SAPBanks;
239
243
  return sap_banks.Banks;
240
244
  } catch (e: any) {
241
245
  throw e;
@@ -158,11 +158,11 @@ const get_sap_brands = async (
158
158
  query?: { updateAt?: string }
159
159
  ): Promise<SAPBrand[]> => {
160
160
  try {
161
- const sap_brands: SAPBrands = await _create(
161
+ const sap_brands: SAPBrands = (await _create(
162
162
  serviceEndPoint,
163
163
  "/ParentCategory",
164
164
  { UpdateAt: "20201230:000000", Active: "Y" }
165
- );
165
+ )) as SAPBrands;
166
166
  return sap_brands.ItemSubGroup;
167
167
  } catch (e: any) {
168
168
  throw e;
@@ -164,11 +164,11 @@ const get_sap_categories = async (
164
164
  query?: { updateAt?: string }
165
165
  ): Promise<SAPCategory[]> => {
166
166
  try {
167
- const sap_categories: SAPCategories = await _create(
167
+ const sap_categories: SAPCategories = (await _create(
168
168
  serviceEndPoint,
169
169
  "/ItemGroup",
170
170
  {}
171
- );
171
+ )) as SAPCategories;
172
172
  return sap_categories.ItemGroup;
173
173
  } catch (e: any) {
174
174
  throw e;
@@ -202,7 +202,7 @@ const get_sap_clients = async (
202
202
  query?: { updateAt?: string; GroupCode?: string }
203
203
  ): Promise<SAPClient[]> => {
204
204
  try {
205
- const sap_clients: SAPClients = await _create(
205
+ const sap_clients: SAPClients = (await _create(
206
206
  serviceEndPoint,
207
207
  "/Customers",
208
208
  {
@@ -211,7 +211,7 @@ const get_sap_clients = async (
211
211
  UpdateAt: date_formatting(query?.updateAt, "YYYYMMDD:000000"),
212
212
  GroupCode: query?.GroupCode || "",
213
213
  }
214
- );
214
+ )) as SAPClients;
215
215
  return sap_clients.Customers;
216
216
  } catch (e: any) {
217
217
  throw e;
@@ -312,7 +312,7 @@ const get_sap_clients = async (
312
312
  query?: { updateAt?: string; GroupCode?: string }
313
313
  ): Promise<SAPClient[]> => {
314
314
  try {
315
- const sap_clients: SAPClients = await _create(
315
+ const sap_clients: SAPClients = (await _create(
316
316
  serviceEndPoint,
317
317
  "/Customers",
318
318
  {
@@ -321,7 +321,7 @@ const get_sap_clients = async (
321
321
  UpdateAt: date_formatting(query?.updateAt, "YYYYMMDD:000000"),
322
322
  GroupCode: query?.GroupCode || "",
323
323
  }
324
- );
324
+ )) as SAPClients;
325
325
  return sap_clients.Customers;
326
326
  } catch (e: any) {
327
327
  throw e;
@@ -142,7 +142,7 @@ const get_sap_clients = async (
142
142
  query?: { updateAt?: string; GroupCode?: string }
143
143
  ): Promise<SAPClient[]> => {
144
144
  try {
145
- const sap_clients: SAPClients = await _create(
145
+ const sap_clients: SAPClients = (await _create(
146
146
  serviceEndPoint,
147
147
  "/Customers",
148
148
  {
@@ -151,7 +151,7 @@ const get_sap_clients = async (
151
151
  UpdateAt: date_formatting(query?.updateAt, "YYYYMMDD:000000"),
152
152
  GroupCode: query?.GroupCode || "",
153
153
  }
154
- );
154
+ )) as SAPClients;
155
155
  return sap_clients.Customers;
156
156
  } catch (e: any) {
157
157
  throw e;
@@ -279,10 +279,10 @@ export const get_sap_UoMs = async (
279
279
  query?: { updateAt?: string }
280
280
  ): Promise<SAPUoM[]> => {
281
281
  try {
282
- const sap_UoMs: SAPUoMs = await _create(serviceEndPoint, "/Uom", {
282
+ const sap_UoMs: SAPUoMs = (await _create(serviceEndPoint, "/Uom", {
283
283
  Inactive: "N",
284
284
  Locked: "N",
285
- });
285
+ })) as SAPUoMs;
286
286
  return sap_UoMs?.UoM;
287
287
  } catch (e: any) {
288
288
  throw e;
@@ -207,7 +207,7 @@ const get_sap_clients = async (
207
207
  query?: { updateAt?: string; GroupCode?: string }
208
208
  ): Promise<SAPClient[]> => {
209
209
  try {
210
- const sap_clients: SAPClients = await _create(
210
+ const sap_clients: SAPClients = (await _create(
211
211
  serviceEndPoint,
212
212
  "/Customers",
213
213
  {
@@ -216,7 +216,7 @@ const get_sap_clients = async (
216
216
  UpdateAt: date_formatting(query?.updateAt, "YYYYMMDD:000000"),
217
217
  GroupCode: query?.GroupCode || "",
218
218
  }
219
- );
219
+ )) as SAPClients;
220
220
  return sap_clients.Customers;
221
221
  } catch (e: any) {
222
222
  throw e;
@@ -463,11 +463,11 @@ const get_sap_price_list = async (
463
463
  query?: { updateAt?: string }
464
464
  ): Promise<SAPPriceListItem[]> => {
465
465
  try {
466
- const sap_price_lists: SAPPriceListItems = await _create(
466
+ const sap_price_lists: SAPPriceListItems = (await _create(
467
467
  serviceEndPoint,
468
468
  "/PriceList",
469
469
  { UpdateAt: query?.updateAt }
470
- );
470
+ )) as SAPPriceListItems;
471
471
  return sap_price_lists.PriceList;
472
472
  } catch (e: any) {
473
473
  throw e;
@@ -182,11 +182,11 @@ const get_sap_price_list = async (
182
182
  query?: { updateAt?: string; PLDID?: string }
183
183
  ): Promise<SAPPriceListItem[]> => {
184
184
  try {
185
- const sap_price_lists: SAPPriceListItems = await _create(
185
+ const sap_price_lists: SAPPriceListItems = (await _create(
186
186
  serviceEndPoint,
187
187
  "/PriceList",
188
188
  { UpdateAt: query?.updateAt, PLDID: query?.PLDID }
189
- );
189
+ )) as SAPPriceListItems;
190
190
  return sap_price_lists.PriceList;
191
191
  } catch (e: any) {
192
192
  throw e;
@@ -326,10 +326,14 @@ const get_sap_products = async (
326
326
  query?: { updateAt?: string }
327
327
  ): Promise<SAPProduct[]> => {
328
328
  try {
329
- const sap_products: SAPProducts = await _create(serviceEndPoint, "/Items", {
330
- Active: "Y",
331
- UpdateAt: date_formatting(query?.updateAt, "YYYYMMDD:000000"),
332
- });
329
+ const sap_products: SAPProducts = (await _create(
330
+ serviceEndPoint,
331
+ "/Items",
332
+ {
333
+ Active: "Y",
334
+ UpdateAt: date_formatting(query?.updateAt, "YYYYMMDD:000000"),
335
+ }
336
+ )) as SAPProducts;
333
337
  return sap_products.Items;
334
338
  } catch (e: any) {
335
339
  throw e;
@@ -144,10 +144,14 @@ const get_sap_disabled_products = async (
144
144
  query?: { updateAt?: string }
145
145
  ): Promise<SAPProduct[]> => {
146
146
  try {
147
- const sap_products: SAPProducts = await _create(serviceEndPoint, "/Items", {
148
- Active: "N",
149
- UpdateAt: date_formatting(query?.updateAt, "YYYYMMDD:000000"),
150
- });
147
+ const sap_products: SAPProducts = (await _create(
148
+ serviceEndPoint,
149
+ "/Items",
150
+ {
151
+ Active: "N",
152
+ UpdateAt: date_formatting(query?.updateAt, "YYYYMMDD:000000"),
153
+ }
154
+ )) as SAPProducts;
151
155
  return sap_products.Items;
152
156
  } catch (e: any) {
153
157
  throw e;
@@ -20,6 +20,8 @@ interface SAPRep {
20
20
  USERCHECKACCTCODE: string; // "124020003";
21
21
  USERWHSCODE: string; // "MToffers";
22
22
  INVOICESTATUS: "Y" | "N";
23
+ VIRTUALWAREHOUSECODE: null | number;
24
+ VIRTUALWAREHOUSENAME: null | string;
23
25
  }
24
26
 
25
27
  interface SAPReps {
@@ -41,6 +43,9 @@ export const sync_rep = async (commandEvent: CommandEvent) => {
41
43
 
42
44
  const new_bench_time = new Date().toISOString();
43
45
  const bench_time_key = "bench_time_rep";
46
+ const consider_virtual_warehouse =
47
+ commandEvent.app.formData?.virtualWarehouses
48
+ ?.consider_virtual_warehouse || false;
44
49
 
45
50
  await commandLog.load(commandEvent.sync_id);
46
51
  await commandLog.addDetail("Repzo SAP: Started Syncing Reps").commit();
@@ -86,6 +91,8 @@ export const sync_rep = async (commandEvent: CommandEvent) => {
86
91
  USERCHECKACCTCODE: true,
87
92
  USERWHSCODE: true,
88
93
  INVOICESTATUS: true,
94
+ VIRTUALWAREHOUSECODE: true,
95
+ VIRTUALWAREHOUSENAME: true,
89
96
  });
90
97
  db.load(sap_reps?.Users);
91
98
 
@@ -117,6 +124,17 @@ export const sync_rep = async (commandEvent: CommandEvent) => {
117
124
  if (warehouse_res) warehouse = warehouse_res._id;
118
125
  }
119
126
 
127
+ let virtual_warehouse;
128
+ if (consider_virtual_warehouse && sap_rep.VIRTUALWAREHOUSECODE) {
129
+ const virtual_warehouse_res = repzo_warehouses?.data.find(
130
+ (w) =>
131
+ w.integration_meta?.is_virtual_warehouse &&
132
+ w.code == `Virtual ${sap_rep.VIRTUALWAREHOUSECODE}`
133
+ );
134
+ if (virtual_warehouse_res)
135
+ virtual_warehouse = virtual_warehouse_res._id;
136
+ }
137
+
120
138
  const body: Service.Rep.Create.Body | Service.Rep.Update.Body = {
121
139
  name: sap_rep.USERDESC,
122
140
  password: Math.round(Math.random() * (9999 - 1000) + 1000).toString(),
@@ -128,9 +146,11 @@ export const sync_rep = async (commandEvent: CommandEvent) => {
128
146
  USERCHECKACCTCODE: sap_rep.USERCHECKACCTCODE,
129
147
  USERWHSCODE: sap_rep.USERWHSCODE,
130
148
  INVOICESTATUS: sap_rep.INVOICESTATUS,
149
+ VIRTUALWAREHOUSECODE: sap_rep.VIRTUALWAREHOUSECODE,
150
+ VIRTUALWAREHOUSENAME: sap_rep.VIRTUALWAREHOUSENAME,
131
151
  id: `${nameSpace}_${sap_rep.USERID}`,
132
152
  },
133
- assigned_warehouse: warehouse,
153
+ assigned_warehouse: virtual_warehouse || warehouse,
134
154
  company_namespace: [nameSpace],
135
155
  "settings.treating_invoice_as_proforma_for_etax":
136
156
  sap_rep.INVOICESTATUS === "Y" ? true : false,
@@ -161,6 +181,10 @@ export const sync_rep = async (commandEvent: CommandEvent) => {
161
181
  USERCHECKACCTCODE: repzo_rep.integration_meta?.USERCHECKACCTCODE,
162
182
  USERWHSCODE: repzo_rep.integration_meta?.USERWHSCODE,
163
183
  INVOICESTATUS: repzo_rep.integration_meta?.INVOICESTATUS,
184
+ VIRTUALWAREHOUSECODE:
185
+ repzo_rep.integration_meta?.VIRTUALWAREHOUSECODE,
186
+ VIRTUALWAREHOUSENAME:
187
+ repzo_rep.integration_meta?.VIRTUALWAREHOUSENAME,
164
188
  });
165
189
  if (found_identical_docs.length) continue;
166
190
  // Update
@@ -214,7 +238,7 @@ const get_sap_reps = async (
214
238
  query?: { updateAt?: string }
215
239
  ): Promise<SAPReps> => {
216
240
  try {
217
- const sap_reps: SAPReps = await _create(serviceEndPoint, "/Users", {});
241
+ const sap_reps = (await _create(serviceEndPoint, "/Users", {})) as SAPReps;
218
242
  return sap_reps;
219
243
  } catch (e: any) {
220
244
  throw e;
@@ -163,9 +163,9 @@ const get_sap_tags = async (
163
163
  query?: { updateAt?: string }
164
164
  ): Promise<SAPTags> => {
165
165
  try {
166
- const sap_tags: SAPTags = await _create(serviceEndPoint, "/Territories", {
166
+ const sap_tags: SAPTags = (await _create(serviceEndPoint, "/Territories", {
167
167
  Inactive: "N",
168
- });
168
+ })) as SAPTags;
169
169
  return sap_tags;
170
170
  } catch (e: any) {
171
171
  throw e;
@@ -168,9 +168,9 @@ const get_sap_taxes = async (
168
168
  query?: { updateAt?: string }
169
169
  ): Promise<SAPTaxes> => {
170
170
  try {
171
- const sap_taxes: SAPTaxes = await _create(serviceEndPoint, "/Taxes", {
171
+ const sap_taxes: SAPTaxes = (await _create(serviceEndPoint, "/Taxes", {
172
172
  Inactive: "N",
173
- });
173
+ })) as SAPTaxes;
174
174
  return sap_taxes;
175
175
  } catch (e: any) {
176
176
  throw e;
@@ -17,7 +17,13 @@ interface WarehouseBody {
17
17
  type: "van" | "main";
18
18
  disabled?: boolean;
19
19
  code?: string;
20
- integration_meta?: { id: string };
20
+ integration_meta?: {
21
+ id?: string;
22
+ is_virtual_warehouse?: boolean;
23
+ main_warehouses_codes?: string[];
24
+ VIRTUALWAREHOUSECODE?: null | number;
25
+ VIRTUALWAREHOUSENAME?: null | string;
26
+ };
21
27
  }
22
28
 
23
29
  interface SAPWarehouse {
@@ -27,6 +33,8 @@ interface SAPWarehouse {
27
33
  INACTIVE: string; // "N";
28
34
  CREATEDATE: string; // "2021-12-20T21:00:00Z";
29
35
  UPDATEDATE: string; // "2021-12-29T21:00:00Z";
36
+ VIRTUALWAREHOUSECODE: null | number;
37
+ VIRTUALWAREHOUSENAME: null | string;
30
38
  }
31
39
 
32
40
  interface SAPWarehouses {
@@ -87,6 +95,8 @@ export const sync_warehouse = async (commandEvent: CommandEvent) => {
87
95
  db.createIndex({
88
96
  WAREHOUSECODE: true,
89
97
  WAREHOUSENAME: true,
98
+ VIRTUALWAREHOUSECODE: true,
99
+ VIRTUALWAREHOUSENAME: true,
90
100
  });
91
101
  db.load(sap_warehouses?.Warehouses);
92
102
 
@@ -114,6 +124,11 @@ export const sync_warehouse = async (commandEvent: CommandEvent) => {
114
124
  ? "van"
115
125
  : "main",
116
126
  disabled: sap_warehouse.INACTIVE == "N" ? false : true,
127
+ integration_meta: {
128
+ is_virtual_warehouse: false,
129
+ VIRTUALWAREHOUSECODE: sap_warehouse.VIRTUALWAREHOUSECODE,
130
+ VIRTUALWAREHOUSENAME: sap_warehouse.VIRTUALWAREHOUSENAME,
131
+ },
117
132
  };
118
133
 
119
134
  if (!repzo_warehouse) {
@@ -134,6 +149,10 @@ export const sync_warehouse = async (commandEvent: CommandEvent) => {
134
149
  const found_identical_docs = db.search({
135
150
  WAREHOUSECODE: repzo_warehouse.code,
136
151
  WAREHOUSENAME: repzo_warehouse.name,
152
+ VIRTUALWAREHOUSECODE:
153
+ repzo_warehouse.integration_meta?.VIRTUALWAREHOUSECODE,
154
+ VIRTUALWAREHOUSENAME:
155
+ repzo_warehouse.integration_meta?.VIRTUALWAREHOUSENAME,
137
156
  });
138
157
  if (found_identical_docs.length) continue; // Nothing has changed so no need for updates
139
158
 
@@ -157,6 +176,120 @@ export const sync_warehouse = async (commandEvent: CommandEvent) => {
157
176
  }
158
177
  }
159
178
 
179
+ if (
180
+ commandEvent.app.formData?.virtualWarehouses?.consider_virtual_warehouse
181
+ ) {
182
+ await commandLog
183
+ .addDetail("Repzo SAP: Started Syncing Virtual Warehouses")
184
+ .commit();
185
+ const unique_virtual_warehouses: {
186
+ [key: number]: {
187
+ code: number;
188
+ name: string;
189
+ main_warehouses_codes: string[];
190
+ };
191
+ } = {};
192
+ sap_warehouses?.Warehouses?.forEach((sap_warehouse) => {
193
+ if (
194
+ sap_warehouse.VIRTUALWAREHOUSECODE &&
195
+ sap_warehouse.VIRTUALWAREHOUSENAME
196
+ ) {
197
+ if (!unique_virtual_warehouses[sap_warehouse.VIRTUALWAREHOUSECODE]) {
198
+ unique_virtual_warehouses[sap_warehouse.VIRTUALWAREHOUSECODE] = {
199
+ code: sap_warehouse.VIRTUALWAREHOUSECODE,
200
+ name: sap_warehouse.VIRTUALWAREHOUSENAME,
201
+ main_warehouses_codes: [sap_warehouse.WAREHOUSECODE],
202
+ };
203
+ } else {
204
+ unique_virtual_warehouses[
205
+ sap_warehouse.VIRTUALWAREHOUSECODE
206
+ ].main_warehouses_codes.push(sap_warehouse.WAREHOUSECODE);
207
+ }
208
+ }
209
+ });
210
+
211
+ const sap_virtual_warehouses = Object.values(unique_virtual_warehouses);
212
+ await commandLog
213
+ .addDetail(
214
+ `${sap_virtual_warehouses?.length} virtual warehouses changed since ever`
215
+ )
216
+ .commit();
217
+
218
+ const db_2 = new DataSet([], { autoIndex: false });
219
+ db_2.createIndex({ name: true, code: true, main_warehouses_codes: true });
220
+ db_2.load(Object.values(sap_virtual_warehouses));
221
+
222
+ for (let i = 0; i < sap_virtual_warehouses.length; i++) {
223
+ const sap_warehouse: { code: number; name: string } =
224
+ sap_virtual_warehouses[i];
225
+ const repzo_warehouse = repzo_warehouses.data.find(
226
+ (r_warehouse) =>
227
+ r_warehouse.integration_meta?.is_virtual_warehouse &&
228
+ (r_warehouse.integration_meta?.code ==
229
+ `Virtual ${sap_warehouse.code}` ||
230
+ r_warehouse.name == sap_warehouse.name)
231
+ );
232
+
233
+ const body: WarehouseBody = {
234
+ _id: repzo_warehouse?._id,
235
+ name: sap_warehouse.name,
236
+ code: `Virtual ${sap_warehouse.code}`,
237
+ type: "main",
238
+ disabled: false,
239
+ integration_meta: {
240
+ is_virtual_warehouse: true,
241
+ main_warehouses_codes:
242
+ unique_virtual_warehouses[sap_warehouse.code]
243
+ .main_warehouses_codes,
244
+ VIRTUALWAREHOUSECODE: sap_warehouse.code,
245
+ VIRTUALWAREHOUSENAME: sap_warehouse.name,
246
+ },
247
+ };
248
+
249
+ if (!repzo_warehouse) {
250
+ // Create
251
+ try {
252
+ const created_warehouse = await repzo.warehouse.create(body);
253
+ result.created++;
254
+ } catch (e: any) {
255
+ // console.log("Create warehouse Failed >> ", e?.response, body);
256
+ failed_docs_report.push({
257
+ method: "create",
258
+ doc: body,
259
+ error_message: set_error(e),
260
+ });
261
+ result.failed++;
262
+ }
263
+ } else {
264
+ const found_identical_docs = db_2.search({
265
+ code: repzo_warehouse.code,
266
+ name: repzo_warehouse.name,
267
+ main_warehouses_codes:
268
+ repzo_warehouse.integration_meta?.main_warehouses_codes || [],
269
+ });
270
+ if (found_identical_docs.length) continue; // Nothing has changed so no need for updates
271
+
272
+ // Update
273
+ try {
274
+ const updated_warehouse = await repzo.warehouse.update(
275
+ repzo_warehouse._id,
276
+ body
277
+ );
278
+ result.updated++;
279
+ } catch (e) {
280
+ // console.log("Update warehouse Failed >> ", e, body);
281
+ failed_docs_report.push({
282
+ method: "update",
283
+ doc_id: repzo_warehouse?._id,
284
+ doc: body,
285
+ error_message: set_error(e),
286
+ });
287
+ result.failed++;
288
+ }
289
+ }
290
+ }
291
+ }
292
+
160
293
  // console.log(result);
161
294
 
162
295
  await update_bench_time(
@@ -188,7 +321,7 @@ const get_sap_warehouses = async (
188
321
  query?: { updateAt?: string }
189
322
  ): Promise<SAPWarehouses> => {
190
323
  try {
191
- const sap_warehouses: SAPWarehouses = await _create(
324
+ const sap_warehouses: SAPWarehouses = (await _create(
192
325
  serviceEndPoint,
193
326
  "/Warehouses",
194
327
  {
@@ -196,7 +329,7 @@ const get_sap_warehouses = async (
196
329
  Inactive: "N",
197
330
  Locked: "N",
198
331
  }
199
- );
332
+ )) as SAPWarehouses;
200
333
  return sap_warehouses;
201
334
  } catch (e: any) {
202
335
  throw e;