@shushed/helpers 0.0.289 → 0.0.290
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/dist/cjs/contracts/order/address.schema.json +14 -18
- package/dist/cjs/contracts/order/item.schema.json +6 -1
- package/dist/cjs/contracts/order/payment.schema.json +4 -4
- package/dist/cjs/contracts/order/shipment/item.schema.json +6 -1
- package/dist/cjs/contracts/order/shipment.schema.json +14 -0
- package/dist/cjs/contracts/order.schema.json +53 -53
- package/dist/cjs/dist-dereferenced/messages/order/delivered.js +1 -1
- package/dist/cjs/dist-dereferenced/messages/order/new.js +1 -1
- package/dist/cjs/dist-dereferenced/messages/order/processed.js +1 -1
- package/dist/cjs/dist-dereferenced/messages/order/return-initiated.js +1 -1
- package/dist/cjs/dist-dereferenced/messages/order/returned.js +1 -1
- package/dist/cjs/dist-dereferenced/messages/order/shipped.js +1 -1
- package/dist/cjs/dist-dereferenced/order/address.js +1 -1
- package/dist/cjs/dist-dereferenced/order/customer.js +1 -1
- package/dist/cjs/dist-dereferenced/order/item.js +1 -1
- package/dist/cjs/dist-dereferenced/order/orderMain.js +1 -1
- package/dist/cjs/dist-dereferenced/order/payment.js +1 -1
- package/dist/cjs/dist-dereferenced/order/shipment/item/itemMain.js +1 -1
- package/dist/cjs/dist-dereferenced/order/shipment/item/returned.js +1 -1
- package/dist/cjs/dist-dereferenced/order/shipment/pos/outbound.js +1 -1
- package/dist/cjs/dist-dereferenced/order/shipment/pos/posMain.js +1 -1
- package/dist/cjs/dist-dereferenced/order/shipment/pos/return.js +1 -1
- package/dist/cjs/dist-dereferenced/order/shipment/shipmentMain.js +1 -1
- package/dist/cjs/dist-dereferenced/order/shipment/shipped/outbound.js +1 -1
- package/dist/cjs/dist-dereferenced/order/shipment/shipped/return.js +1 -1
- package/dist/cjs/dist-dereferenced/order/shipment/shipped/shippedMain.js +1 -1
- package/dist/cjs/src-public/bcOrder.js +125 -110
- package/dist/types/dist-dereferenced/messages/order/delivered.d.ts +117 -105
- package/dist/types/dist-dereferenced/messages/order/new.d.ts +117 -105
- package/dist/types/dist-dereferenced/messages/order/processed.d.ts +117 -105
- package/dist/types/dist-dereferenced/messages/order/return-initiated.d.ts +117 -105
- package/dist/types/dist-dereferenced/messages/order/returned.d.ts +117 -105
- package/dist/types/dist-dereferenced/messages/order/shipped.d.ts +117 -105
- package/dist/types/dist-dereferenced/order/address.d.ts +10 -14
- package/dist/types/dist-dereferenced/order/customer.d.ts +10 -14
- package/dist/types/dist-dereferenced/order/index.d.ts +117 -105
- package/dist/types/dist-dereferenced/order/item.d.ts +3 -0
- package/dist/types/dist-dereferenced/order/orderMain.d.ts +117 -105
- package/dist/types/dist-dereferenced/order/payment.d.ts +2 -2
- package/dist/types/dist-dereferenced/order/shipment/index.d.ts +15 -1
- package/dist/types/dist-dereferenced/order/shipment/item/index.d.ts +4 -1
- package/dist/types/dist-dereferenced/order/shipment/item/itemMain.d.ts +4 -1
- package/dist/types/dist-dereferenced/order/shipment/item/returned.d.ts +4 -1
- package/dist/types/dist-dereferenced/order/shipment/pos/index.d.ts +15 -1
- package/dist/types/dist-dereferenced/order/shipment/pos/outbound.d.ts +15 -1
- package/dist/types/dist-dereferenced/order/shipment/pos/posMain.d.ts +15 -1
- package/dist/types/dist-dereferenced/order/shipment/pos/return.d.ts +15 -1
- package/dist/types/dist-dereferenced/order/shipment/shipmentMain.d.ts +15 -1
- package/dist/types/dist-dereferenced/order/shipment/shipped/index.d.ts +15 -1
- package/dist/types/dist-dereferenced/order/shipment/shipped/outbound.d.ts +15 -1
- package/dist/types/dist-dereferenced/order/shipment/shipped/return.d.ts +15 -1
- package/dist/types/dist-dereferenced/order/shipment/shipped/shippedMain.d.ts +15 -1
- package/dist/types/dist-types/messages/order/delivered.d.ts +14 -9
- package/dist/types/dist-types/messages/order/new.d.ts +14 -9
- package/dist/types/dist-types/messages/order/processed.d.ts +14 -9
- package/dist/types/dist-types/messages/order/return-initiated.d.ts +14 -9
- package/dist/types/dist-types/messages/order/returned.d.ts +14 -9
- package/dist/types/dist-types/messages/order/shipped.d.ts +14 -9
- package/dist/types/dist-types/order/address.d.ts +3 -3
- package/dist/types/dist-types/order/customer.d.ts +6 -6
- package/dist/types/dist-types/order/item.d.ts +1 -0
- package/dist/types/dist-types/order/orderMain.d.ts +14 -9
- package/dist/types/dist-types/order/payment.d.ts +2 -2
- package/dist/types/dist-types/order/shipment/item/itemMain.d.ts +1 -0
- package/dist/types/dist-types/order/shipment/item/returned.d.ts +1 -0
- package/dist/types/dist-types/order/shipment/pos/outbound.d.ts +4 -0
- package/dist/types/dist-types/order/shipment/pos/posMain.d.ts +4 -0
- package/dist/types/dist-types/order/shipment/pos/return.d.ts +4 -0
- package/dist/types/dist-types/order/shipment/shipmentMain.d.ts +4 -0
- package/dist/types/dist-types/order/shipment/shipped/outbound.d.ts +4 -0
- package/dist/types/dist-types/order/shipment/shipped/return.d.ts +4 -0
- package/dist/types/dist-types/order/shipment/shipped/shippedMain.d.ts +4 -0
- package/package.json +1 -1
|
@@ -40,6 +40,12 @@ function moneyFromMinor(minor) {
|
|
|
40
40
|
lcy_decimal_places: 2,
|
|
41
41
|
};
|
|
42
42
|
}
|
|
43
|
+
function trimAddr(val) {
|
|
44
|
+
if (val === null || val === undefined)
|
|
45
|
+
return null;
|
|
46
|
+
const s = String(val).replace(/[,\s]+$/g, '').replace(/^[\s]+/, '');
|
|
47
|
+
return s.length > 0 ? s : null;
|
|
48
|
+
}
|
|
43
49
|
function buildLineGrouping(payload) {
|
|
44
50
|
const group = {};
|
|
45
51
|
const makeKey = (orderNo, lineNo) => `${orderNo}:${lineNo}`;
|
|
@@ -102,8 +108,10 @@ function buildLineGrouping(payload) {
|
|
|
102
108
|
}
|
|
103
109
|
function transformMasterOrder(payload) {
|
|
104
110
|
const erp_order_id = payload.no || payload.system_id || "";
|
|
105
|
-
const
|
|
111
|
+
const allSales = Array.isArray(payload.sales) ? payload.sales : [];
|
|
112
|
+
const sale0 = allSales.find((s) => (s.sale_lines || []).length > 0) || allSales[0] || undefined;
|
|
106
113
|
const invoice0 = Array.isArray(payload.invoices) && payload.invoices.length > 0 ? payload.invoices[0] : undefined;
|
|
114
|
+
const allSaleLines = allSales.flatMap((s) => s.sale_lines || []);
|
|
107
115
|
const normalizeIntegerOrNull = (value) => {
|
|
108
116
|
if (value === null || value === undefined)
|
|
109
117
|
return null;
|
|
@@ -169,7 +177,7 @@ function transformMasterOrder(payload) {
|
|
|
169
177
|
const is_gift = Boolean((sale0?.gift_message && String(sale0?.gift_message).trim().length > 0) ||
|
|
170
178
|
(payload.invoices?.some((i) => (i.gift_message || "").trim().length > 0)));
|
|
171
179
|
const invoiceLinesForReturn = payload.invoices?.flatMap((i) => i.invoice_lines || []) || [];
|
|
172
|
-
const salesLinesFallbackForReturn =
|
|
180
|
+
const salesLinesFallbackForReturn = allSaleLines;
|
|
173
181
|
const sourceLinesForReturn = invoiceLinesForReturn.length > 0 ? invoiceLinesForReturn : salesLinesFallbackForReturn;
|
|
174
182
|
const skuByDescription = {};
|
|
175
183
|
sourceLinesForReturn.forEach((line) => {
|
|
@@ -225,7 +233,7 @@ function transformMasterOrder(payload) {
|
|
|
225
233
|
});
|
|
226
234
|
});
|
|
227
235
|
const invoiceLines = payload.invoices?.flatMap((i) => i.invoice_lines || []) || [];
|
|
228
|
-
const salesLinesFallback =
|
|
236
|
+
const salesLinesFallback = allSaleLines.map((l) => ({
|
|
229
237
|
type: l.type || "Item",
|
|
230
238
|
no: l.no,
|
|
231
239
|
variant_code: l.variant_code,
|
|
@@ -416,8 +424,8 @@ function transformMasterOrder(payload) {
|
|
|
416
424
|
if (invoicesGrossMinor === 0 && (payload.invoices || []).length > 0) {
|
|
417
425
|
invoicesGrossMinor = toMinorUnits((payload.invoices || []).reduce((acc, inv) => acc + (inv.amount_incl_vat || 0), 0));
|
|
418
426
|
}
|
|
419
|
-
if (invoicesGrossMinor === 0 && invoicedLineNos.size > 0 &&
|
|
420
|
-
const
|
|
427
|
+
if (invoicesGrossMinor === 0 && invoicedLineNos.size > 0 && allSaleLines.length > 0) {
|
|
428
|
+
const invoicedSaleLineItems = allSaleLines.filter((l) => {
|
|
421
429
|
if (isDeliveryItem(l))
|
|
422
430
|
return false;
|
|
423
431
|
if (String(l?.type || '').toLowerCase() !== 'item')
|
|
@@ -425,18 +433,18 @@ function transformMasterOrder(payload) {
|
|
|
425
433
|
const lineNo = Number(l?.line_no || -1);
|
|
426
434
|
return invoicedLineNos.has(lineNo);
|
|
427
435
|
});
|
|
428
|
-
invoicesGrossMinor = toMinorUnits(
|
|
436
|
+
invoicesGrossMinor = toMinorUnits(invoicedSaleLineItems.reduce((acc, l) => acc + (Number(l?.amount_including_vat || 0)), 0));
|
|
429
437
|
}
|
|
430
438
|
let nonInvSalesGrossMinor = 0;
|
|
431
|
-
if (
|
|
432
|
-
const
|
|
439
|
+
if (allSaleLines.length > 0) {
|
|
440
|
+
const saleLinesNoDelivery = allSaleLines.filter((l) => {
|
|
433
441
|
if (isDeliveryItem(l))
|
|
434
442
|
return false;
|
|
435
443
|
if (String(l?.type || '').toLowerCase() !== 'item')
|
|
436
444
|
return false;
|
|
437
445
|
return true;
|
|
438
446
|
});
|
|
439
|
-
const totalSaleGrossMinor = toMinorUnits(
|
|
447
|
+
const totalSaleGrossMinor = toMinorUnits(saleLinesNoDelivery.reduce((acc, l) => acc + (Number(l?.amount_including_vat || 0)), 0));
|
|
440
448
|
if (invoicesGrossMinor > 0) {
|
|
441
449
|
nonInvSalesGrossMinor = Math.max(0, totalSaleGrossMinor - invoicesGrossMinor);
|
|
442
450
|
}
|
|
@@ -445,7 +453,7 @@ function transformMasterOrder(payload) {
|
|
|
445
453
|
}
|
|
446
454
|
}
|
|
447
455
|
else if (sale0 && (sale0.amount_excl_vat || sale0.amount_incl_vat)) {
|
|
448
|
-
const saleGrossMinor = toMinorUnits(
|
|
456
|
+
const saleGrossMinor = toMinorUnits(allSales.reduce((acc, s) => acc + (s.amount_incl_vat || 0), 0));
|
|
449
457
|
if (invoicesGrossMinor > 0) {
|
|
450
458
|
nonInvSalesGrossMinor = Math.max(0, saleGrossMinor - invoicesGrossMinor);
|
|
451
459
|
}
|
|
@@ -454,7 +462,7 @@ function transformMasterOrder(payload) {
|
|
|
454
462
|
}
|
|
455
463
|
}
|
|
456
464
|
else {
|
|
457
|
-
const
|
|
465
|
+
const nonInvoicedSaleItems = allSaleLines.filter((l) => {
|
|
458
466
|
if (isDeliveryItem(l))
|
|
459
467
|
return false;
|
|
460
468
|
if (String(l?.type || '').toLowerCase() !== 'item')
|
|
@@ -462,21 +470,21 @@ function transformMasterOrder(payload) {
|
|
|
462
470
|
const lineNo = Number(l?.line_no || -1);
|
|
463
471
|
return !invoicedLineNos.has(lineNo);
|
|
464
472
|
});
|
|
465
|
-
nonInvSalesGrossMinor = toMinorUnits(
|
|
473
|
+
nonInvSalesGrossMinor = toMinorUnits(nonInvoicedSaleItems.reduce((acc, l) => acc + (Number(l?.amount_including_vat || 0)), 0));
|
|
466
474
|
}
|
|
467
475
|
const totalGrossMinor = invoicesGrossMinor + nonInvSalesGrossMinor;
|
|
468
476
|
const invoicesNetMinorWithDelivery = toMinorUnits((payload.invoices || []).reduce((acc, inv) => acc + (inv.amount_excl_vat || 0), 0));
|
|
469
477
|
const invoicesGrossMinorWithDelivery = toMinorUnits((payload.invoices || []).reduce((acc, inv) => acc + (inv.amount_incl_vat || 0), 0));
|
|
470
478
|
let nonInvSalesNetMinorWithDelivery = 0;
|
|
471
479
|
let nonInvSalesGrossMinorWithDelivery = 0;
|
|
472
|
-
if (
|
|
473
|
-
const
|
|
480
|
+
if (allSaleLines.length > 0) {
|
|
481
|
+
const saleLinesWithDelivery = allSaleLines.filter((l) => {
|
|
474
482
|
if (String(l?.type || '').toLowerCase() !== 'item')
|
|
475
483
|
return false;
|
|
476
484
|
return true;
|
|
477
485
|
});
|
|
478
|
-
const totalSaleNetMinorWithDelivery = toMinorUnits(
|
|
479
|
-
const totalSaleGrossMinorWithDelivery = toMinorUnits(
|
|
486
|
+
const totalSaleNetMinorWithDelivery = toMinorUnits(saleLinesWithDelivery.reduce((acc, l) => acc + (Number(l?.amount || 0)), 0));
|
|
487
|
+
const totalSaleGrossMinorWithDelivery = toMinorUnits(saleLinesWithDelivery.reduce((acc, l) => acc + (Number(l?.amount_including_vat || 0)), 0));
|
|
480
488
|
if (invoicesGrossMinorWithDelivery > 0) {
|
|
481
489
|
nonInvSalesNetMinorWithDelivery = Math.max(0, totalSaleNetMinorWithDelivery - invoicesNetMinorWithDelivery);
|
|
482
490
|
nonInvSalesGrossMinorWithDelivery = Math.max(0, totalSaleGrossMinorWithDelivery - invoicesGrossMinorWithDelivery);
|
|
@@ -487,8 +495,8 @@ function transformMasterOrder(payload) {
|
|
|
487
495
|
}
|
|
488
496
|
}
|
|
489
497
|
else if (sale0 && (sale0.amount_excl_vat || sale0.amount_incl_vat)) {
|
|
490
|
-
const saleNetMinor = toMinorUnits(
|
|
491
|
-
const saleGrossMinor = toMinorUnits(
|
|
498
|
+
const saleNetMinor = toMinorUnits(allSales.reduce((acc, s) => acc + (s.amount_excl_vat || 0), 0));
|
|
499
|
+
const saleGrossMinor = toMinorUnits(allSales.reduce((acc, s) => acc + (s.amount_incl_vat || 0), 0));
|
|
492
500
|
if (invoicesGrossMinorWithDelivery > 0) {
|
|
493
501
|
nonInvSalesNetMinorWithDelivery = Math.max(0, saleNetMinor - invoicesNetMinorWithDelivery);
|
|
494
502
|
nonInvSalesGrossMinorWithDelivery = Math.max(0, saleGrossMinor - invoicesGrossMinorWithDelivery);
|
|
@@ -499,7 +507,7 @@ function transformMasterOrder(payload) {
|
|
|
499
507
|
}
|
|
500
508
|
}
|
|
501
509
|
else {
|
|
502
|
-
const nonInvoicedSalesWithDelivery =
|
|
510
|
+
const nonInvoicedSalesWithDelivery = allSaleLines.filter((l) => {
|
|
503
511
|
if (String(l?.type || '').toLowerCase() !== 'item')
|
|
504
512
|
return false;
|
|
505
513
|
const lineNo = Number(l?.line_no || -1);
|
|
@@ -542,7 +550,26 @@ function transformMasterOrder(payload) {
|
|
|
542
550
|
}
|
|
543
551
|
return 'STANDARD';
|
|
544
552
|
};
|
|
545
|
-
|
|
553
|
+
let deliveryGrossMinor = 0;
|
|
554
|
+
let deliveryNetMinor = 0;
|
|
555
|
+
let deliveryDiscountGrossMinor = 0;
|
|
556
|
+
let deliveryDiscountNetMinor = 0;
|
|
557
|
+
const deliverySourceLines = invoiceLines.length > 0 ? invoiceLines : allSaleLines;
|
|
558
|
+
deliverySourceLines.forEach((line) => {
|
|
559
|
+
if (!isDeliveryItem(line))
|
|
560
|
+
return;
|
|
561
|
+
const gross = toMinorUnits(Number(line?.amount_including_vat || line?.amount_incl_vat || 0));
|
|
562
|
+
const net = toMinorUnits(Number(line?.amount || line?.amount_excl_vat || 0));
|
|
563
|
+
deliveryGrossMinor += gross;
|
|
564
|
+
deliveryNetMinor += net;
|
|
565
|
+
const lineDiscGross = toMinorUnits(Number(line?.line_discount_amount || 0));
|
|
566
|
+
const lineDiscNet = headerPricesInclVat
|
|
567
|
+
? Math.round(lineDiscGross / (1 + 0.2))
|
|
568
|
+
: lineDiscGross;
|
|
569
|
+
deliveryDiscountGrossMinor += lineDiscGross;
|
|
570
|
+
deliveryDiscountNetMinor += lineDiscNet;
|
|
571
|
+
});
|
|
572
|
+
const mappedShipments = (payload.shipments || []).map((s, shipIdx) => {
|
|
546
573
|
const rawLines = (s.shipment_lines || [])
|
|
547
574
|
.filter((l) => isShippableItem(l));
|
|
548
575
|
const qtyBySku = new Map();
|
|
@@ -559,6 +586,11 @@ function transformMasterOrder(payload) {
|
|
|
559
586
|
sku,
|
|
560
587
|
quantity: Math.max(1, qtyBySku.get(sku) || 0),
|
|
561
588
|
}));
|
|
589
|
+
const isFirstShipment = shipIdx === 0;
|
|
590
|
+
const shipGross = isFirstShipment ? deliveryGrossMinor : 0;
|
|
591
|
+
const shipNet = isFirstShipment ? deliveryNetMinor : 0;
|
|
592
|
+
const shipDiscGross = isFirstShipment ? deliveryDiscountGrossMinor : 0;
|
|
593
|
+
const shipDiscNet = isFirstShipment ? deliveryDiscountNetMinor : 0;
|
|
562
594
|
const statusVal = shipmentLines.length > 0 ? "SHIPPED" : "PENDING";
|
|
563
595
|
return {
|
|
564
596
|
erp_id: s.no || s.system_id || "",
|
|
@@ -570,10 +602,10 @@ function transformMasterOrder(payload) {
|
|
|
570
602
|
tracking_code: (s.torque_tracking_id && typeof s.torque_tracking_id === 'string') ? s.torque_tracking_id : null,
|
|
571
603
|
class_name: mapClassName(s.shipping_agent_service_code),
|
|
572
604
|
items: shipmentLines,
|
|
573
|
-
amount_net:
|
|
574
|
-
amount_gross:
|
|
575
|
-
discount_amount_net:
|
|
576
|
-
discount_amount_gross:
|
|
605
|
+
amount_net: moneyFromMinor(shipNet),
|
|
606
|
+
amount_gross: moneyFromMinor(shipGross),
|
|
607
|
+
discount_amount_net: moneyFromMinor(shipDiscNet),
|
|
608
|
+
discount_amount_gross: moneyFromMinor(shipDiscGross),
|
|
577
609
|
discount_amount_percent: 0,
|
|
578
610
|
};
|
|
579
611
|
});
|
|
@@ -754,7 +786,7 @@ function transformMasterOrder(payload) {
|
|
|
754
786
|
sourceLineMap.set(lineNo, line);
|
|
755
787
|
}
|
|
756
788
|
});
|
|
757
|
-
|
|
789
|
+
allSaleLines.forEach((line) => {
|
|
758
790
|
const lineNo = Number(line.line_no || -1);
|
|
759
791
|
if (lineNo > 0) {
|
|
760
792
|
const existing = sourceLineMap.get(lineNo);
|
|
@@ -779,7 +811,7 @@ function transformMasterOrder(payload) {
|
|
|
779
811
|
}
|
|
780
812
|
}
|
|
781
813
|
});
|
|
782
|
-
|
|
814
|
+
allSaleLines.forEach((line) => {
|
|
783
815
|
if (line.type === "Item") {
|
|
784
816
|
const itemNo = String(line.no || "");
|
|
785
817
|
const variant = String(line.variant_code || "");
|
|
@@ -857,7 +889,7 @@ function transformMasterOrder(payload) {
|
|
|
857
889
|
}
|
|
858
890
|
if (c.includes('MOTO'))
|
|
859
891
|
return 'MOTO';
|
|
860
|
-
if (c.includes('ADYEN') &&
|
|
892
|
+
if (c.includes('ADYEN') && giftCertId.length === 0) {
|
|
861
893
|
return 'CREDIT-CARD';
|
|
862
894
|
}
|
|
863
895
|
const serviceType = String(card?.service_type || '').toUpperCase();
|
|
@@ -884,23 +916,27 @@ function transformMasterOrder(payload) {
|
|
|
884
916
|
order_no: c.order_no,
|
|
885
917
|
captured: c.captured,
|
|
886
918
|
}));
|
|
887
|
-
const normalPays = (payload.payments || []).map((p) =>
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
919
|
+
const normalPays = (payload.payments || []).map((p) => {
|
|
920
|
+
const pc = (p.payment_cards || [])[0];
|
|
921
|
+
return {
|
|
922
|
+
created_at: p.created_at,
|
|
923
|
+
amount: p.amount,
|
|
924
|
+
type: p.type,
|
|
925
|
+
payment_method_code: p.payment_method_code,
|
|
926
|
+
transaction_id: p.transaction_id,
|
|
927
|
+
card_number: pc?.card_number || undefined,
|
|
928
|
+
psp_reference: p.psp_reference,
|
|
929
|
+
payment_gateway_token: p.payment_gateway_token,
|
|
930
|
+
payment_number: p.payment_number || p.payment_no,
|
|
931
|
+
payment_refund_number: p.payment_refund_number || p.payment_reference_no,
|
|
932
|
+
original_payment_refund_id: p.original_payment_refund_id,
|
|
933
|
+
created_by: p.created_by || invoice0?.created_by || sale0?.created_by,
|
|
934
|
+
source: 'payment',
|
|
935
|
+
order_no: p.master_order_no,
|
|
936
|
+
ledger_entry_no: p.ledger_entry_no,
|
|
937
|
+
service_type: pc?.card_type || p.card_type || undefined,
|
|
938
|
+
};
|
|
939
|
+
});
|
|
904
940
|
const flat = [...cardPays, ...normalPays];
|
|
905
941
|
const groupsMap = new Map();
|
|
906
942
|
flat.forEach((p) => {
|
|
@@ -1105,13 +1141,15 @@ function transformMasterOrder(payload) {
|
|
|
1105
1141
|
}
|
|
1106
1142
|
if (originalPaymentForTransaction?.payment_gateway_token) {
|
|
1107
1143
|
const originalCardPayment = cardPaymentsByToken.get(originalPaymentForTransaction.payment_gateway_token);
|
|
1108
|
-
transaction_id = originalCardPayment?.transaction_id || null;
|
|
1144
|
+
transaction_id = originalCardPayment?.transaction_id || originalPaymentForTransaction.payment_gateway_token || null;
|
|
1109
1145
|
}
|
|
1110
1146
|
}
|
|
1111
1147
|
else {
|
|
1112
|
-
transaction_id = cardPaymentEntryForTransaction?.transaction_id || primary.transaction_id || null;
|
|
1148
|
+
transaction_id = cardPaymentEntryForTransaction?.transaction_id || primary.transaction_id || primary.payment_gateway_token || null;
|
|
1113
1149
|
}
|
|
1114
|
-
const psp_reference = transaction_id
|
|
1150
|
+
const psp_reference = transaction_id
|
|
1151
|
+
|| sortedEntries.find(e => e.payment_gateway_token)?.payment_gateway_token
|
|
1152
|
+
|| null;
|
|
1115
1153
|
const paymentCreatedByRaw = invoice0?.created_by || primary.created_by || sale0?.created_by || "";
|
|
1116
1154
|
const paymentCreatedByUpper = String(paymentCreatedByRaw).toUpperCase();
|
|
1117
1155
|
const isEmail = typeof paymentCreatedByRaw === 'string' && paymentCreatedByRaw.includes('@');
|
|
@@ -1130,12 +1168,16 @@ function transformMasterOrder(payload) {
|
|
|
1130
1168
|
card_number = cardPaymentEntryForTransaction.card_number || null;
|
|
1131
1169
|
}
|
|
1132
1170
|
let cardPaymentForBrand = sortedEntries.find(e => e.source === 'card_payment' && e.service_type);
|
|
1171
|
+
if (!cardPaymentForBrand) {
|
|
1172
|
+
cardPaymentForBrand = sortedEntries.find(e => e.service_type);
|
|
1173
|
+
}
|
|
1133
1174
|
if (!cardPaymentForBrand && g.type === 'REFUND' && cardPaymentEntryForTransaction) {
|
|
1134
1175
|
cardPaymentForBrand = cardPaymentEntryForTransaction;
|
|
1135
1176
|
}
|
|
1136
|
-
const
|
|
1177
|
+
const rawBrand = cardPaymentForBrand?.service_type
|
|
1137
1178
|
? String(cardPaymentForBrand.service_type).trim() || null
|
|
1138
1179
|
: null;
|
|
1180
|
+
const card_brand = (pm === 'PAYPAL' || pm === 'GIFT-CARD' || rawBrand === 'VOUCHER') ? null : rawBrand;
|
|
1139
1181
|
let status = 'CAPTURED';
|
|
1140
1182
|
const isVoucher = pm === 'GIFT-CARD' ||
|
|
1141
1183
|
sortedEntries.some(e => e.gift_certificate_id) ||
|
|
@@ -1199,39 +1241,6 @@ function transformMasterOrder(payload) {
|
|
|
1199
1241
|
};
|
|
1200
1242
|
});
|
|
1201
1243
|
const shipmentsWithItems = shipments.filter((s) => s.items && s.items.length > 0);
|
|
1202
|
-
shipmentsWithItems.forEach((shipment) => {
|
|
1203
|
-
let shipmentNetMinor = 0;
|
|
1204
|
-
let shipmentGrossMinor = 0;
|
|
1205
|
-
let shipmentDiscountNetMinor = 0;
|
|
1206
|
-
let shipmentDiscountGrossMinor = 0;
|
|
1207
|
-
let maxDiscountPercent = 0;
|
|
1208
|
-
(shipment.items || []).forEach((shipItem) => {
|
|
1209
|
-
const sku = shipItem.sku;
|
|
1210
|
-
const shipQty = Number(shipItem.quantity || 0);
|
|
1211
|
-
const matchingItems = itemsList.filter((item) => item.sku === sku);
|
|
1212
|
-
let remainingQty = shipQty;
|
|
1213
|
-
for (const item of matchingItems) {
|
|
1214
|
-
if (remainingQty <= 0)
|
|
1215
|
-
break;
|
|
1216
|
-
const itemQty = Number(item.quantity || 0);
|
|
1217
|
-
if (itemQty > 0) {
|
|
1218
|
-
const takeQty = Math.min(remainingQty, itemQty);
|
|
1219
|
-
const ratio = takeQty / itemQty;
|
|
1220
|
-
shipmentNetMinor += item.amount_net.value * ratio;
|
|
1221
|
-
shipmentGrossMinor += item.amount_gross.value * ratio;
|
|
1222
|
-
shipmentDiscountNetMinor += item.discount_amount_net.value * ratio;
|
|
1223
|
-
shipmentDiscountGrossMinor += item.discount_amount_gross.value * ratio;
|
|
1224
|
-
maxDiscountPercent = Math.max(maxDiscountPercent, item.discount_amount_percent || 0);
|
|
1225
|
-
remainingQty -= takeQty;
|
|
1226
|
-
}
|
|
1227
|
-
}
|
|
1228
|
-
});
|
|
1229
|
-
shipment.amount_net = moneyFromMinor(shipmentNetMinor);
|
|
1230
|
-
shipment.amount_gross = moneyFromMinor(shipmentGrossMinor);
|
|
1231
|
-
shipment.discount_amount_net = moneyFromMinor(shipmentDiscountNetMinor);
|
|
1232
|
-
shipment.discount_amount_gross = moneyFromMinor(shipmentDiscountGrossMinor);
|
|
1233
|
-
shipment.discount_amount_percent = maxDiscountPercent;
|
|
1234
|
-
});
|
|
1235
1244
|
let totalDiscountNetMinor = 0;
|
|
1236
1245
|
let totalDiscountGrossMinor = 0;
|
|
1237
1246
|
let maxTotalDiscountPercent = 0;
|
|
@@ -1252,45 +1261,51 @@ function transformMasterOrder(payload) {
|
|
|
1252
1261
|
centra_order_id,
|
|
1253
1262
|
sfcc_order_id,
|
|
1254
1263
|
globale_order_id: (() => {
|
|
1255
|
-
const v = (invoice0?.globale_order_id ?? sale0?.globale_order_id ?? null);
|
|
1264
|
+
const v = (invoice0?.globale_order_id ?? sale0?.globale_order_id ?? payload.glopal_order_id ?? null);
|
|
1256
1265
|
if (typeof v !== 'string')
|
|
1257
1266
|
return v;
|
|
1258
1267
|
const s = v.trim();
|
|
1259
1268
|
return s.length > 0 ? s : null;
|
|
1260
1269
|
})(),
|
|
1261
|
-
sitoo_order_id:
|
|
1270
|
+
sitoo_order_id: (() => {
|
|
1271
|
+
const v = payload.sitoo_order_id ?? sale0?.sitoo_order_id ?? null;
|
|
1272
|
+
if (v === null || v === undefined)
|
|
1273
|
+
return null;
|
|
1274
|
+
const s = String(v).trim();
|
|
1275
|
+
return s.length > 0 ? s : null;
|
|
1276
|
+
})(),
|
|
1262
1277
|
system_created_by,
|
|
1263
1278
|
ordered_at,
|
|
1264
1279
|
is_gift,
|
|
1265
1280
|
is_pos: created_by_raw === "SITOO",
|
|
1266
1281
|
billing_address: billing_from ? (() => {
|
|
1267
1282
|
const billToName = billing_from.bill_to_name || "";
|
|
1268
|
-
const firstName = billing_from.bill_to_first_name || (billToName ? billToName.split(' ')[0] : "");
|
|
1269
|
-
const lastName = billing_from.bill_to_surname || (billToName ? billToName.split(' ').slice(1).join(' ') : "");
|
|
1283
|
+
const firstName = trimAddr(billing_from.bill_to_first_name) || trimAddr(billToName ? billToName.split(' ')[0] : "");
|
|
1284
|
+
const lastName = trimAddr(billing_from.bill_to_surname) || trimAddr(billToName ? billToName.split(' ').slice(1).join(' ') : "");
|
|
1270
1285
|
return {
|
|
1271
|
-
first_name: firstName,
|
|
1272
|
-
last_name: lastName,
|
|
1273
|
-
address1: billing_from.bill_to_address || "",
|
|
1274
|
-
address2: billing_from.bill_to_address_2 || null,
|
|
1275
|
-
city: billing_from.bill_to_city ||
|
|
1276
|
-
post_code: billing_from.bill_to_post_code || null,
|
|
1277
|
-
country_code: billing_from.bill_to_country_region_code || "",
|
|
1278
|
-
phone: billing_from.sell_to_phone_no || null,
|
|
1286
|
+
first_name: firstName || null,
|
|
1287
|
+
last_name: lastName || null,
|
|
1288
|
+
address1: trimAddr(billing_from.bill_to_address) || "",
|
|
1289
|
+
address2: trimAddr(billing_from.bill_to_address_2) || null,
|
|
1290
|
+
city: trimAddr(billing_from.bill_to_city) || null,
|
|
1291
|
+
post_code: trimAddr(billing_from.bill_to_post_code) || null,
|
|
1292
|
+
country_code: trimAddr(billing_from.bill_to_country_region_code) || "",
|
|
1293
|
+
phone: trimAddr(billing_from.sell_to_phone_no) || null,
|
|
1279
1294
|
created_at: (typeof billing_from.system_created_at === 'string' && billing_from.system_created_at) || undefined,
|
|
1280
1295
|
};
|
|
1281
1296
|
})() : null,
|
|
1282
1297
|
shipping_address: shipping_from ? (() => {
|
|
1283
1298
|
const shipToName = shipping_from.ship_to_name || "";
|
|
1284
|
-
const firstName = shipping_from.ship_to_first_name || (shipToName ? shipToName.split(' ')[0] : "");
|
|
1285
|
-
const lastName = shipping_from.ship_to_surname || (shipToName ? shipToName.split(' ').slice(1).join(' ') : "");
|
|
1299
|
+
const firstName = trimAddr(shipping_from.ship_to_first_name) || trimAddr(shipToName ? shipToName.split(' ')[0] : "");
|
|
1300
|
+
const lastName = trimAddr(shipping_from.ship_to_surname) || trimAddr(shipToName ? shipToName.split(' ').slice(1).join(' ') : "");
|
|
1286
1301
|
return {
|
|
1287
|
-
first_name: firstName,
|
|
1288
|
-
last_name: lastName,
|
|
1289
|
-
address1: shipping_from.ship_to_address || "",
|
|
1290
|
-
address2: shipping_from.ship_to_address_2 || null,
|
|
1291
|
-
city: shipping_from.ship_to_city ||
|
|
1292
|
-
post_code: shipping_from.ship_to_post_code || null,
|
|
1293
|
-
country_code: shipping_from.ship_to_country_region_code || "",
|
|
1302
|
+
first_name: firstName || null,
|
|
1303
|
+
last_name: lastName || null,
|
|
1304
|
+
address1: trimAddr(shipping_from.ship_to_address) || "",
|
|
1305
|
+
address2: trimAddr(shipping_from.ship_to_address_2) || null,
|
|
1306
|
+
city: trimAddr(shipping_from.ship_to_city) || null,
|
|
1307
|
+
post_code: trimAddr(shipping_from.ship_to_post_code) || null,
|
|
1308
|
+
country_code: trimAddr(shipping_from.ship_to_country_region_code) || "",
|
|
1294
1309
|
phone: shipping_from.ship_to_phone_no || null,
|
|
1295
1310
|
created_at: (typeof shipping_from.system_created_at === 'string' && shipping_from.system_created_at) || undefined,
|
|
1296
1311
|
};
|
|
@@ -1313,14 +1328,14 @@ function transformMasterOrder(payload) {
|
|
|
1313
1328
|
if (c.no || payload.customer_no)
|
|
1314
1329
|
mapped.bc_customer_id = String(c.no || payload.customer_no);
|
|
1315
1330
|
const buildAddr = (src) => src ? {
|
|
1316
|
-
first_name: src.bill_to_first_name || src.ship_to_first_name ||
|
|
1317
|
-
last_name: src.bill_to_surname || src.ship_to_surname ||
|
|
1318
|
-
address1: (src.bill_to_address || src.ship_to_address || ""
|
|
1319
|
-
address2: (src.bill_to_address_2 || src.ship_to_address_2 || null
|
|
1320
|
-
city: (src.bill_to_city || src.ship_to_city ||
|
|
1321
|
-
post_code: (src.bill_to_post_code || src.ship_to_post_code || null
|
|
1322
|
-
country_code: (src.bill_to_country_region_code || src.ship_to_country_region_code || ""
|
|
1323
|
-
phone: (src.sell_to_phone_no || src.ship_to_phone_no || null
|
|
1331
|
+
first_name: trimAddr(src.bill_to_first_name || src.ship_to_first_name) || null,
|
|
1332
|
+
last_name: trimAddr(src.bill_to_surname || src.ship_to_surname) || null,
|
|
1333
|
+
address1: trimAddr(src.bill_to_address || src.ship_to_address) || "",
|
|
1334
|
+
address2: trimAddr(src.bill_to_address_2 || src.ship_to_address_2) || null,
|
|
1335
|
+
city: trimAddr(src.bill_to_city || src.ship_to_city) || null,
|
|
1336
|
+
post_code: trimAddr(src.bill_to_post_code || src.ship_to_post_code) || null,
|
|
1337
|
+
country_code: trimAddr(src.bill_to_country_region_code || src.ship_to_country_region_code) || "",
|
|
1338
|
+
phone: trimAddr(src.sell_to_phone_no || src.ship_to_phone_no) || null,
|
|
1324
1339
|
created_at: (typeof src.system_created_at === 'string' && src.system_created_at) || undefined,
|
|
1325
1340
|
} : null;
|
|
1326
1341
|
const custBilling = buildAddr(billing_from);
|