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