repzo-sap-absjo 1.0.20 → 1.0.22

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 (90) hide show
  1. package/changelog.md +1 -0
  2. package/lib/actions/create_invoice.d.ts +19 -13
  3. package/lib/actions/create_invoice.js +359 -210
  4. package/lib/actions/create_payment.d.ts +4 -1
  5. package/lib/actions/create_payment.js +195 -107
  6. package/lib/actions/create_proforma.d.ts +4 -1
  7. package/lib/actions/create_proforma.js +288 -147
  8. package/lib/actions/create_return_invoice.d.ts +4 -1
  9. package/lib/actions/create_return_invoice.js +312 -158
  10. package/lib/actions/create_transfer.d.ts +4 -1
  11. package/lib/actions/create_transfer.js +223 -126
  12. package/lib/actions/index.js +23 -15
  13. package/lib/commands/adjust_inventory.d.ts +4 -2
  14. package/lib/commands/adjust_inventory.js +290 -160
  15. package/lib/commands/bank.d.ts +4 -2
  16. package/lib/commands/bank.js +279 -157
  17. package/lib/commands/basic.js +68 -48
  18. package/lib/commands/brand.d.ts +3 -1
  19. package/lib/commands/brand.js +174 -100
  20. package/lib/commands/category.d.ts +3 -1
  21. package/lib/commands/category.js +178 -104
  22. package/lib/commands/channel.d.ts +3 -1
  23. package/lib/commands/channel.js +198 -117
  24. package/lib/commands/client.d.ts +34 -32
  25. package/lib/commands/client.js +404 -244
  26. package/lib/commands/client_disabled.d.ts +3 -1
  27. package/lib/commands/client_disabled.js +163 -94
  28. package/lib/commands/index.d.ts +24 -11
  29. package/lib/commands/index.js +65 -62
  30. package/lib/commands/join.js +192 -66
  31. package/lib/commands/measureunit.d.ts +19 -14
  32. package/lib/commands/measureunit.js +289 -191
  33. package/lib/commands/measureunit_family.d.ts +10 -8
  34. package/lib/commands/measureunit_family.js +250 -138
  35. package/lib/commands/payment_term.d.ts +3 -1
  36. package/lib/commands/payment_term.js +202 -123
  37. package/lib/commands/price_list.d.ts +15 -15
  38. package/lib/commands/price_list.js +617 -326
  39. package/lib/commands/price_list_disabled.d.ts +12 -0
  40. package/lib/commands/price_list_disabled.js +216 -0
  41. package/lib/commands/product.d.ts +32 -30
  42. package/lib/commands/product.js +485 -263
  43. package/lib/commands/product_disabled.d.ts +3 -1
  44. package/lib/commands/product_disabled.js +160 -94
  45. package/lib/commands/rep.js +221 -141
  46. package/lib/commands/tag.js +174 -109
  47. package/lib/commands/tax.js +177 -112
  48. package/lib/commands/warehouse.d.ts +3 -1
  49. package/lib/commands/warehouse.js +207 -119
  50. package/lib/index.d.ts +30 -12
  51. package/lib/test/actions/create_invoice.js +193 -188
  52. package/lib/test/actions/create_payment.js +112 -107
  53. package/lib/test/actions/create_proforma.js +220 -216
  54. package/lib/test/actions/create_return_invoice.js +205 -200
  55. package/lib/test/actions/create_transfer.js +120 -115
  56. package/lib/test/commands/adjust_inventory.js +90 -90
  57. package/lib/test/commands/bank.js +90 -90
  58. package/lib/test/commands/basic.js +335 -327
  59. package/lib/test/commands/brand.js +90 -90
  60. package/lib/test/commands/category.js +90 -90
  61. package/lib/test/commands/channel.js +90 -90
  62. package/lib/test/commands/client.js +355 -353
  63. package/lib/test/commands/client_disabled.js +90 -90
  64. package/lib/test/commands/join.js +328 -327
  65. package/lib/test/commands/measureunit.js +90 -90
  66. package/lib/test/commands/measureunit_family.js +90 -90
  67. package/lib/test/commands/payment_term.js +90 -90
  68. package/lib/test/commands/price_list.js +337 -334
  69. package/lib/test/commands/price_list_disabled.d.ts +1 -0
  70. package/lib/test/commands/price_list_disabled.js +359 -0
  71. package/lib/test/commands/product.js +90 -90
  72. package/lib/test/commands/product_disabled.js +90 -90
  73. package/lib/test/commands/rep.js +346 -344
  74. package/lib/test/commands/tag.js +90 -90
  75. package/lib/test/commands/tax.js +90 -90
  76. package/lib/test/commands/warehouse.js +90 -90
  77. package/lib/types.d.ts +78 -53
  78. package/lib/util.d.ts +76 -19
  79. package/lib/util.js +179 -144
  80. package/package.json +1 -1
  81. package/src/actions/create_invoice.ts +7 -18
  82. package/src/actions/create_proforma.ts +11 -1
  83. package/src/commands/basic.ts +1 -0
  84. package/src/commands/index.ts +3 -0
  85. package/src/commands/price_list.ts +7 -2
  86. package/src/commands/price_list_disabled.ts +194 -0
  87. package/src/test/commands/basic.ts +7 -0
  88. package/src/test/commands/price_list_disabled.ts +362 -0
  89. package/src/types.ts +1 -0
  90. package/src/util.ts +18 -1
package/changelog.md CHANGED
@@ -7,6 +7,7 @@
7
7
  - [actions/***] add 2 keys in integration_meta: sync_to_sap_started & sync_to_sap_succeeded @maramalshen
8
8
  - [commands/product] assign Foreign Name (ITEMDESCF) in SAP to local_name in Repzo @maramalshen
9
9
  - [actions/payment] add the field UserId where it equals USERID of the SAP API REP @maramalshen
10
+ - [commands/price_list_disabled] new command: Disabled PriceListItems from Repzo if it is not found on SAP @maramalshen
10
11
 
11
12
  ### Changed
12
13
 
@@ -1,20 +1,26 @@
1
1
  import { EVENT, Config } from "../types";
2
2
  interface SAPOpenInvoice {
3
- CustomerNumber: string;
4
- FatherCode: string;
5
- DocDate: string;
6
- DocDueDate: string;
7
- InvoiceID: number;
8
- InvoiceNumber: string;
9
- InvoiceClientID: string;
10
- InvoiceFinalAmount: number;
11
- InvoiceRemainingAmount: number;
12
- InvoiceStatus: string;
3
+ CustomerNumber: string;
4
+ FatherCode: string;
5
+ DocDate: string;
6
+ DocDueDate: string;
7
+ InvoiceID: number;
8
+ InvoiceNumber: string;
9
+ InvoiceClientID: string;
10
+ InvoiceFinalAmount: number;
11
+ InvoiceRemainingAmount: number;
12
+ InvoiceStatus: string;
13
13
  }
14
- export declare const create_invoice: (event: EVENT, options: Config) => Promise<any>;
15
- export declare const get_invoice_from_sap: (serviceEndPoint: string, query?: {
14
+ export declare const create_invoice: (
15
+ event: EVENT,
16
+ options: Config
17
+ ) => Promise<any>;
18
+ export declare const get_invoice_from_sap: (
19
+ serviceEndPoint: string,
20
+ query?: {
16
21
  updatedAt: string;
17
22
  Status: string;
18
23
  InvoiceId: string;
19
- }) => Promise<SAPOpenInvoice[]>;
24
+ }
25
+ ) => Promise<SAPOpenInvoice[]>;
20
26
  export {};
@@ -1,222 +1,371 @@
1
1
  import Repzo from "repzo";
2
- import { _create } from "../util.js";
2
+ import { _create, getUniqueConcatenatedValues } from "../util.js";
3
3
  import { v4 as uuid } from "uuid";
4
4
  import moment from "moment-timezone";
5
5
  export const create_invoice = async (event, options) => {
6
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
7
- const repzo = new Repzo((_a = options.data) === null || _a === void 0 ? void 0 : _a.repzoApiKey, { env: options.env });
8
- const action_sync_id = ((_b = event === null || event === void 0 ? void 0 : event.headers) === null || _b === void 0 ? void 0 : _b.action_sync_id) || uuid();
9
- const actionLog = new Repzo.ActionLogs(repzo, action_sync_id);
10
- let body;
6
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
7
+ const repzo = new Repzo(
8
+ (_a = options.data) === null || _a === void 0 ? void 0 : _a.repzoApiKey,
9
+ { env: options.env }
10
+ );
11
+ const action_sync_id =
12
+ ((_b = event === null || event === void 0 ? void 0 : event.headers) ===
13
+ null || _b === void 0
14
+ ? void 0
15
+ : _b.action_sync_id) || uuid();
16
+ const actionLog = new Repzo.ActionLogs(repzo, action_sync_id);
17
+ let body;
18
+ try {
19
+ // console.log("create_invoice");
20
+ await actionLog.load(action_sync_id);
21
+ body = event.body;
11
22
  try {
12
- // console.log("create_invoice");
13
- await actionLog.load(action_sync_id);
14
- body = event.body;
15
- try {
16
- if (body)
17
- body = JSON.parse(body);
18
- }
19
- catch (e) { }
20
- const repzo_serial_number = (_c = body === null || body === void 0 ? void 0 : body.serial_number) === null || _c === void 0 ? void 0 : _c.formatted;
21
- try {
22
- await repzo.updateIntegrationMeta.create([
23
- { key: "sync_to_sap_started", value: true },
24
- { key: "sync_to_sap_succeeded", value: false },
25
- ], { _id: body._id, type: "fullinvoices" });
26
- }
27
- catch (e) {
28
- console.error(e);
29
- }
30
- await actionLog
31
- .addDetail(`Invoice - ${repzo_serial_number} => ${body === null || body === void 0 ? void 0 : body.sync_id}`)
32
- .addDetail(`Repzo => SAP: Started Create Invoice - ${repzo_serial_number}`)
33
- .commit();
34
- const SAP_HOST_URL = (_d = options.data) === null || _d === void 0 ? void 0 : _d.sapHostUrl;
35
- if (!SAP_HOST_URL)
36
- throw `SAP Host Url is missing and Required: ${SAP_HOST_URL}`;
37
- const repzo_invoice = body;
38
- // Check if it is already exist in SAP
39
- const sap_open_invoices = await get_invoice_from_sap(SAP_HOST_URL, {
40
- updatedAt: "",
41
- Status: "",
42
- InvoiceId: repzo_serial_number,
43
- });
44
- const open_invoice = sap_open_invoices === null || sap_open_invoices === void 0 ? void 0 : sap_open_invoices.find((inv) => (inv === null || inv === void 0 ? void 0 : inv.InvoiceNumber) === repzo_serial_number);
45
- if (open_invoice) {
46
- await actionLog
47
- .addDetail(`Checked Already in SAP `, open_invoice)
48
- .addDetail(`Invoice - ${repzo_serial_number} Checked Already in SAP`)
49
- .setStatus("success")
50
- .setBody(repzo_invoice)
51
- .commit();
52
- return {
53
- message: "Checked Already in SAP",
54
- result: open_invoice,
55
- };
56
- }
57
- // Check closed invoice in SAP
58
- const sap_closed_invoices = await get_invoice_from_sap(SAP_HOST_URL, {
59
- updatedAt: "",
60
- Status: "closed",
61
- InvoiceId: repzo_serial_number,
62
- });
63
- const closed_invoice = sap_closed_invoices === null || sap_closed_invoices === void 0 ? void 0 : sap_closed_invoices.find((inv) => (inv === null || inv === void 0 ? void 0 : inv.InvoiceNumber) === repzo_serial_number);
64
- if (closed_invoice) {
65
- await actionLog
66
- .addDetail(`Checked Closed Already in SAP `, closed_invoice)
67
- .addDetail(`Invoice - ${repzo_serial_number} Checked Closed Already in SAP`)
68
- .setStatus("success")
69
- .setBody(repzo_invoice)
70
- .commit();
71
- return {
72
- message: "Checked Closed Already in SAP",
73
- result: closed_invoice,
74
- };
75
- }
76
- // Get Repzo Rep
77
- let repzo_rep;
78
- if (repzo_invoice.creator.type == "rep") {
79
- repzo_rep = await repzo.rep.get((_e = repzo_invoice === null || repzo_invoice === void 0 ? void 0 : repzo_invoice.creator) === null || _e === void 0 ? void 0 : _e._id);
80
- if (!repzo_rep)
81
- throw `Rep with _id: ${repzo_invoice.creator._id} not found in Repzo`;
82
- }
83
- // Get Repzo Client
84
- const repzo_client = await repzo.client.get(repzo_invoice === null || repzo_invoice === void 0 ? void 0 : repzo_invoice.client_id);
85
- if (!repzo_client)
86
- throw `Client with _id: ${repzo_invoice.client_id} not found in Repzo`;
87
- // Get Repzo Warehouse
88
- const repzo_warehouse = await repzo.warehouse.get(repzo_invoice.origin_warehouse);
89
- if (!repzo_warehouse)
90
- throw `warehouse with _id: ${repzo_invoice.origin_warehouse} not found in Repzo`;
91
- const repzo_tax_ids = {};
92
- const repzo_measureunit_ids = {};
93
- const repzo_product_ids = {};
94
- (_f = repzo_invoice.items) === null || _f === void 0 ? void 0 : _f.forEach((item) => {
95
- var _a, _b, _c;
96
- if (item) {
97
- repzo_tax_ids[(_a = item.tax) === null || _a === void 0 ? void 0 : _a._id] = true;
98
- repzo_measureunit_ids[(_b = item.measureunit) === null || _b === void 0 ? void 0 : _b._id] = true;
99
- repzo_product_ids[(_c = item.variant) === null || _c === void 0 ? void 0 : _c.product_id] = true;
100
- }
23
+ if (body) body = JSON.parse(body);
24
+ } catch (e) {}
25
+ const repzo_serial_number =
26
+ (_c = body === null || body === void 0 ? void 0 : body.serial_number) ===
27
+ null || _c === void 0
28
+ ? void 0
29
+ : _c.formatted;
30
+ try {
31
+ await repzo.updateIntegrationMeta.create(
32
+ [
33
+ { key: "sync_to_sap_started", value: true },
34
+ { key: "sync_to_sap_succeeded", value: false },
35
+ ],
36
+ { _id: body._id, type: "fullinvoices" }
37
+ );
38
+ } catch (e) {
39
+ console.error(e);
40
+ }
41
+ await actionLog
42
+ .addDetail(
43
+ `Invoice - ${repzo_serial_number} => ${
44
+ body === null || body === void 0 ? void 0 : body.sync_id
45
+ }`
46
+ )
47
+ .addDetail(
48
+ `Repzo => SAP: Started Create Invoice - ${repzo_serial_number}`
49
+ )
50
+ .commit();
51
+ const SAP_HOST_URL =
52
+ (_d = options.data) === null || _d === void 0 ? void 0 : _d.sapHostUrl;
53
+ if (!SAP_HOST_URL)
54
+ throw `SAP Host Url is missing and Required: ${SAP_HOST_URL}`;
55
+ const repzo_invoice = body;
56
+ // Check if it is already exist in SAP
57
+ const sap_open_invoices = await get_invoice_from_sap(SAP_HOST_URL, {
58
+ updatedAt: "",
59
+ Status: "",
60
+ InvoiceId: repzo_serial_number,
61
+ });
62
+ const open_invoice =
63
+ sap_open_invoices === null || sap_open_invoices === void 0
64
+ ? void 0
65
+ : sap_open_invoices.find(
66
+ (inv) =>
67
+ (inv === null || inv === void 0 ? void 0 : inv.InvoiceNumber) ===
68
+ repzo_serial_number
69
+ );
70
+ if (open_invoice) {
71
+ await actionLog
72
+ .addDetail(`Checked Already in SAP `, open_invoice)
73
+ .addDetail(`Invoice - ${repzo_serial_number} Checked Already in SAP`)
74
+ .setStatus("success")
75
+ .setBody(repzo_invoice)
76
+ .commit();
77
+ return {
78
+ message: "Checked Already in SAP",
79
+ result: open_invoice,
80
+ };
81
+ }
82
+ // Check closed invoice in SAP
83
+ const sap_closed_invoices = await get_invoice_from_sap(SAP_HOST_URL, {
84
+ updatedAt: "",
85
+ Status: "closed",
86
+ InvoiceId: repzo_serial_number,
87
+ });
88
+ const closed_invoice =
89
+ sap_closed_invoices === null || sap_closed_invoices === void 0
90
+ ? void 0
91
+ : sap_closed_invoices.find(
92
+ (inv) =>
93
+ (inv === null || inv === void 0 ? void 0 : inv.InvoiceNumber) ===
94
+ repzo_serial_number
95
+ );
96
+ if (closed_invoice) {
97
+ await actionLog
98
+ .addDetail(`Checked Closed Already in SAP `, closed_invoice)
99
+ .addDetail(
100
+ `Invoice - ${repzo_serial_number} Checked Closed Already in SAP`
101
+ )
102
+ .setStatus("success")
103
+ .setBody(repzo_invoice)
104
+ .commit();
105
+ return {
106
+ message: "Checked Closed Already in SAP",
107
+ result: closed_invoice,
108
+ };
109
+ }
110
+ // Get Repzo Rep
111
+ let repzo_rep;
112
+ if (repzo_invoice.creator.type == "rep") {
113
+ repzo_rep = await repzo.rep.get(
114
+ (_e =
115
+ repzo_invoice === null || repzo_invoice === void 0
116
+ ? void 0
117
+ : repzo_invoice.creator) === null || _e === void 0
118
+ ? void 0
119
+ : _e._id
120
+ );
121
+ if (!repzo_rep)
122
+ throw `Rep with _id: ${repzo_invoice.creator._id} not found in Repzo`;
123
+ }
124
+ // Get Repzo Client
125
+ const repzo_client = await repzo.client.get(
126
+ repzo_invoice === null || repzo_invoice === void 0
127
+ ? void 0
128
+ : repzo_invoice.client_id
129
+ );
130
+ if (!repzo_client)
131
+ throw `Client with _id: ${repzo_invoice.client_id} not found in Repzo`;
132
+ // Get Repzo Warehouse
133
+ const repzo_warehouse = await repzo.warehouse.get(
134
+ repzo_invoice.origin_warehouse
135
+ );
136
+ if (!repzo_warehouse)
137
+ throw `warehouse with _id: ${repzo_invoice.origin_warehouse} not found in Repzo`;
138
+ const repzo_tax_ids = {};
139
+ const repzo_measureunit_ids = {};
140
+ const repzo_product_ids = {};
141
+ (_f = repzo_invoice.items) === null || _f === void 0
142
+ ? void 0
143
+ : _f.forEach((item) => {
144
+ var _a, _b, _c;
145
+ if (item) {
146
+ repzo_tax_ids[
147
+ (_a = item.tax) === null || _a === void 0 ? void 0 : _a._id
148
+ ] = true;
149
+ repzo_measureunit_ids[
150
+ (_b = item.measureunit) === null || _b === void 0
151
+ ? void 0
152
+ : _b._id
153
+ ] = true;
154
+ repzo_product_ids[
155
+ (_c = item.variant) === null || _c === void 0
156
+ ? void 0
157
+ : _c.product_id
158
+ ] = true;
159
+ }
101
160
  });
102
- const repzo_taxes = await repzo.patchAction.create({
103
- slug: "tax",
104
- readQuery: [
105
- {
106
- key: "_id",
107
- value: Object.keys(repzo_tax_ids),
108
- operator: "in",
109
- },
110
- ],
111
- }, { per_page: 50000 });
112
- const repzo_measureunits = await repzo.patchAction.create({
113
- slug: "measureunits",
114
- readQuery: [
115
- {
116
- key: "_id",
117
- value: Object.keys(repzo_measureunit_ids),
118
- operator: "in",
119
- },
120
- ],
121
- }, { per_page: 50000 });
122
- const repzo_products = await repzo.patchAction.create({
123
- slug: "product",
124
- readQuery: [
125
- {
126
- key: "_id",
127
- value: Object.keys(repzo_product_ids),
128
- operator: "in",
129
- },
130
- ],
131
- }, { per_page: 50000 });
132
- // Prepare SAP_invoice_items
133
- const items = [];
134
- for (let i = 0; i < ((_g = repzo_invoice === null || repzo_invoice === void 0 ? void 0 : repzo_invoice.items) === null || _g === void 0 ? void 0 : _g.length); i++) {
135
- const item = repzo_invoice.items[i];
136
- // Get Repzo Tax
137
- const repzo_tax = (_h = repzo_taxes === null || repzo_taxes === void 0 ? void 0 : repzo_taxes.data) === null || _h === void 0 ? void 0 : _h.find((t) => { var _a, _b, _c; return ((_a = t._id) === null || _a === void 0 ? void 0 : _a.toString()) == ((_c = (_b = item.tax) === null || _b === void 0 ? void 0 : _b._id) === null || _c === void 0 ? void 0 : _c.toString()); });
138
- if (!repzo_tax)
139
- throw `Tax with _id: ${item.tax._id} not found in Repzo`;
140
- // Get Repzo UoM
141
- const repzo_measureunit = (_j = repzo_measureunits === null || repzo_measureunits === void 0 ? void 0 : repzo_measureunits.data) === null || _j === void 0 ? void 0 : _j.find((m) => { var _a, _b, _c; return ((_a = m._id) === null || _a === void 0 ? void 0 : _a.toString()) == ((_c = (_b = item.measureunit) === null || _b === void 0 ? void 0 : _b._id) === null || _c === void 0 ? void 0 : _c.toString()); });
142
- if (!repzo_measureunit)
143
- throw `Uom with _id: ${(_k = item.measureunit) === null || _k === void 0 ? void 0 : _k._id} not found in Repzo`;
144
- // Get Repzo Product
145
- const repzo_product = (_l = repzo_products === null || repzo_products === void 0 ? void 0 : repzo_products.data) === null || _l === void 0 ? void 0 : _l.find((p) => { var _a, _b, _c; return ((_a = p._id) === null || _a === void 0 ? void 0 : _a.toString()) == ((_c = (_b = item.variant) === null || _b === void 0 ? void 0 : _b.product_id) === null || _c === void 0 ? void 0 : _c.toString()); });
146
- if (!repzo_product)
147
- throw `Product with _id: ${item.measureunit._id} not found in Repzo`;
148
- items.push({
149
- MEO_Serial: getUniqueConcatenatedValues(item, "ref", " | "),
150
- Promotion_Name: getUniqueConcatenatedValues(item, "name", " | "),
151
- ItemCode: item.variant.variant_name,
152
- Quantity: item.qty,
153
- TaxCode: repzo_tax.integration_meta.TaxCode,
154
- UnitPrice: (item.price * repzo_measureunit.factor) / 1000,
155
- DiscountPerc: "0",
156
- //@ts-ignore
157
- LineTotal: item.total_before_tax / 1000,
158
- UomCode: repzo_measureunit.integration_meta.ALTUOMID,
159
- Brand: (_m = repzo_product.integration_meta) === null || _m === void 0 ? void 0 : _m.BRAND,
160
- Department: ((_o = repzo_rep === null || repzo_rep === void 0 ? void 0 : repzo_rep.integration_meta) === null || _o === void 0 ? void 0 : _o.DEPARTMENTCODE) ||
161
- ((_p = options.data) === null || _p === void 0 ? void 0 : _p.DepartmentCode), // "D2",
161
+ const repzo_taxes = await repzo.patchAction.create(
162
+ {
163
+ slug: "tax",
164
+ readQuery: [
165
+ {
166
+ key: "_id",
167
+ value: Object.keys(repzo_tax_ids),
168
+ operator: "in",
169
+ },
170
+ ],
171
+ },
172
+ { per_page: 50000 }
173
+ );
174
+ const repzo_measureunits = await repzo.patchAction.create(
175
+ {
176
+ slug: "measureunits",
177
+ readQuery: [
178
+ {
179
+ key: "_id",
180
+ value: Object.keys(repzo_measureunit_ids),
181
+ operator: "in",
182
+ },
183
+ ],
184
+ },
185
+ { per_page: 50000 }
186
+ );
187
+ const repzo_products = await repzo.patchAction.create(
188
+ {
189
+ slug: "product",
190
+ readQuery: [
191
+ {
192
+ key: "_id",
193
+ value: Object.keys(repzo_product_ids),
194
+ operator: "in",
195
+ },
196
+ ],
197
+ },
198
+ { per_page: 50000 }
199
+ );
200
+ // Prepare SAP_invoice_items
201
+ const items = [];
202
+ for (
203
+ let i = 0;
204
+ i <
205
+ ((_g =
206
+ repzo_invoice === null || repzo_invoice === void 0
207
+ ? void 0
208
+ : repzo_invoice.items) === null || _g === void 0
209
+ ? void 0
210
+ : _g.length);
211
+ i++
212
+ ) {
213
+ const item = repzo_invoice.items[i];
214
+ // Get Repzo Tax
215
+ const repzo_tax =
216
+ (_h =
217
+ repzo_taxes === null || repzo_taxes === void 0
218
+ ? void 0
219
+ : repzo_taxes.data) === null || _h === void 0
220
+ ? void 0
221
+ : _h.find((t) => {
222
+ var _a, _b, _c;
223
+ return (
224
+ ((_a = t._id) === null || _a === void 0
225
+ ? void 0
226
+ : _a.toString()) ==
227
+ ((_c =
228
+ (_b = item.tax) === null || _b === void 0
229
+ ? void 0
230
+ : _b._id) === null || _c === void 0
231
+ ? void 0
232
+ : _c.toString())
233
+ );
162
234
  });
163
- }
164
- const sap_invoice = {
165
- RefNum: repzo_invoice.serial_number.formatted,
166
- SalPersCode: repzo_rep === null || repzo_rep === void 0 ? void 0 : repzo_rep.integration_id,
167
- DocDate: moment(repzo_invoice.issue_date, "YYYY-MM-DD").format("YYYYMMDD"),
168
- DocDueDate: moment(repzo_invoice.due_date, "YYYY-MM-DD").format("YYYYMMDD"),
169
- ClientCode: repzo_client.client_code,
170
- DiscountPerc: "0",
171
- Note: repzo_invoice.comment,
172
- WarehouseCode: repzo_warehouse.code,
173
- LinesDetails: items,
174
- };
175
- // console.dir(sap_invoice, { depth: null });
176
- actionLog.addDetail(`Repzo => SAP: Invoice - ${repzo_serial_number}`, sap_invoice);
177
- const result = await _create(SAP_HOST_URL, "/AddInvoice", sap_invoice);
178
- // console.log(result);
179
- try {
180
- await repzo.updateIntegrationMeta.create([{ key: "sync_to_sap_succeeded", value: true }], { _id: body._id, type: "fullinvoices" });
181
- }
182
- catch (e) {
183
- console.error(e);
184
- }
185
- await actionLog
186
- .addDetail(`SAP Responded with `, result)
187
- .addDetail(`Repzo => SAP: Invoice - ${repzo_serial_number}`)
188
- .setStatus("success")
189
- .setBody(repzo_invoice)
190
- .commit();
191
- return result;
192
- }
193
- catch (e) {
235
+ if (!repzo_tax) throw `Tax with _id: ${item.tax._id} not found in Repzo`;
236
+ // Get Repzo UoM
237
+ const repzo_measureunit =
238
+ (_j =
239
+ repzo_measureunits === null || repzo_measureunits === void 0
240
+ ? void 0
241
+ : repzo_measureunits.data) === null || _j === void 0
242
+ ? void 0
243
+ : _j.find((m) => {
244
+ var _a, _b, _c;
245
+ return (
246
+ ((_a = m._id) === null || _a === void 0
247
+ ? void 0
248
+ : _a.toString()) ==
249
+ ((_c =
250
+ (_b = item.measureunit) === null || _b === void 0
251
+ ? void 0
252
+ : _b._id) === null || _c === void 0
253
+ ? void 0
254
+ : _c.toString())
255
+ );
256
+ });
257
+ if (!repzo_measureunit)
258
+ throw `Uom with _id: ${
259
+ (_k = item.measureunit) === null || _k === void 0 ? void 0 : _k._id
260
+ } not found in Repzo`;
261
+ // Get Repzo Product
262
+ const repzo_product =
263
+ (_l =
264
+ repzo_products === null || repzo_products === void 0
265
+ ? void 0
266
+ : repzo_products.data) === null || _l === void 0
267
+ ? void 0
268
+ : _l.find((p) => {
269
+ var _a, _b, _c;
270
+ return (
271
+ ((_a = p._id) === null || _a === void 0
272
+ ? void 0
273
+ : _a.toString()) ==
274
+ ((_c =
275
+ (_b = item.variant) === null || _b === void 0
276
+ ? void 0
277
+ : _b.product_id) === null || _c === void 0
278
+ ? void 0
279
+ : _c.toString())
280
+ );
281
+ });
282
+ if (!repzo_product)
283
+ throw `Product with _id: ${item.measureunit._id} not found in Repzo`;
284
+ items.push({
285
+ MEO_Serial: getUniqueConcatenatedValues(item, "ref", " | "),
286
+ Promotion_Name: getUniqueConcatenatedValues(item, "name", " | "),
287
+ ItemCode: item.variant.variant_name,
288
+ Quantity: item.qty,
289
+ TaxCode: repzo_tax.integration_meta.TaxCode,
290
+ UnitPrice: (item.price * repzo_measureunit.factor) / 1000,
291
+ DiscountPerc: "0", // ??
194
292
  //@ts-ignore
195
- console.error((e === null || e === void 0 ? void 0 : e.response) || e);
196
- await actionLog.setStatus("fail", e).setBody(body).commit();
197
- throw e;
293
+ LineTotal: item.total_before_tax / 1000,
294
+ UomCode: repzo_measureunit.integration_meta.ALTUOMID,
295
+ Brand:
296
+ (_m = repzo_product.integration_meta) === null || _m === void 0
297
+ ? void 0
298
+ : _m.BRAND, // "B1", // ??
299
+ Department:
300
+ ((_o =
301
+ repzo_rep === null || repzo_rep === void 0
302
+ ? void 0
303
+ : repzo_rep.integration_meta) === null || _o === void 0
304
+ ? void 0
305
+ : _o.DEPARTMENTCODE) ||
306
+ ((_p = options.data) === null || _p === void 0
307
+ ? void 0
308
+ : _p.DepartmentCode), // "D2",
309
+ });
198
310
  }
199
- };
200
- export const get_invoice_from_sap = async (serviceEndPoint, query) => {
311
+ const sap_invoice = {
312
+ RefNum: repzo_invoice.serial_number.formatted,
313
+ SalPersCode:
314
+ repzo_rep === null || repzo_rep === void 0
315
+ ? void 0
316
+ : repzo_rep.integration_id,
317
+ DocDate: moment(repzo_invoice.issue_date, "YYYY-MM-DD").format(
318
+ "YYYYMMDD"
319
+ ),
320
+ DocDueDate: moment(repzo_invoice.due_date, "YYYY-MM-DD").format(
321
+ "YYYYMMDD"
322
+ ),
323
+ ClientCode: repzo_client.client_code,
324
+ DiscountPerc: "0",
325
+ Note: repzo_invoice.comment,
326
+ WarehouseCode: repzo_warehouse.code,
327
+ LinesDetails: items,
328
+ };
329
+ // console.dir(sap_invoice, { depth: null });
330
+ actionLog.addDetail(
331
+ `Repzo => SAP: Invoice - ${repzo_serial_number}`,
332
+ sap_invoice
333
+ );
334
+ const result = await _create(SAP_HOST_URL, "/AddInvoice", sap_invoice);
335
+ // console.log(result);
201
336
  try {
202
- const sap_openInvoices = await _create(serviceEndPoint, "/OpenInvoices", {
203
- updatedAt: query === null || query === void 0 ? void 0 : query.updatedAt,
204
- Status: query === null || query === void 0 ? void 0 : query.Status,
205
- InvoiceId: query === null || query === void 0 ? void 0 : query.InvoiceId,
206
- });
207
- return sap_openInvoices === null || sap_openInvoices === void 0 ? void 0 : sap_openInvoices.OpenInvoices;
208
- }
209
- catch (e) {
210
- throw e;
337
+ await repzo.updateIntegrationMeta.create(
338
+ [{ key: "sync_to_sap_succeeded", value: true }],
339
+ { _id: body._id, type: "fullinvoices" }
340
+ );
341
+ } catch (e) {
342
+ console.error(e);
211
343
  }
344
+ await actionLog
345
+ .addDetail(`SAP Responded with `, result)
346
+ .addDetail(`Repzo => SAP: Invoice - ${repzo_serial_number}`)
347
+ .setStatus("success")
348
+ .setBody(repzo_invoice)
349
+ .commit();
350
+ return result;
351
+ } catch (e) {
352
+ //@ts-ignore
353
+ console.error((e === null || e === void 0 ? void 0 : e.response) || e);
354
+ await actionLog.setStatus("fail", e).setBody(body).commit();
355
+ throw e;
356
+ }
357
+ };
358
+ export const get_invoice_from_sap = async (serviceEndPoint, query) => {
359
+ try {
360
+ const sap_openInvoices = await _create(serviceEndPoint, "/OpenInvoices", {
361
+ updatedAt: query === null || query === void 0 ? void 0 : query.updatedAt,
362
+ Status: query === null || query === void 0 ? void 0 : query.Status,
363
+ InvoiceId: query === null || query === void 0 ? void 0 : query.InvoiceId,
364
+ });
365
+ return sap_openInvoices === null || sap_openInvoices === void 0
366
+ ? void 0
367
+ : sap_openInvoices.OpenInvoices;
368
+ } catch (e) {
369
+ throw e;
370
+ }
212
371
  };
213
- function getUniqueConcatenatedValues(item, key, delimiter) {
214
- item.general_promotions = item.general_promotions || [];
215
- item.used_promotions = item.used_promotions || [];
216
- const allPromotions = [
217
- ...item.general_promotions,
218
- ...item.used_promotions,
219
- ];
220
- const uniqueValues = new Set(allPromotions.map((promotion) => promotion[key]).filter((value) => value));
221
- return [...uniqueValues].join(delimiter);
222
- }
@@ -1,2 +1,5 @@
1
1
  import { EVENT, Config } from "../types";
2
- export declare const create_payment: (event: EVENT, options: Config) => Promise<any>;
2
+ export declare const create_payment: (
3
+ event: EVENT,
4
+ options: Config
5
+ ) => Promise<any>;