repzo-sap-absjo 1.0.54 → 1.0.56

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.
@@ -2,7 +2,7 @@ import Repzo from "repzo";
2
2
  import DataSet from "data-set-query";
3
3
  import { _create, update_bench_time, set_error } from "../util.js";
4
4
  export const sync_rep = async (commandEvent) => {
5
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
5
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r;
6
6
  const repzo = new Repzo(
7
7
  (_a = commandEvent.app.formData) === null || _a === void 0
8
8
  ? void 0
@@ -20,6 +20,13 @@ export const sync_rep = async (commandEvent) => {
20
20
  console.log("sync_rep");
21
21
  const new_bench_time = new Date().toISOString();
22
22
  const bench_time_key = "bench_time_rep";
23
+ const consider_virtual_warehouse =
24
+ ((_c =
25
+ (_b = commandEvent.app.formData) === null || _b === void 0
26
+ ? void 0
27
+ : _b.virtualWarehouses) === null || _c === void 0
28
+ ? void 0
29
+ : _c.consider_virtual_warehouse) || false;
23
30
  await commandLog.load(commandEvent.sync_id);
24
31
  await commandLog.addDetail("Repzo SAP: Started Syncing Reps").commit();
25
32
  const nameSpace = commandEvent.nameSpace.join("_");
@@ -36,11 +43,11 @@ export const sync_rep = async (commandEvent) => {
36
43
  {}
37
44
  );
38
45
  result.sap_total =
39
- (_b =
46
+ (_d =
40
47
  sap_reps === null || sap_reps === void 0 ? void 0 : sap_reps.Users) ===
41
- null || _b === void 0
48
+ null || _d === void 0
42
49
  ? void 0
43
- : _b.length;
50
+ : _d.length;
44
51
  await commandLog.addDetail(`${result.sap_total} reps in SAP`).commit();
45
52
  // hard code ************************************
46
53
  // sap_reps?.Users?.forEach((sap_rep) => {
@@ -64,25 +71,27 @@ export const sync_rep = async (commandEvent) => {
64
71
  USERCHECKACCTCODE: true,
65
72
  USERWHSCODE: true,
66
73
  INVOICESTATUS: true,
74
+ VIRTUALWAREHOUSECODE: true,
75
+ VIRTUALWAREHOUSENAME: true,
67
76
  });
68
77
  db.load(sap_reps === null || sap_reps === void 0 ? void 0 : sap_reps.Users);
69
78
  const repzo_reps = await repzo.rep.find({ per_page: 50000 });
70
79
  result.repzo_total =
71
- (_c =
80
+ (_e =
72
81
  repzo_reps === null || repzo_reps === void 0
73
82
  ? void 0
74
- : repzo_reps.data) === null || _c === void 0
83
+ : repzo_reps.data) === null || _e === void 0
75
84
  ? void 0
76
- : _c.length;
85
+ : _e.length;
77
86
  await commandLog
78
87
  .addDetail(
79
88
  `${
80
- (_d =
89
+ (_f =
81
90
  repzo_reps === null || repzo_reps === void 0
82
91
  ? void 0
83
- : repzo_reps.data) === null || _d === void 0
92
+ : repzo_reps.data) === null || _f === void 0
84
93
  ? void 0
85
- : _d.length
94
+ : _f.length
86
95
  } reps in Repzo`
87
96
  )
88
97
  .commit();
@@ -94,11 +103,11 @@ export const sync_rep = async (commandEvent) => {
94
103
  for (
95
104
  let i = 0;
96
105
  i <
97
- ((_e =
106
+ ((_g =
98
107
  sap_reps === null || sap_reps === void 0 ? void 0 : sap_reps.Users) ===
99
- null || _e === void 0
108
+ null || _g === void 0
100
109
  ? void 0
101
- : _e.length);
110
+ : _g.length);
102
111
  i++
103
112
  ) {
104
113
  const sap_rep = sap_reps.Users[i];
@@ -119,23 +128,42 @@ export const sync_rep = async (commandEvent) => {
119
128
  // await repzo.warehouse.find({ code: sap_rep.USERWHSCODE });
120
129
  if (warehouse_res) warehouse = warehouse_res._id;
121
130
  }
131
+ let virtual_warehouse;
132
+ if (consider_virtual_warehouse && sap_rep.VIRTUALWAREHOUSECODE) {
133
+ const virtual_warehouse_res =
134
+ repzo_warehouses === null || repzo_warehouses === void 0
135
+ ? void 0
136
+ : repzo_warehouses.data.find((w) => {
137
+ var _a;
138
+ return (
139
+ ((_a = w.integration_meta) === null || _a === void 0
140
+ ? void 0
141
+ : _a.is_virtual_warehouse) &&
142
+ w.code == `Virtual ${sap_rep.VIRTUALWAREHOUSECODE}`
143
+ );
144
+ });
145
+ if (virtual_warehouse_res)
146
+ virtual_warehouse = virtual_warehouse_res._id;
147
+ }
122
148
  const body = {
123
149
  name: sap_rep.USERDESC,
124
150
  password: Math.round(Math.random() * (9999 - 1000) + 1000).toString(),
125
151
  username: nameSpace + sap_rep.USERID,
126
152
  integration_id:
127
- (_f = sap_rep.USERID) === null || _f === void 0
153
+ (_h = sap_rep.USERID) === null || _h === void 0
128
154
  ? void 0
129
- : _f.toString(),
155
+ : _h.toString(),
130
156
  integration_meta: {
131
157
  DEPARTMENTCODE: sap_rep.DEPARTMENTCODE,
132
158
  USERCASHACCOUNT: sap_rep.USERCASHACCOUNT,
133
159
  USERCHECKACCTCODE: sap_rep.USERCHECKACCTCODE,
134
160
  USERWHSCODE: sap_rep.USERWHSCODE,
135
161
  INVOICESTATUS: sap_rep.INVOICESTATUS,
162
+ VIRTUALWAREHOUSECODE: sap_rep.VIRTUALWAREHOUSECODE,
163
+ VIRTUALWAREHOUSENAME: sap_rep.VIRTUALWAREHOUSENAME,
136
164
  id: `${nameSpace}_${sap_rep.USERID}`,
137
165
  },
138
- assigned_warehouse: warehouse,
166
+ assigned_warehouse: virtual_warehouse || warehouse,
139
167
  company_namespace: [nameSpace],
140
168
  "settings.treating_invoice_as_proforma_for_etax":
141
169
  sap_rep.INVOICESTATUS === "Y" ? true : false,
@@ -159,25 +187,33 @@ export const sync_rep = async (commandEvent) => {
159
187
  USERID: repzo_rep.integration_id,
160
188
  USERDESC: repzo_rep.name,
161
189
  DEPARTMENTCODE:
162
- (_g = repzo_rep.integration_meta) === null || _g === void 0
190
+ (_j = repzo_rep.integration_meta) === null || _j === void 0
163
191
  ? void 0
164
- : _g.DEPARTMENTCODE,
192
+ : _j.DEPARTMENTCODE,
165
193
  USERCASHACCOUNT:
166
- (_h = repzo_rep.integration_meta) === null || _h === void 0
194
+ (_k = repzo_rep.integration_meta) === null || _k === void 0
167
195
  ? void 0
168
- : _h.USERCASHACCOUNT,
196
+ : _k.USERCASHACCOUNT,
169
197
  USERCHECKACCTCODE:
170
- (_j = repzo_rep.integration_meta) === null || _j === void 0
198
+ (_l = repzo_rep.integration_meta) === null || _l === void 0
171
199
  ? void 0
172
- : _j.USERCHECKACCTCODE,
200
+ : _l.USERCHECKACCTCODE,
173
201
  USERWHSCODE:
174
- (_k = repzo_rep.integration_meta) === null || _k === void 0
202
+ (_m = repzo_rep.integration_meta) === null || _m === void 0
175
203
  ? void 0
176
- : _k.USERWHSCODE,
204
+ : _m.USERWHSCODE,
177
205
  INVOICESTATUS:
178
- (_l = repzo_rep.integration_meta) === null || _l === void 0
206
+ (_o = repzo_rep.integration_meta) === null || _o === void 0
207
+ ? void 0
208
+ : _o.INVOICESTATUS,
209
+ VIRTUALWAREHOUSECODE:
210
+ (_p = repzo_rep.integration_meta) === null || _p === void 0
211
+ ? void 0
212
+ : _p.VIRTUALWAREHOUSECODE,
213
+ VIRTUALWAREHOUSENAME:
214
+ (_q = repzo_rep.integration_meta) === null || _q === void 0
179
215
  ? void 0
180
- : _l.INVOICESTATUS,
216
+ : _q.VIRTUALWAREHOUSENAME,
181
217
  });
182
218
  if (found_identical_docs.length) continue;
183
219
  // Update
@@ -219,10 +255,10 @@ export const sync_rep = async (commandEvent) => {
219
255
  } catch (e) {
220
256
  //@ts-ignore
221
257
  console.error(
222
- ((_m = e === null || e === void 0 ? void 0 : e.response) === null ||
223
- _m === void 0
258
+ ((_r = e === null || e === void 0 ? void 0 : e.response) === null ||
259
+ _r === void 0
224
260
  ? void 0
225
- : _m.data) || e
261
+ : _r.data) || e
226
262
  );
227
263
  await commandLog.setStatus("fail", e).commit();
228
264
  throw e;
@@ -7,7 +7,7 @@ import {
7
7
  set_error,
8
8
  } from "../util.js";
9
9
  export const sync_warehouse = async (commandEvent) => {
10
- var _a, _b, _c, _d, _e, _f, _g, _h, _j;
10
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
11
11
  const repzo = new Repzo(
12
12
  (_a = commandEvent.app.formData) === null || _a === void 0
13
13
  ? void 0
@@ -77,6 +77,8 @@ export const sync_warehouse = async (commandEvent) => {
77
77
  db.createIndex({
78
78
  WAREHOUSECODE: true,
79
79
  WAREHOUSENAME: true,
80
+ VIRTUALWAREHOUSECODE: true,
81
+ VIRTUALWAREHOUSENAME: true,
80
82
  });
81
83
  db.load(
82
84
  sap_warehouses === null || sap_warehouses === void 0
@@ -133,6 +135,11 @@ export const sync_warehouse = async (commandEvent) => {
133
135
  ? "van"
134
136
  : "main",
135
137
  disabled: sap_warehouse.INACTIVE == "N" ? false : true,
138
+ integration_meta: {
139
+ is_virtual_warehouse: false,
140
+ VIRTUALWAREHOUSECODE: sap_warehouse.VIRTUALWAREHOUSECODE,
141
+ VIRTUALWAREHOUSENAME: sap_warehouse.VIRTUALWAREHOUSENAME,
142
+ },
136
143
  };
137
144
  if (!repzo_warehouse) {
138
145
  // Create
@@ -152,6 +159,14 @@ export const sync_warehouse = async (commandEvent) => {
152
159
  const found_identical_docs = db.search({
153
160
  WAREHOUSECODE: repzo_warehouse.code,
154
161
  WAREHOUSENAME: repzo_warehouse.name,
162
+ VIRTUALWAREHOUSECODE:
163
+ (_j = repzo_warehouse.integration_meta) === null || _j === void 0
164
+ ? void 0
165
+ : _j.VIRTUALWAREHOUSECODE,
166
+ VIRTUALWAREHOUSENAME:
167
+ (_k = repzo_warehouse.integration_meta) === null || _k === void 0
168
+ ? void 0
169
+ : _k.VIRTUALWAREHOUSENAME,
155
170
  });
156
171
  if (found_identical_docs.length) continue; // Nothing has changed so no need for updates
157
172
  // Update
@@ -176,6 +191,136 @@ export const sync_warehouse = async (commandEvent) => {
176
191
  }
177
192
  }
178
193
  }
194
+ if (
195
+ (_m =
196
+ (_l = commandEvent.app.formData) === null || _l === void 0
197
+ ? void 0
198
+ : _l.virtualWarehouses) === null || _m === void 0
199
+ ? void 0
200
+ : _m.consider_virtual_warehouse
201
+ ) {
202
+ await commandLog
203
+ .addDetail("Repzo SAP: Started Syncing Virtual Warehouses")
204
+ .commit();
205
+ const unique_virtual_warehouses = {};
206
+ (_o =
207
+ sap_warehouses === null || sap_warehouses === void 0
208
+ ? void 0
209
+ : sap_warehouses.Warehouses) === null || _o === void 0
210
+ ? void 0
211
+ : _o.forEach((sap_warehouse) => {
212
+ if (
213
+ sap_warehouse.VIRTUALWAREHOUSECODE &&
214
+ sap_warehouse.VIRTUALWAREHOUSENAME
215
+ ) {
216
+ if (
217
+ !unique_virtual_warehouses[sap_warehouse.VIRTUALWAREHOUSECODE]
218
+ ) {
219
+ unique_virtual_warehouses[sap_warehouse.VIRTUALWAREHOUSECODE] =
220
+ {
221
+ code: sap_warehouse.VIRTUALWAREHOUSECODE,
222
+ name: sap_warehouse.VIRTUALWAREHOUSENAME,
223
+ main_warehouses_codes: [sap_warehouse.WAREHOUSECODE],
224
+ };
225
+ } else {
226
+ unique_virtual_warehouses[
227
+ sap_warehouse.VIRTUALWAREHOUSECODE
228
+ ].main_warehouses_codes.push(sap_warehouse.WAREHOUSECODE);
229
+ }
230
+ }
231
+ });
232
+ const sap_virtual_warehouses = Object.values(unique_virtual_warehouses);
233
+ await commandLog
234
+ .addDetail(
235
+ `${
236
+ sap_virtual_warehouses === null || sap_virtual_warehouses === void 0
237
+ ? void 0
238
+ : sap_virtual_warehouses.length
239
+ } virtual warehouses changed since ever`
240
+ )
241
+ .commit();
242
+ const db_2 = new DataSet([], { autoIndex: false });
243
+ db_2.createIndex({ name: true, code: true, main_warehouses_codes: true });
244
+ db_2.load(Object.values(sap_virtual_warehouses));
245
+ for (let i = 0; i < sap_virtual_warehouses.length; i++) {
246
+ const sap_warehouse = sap_virtual_warehouses[i];
247
+ const repzo_warehouse = repzo_warehouses.data.find((r_warehouse) => {
248
+ var _a, _b;
249
+ return (
250
+ ((_a = r_warehouse.integration_meta) === null || _a === void 0
251
+ ? void 0
252
+ : _a.is_virtual_warehouse) &&
253
+ (((_b = r_warehouse.integration_meta) === null || _b === void 0
254
+ ? void 0
255
+ : _b.code) == `Virtual ${sap_warehouse.code}` ||
256
+ r_warehouse.name == sap_warehouse.name)
257
+ );
258
+ });
259
+ const body = {
260
+ _id:
261
+ repzo_warehouse === null || repzo_warehouse === void 0
262
+ ? void 0
263
+ : repzo_warehouse._id,
264
+ name: sap_warehouse.name,
265
+ code: `Virtual ${sap_warehouse.code}`,
266
+ type: "main",
267
+ disabled: false,
268
+ integration_meta: {
269
+ is_virtual_warehouse: true,
270
+ main_warehouses_codes:
271
+ unique_virtual_warehouses[sap_warehouse.code]
272
+ .main_warehouses_codes,
273
+ VIRTUALWAREHOUSECODE: sap_warehouse.code,
274
+ VIRTUALWAREHOUSENAME: sap_warehouse.name,
275
+ },
276
+ };
277
+ if (!repzo_warehouse) {
278
+ // Create
279
+ try {
280
+ const created_warehouse = await repzo.warehouse.create(body);
281
+ result.created++;
282
+ } catch (e) {
283
+ // console.log("Create warehouse Failed >> ", e?.response, body);
284
+ failed_docs_report.push({
285
+ method: "create",
286
+ doc: body,
287
+ error_message: set_error(e),
288
+ });
289
+ result.failed++;
290
+ }
291
+ } else {
292
+ const found_identical_docs = db_2.search({
293
+ code: repzo_warehouse.code,
294
+ name: repzo_warehouse.name,
295
+ main_warehouses_codes:
296
+ ((_p = repzo_warehouse.integration_meta) === null || _p === void 0
297
+ ? void 0
298
+ : _p.main_warehouses_codes) || [],
299
+ });
300
+ if (found_identical_docs.length) continue; // Nothing has changed so no need for updates
301
+ // Update
302
+ try {
303
+ const updated_warehouse = await repzo.warehouse.update(
304
+ repzo_warehouse._id,
305
+ body
306
+ );
307
+ result.updated++;
308
+ } catch (e) {
309
+ // console.log("Update warehouse Failed >> ", e, body);
310
+ failed_docs_report.push({
311
+ method: "update",
312
+ doc_id:
313
+ repzo_warehouse === null || repzo_warehouse === void 0
314
+ ? void 0
315
+ : repzo_warehouse._id,
316
+ doc: body,
317
+ error_message: set_error(e),
318
+ });
319
+ result.failed++;
320
+ }
321
+ }
322
+ }
323
+ }
179
324
  // console.log(result);
180
325
  await update_bench_time(
181
326
  repzo,
@@ -195,10 +340,10 @@ export const sync_warehouse = async (commandEvent) => {
195
340
  } catch (e) {
196
341
  //@ts-ignore
197
342
  console.error(
198
- ((_j = e === null || e === void 0 ? void 0 : e.response) === null ||
199
- _j === void 0
343
+ ((_q = e === null || e === void 0 ? void 0 : e.response) === null ||
344
+ _q === void 0
200
345
  ? void 0
201
- : _j.data) || e
346
+ : _q.data) || e
202
347
  );
203
348
  await commandLog.setStatus("fail", e).commit();
204
349
  throw e === null || e === void 0 ? void 0 : e.response;
package/lib/types.d.ts CHANGED
@@ -99,6 +99,10 @@ export interface FormData {
99
99
  itemCode: string;
100
100
  uom: string;
101
101
  }[];
102
+ virtualWarehouses?: {
103
+ consider_virtual_warehouse: boolean;
104
+ absolute_qty_for_virtual_warehouses_before_accumulation: boolean;
105
+ };
102
106
  invoices?: {
103
107
  createInvoiceHook?: boolean;
104
108
  createReturnInvoiceHook?: boolean;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "repzo-sap-absjo",
3
3
  "description": "repzo SAP ABS jo integration",
4
- "version": "1.0.54",
4
+ "version": "1.0.56",
5
5
  "homepage": "",
6
6
  "keywords": [],
7
7
  "author": {
@@ -150,7 +150,7 @@ export const create_invoice = async (event: EVENT, options: Config) => {
150
150
  }
151
151
 
152
152
  // Get Repzo Rep
153
- let repzo_rep;
153
+ let repzo_rep: Service.Rep.Data | undefined;
154
154
  if (repzo_invoice.creator.type == "rep") {
155
155
  repzo_rep = await repzo.rep.get(repzo_invoice?.creator?._id);
156
156
  if (!repzo_rep)
@@ -295,6 +295,15 @@ export const create_invoice = async (event: EVENT, options: Config) => {
295
295
  });
296
296
  }
297
297
 
298
+ let WarehouseCode = repzo_warehouse.code;
299
+ if (options.data?.virtualWarehouses?.consider_virtual_warehouse) {
300
+ if (repzo_warehouse.integration_meta?.is_virtual_warehouse) {
301
+ if (repzo_rep?.integration_meta?.USERWHSCODE) {
302
+ WarehouseCode = repzo_rep.integration_meta.USERWHSCODE;
303
+ }
304
+ }
305
+ }
306
+
298
307
  const sap_invoice: SAPInvoice = {
299
308
  RepzoSerial: repzo_invoice.serial_number.formatted,
300
309
  RefNum:
@@ -314,7 +323,7 @@ export const create_invoice = async (event: EVENT, options: Config) => {
314
323
  ClientCode: repzo_client.client_code,
315
324
  DiscountPerc: "0",
316
325
  Note: repzo_invoice.comment,
317
- WarehouseCode: repzo_warehouse.code,
326
+ WarehouseCode: WarehouseCode || repzo_warehouse.code,
318
327
  LinesDetails: items,
319
328
  U_ISTDQR: repzo_invoice.ubl_qr
320
329
  ? repzo_invoice.ubl_qr
@@ -95,12 +95,18 @@ export const create_proforma = async (event: EVENT, options: Config) => {
95
95
  | Service.Warehouse.WarehouseSchema["code"]
96
96
  | undefined;
97
97
  if (repzo_rep && repzo_rep?.assigned_warehouse) {
98
- rep_warehouse_code = (
99
- repzo_rep?.assigned_warehouse as Pick<
100
- Service.Warehouse.WarehouseSchema,
101
- "_id" | "code"
102
- >
103
- )?.code;
98
+ const warehouse = repzo_rep?.assigned_warehouse as Pick<
99
+ Service.Warehouse.WarehouseSchema,
100
+ "_id" | "code" | "name" | "integration_meta"
101
+ >;
102
+ rep_warehouse_code = warehouse?.code;
103
+ if (options.data?.virtualWarehouses?.consider_virtual_warehouse) {
104
+ if (warehouse.integration_meta?.is_virtual_warehouse) {
105
+ if (repzo_rep?.integration_meta?.USERWHSCODE) {
106
+ rep_warehouse_code = repzo_rep.integration_meta.USERWHSCODE;
107
+ }
108
+ }
109
+ }
104
110
  }
105
111
 
106
112
  // Get Repzo Client
@@ -208,6 +208,15 @@ export const create_return_invoice = async (event: EVENT, options: Config) => {
208
208
  });
209
209
  }
210
210
 
211
+ let WarehouseCode = repzo_warehouse.code;
212
+ if (options.data?.virtualWarehouses?.consider_virtual_warehouse) {
213
+ if (repzo_warehouse.integration_meta?.is_virtual_warehouse) {
214
+ if (repzo_rep?.integration_meta?.USERWHSCODE) {
215
+ WarehouseCode = repzo_rep.integration_meta.USERWHSCODE;
216
+ }
217
+ }
218
+ }
219
+
211
220
  const sap_return_invoice: SAPInvoice = {
212
221
  RepzoSerial: repzo_invoice.serial_number.formatted,
213
222
  RefNum:
@@ -223,7 +232,7 @@ export const create_return_invoice = async (event: EVENT, options: Config) => {
223
232
  ClientCode: repzo_client.client_code,
224
233
  DiscountPerc: "0",
225
234
  Note: repzo_invoice.comment,
226
- WarehouseCode: repzo_warehouse.code,
235
+ WarehouseCode: WarehouseCode || repzo_warehouse.code,
227
236
  LinesDetails: return_items,
228
237
  U_ISTDQR: repzo_invoice.ubl_qr
229
238
  ? repzo_invoice.ubl_qr
@@ -160,7 +160,7 @@ export const adjust_inventory = async (commandEvent: CommandEvent) => {
160
160
  throw `MeasureUnit with UNITNAME: ${sap_item.UNITNAME} & ALTUOMID: ${sap_item.UNITID} was not found`;
161
161
  }
162
162
 
163
- const qty = measureUnit.factor * sap_item.QTY;
163
+ const qty = sap_item.QTY; //* measureUnit.factor *;
164
164
 
165
165
  const match_item_in_repzo_inventory =
166
166
  repzo_inventory?.data?.find(
@@ -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 = sap_item.QTY; //* measureUnit.factor
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(