repzo-sap-absjo 1.0.1
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/README.md +3 -0
- package/changelog.md +17 -0
- package/lib/actions/create_invoice.d.ts +26 -0
- package/lib/actions/create_invoice.js +311 -0
- package/lib/actions/create_payment.d.ts +5 -0
- package/lib/actions/create_payment.js +177 -0
- package/lib/actions/create_proforma.d.ts +5 -0
- package/lib/actions/create_proforma.js +241 -0
- package/lib/actions/create_return_invoice.d.ts +5 -0
- package/lib/actions/create_return_invoice.js +268 -0
- package/lib/actions/create_transfer.d.ts +5 -0
- package/lib/actions/create_transfer.js +171 -0
- package/lib/actions/index.d.ts +3 -0
- package/lib/actions/index.js +31 -0
- package/lib/commands/adjust_inventory.d.ts +6 -0
- package/lib/commands/adjust_inventory.js +303 -0
- package/lib/commands/bank.d.ts +7 -0
- package/lib/commands/bank.js +301 -0
- package/lib/commands/basic.d.ts +2 -0
- package/lib/commands/basic.js +73 -0
- package/lib/commands/category.d.ts +4 -0
- package/lib/commands/category.js +193 -0
- package/lib/commands/channel.d.ts +4 -0
- package/lib/commands/channel.js +213 -0
- package/lib/commands/client.d.ts +39 -0
- package/lib/commands/client.js +410 -0
- package/lib/commands/client_disabled.d.ts +4 -0
- package/lib/commands/client_disabled.js +183 -0
- package/lib/commands/index.d.ts +23 -0
- package/lib/commands/index.js +82 -0
- package/lib/commands/join.d.ts +2 -0
- package/lib/commands/join.js +134 -0
- package/lib/commands/measureunit.d.ts +23 -0
- package/lib/commands/measureunit.js +314 -0
- package/lib/commands/measureunit_family.d.ts +12 -0
- package/lib/commands/measureunit_family.js +263 -0
- package/lib/commands/payment_term.d.ts +4 -0
- package/lib/commands/payment_term.js +217 -0
- package/lib/commands/price_list.d.ts +18 -0
- package/lib/commands/price_list.js +608 -0
- package/lib/commands/product.d.ts +39 -0
- package/lib/commands/product.js +454 -0
- package/lib/commands/product_disabled.d.ts +4 -0
- package/lib/commands/product_disabled.js +178 -0
- package/lib/commands/rep.d.ts +2 -0
- package/lib/commands/rep.js +246 -0
- package/lib/commands/tag.d.ts +2 -0
- package/lib/commands/tag.js +195 -0
- package/lib/commands/tax.d.ts +2 -0
- package/lib/commands/tax.js +198 -0
- package/lib/commands/warehouse.d.ts +4 -0
- package/lib/commands/warehouse.js +221 -0
- package/lib/index.d.ts +29 -0
- package/lib/index.js +6 -0
- package/lib/test/actions/create_invoice.d.ts +1 -0
- package/lib/test/actions/create_invoice.js +214 -0
- package/lib/test/actions/create_payment.d.ts +1 -0
- package/lib/test/actions/create_payment.js +131 -0
- package/lib/test/actions/create_proforma.d.ts +1 -0
- package/lib/test/actions/create_proforma.js +242 -0
- package/lib/test/actions/create_return_invoice.d.ts +1 -0
- package/lib/test/actions/create_return_invoice.js +226 -0
- package/lib/test/actions/create_transfer.d.ts +1 -0
- package/lib/test/actions/create_transfer.js +142 -0
- package/lib/test/commands/adjust_inventory.d.ts +1 -0
- package/lib/test/commands/adjust_inventory.js +97 -0
- package/lib/test/commands/bank.d.ts +1 -0
- package/lib/test/commands/bank.js +97 -0
- package/lib/test/commands/basic.d.ts +1 -0
- package/lib/test/commands/basic.js +339 -0
- package/lib/test/commands/category.d.ts +1 -0
- package/lib/test/commands/category.js +97 -0
- package/lib/test/commands/channel.d.ts +1 -0
- package/lib/test/commands/channel.js +97 -0
- package/lib/test/commands/client.d.ts +1 -0
- package/lib/test/commands/client.js +97 -0
- package/lib/test/commands/client_disabled.d.ts +1 -0
- package/lib/test/commands/client_disabled.js +97 -0
- package/lib/test/commands/join.d.ts +1 -0
- package/lib/test/commands/join.js +339 -0
- package/lib/test/commands/measureunit.d.ts +1 -0
- package/lib/test/commands/measureunit.js +97 -0
- package/lib/test/commands/measureunit_family.d.ts +1 -0
- package/lib/test/commands/measureunit_family.js +97 -0
- package/lib/test/commands/payment_term.d.ts +1 -0
- package/lib/test/commands/payment_term.js +97 -0
- package/lib/test/commands/price_list.d.ts +1 -0
- package/lib/test/commands/price_list.js +97 -0
- package/lib/test/commands/product.d.ts +1 -0
- package/lib/test/commands/product.js +97 -0
- package/lib/test/commands/product_disabled.d.ts +1 -0
- package/lib/test/commands/product_disabled.js +97 -0
- package/lib/test/commands/rep.d.ts +1 -0
- package/lib/test/commands/rep.js +97 -0
- package/lib/test/commands/tag.d.ts +1 -0
- package/lib/test/commands/tag.js +97 -0
- package/lib/test/commands/tax.d.ts +1 -0
- package/lib/test/commands/tax.js +97 -0
- package/lib/test/commands/warehouse.d.ts +1 -0
- package/lib/test/commands/warehouse.js +97 -0
- package/lib/types.d.ts +77 -0
- package/lib/types.js +1 -0
- package/lib/util.d.ts +70 -0
- package/lib/util.js +164 -0
- package/package.json +51 -0
- package/src/actions/create_invoice.ts +275 -0
- package/src/actions/create_payment.ts +138 -0
- package/src/actions/create_proforma.ts +187 -0
- package/src/actions/create_return_invoice.ts +217 -0
- package/src/actions/create_transfer.ts +160 -0
- package/src/actions/index.ts +27 -0
- package/src/commands/adjust_inventory.ts +250 -0
- package/src/commands/bank.ts +243 -0
- package/src/commands/basic.ts +64 -0
- package/src/commands/category.ts +176 -0
- package/src/commands/channel.ts +219 -0
- package/src/commands/client.ts +368 -0
- package/src/commands/client_disabled.ts +158 -0
- package/src/commands/index.ts +87 -0
- package/src/commands/join.ts +80 -0
- package/src/commands/measureunit.ts +276 -0
- package/src/commands/measureunit_family.ts +209 -0
- package/src/commands/payment_term.ts +224 -0
- package/src/commands/price_list.ts +434 -0
- package/src/commands/product.ts +359 -0
- package/src/commands/product_disabled.ts +152 -0
- package/src/commands/rep.ts +209 -0
- package/src/commands/tag.ts +173 -0
- package/src/commands/tax.ts +178 -0
- package/src/commands/warehouse.ts +204 -0
- package/src/index.ts +7 -0
- package/src/test/actions/create_invoice.ts +214 -0
- package/src/test/actions/create_payment.ts +131 -0
- package/src/test/actions/create_proforma.ts +242 -0
- package/src/test/actions/create_return_invoice.ts +226 -0
- package/src/test/actions/create_transfer.ts +142 -0
- package/src/test/commands/adjust_inventory.ts +100 -0
- package/src/test/commands/bank.ts +100 -0
- package/src/test/commands/basic.ts +342 -0
- package/src/test/commands/category.ts +100 -0
- package/src/test/commands/channel.ts +100 -0
- package/src/test/commands/client.ts +100 -0
- package/src/test/commands/client_disabled.ts +100 -0
- package/src/test/commands/join.ts +342 -0
- package/src/test/commands/measureunit.ts +100 -0
- package/src/test/commands/measureunit_family.ts +100 -0
- package/src/test/commands/payment_term.ts +100 -0
- package/src/test/commands/price_list.ts +100 -0
- package/src/test/commands/product.ts +100 -0
- package/src/test/commands/product_disabled.ts +100 -0
- package/src/test/commands/rep.ts +100 -0
- package/src/test/commands/tag.ts +100 -0
- package/src/test/commands/tax.ts +100 -0
- package/src/test/commands/warehouse.ts +100 -0
- package/src/types.ts +76 -0
- package/src/util.ts +205 -0
- package/tsconfig.json +15 -0
|
@@ -0,0 +1,434 @@
|
|
|
1
|
+
import Repzo from "repzo";
|
|
2
|
+
import { Service } from "repzo/src/types";
|
|
3
|
+
import DataSet from "data-set-query";
|
|
4
|
+
import { CommandEvent, Result, FailedDocsReport } from "../types";
|
|
5
|
+
import {
|
|
6
|
+
_fetch,
|
|
7
|
+
_create,
|
|
8
|
+
_update,
|
|
9
|
+
_delete,
|
|
10
|
+
update_bench_time,
|
|
11
|
+
date_formatting,
|
|
12
|
+
set_error,
|
|
13
|
+
} from "../util.js";
|
|
14
|
+
|
|
15
|
+
import { SAPUoM, get_sap_UoMs } from "./measureunit.js";
|
|
16
|
+
|
|
17
|
+
interface SAPPriceListItem {
|
|
18
|
+
PLDID: number; // 31,
|
|
19
|
+
PLITEMID: string; // "010-HEI-HE0088",
|
|
20
|
+
PLITEMPRICEVALUE: number; // 6.5864;
|
|
21
|
+
PLITEMUNIT: string; // "CRTN",
|
|
22
|
+
CREATEDATE: string; // "2021-12-07T21:00:00Z",
|
|
23
|
+
UPDATEDATE: string; // "2022-11-29T21:00:00Z"
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface SAPPriceListItems {
|
|
27
|
+
result: "Success";
|
|
28
|
+
PriceList: SAPPriceListItem[];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export const sync_price_list = async (commandEvent: CommandEvent) => {
|
|
32
|
+
const repzo = new Repzo(commandEvent.app.formData?.repzoApiKey, {
|
|
33
|
+
env: commandEvent.env,
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const commandLog = new Repzo.CommandLog(
|
|
37
|
+
repzo,
|
|
38
|
+
commandEvent.app,
|
|
39
|
+
commandEvent.command
|
|
40
|
+
);
|
|
41
|
+
try {
|
|
42
|
+
console.log("sync_price_list");
|
|
43
|
+
|
|
44
|
+
const new_bench_time = new Date().toISOString();
|
|
45
|
+
const bench_time_key = "bench_time_price_list";
|
|
46
|
+
|
|
47
|
+
await commandLog.load(commandEvent.sync_id);
|
|
48
|
+
await commandLog
|
|
49
|
+
.addDetail("Repzo SAP: Started Syncing Price Lists")
|
|
50
|
+
.commit();
|
|
51
|
+
|
|
52
|
+
const nameSpace: string = commandEvent.nameSpace.join("_");
|
|
53
|
+
const result = {
|
|
54
|
+
PL: { created: 0, updated: 0, failed: 0 },
|
|
55
|
+
PL_items: { created: 0, updated: 0, failed: 0 },
|
|
56
|
+
sap_total: 0,
|
|
57
|
+
repzo_total: 0,
|
|
58
|
+
repzo_PL_items: 0,
|
|
59
|
+
sap_UoMs_total: 0,
|
|
60
|
+
repzo_products_total: 0,
|
|
61
|
+
};
|
|
62
|
+
const failed_docs_report: FailedDocsReport = [];
|
|
63
|
+
|
|
64
|
+
// Get SAP Price Lists
|
|
65
|
+
const sap_price_lists: SAPPriceListItem[] = await get_sap_price_list(
|
|
66
|
+
commandEvent.app.formData.sapHostUrl,
|
|
67
|
+
{ updateAt: commandEvent.app.options_formData[bench_time_key] }
|
|
68
|
+
);
|
|
69
|
+
result.sap_total = sap_price_lists?.length;
|
|
70
|
+
|
|
71
|
+
await commandLog
|
|
72
|
+
.addDetail(
|
|
73
|
+
`${result.sap_total} Price Lists in SAP changed since ${
|
|
74
|
+
commandEvent.app.options_formData[bench_time_key] || "ever"
|
|
75
|
+
}`
|
|
76
|
+
)
|
|
77
|
+
.commit();
|
|
78
|
+
|
|
79
|
+
// Get SAP UoMs
|
|
80
|
+
const sap_UoMs: SAPUoM[] = await get_sap_UoMs(
|
|
81
|
+
commandEvent.app.formData.sapHostUrl,
|
|
82
|
+
{}
|
|
83
|
+
);
|
|
84
|
+
result.sap_UoMs_total = sap_UoMs?.length;
|
|
85
|
+
|
|
86
|
+
await commandLog
|
|
87
|
+
.addDetail(`${result.sap_total} Unit of Measures in SAP`)
|
|
88
|
+
.commit();
|
|
89
|
+
|
|
90
|
+
// Get Repzo Price Lists
|
|
91
|
+
const repzo_price_lists = await repzo.priceList.find({ per_page: 50000 });
|
|
92
|
+
result.repzo_total = repzo_price_lists?.data?.length;
|
|
93
|
+
await commandLog
|
|
94
|
+
.addDetail(`${repzo_price_lists?.data?.length} Price Lists in Repzo`)
|
|
95
|
+
.commit();
|
|
96
|
+
|
|
97
|
+
// Get Repzo Products
|
|
98
|
+
const repzo_products = await repzo.product.find({
|
|
99
|
+
active: true,
|
|
100
|
+
withVariants: true,
|
|
101
|
+
per_page: 50000,
|
|
102
|
+
});
|
|
103
|
+
result.repzo_products_total = repzo_products?.data?.length;
|
|
104
|
+
await commandLog
|
|
105
|
+
.addDetail(`${repzo_products?.data?.length} Products in Repzo`)
|
|
106
|
+
.commit();
|
|
107
|
+
|
|
108
|
+
// Get Repzo MeasureUnits
|
|
109
|
+
const repzo_UoMs = await repzo.measureunit.find({
|
|
110
|
+
disabled: false,
|
|
111
|
+
per_page: 50000,
|
|
112
|
+
});
|
|
113
|
+
result.repzo_total = repzo_UoMs?.data?.length;
|
|
114
|
+
await commandLog
|
|
115
|
+
.addDetail(`${repzo_UoMs?.data?.length} Measure Units in Repzo`)
|
|
116
|
+
.commit();
|
|
117
|
+
|
|
118
|
+
if (!repzo_UoMs?.data?.length) throw "MeasureUnits in Repzo was not found";
|
|
119
|
+
|
|
120
|
+
const sap_unique_UoMs: { [key: string]: number } = {};
|
|
121
|
+
sap_UoMs.forEach((doc) => {
|
|
122
|
+
const key: string = `${doc.ITEMCODE}__${doc.ALTUOMCODE}`;
|
|
123
|
+
sap_unique_UoMs[key] = doc.ALTQTY;
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
// sort the data
|
|
127
|
+
const priceLists_withItems: { [key: string]: SAPPriceListItem[] } = {};
|
|
128
|
+
sap_price_lists.forEach((doc) => {
|
|
129
|
+
if (!priceLists_withItems[doc.PLDID])
|
|
130
|
+
priceLists_withItems[doc.PLDID] = [];
|
|
131
|
+
priceLists_withItems[doc.PLDID].push(doc);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
// create priceLists
|
|
135
|
+
const priceLists_names = Object.keys(priceLists_withItems);
|
|
136
|
+
for (let i = 0; i < priceLists_names.length; i++) {
|
|
137
|
+
const price_list_name = priceLists_names[i];
|
|
138
|
+
const body = {
|
|
139
|
+
name: `PL_${price_list_name}`,
|
|
140
|
+
integration_meta: { id: `${nameSpace}_${price_list_name}` },
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
const repzo_price_list = repzo_price_lists?.data?.find(
|
|
144
|
+
(pl) => pl?.integration_meta?.id == body?.integration_meta?.id
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
if (!repzo_price_list) {
|
|
148
|
+
// Create
|
|
149
|
+
try {
|
|
150
|
+
const created_price_list = await repzo.priceList.create(
|
|
151
|
+
body as Service.PriceList.Create.Body
|
|
152
|
+
);
|
|
153
|
+
result.PL.created++;
|
|
154
|
+
} catch (e: any) {
|
|
155
|
+
console.log("Create Price List Failed >> ", e?.response, body);
|
|
156
|
+
failed_docs_report.push({
|
|
157
|
+
method: "create",
|
|
158
|
+
doc: body,
|
|
159
|
+
error_message: set_error(e),
|
|
160
|
+
});
|
|
161
|
+
result.PL.failed++;
|
|
162
|
+
}
|
|
163
|
+
} else {
|
|
164
|
+
if (repzo_price_list?.name == body?.name) continue;
|
|
165
|
+
// Update
|
|
166
|
+
try {
|
|
167
|
+
const updated_price_list = await repzo.priceList.update(
|
|
168
|
+
repzo_price_list._id,
|
|
169
|
+
body as Service.PriceList.Update.Body
|
|
170
|
+
);
|
|
171
|
+
result.PL.updated++;
|
|
172
|
+
} catch (e: any) {
|
|
173
|
+
console.log("Update Price List Failed >> ", e?.response?.data, body);
|
|
174
|
+
failed_docs_report.push({
|
|
175
|
+
method: "update",
|
|
176
|
+
doc_id: repzo_price_list?._id,
|
|
177
|
+
doc: body,
|
|
178
|
+
error_message: set_error(e),
|
|
179
|
+
});
|
|
180
|
+
result.PL.failed++;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
// Price List Items ***************************************
|
|
185
|
+
|
|
186
|
+
await commandLog
|
|
187
|
+
.addDetail(`Start Sync Price List Items From SAP to Repzo`)
|
|
188
|
+
.commit();
|
|
189
|
+
|
|
190
|
+
const repzo_all_priceLists = await repzo.priceList.find({
|
|
191
|
+
disabled: false,
|
|
192
|
+
per_page: 50000,
|
|
193
|
+
});
|
|
194
|
+
await commandLog
|
|
195
|
+
.addDetail(`${repzo_all_priceLists?.data?.length} Price Lists in Repzo`)
|
|
196
|
+
.commit();
|
|
197
|
+
|
|
198
|
+
if (!repzo_all_priceLists?.data?.length)
|
|
199
|
+
throw `No Price Lists was found On Repzo`;
|
|
200
|
+
|
|
201
|
+
for (let priceList_name in priceLists_withItems) {
|
|
202
|
+
const repzo_PriceList = repzo_all_priceLists?.data?.find(
|
|
203
|
+
(pl) => pl.integration_meta?.id == `${nameSpace}_${priceList_name}`
|
|
204
|
+
);
|
|
205
|
+
if (!repzo_PriceList) {
|
|
206
|
+
console.log(
|
|
207
|
+
`Price list with PLDID: ${priceList_name} was not created or disabled`
|
|
208
|
+
);
|
|
209
|
+
failed_docs_report.push({
|
|
210
|
+
method: "create",
|
|
211
|
+
// doc: priceLists_withItems[priceList_name],
|
|
212
|
+
error_message: set_error(
|
|
213
|
+
`Failed Create PriceList Items: number of PL items: ${priceLists_withItems[priceList_name].length} => Because Price list with PLDID: ${priceList_name} was not created or disabled`
|
|
214
|
+
),
|
|
215
|
+
});
|
|
216
|
+
result.PL_items.failed +=
|
|
217
|
+
priceLists_withItems[priceList_name]?.length || 0;
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const repzo_price_list_items = await repzo.priceListItem.find({
|
|
222
|
+
disabled: false,
|
|
223
|
+
pricelist_id: repzo_PriceList?._id,
|
|
224
|
+
per_page: 50000,
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
// Create Price list items
|
|
228
|
+
const priceList_items: {
|
|
229
|
+
[ket: string]: any;
|
|
230
|
+
} = {};
|
|
231
|
+
priceLists_withItems[priceList_name].forEach((doc: any) => {
|
|
232
|
+
if (!sap_unique_UoMs[`${doc.PLITEMID}__${doc.PLITEMUNIT}`]) {
|
|
233
|
+
console.log(
|
|
234
|
+
`error => ${doc.PLITEMID}__${doc.PLITEMUNIT} was not found on the Uom`
|
|
235
|
+
);
|
|
236
|
+
failed_docs_report.push({
|
|
237
|
+
method: "create",
|
|
238
|
+
// doc: priceLists_withItems[priceList_name],
|
|
239
|
+
error_message: set_error(
|
|
240
|
+
`Create PL items => PLITEMID: ${doc.PLITEMID}, PLITEMUNIT:${doc.PLITEMUNIT} was not found on the Uom`
|
|
241
|
+
),
|
|
242
|
+
});
|
|
243
|
+
result.PL_items.failed++;
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
doc.factor = sap_unique_UoMs[`${doc.PLITEMID}__${doc.PLITEMUNIT}`];
|
|
247
|
+
const key = `${doc.PLITEMID}`;
|
|
248
|
+
if (!priceList_items[key]) {
|
|
249
|
+
priceList_items[key] = doc;
|
|
250
|
+
} else {
|
|
251
|
+
const current_doc = priceList_items[key];
|
|
252
|
+
if (current_doc?.factor > doc?.factor) {
|
|
253
|
+
priceList_items[key] = doc;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
const priceListItems = Object.values(priceList_items);
|
|
259
|
+
for (let j = 0; j < priceListItems.length; j++) {
|
|
260
|
+
const item = priceListItems[j];
|
|
261
|
+
if (!item.factor && item.factor !== 0) {
|
|
262
|
+
failed_docs_report.push({
|
|
263
|
+
method: "create",
|
|
264
|
+
// doc: priceLists_withItems[priceList_name],
|
|
265
|
+
error_message: set_error(
|
|
266
|
+
`Price List: ${item.PLDID} of Product with PLITEMID: ${item.PLITEMID} does not have Uom`
|
|
267
|
+
),
|
|
268
|
+
});
|
|
269
|
+
result.PL_items.failed++;
|
|
270
|
+
continue;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
const repzo_product = repzo_products?.data?.find(
|
|
274
|
+
(product) =>
|
|
275
|
+
product?.integration_meta?.id == `${nameSpace}_${item.PLITEMID}`
|
|
276
|
+
);
|
|
277
|
+
|
|
278
|
+
if (!repzo_product) {
|
|
279
|
+
failed_docs_report.push({
|
|
280
|
+
method: "create",
|
|
281
|
+
// doc: priceLists_withItems[priceList_name],
|
|
282
|
+
error_message: set_error(
|
|
283
|
+
`Price List: ${item.PLDID} of Product with PLITEMID: ${item.PLITEMID} was not found or disabled`
|
|
284
|
+
),
|
|
285
|
+
});
|
|
286
|
+
result.PL_items.failed++;
|
|
287
|
+
continue;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
const repzo_product_uom = repzo_UoMs?.data?.find(
|
|
291
|
+
(uom) =>
|
|
292
|
+
uom?._id?.toString() == repzo_product?.sv_measureUnit?.toString()
|
|
293
|
+
);
|
|
294
|
+
|
|
295
|
+
if (!repzo_product_uom) {
|
|
296
|
+
failed_docs_report.push({
|
|
297
|
+
method: "create",
|
|
298
|
+
// doc: priceLists_withItems[priceList_name],
|
|
299
|
+
error_message: set_error(
|
|
300
|
+
`Price List: ${
|
|
301
|
+
item.PLDID
|
|
302
|
+
} of MeasureUnit with _id: ${repzo_product?.sv_measureUnit?.toString()} was not found or disabled`
|
|
303
|
+
),
|
|
304
|
+
});
|
|
305
|
+
result.PL_items.failed++;
|
|
306
|
+
continue;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
const price =
|
|
310
|
+
repzo_product_uom && repzo_product_uom?.factor == 1
|
|
311
|
+
? Math.round(item.PLITEMPRICEVALUE * 1000)
|
|
312
|
+
: Math.round(
|
|
313
|
+
(item.PLITEMPRICEVALUE * 1000) / repzo_product_uom.factor
|
|
314
|
+
);
|
|
315
|
+
|
|
316
|
+
const variant = repzo_product?.variants?.find(
|
|
317
|
+
(variant) =>
|
|
318
|
+
variant?.integration_meta?.id == `${nameSpace}_${item.PLITEMID}`
|
|
319
|
+
);
|
|
320
|
+
if (!variant) {
|
|
321
|
+
console.log(
|
|
322
|
+
`Price List: ${item.PLDID} of Variant with PLITEMID: ${item.PLITEMID} was not found`
|
|
323
|
+
);
|
|
324
|
+
failed_docs_report.push({
|
|
325
|
+
method: "create",
|
|
326
|
+
// doc: priceLists_withItems[priceList_name],
|
|
327
|
+
error_message: set_error(
|
|
328
|
+
`Price List: ${item.PLDID} of Variant with PLITEMID: ${item.PLITEMID} was not found`
|
|
329
|
+
),
|
|
330
|
+
});
|
|
331
|
+
result.PL_items.failed++;
|
|
332
|
+
continue;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
const body = {
|
|
336
|
+
integration_meta: {
|
|
337
|
+
id: `${nameSpace}_${item.PLDID}_${item.PLITEMID}`,
|
|
338
|
+
},
|
|
339
|
+
product_id: repzo_product._id,
|
|
340
|
+
variant_id: variant._id,
|
|
341
|
+
pricelist_id: repzo_PriceList._id,
|
|
342
|
+
price: price,
|
|
343
|
+
};
|
|
344
|
+
|
|
345
|
+
// console.log(data);
|
|
346
|
+
|
|
347
|
+
const is_found_in_repzo_db = repzo_price_list_items?.data?.find(
|
|
348
|
+
(item) => item?.integration_meta?.id == body?.integration_meta?.id
|
|
349
|
+
);
|
|
350
|
+
|
|
351
|
+
// console.log(`${data.integration_meta?.id} => ${is_found_in_repzo_db ? "create" : "update"}`)
|
|
352
|
+
|
|
353
|
+
if (!is_found_in_repzo_db) {
|
|
354
|
+
// Create
|
|
355
|
+
try {
|
|
356
|
+
const created_PL_item = await repzo.priceListItem.create(
|
|
357
|
+
body as Service.PriceListItem.Create.Body
|
|
358
|
+
);
|
|
359
|
+
result.PL_items.created++;
|
|
360
|
+
} catch (e: any) {
|
|
361
|
+
console.log("Create PL Item Failed >> ", e?.response?.data, body);
|
|
362
|
+
failed_docs_report.push({
|
|
363
|
+
method: "create",
|
|
364
|
+
// doc: body,
|
|
365
|
+
error_message: set_error(e),
|
|
366
|
+
});
|
|
367
|
+
result.PL_items.failed++;
|
|
368
|
+
}
|
|
369
|
+
} else {
|
|
370
|
+
if (is_found_in_repzo_db.price == body.price) continue;
|
|
371
|
+
// Update
|
|
372
|
+
try {
|
|
373
|
+
const updated_PL_item = await repzo.priceListItem.update(
|
|
374
|
+
is_found_in_repzo_db._id,
|
|
375
|
+
body as Service.PriceListItem.Update.Body
|
|
376
|
+
);
|
|
377
|
+
result.PL_items.updated++;
|
|
378
|
+
} catch (e: any) {
|
|
379
|
+
console.log(
|
|
380
|
+
"Update Price List Item Failed >> ",
|
|
381
|
+
e?.response?.data,
|
|
382
|
+
body
|
|
383
|
+
);
|
|
384
|
+
failed_docs_report.push({
|
|
385
|
+
method: "update",
|
|
386
|
+
doc_id: is_found_in_repzo_db?._id,
|
|
387
|
+
doc: body,
|
|
388
|
+
error_message: set_error(e),
|
|
389
|
+
});
|
|
390
|
+
result.PL_items.failed++;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
// console.log(result);
|
|
397
|
+
|
|
398
|
+
await update_bench_time(
|
|
399
|
+
repzo,
|
|
400
|
+
commandEvent.app._id,
|
|
401
|
+
bench_time_key,
|
|
402
|
+
new_bench_time
|
|
403
|
+
);
|
|
404
|
+
await commandLog
|
|
405
|
+
.setStatus(
|
|
406
|
+
"success",
|
|
407
|
+
failed_docs_report.length ? failed_docs_report : null
|
|
408
|
+
)
|
|
409
|
+
.setBody(result)
|
|
410
|
+
.commit();
|
|
411
|
+
return result;
|
|
412
|
+
} catch (e: any) {
|
|
413
|
+
//@ts-ignore
|
|
414
|
+
console.error(e?.response?.data || e);
|
|
415
|
+
await commandLog.setStatus("fail", e).commit();
|
|
416
|
+
throw e;
|
|
417
|
+
}
|
|
418
|
+
};
|
|
419
|
+
|
|
420
|
+
const get_sap_price_list = async (
|
|
421
|
+
serviceEndPoint: string,
|
|
422
|
+
query?: { updateAt?: string }
|
|
423
|
+
): Promise<SAPPriceListItem[]> => {
|
|
424
|
+
try {
|
|
425
|
+
const sap_price_lists: SAPPriceListItems = await _create(
|
|
426
|
+
serviceEndPoint,
|
|
427
|
+
"/PriceList",
|
|
428
|
+
{ UpdateAt: query?.updateAt }
|
|
429
|
+
);
|
|
430
|
+
return sap_price_lists.PriceList;
|
|
431
|
+
} catch (e: any) {
|
|
432
|
+
throw e;
|
|
433
|
+
}
|
|
434
|
+
};
|