@shushed/helpers 0.0.290 → 0.0.292
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.
|
@@ -7,6 +7,7 @@ exports.BCOrderHelper = void 0;
|
|
|
7
7
|
exports.buildLineGrouping = buildLineGrouping;
|
|
8
8
|
exports.transformMasterOrder = transformMasterOrder;
|
|
9
9
|
exports.validateOrder = validateOrder;
|
|
10
|
+
const currency_js_1 = __importDefault(require("currency.js"));
|
|
10
11
|
const order_schema_json_1 = __importDefault(require("../contracts/order.schema.json"));
|
|
11
12
|
const runtime_1 = __importDefault(require("./runtime"));
|
|
12
13
|
const validate_1 = __importDefault(require("./validate"));
|
|
@@ -14,10 +15,7 @@ const GBP = "GBP";
|
|
|
14
15
|
function toMinorUnits(amount) {
|
|
15
16
|
if (typeof amount !== "number" || !isFinite(amount))
|
|
16
17
|
return 0;
|
|
17
|
-
|
|
18
|
-
return Math.round(Number((amount * 100).toPrecision(15)));
|
|
19
|
-
}
|
|
20
|
-
return Math.round(amount * 100);
|
|
18
|
+
return (0, currency_js_1.default)(amount).intValue;
|
|
21
19
|
}
|
|
22
20
|
function money(value) {
|
|
23
21
|
const v = toMinorUnits(value);
|
|
@@ -300,30 +298,30 @@ function transformMasterOrder(payload) {
|
|
|
300
298
|
vatRate = vatPercent / 100;
|
|
301
299
|
}
|
|
302
300
|
else if (netTotal > 0 && grossTotal > 0) {
|
|
303
|
-
vatRate = Math.max(0, (grossTotal
|
|
301
|
+
vatRate = Math.max(0, (0, currency_js_1.default)(grossTotal).subtract(netTotal).divide(netTotal).value);
|
|
304
302
|
}
|
|
305
303
|
let unitNet = 0;
|
|
306
304
|
let unitGross = 0;
|
|
307
305
|
if (unitPriceRaw > 0) {
|
|
308
306
|
if (headerPricesInclVat) {
|
|
309
307
|
unitGross = unitPriceRaw;
|
|
310
|
-
unitNet = vatRate > 0 ? (
|
|
308
|
+
unitNet = vatRate > 0 ? (0, currency_js_1.default)(unitGross).divide(1 + vatRate).value : unitGross;
|
|
311
309
|
}
|
|
312
310
|
else {
|
|
313
311
|
unitNet = unitPriceRaw;
|
|
314
|
-
unitGross = vatRate > 0 ? (
|
|
312
|
+
unitGross = vatRate > 0 ? (0, currency_js_1.default)(unitNet).multiply(1 + vatRate).value : unitNet;
|
|
315
313
|
}
|
|
316
314
|
}
|
|
317
315
|
else {
|
|
318
316
|
unitNet = quantity > 0
|
|
319
317
|
? (netTotal > 0
|
|
320
|
-
? netTotal
|
|
321
|
-
: (grossTotal > 0 && vatRate > 0 ? (
|
|
318
|
+
? (0, currency_js_1.default)(netTotal).divide(quantity).value
|
|
319
|
+
: (grossTotal > 0 && vatRate > 0 ? (0, currency_js_1.default)(grossTotal).divide(1 + vatRate).divide(quantity).value : 0))
|
|
322
320
|
: 0;
|
|
323
321
|
unitGross = quantity > 0
|
|
324
322
|
? (grossTotal > 0
|
|
325
|
-
? grossTotal
|
|
326
|
-
: (netTotal > 0 && vatRate > 0 ? (
|
|
323
|
+
? (0, currency_js_1.default)(grossTotal).divide(quantity).value
|
|
324
|
+
: (netTotal > 0 && vatRate > 0 ? (0, currency_js_1.default)(netTotal).multiply(1 + vatRate).divide(quantity).value : 0))
|
|
327
325
|
: 0;
|
|
328
326
|
}
|
|
329
327
|
const discountCode = (line.salesforce_promotion_id && line.salesforce_promotion_id.trim().length > 0)
|
|
@@ -344,22 +342,22 @@ function transformMasterOrder(payload) {
|
|
|
344
342
|
if (discountAmountRaw > 0) {
|
|
345
343
|
if (headerPricesInclVat) {
|
|
346
344
|
discountGross = discountAmountRaw;
|
|
347
|
-
discountNet = vatRate > 0 ? (
|
|
345
|
+
discountNet = vatRate > 0 ? (0, currency_js_1.default)(discountGross).divide(1 + vatRate).value : discountGross;
|
|
348
346
|
}
|
|
349
347
|
else {
|
|
350
348
|
discountNet = discountAmountRaw;
|
|
351
|
-
discountGross = vatRate > 0 ? (
|
|
349
|
+
discountGross = vatRate > 0 ? (0, currency_js_1.default)(discountNet).multiply(1 + vatRate).value : discountNet;
|
|
352
350
|
}
|
|
353
351
|
}
|
|
354
352
|
else if (discountPercent > 0) {
|
|
355
353
|
if (netTotal > 0)
|
|
356
|
-
discountNet = (netTotal
|
|
354
|
+
discountNet = (0, currency_js_1.default)(netTotal).multiply(discountPercent).divide(100).value;
|
|
357
355
|
if (grossTotal > 0)
|
|
358
|
-
discountGross = (grossTotal
|
|
356
|
+
discountGross = (0, currency_js_1.default)(grossTotal).multiply(discountPercent).divide(100).value;
|
|
359
357
|
if (grossTotal === 0 && discountNet > 0 && vatRate > 0)
|
|
360
|
-
discountGross =
|
|
358
|
+
discountGross = (0, currency_js_1.default)(discountNet).multiply(1 + vatRate).value;
|
|
361
359
|
if (netTotal === 0 && discountGross > 0 && vatRate > 0)
|
|
362
|
-
discountNet =
|
|
360
|
+
discountNet = (0, currency_js_1.default)(discountGross).divide(1 + vatRate).value;
|
|
363
361
|
}
|
|
364
362
|
const take = Math.min(shippedRemainingBySku[sku] || 0, quantity);
|
|
365
363
|
const remaining = quantity - take;
|
|
@@ -383,10 +381,10 @@ function transformMasterOrder(payload) {
|
|
|
383
381
|
...baseFields,
|
|
384
382
|
quantity: qty,
|
|
385
383
|
returned_quantity: Math.max(0, Number(returnedQtyForLine || 0)),
|
|
386
|
-
amount_net: money(unitNet
|
|
387
|
-
amount_gross: money(unitGross
|
|
388
|
-
discount_amount_net: money(
|
|
389
|
-
discount_amount_gross: money(
|
|
384
|
+
amount_net: money((0, currency_js_1.default)(unitNet).multiply(qty).value),
|
|
385
|
+
amount_gross: money((0, currency_js_1.default)(unitGross).multiply(qty).value),
|
|
386
|
+
discount_amount_net: money((0, currency_js_1.default)(discountNet).multiply(qty).divide(Math.max(1, quantity)).value),
|
|
387
|
+
discount_amount_gross: money((0, currency_js_1.default)(discountGross).multiply(qty).divide(Math.max(1, quantity)).value),
|
|
390
388
|
discount_amount_percent: line.line_discount_percent || 0,
|
|
391
389
|
});
|
|
392
390
|
};
|
|
@@ -422,7 +420,7 @@ function transformMasterOrder(payload) {
|
|
|
422
420
|
});
|
|
423
421
|
});
|
|
424
422
|
if (invoicesGrossMinor === 0 && (payload.invoices || []).length > 0) {
|
|
425
|
-
invoicesGrossMinor = toMinorUnits((payload.invoices || []).reduce((acc, inv) =>
|
|
423
|
+
invoicesGrossMinor = toMinorUnits((payload.invoices || []).reduce((acc, inv) => (0, currency_js_1.default)(acc).add(inv.amount_incl_vat || 0).value, 0));
|
|
426
424
|
}
|
|
427
425
|
if (invoicesGrossMinor === 0 && invoicedLineNos.size > 0 && allSaleLines.length > 0) {
|
|
428
426
|
const invoicedSaleLineItems = allSaleLines.filter((l) => {
|
|
@@ -433,7 +431,7 @@ function transformMasterOrder(payload) {
|
|
|
433
431
|
const lineNo = Number(l?.line_no || -1);
|
|
434
432
|
return invoicedLineNos.has(lineNo);
|
|
435
433
|
});
|
|
436
|
-
invoicesGrossMinor = toMinorUnits(invoicedSaleLineItems.reduce((acc, l) =>
|
|
434
|
+
invoicesGrossMinor = toMinorUnits(invoicedSaleLineItems.reduce((acc, l) => (0, currency_js_1.default)(acc).add(Number(l?.amount_including_vat || 0)).value, 0));
|
|
437
435
|
}
|
|
438
436
|
let nonInvSalesGrossMinor = 0;
|
|
439
437
|
if (allSaleLines.length > 0) {
|
|
@@ -444,7 +442,7 @@ function transformMasterOrder(payload) {
|
|
|
444
442
|
return false;
|
|
445
443
|
return true;
|
|
446
444
|
});
|
|
447
|
-
const totalSaleGrossMinor = toMinorUnits(saleLinesNoDelivery.reduce((acc, l) =>
|
|
445
|
+
const totalSaleGrossMinor = toMinorUnits(saleLinesNoDelivery.reduce((acc, l) => (0, currency_js_1.default)(acc).add(Number(l?.amount_including_vat || 0)).value, 0));
|
|
448
446
|
if (invoicesGrossMinor > 0) {
|
|
449
447
|
nonInvSalesGrossMinor = Math.max(0, totalSaleGrossMinor - invoicesGrossMinor);
|
|
450
448
|
}
|
|
@@ -453,7 +451,7 @@ function transformMasterOrder(payload) {
|
|
|
453
451
|
}
|
|
454
452
|
}
|
|
455
453
|
else if (sale0 && (sale0.amount_excl_vat || sale0.amount_incl_vat)) {
|
|
456
|
-
const saleGrossMinor = toMinorUnits(allSales.reduce((acc, s) =>
|
|
454
|
+
const saleGrossMinor = toMinorUnits(allSales.reduce((acc, s) => (0, currency_js_1.default)(acc).add(s.amount_incl_vat || 0).value, 0));
|
|
457
455
|
if (invoicesGrossMinor > 0) {
|
|
458
456
|
nonInvSalesGrossMinor = Math.max(0, saleGrossMinor - invoicesGrossMinor);
|
|
459
457
|
}
|
|
@@ -470,11 +468,11 @@ function transformMasterOrder(payload) {
|
|
|
470
468
|
const lineNo = Number(l?.line_no || -1);
|
|
471
469
|
return !invoicedLineNos.has(lineNo);
|
|
472
470
|
});
|
|
473
|
-
nonInvSalesGrossMinor = toMinorUnits(nonInvoicedSaleItems.reduce((acc, l) =>
|
|
471
|
+
nonInvSalesGrossMinor = toMinorUnits(nonInvoicedSaleItems.reduce((acc, l) => (0, currency_js_1.default)(acc).add(Number(l?.amount_including_vat || 0)).value, 0));
|
|
474
472
|
}
|
|
475
473
|
const totalGrossMinor = invoicesGrossMinor + nonInvSalesGrossMinor;
|
|
476
|
-
const invoicesNetMinorWithDelivery = toMinorUnits((payload.invoices || []).reduce((acc, inv) =>
|
|
477
|
-
const invoicesGrossMinorWithDelivery = toMinorUnits((payload.invoices || []).reduce((acc, inv) =>
|
|
474
|
+
const invoicesNetMinorWithDelivery = toMinorUnits((payload.invoices || []).reduce((acc, inv) => (0, currency_js_1.default)(acc).add(inv.amount_excl_vat || 0).value, 0));
|
|
475
|
+
const invoicesGrossMinorWithDelivery = toMinorUnits((payload.invoices || []).reduce((acc, inv) => (0, currency_js_1.default)(acc).add(inv.amount_incl_vat || 0).value, 0));
|
|
478
476
|
let nonInvSalesNetMinorWithDelivery = 0;
|
|
479
477
|
let nonInvSalesGrossMinorWithDelivery = 0;
|
|
480
478
|
if (allSaleLines.length > 0) {
|
|
@@ -483,8 +481,8 @@ function transformMasterOrder(payload) {
|
|
|
483
481
|
return false;
|
|
484
482
|
return true;
|
|
485
483
|
});
|
|
486
|
-
const totalSaleNetMinorWithDelivery = toMinorUnits(saleLinesWithDelivery.reduce((acc, l) =>
|
|
487
|
-
const totalSaleGrossMinorWithDelivery = toMinorUnits(saleLinesWithDelivery.reduce((acc, l) =>
|
|
484
|
+
const totalSaleNetMinorWithDelivery = toMinorUnits(saleLinesWithDelivery.reduce((acc, l) => (0, currency_js_1.default)(acc).add(Number(l?.amount || 0)).value, 0));
|
|
485
|
+
const totalSaleGrossMinorWithDelivery = toMinorUnits(saleLinesWithDelivery.reduce((acc, l) => (0, currency_js_1.default)(acc).add(Number(l?.amount_including_vat || 0)).value, 0));
|
|
488
486
|
if (invoicesGrossMinorWithDelivery > 0) {
|
|
489
487
|
nonInvSalesNetMinorWithDelivery = Math.max(0, totalSaleNetMinorWithDelivery - invoicesNetMinorWithDelivery);
|
|
490
488
|
nonInvSalesGrossMinorWithDelivery = Math.max(0, totalSaleGrossMinorWithDelivery - invoicesGrossMinorWithDelivery);
|
|
@@ -495,8 +493,8 @@ function transformMasterOrder(payload) {
|
|
|
495
493
|
}
|
|
496
494
|
}
|
|
497
495
|
else if (sale0 && (sale0.amount_excl_vat || sale0.amount_incl_vat)) {
|
|
498
|
-
const saleNetMinor = toMinorUnits(allSales.reduce((acc, s) =>
|
|
499
|
-
const saleGrossMinor = toMinorUnits(allSales.reduce((acc, s) =>
|
|
496
|
+
const saleNetMinor = toMinorUnits(allSales.reduce((acc, s) => (0, currency_js_1.default)(acc).add(s.amount_excl_vat || 0).value, 0));
|
|
497
|
+
const saleGrossMinor = toMinorUnits(allSales.reduce((acc, s) => (0, currency_js_1.default)(acc).add(s.amount_incl_vat || 0).value, 0));
|
|
500
498
|
if (invoicesGrossMinorWithDelivery > 0) {
|
|
501
499
|
nonInvSalesNetMinorWithDelivery = Math.max(0, saleNetMinor - invoicesNetMinorWithDelivery);
|
|
502
500
|
nonInvSalesGrossMinorWithDelivery = Math.max(0, saleGrossMinor - invoicesGrossMinorWithDelivery);
|
|
@@ -513,8 +511,8 @@ function transformMasterOrder(payload) {
|
|
|
513
511
|
const lineNo = Number(l?.line_no || -1);
|
|
514
512
|
return !invoicedLineNos.has(lineNo);
|
|
515
513
|
});
|
|
516
|
-
nonInvSalesNetMinorWithDelivery = toMinorUnits(nonInvoicedSalesWithDelivery.reduce((acc, l) =>
|
|
517
|
-
nonInvSalesGrossMinorWithDelivery = toMinorUnits(nonInvoicedSalesWithDelivery.reduce((acc, l) =>
|
|
514
|
+
nonInvSalesNetMinorWithDelivery = toMinorUnits(nonInvoicedSalesWithDelivery.reduce((acc, l) => (0, currency_js_1.default)(acc).add(Number(l?.amount || 0)).value, 0));
|
|
515
|
+
nonInvSalesGrossMinorWithDelivery = toMinorUnits(nonInvoicedSalesWithDelivery.reduce((acc, l) => (0, currency_js_1.default)(acc).add(Number(l?.amount_including_vat || 0)).value, 0));
|
|
518
516
|
}
|
|
519
517
|
const totalNetMinorWithDelivery = invoicesNetMinorWithDelivery + nonInvSalesNetMinorWithDelivery;
|
|
520
518
|
const totalGrossMinorWithDelivery = invoicesGrossMinorWithDelivery + nonInvSalesGrossMinorWithDelivery;
|
|
@@ -564,7 +562,7 @@ function transformMasterOrder(payload) {
|
|
|
564
562
|
deliveryNetMinor += net;
|
|
565
563
|
const lineDiscGross = toMinorUnits(Number(line?.line_discount_amount || 0));
|
|
566
564
|
const lineDiscNet = headerPricesInclVat
|
|
567
|
-
?
|
|
565
|
+
? (0, currency_js_1.default)(lineDiscGross, { fromCents: true }).divide(1.2).intValue
|
|
568
566
|
: lineDiscGross;
|
|
569
567
|
deliveryDiscountGrossMinor += lineDiscGross;
|
|
570
568
|
deliveryDiscountNetMinor += lineDiscNet;
|
|
@@ -692,7 +690,7 @@ function transformMasterOrder(payload) {
|
|
|
692
690
|
const isRefund = itemNo.toUpperCase() === 'REFUND';
|
|
693
691
|
if ((l?.quantity || 0) > 0 && isItemish && !isRefund && !isDeliveryItem(l)) {
|
|
694
692
|
hasQualifying = true;
|
|
695
|
-
return
|
|
693
|
+
return (0, currency_js_1.default)(s).add(l?.amount_including_vat || 0).value;
|
|
696
694
|
}
|
|
697
695
|
return s;
|
|
698
696
|
}, 0);
|
|
@@ -704,16 +702,16 @@ function transformMasterOrder(payload) {
|
|
|
704
702
|
const isItemish = typeStr.includes('item') || looksProduct;
|
|
705
703
|
const isRefund = itemNo.toUpperCase() === 'REFUND';
|
|
706
704
|
if ((l?.quantity || 0) > 0 && isItemish && !isRefund && !isDeliveryItem(l)) {
|
|
707
|
-
return
|
|
705
|
+
return (0, currency_js_1.default)(s).add(l?.amount || l?.amount_excl_vat || 0).value;
|
|
708
706
|
}
|
|
709
707
|
return s;
|
|
710
708
|
}, 0);
|
|
711
709
|
if (!hasQualifying)
|
|
712
710
|
return acc;
|
|
713
711
|
if (cm?.amount_incl_vat != null && cm.amount_incl_vat > 0) {
|
|
714
|
-
return { gross: acc.gross
|
|
712
|
+
return { gross: (0, currency_js_1.default)(acc.gross).add(cm.amount_incl_vat).value, net: (0, currency_js_1.default)(acc.net).add(cm.amount_excl_vat || 0).value };
|
|
715
713
|
}
|
|
716
|
-
return { gross: acc.gross
|
|
714
|
+
return { gross: (0, currency_js_1.default)(acc.gross).add(sumGross).value, net: (0, currency_js_1.default)(acc.net).add(sumNet).value };
|
|
717
715
|
}, { gross: 0, net: 0 });
|
|
718
716
|
const merchReturnGrossFromReturnSales = (payload.sales || []).reduce((acc, sale) => {
|
|
719
717
|
const docTypeRaw = String(sale?.document_type || sale?.documentType || "");
|
|
@@ -721,7 +719,7 @@ function transformMasterOrder(payload) {
|
|
|
721
719
|
if (!isReturnOrder)
|
|
722
720
|
return acc;
|
|
723
721
|
if (sale?.amount_incl_vat != null && sale.amount_incl_vat > 0) {
|
|
724
|
-
return { gross: acc.gross
|
|
722
|
+
return { gross: (0, currency_js_1.default)(acc.gross).add(sale.amount_incl_vat).value, net: (0, currency_js_1.default)(acc.net).add(sale.amount_excl_vat || sale.amount || 0).value };
|
|
725
723
|
}
|
|
726
724
|
const lines = sale?.sale_lines || [];
|
|
727
725
|
let hasQualifying = false;
|
|
@@ -734,7 +732,7 @@ function transformMasterOrder(payload) {
|
|
|
734
732
|
const isRefund = itemNo.toUpperCase() === 'REFUND';
|
|
735
733
|
if ((l?.quantity || 0) > 0 && isItemish && !isRefund && !isDeliveryItem(l)) {
|
|
736
734
|
hasQualifying = true;
|
|
737
|
-
return
|
|
735
|
+
return (0, currency_js_1.default)(s).add(l?.amount_including_vat || 0).value;
|
|
738
736
|
}
|
|
739
737
|
return s;
|
|
740
738
|
}, 0);
|
|
@@ -746,22 +744,22 @@ function transformMasterOrder(payload) {
|
|
|
746
744
|
const isItemish = typeStr.includes('item') || looksProduct;
|
|
747
745
|
const isRefund = itemNo.toUpperCase() === 'REFUND';
|
|
748
746
|
if ((l?.quantity || 0) > 0 && isItemish && !isRefund && !isDeliveryItem(l)) {
|
|
749
|
-
return
|
|
747
|
+
return (0, currency_js_1.default)(s).add(l?.amount || l?.amount_excl_vat || 0).value;
|
|
750
748
|
}
|
|
751
749
|
return s;
|
|
752
750
|
}, 0);
|
|
753
751
|
if (sumGross > 0) {
|
|
754
|
-
return { gross: acc.gross
|
|
752
|
+
return { gross: (0, currency_js_1.default)(acc.gross).add(sumGross).value, net: (0, currency_js_1.default)(acc.net).add(sumNet).value };
|
|
755
753
|
}
|
|
756
754
|
if (hasQualifying) {
|
|
757
|
-
return { gross: acc.gross
|
|
755
|
+
return { gross: (0, currency_js_1.default)(acc.gross).add(sale?.amount_incl_vat || sale?.amount_including_vat || 0).value, net: (0, currency_js_1.default)(acc.net).add(sale?.amount_excl_vat || sale?.amount || 0).value };
|
|
758
756
|
}
|
|
759
757
|
return acc;
|
|
760
758
|
}, { gross: 0, net: 0 });
|
|
761
|
-
const merchReturnGross = merchReturnGrossFromCreditMemos.gross
|
|
759
|
+
const merchReturnGross = (0, currency_js_1.default)(merchReturnGrossFromCreditMemos.gross).add(merchReturnGrossFromReturnSales.gross).value;
|
|
762
760
|
const hasMerchReturn = merchReturnGross > 0;
|
|
763
|
-
const returnsNetMinor = toMinorUnits((payload.credit_memos || []).reduce((acc, cm) =>
|
|
764
|
-
const returnsGrossMinor = toMinorUnits((payload.credit_memos || []).reduce((acc, cm) =>
|
|
761
|
+
const returnsNetMinor = toMinorUnits((payload.credit_memos || []).reduce((acc, cm) => (0, currency_js_1.default)(acc).add(cm.amount_excl_vat || 0).value, 0));
|
|
762
|
+
const returnsGrossMinor = toMinorUnits((payload.credit_memos || []).reduce((acc, cm) => (0, currency_js_1.default)(acc).add(cm.amount_incl_vat || 0).value, 0));
|
|
765
763
|
const return_total = (returnsGrossMinor > 0)
|
|
766
764
|
? {
|
|
767
765
|
amount_net: moneyFromMinor(returnsNetMinor),
|
|
@@ -829,7 +827,7 @@ function transformMasterOrder(payload) {
|
|
|
829
827
|
});
|
|
830
828
|
const shippedGrossDecimal = (payload.shipments || []).reduce((acc, s) => {
|
|
831
829
|
if (s.amount_incl_vat != null && s.amount_incl_vat > 0) {
|
|
832
|
-
return
|
|
830
|
+
return (0, currency_js_1.default)(acc).add(s.amount_incl_vat || 0).value;
|
|
833
831
|
}
|
|
834
832
|
const lineAmounts = (s.shipment_lines || []).reduce((lineAcc, l) => {
|
|
835
833
|
if (!isShippableItem(l))
|
|
@@ -847,18 +845,18 @@ function transformMasterOrder(payload) {
|
|
|
847
845
|
const sourceGross = Number(sourceLine.amount_including_vat || sourceLine.amount_incl_vat || 0);
|
|
848
846
|
const shippedQty = Number(l?.quantity || 0);
|
|
849
847
|
if (sourceQty > 0 && sourceGross > 0) {
|
|
850
|
-
const unitPrice = sourceGross
|
|
851
|
-
return lineAcc
|
|
848
|
+
const unitPrice = (0, currency_js_1.default)(sourceGross).divide(sourceQty).value;
|
|
849
|
+
return (0, currency_js_1.default)(lineAcc).add((0, currency_js_1.default)(unitPrice).multiply(shippedQty)).value;
|
|
852
850
|
}
|
|
853
851
|
}
|
|
854
|
-
return
|
|
852
|
+
return (0, currency_js_1.default)(lineAcc).add(l?.amount_including_vat || l?.amount_incl_vat || 0).value;
|
|
855
853
|
}, 0);
|
|
856
|
-
return acc
|
|
854
|
+
return (0, currency_js_1.default)(acc).add(lineAmounts).value;
|
|
857
855
|
}, 0);
|
|
858
856
|
const shippedGrossMinor = toMinorUnits(shippedGrossDecimal);
|
|
859
857
|
let status = "PROCESSED";
|
|
860
858
|
if (hasMerchReturn) {
|
|
861
|
-
const totalOrderGrossDecimal = totalOrderGrossMinor
|
|
859
|
+
const totalOrderGrossDecimal = (0, currency_js_1.default)(totalOrderGrossMinor, { fromCents: true }).value;
|
|
862
860
|
status = totalOrderGrossDecimal > 0 && merchReturnGross >= totalOrderGrossDecimal ? "RETURNED" : "PARTIALLY-RETURNED";
|
|
863
861
|
}
|
|
864
862
|
else if (shippedGrossMinor > 0 && totalOrderGrossMinor > 0) {
|
|
@@ -940,60 +938,48 @@ function transformMasterOrder(payload) {
|
|
|
940
938
|
const flat = [...cardPays, ...normalPays];
|
|
941
939
|
const groupsMap = new Map();
|
|
942
940
|
flat.forEach((p) => {
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
941
|
+
const isRefund = String(p.type || '').toLowerCase() === 'refund';
|
|
942
|
+
const logicalType = isRefund ? 'REFUND' : 'CHARGE';
|
|
943
|
+
let keyBase;
|
|
944
|
+
const unifiedId = p.transaction_id || p.payment_gateway_token;
|
|
945
|
+
if (unifiedId) {
|
|
946
|
+
keyBase = unifiedId;
|
|
947
|
+
}
|
|
948
|
+
else if (p.payment_number) {
|
|
949
|
+
keyBase = `PN:${p.payment_number}`;
|
|
950
|
+
}
|
|
951
|
+
else if (!isRefund) {
|
|
952
|
+
const paymentType = String(p.type || '').toUpperCase();
|
|
953
|
+
const amount = Number(p.amount || 0);
|
|
954
|
+
const paymentMethod = String(p.payment_method_code || '').toUpperCase().trim();
|
|
955
|
+
const orderNo = p.order_no || erp_order_id;
|
|
956
|
+
if (paymentType === 'CAPTURE' || paymentType === 'AUTHORISATION') {
|
|
957
|
+
keyBase = `CHARGE-${orderNo}-${amount}-${paymentMethod}`;
|
|
950
958
|
}
|
|
951
|
-
else if (p.
|
|
952
|
-
keyBase = `
|
|
959
|
+
else if (p.gift_certificate_id) {
|
|
960
|
+
keyBase = `GIFT:${p.gift_certificate_id}`;
|
|
953
961
|
}
|
|
954
|
-
else
|
|
955
|
-
const paymentType = String(p.type || '').toUpperCase();
|
|
956
|
-
const amount = Number(p.amount || 0);
|
|
957
|
-
const paymentMethod = String(p.payment_method_code || '').toUpperCase().trim();
|
|
962
|
+
else {
|
|
958
963
|
const orderNo = p.order_no || erp_order_id;
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
const orderNo = p.order_no || erp_order_id;
|
|
967
|
-
const paymentMethod = String(p.payment_method_code || '').toUpperCase().trim();
|
|
968
|
-
keyBase = `${orderNo}-${amount}-${paymentMethod}`;
|
|
969
|
-
}
|
|
964
|
+
const paymentMethod = String(p.payment_method_code || '').toUpperCase().trim();
|
|
965
|
+
keyBase = `${orderNo}-${amount}-${paymentMethod}`;
|
|
966
|
+
}
|
|
967
|
+
}
|
|
968
|
+
else {
|
|
969
|
+
if (p.payment_refund_number) {
|
|
970
|
+
keyBase = `PRN:${p.payment_refund_number}`;
|
|
970
971
|
}
|
|
971
972
|
else {
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
const orderNo = p.order_no || erp_order_id;
|
|
977
|
-
const amount = Number(p.amount || 0);
|
|
978
|
-
const paymentMethod = String(p.payment_method_code || '').toUpperCase().trim();
|
|
979
|
-
keyBase = `${orderNo}-${amount}-${paymentMethod}`;
|
|
980
|
-
}
|
|
973
|
+
const orderNo = p.order_no || erp_order_id;
|
|
974
|
+
const amount = Number(p.amount || 0);
|
|
975
|
+
const paymentMethod = String(p.payment_method_code || '').toUpperCase().trim();
|
|
976
|
+
keyBase = `${orderNo}-${amount}-${paymentMethod}`;
|
|
981
977
|
}
|
|
982
|
-
const key = `${logicalType}:${keyBase}`;
|
|
983
|
-
const g = groupsMap.get(key) || { entries: [], type: logicalType, key };
|
|
984
|
-
g.entries.push(p);
|
|
985
|
-
groupsMap.set(key, g);
|
|
986
|
-
}
|
|
987
|
-
catch (error) {
|
|
988
|
-
console.error(`Error processing payment entry:`, {
|
|
989
|
-
error: error instanceof Error ? error.message : String(error),
|
|
990
|
-
payment_type: p?.type,
|
|
991
|
-
amount: p?.amount,
|
|
992
|
-
order_no: p?.order_no || erp_order_id,
|
|
993
|
-
transaction_id: p?.transaction_id,
|
|
994
|
-
payment_gateway_token: p?.payment_gateway_token,
|
|
995
|
-
});
|
|
996
978
|
}
|
|
979
|
+
const key = `${logicalType}:${keyBase}`;
|
|
980
|
+
const g = groupsMap.get(key) || { entries: [], type: logicalType, key };
|
|
981
|
+
g.entries.push(p);
|
|
982
|
+
groupsMap.set(key, g);
|
|
997
983
|
});
|
|
998
984
|
const allPaymentEntriesByOrder = new Map();
|
|
999
985
|
flat.forEach(p => {
|
|
@@ -59,7 +59,6 @@ class PubSubHelper extends runtime_1.default {
|
|
|
59
59
|
body: messageBody,
|
|
60
60
|
bodyTxt: rawBody,
|
|
61
61
|
extraAttributes: (0, lodash_omit_1.default)(rawMessage.message?.attributes || {}, ['buildship_id', 'publish_time']),
|
|
62
|
-
ackId: rawMessage.ackId || undefined,
|
|
63
62
|
subscriptionName: rawMessage.subscription?.split('/').pop(),
|
|
64
63
|
};
|
|
65
64
|
return message;
|
package/dist/package.json
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shushed/helpers",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.292",
|
|
4
4
|
"author": "",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"description": "",
|
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
"ajv-formats": "^3.0.1",
|
|
12
12
|
"archiver": "^7.0.1",
|
|
13
13
|
"co-body": "^6.2.0",
|
|
14
|
+
"currency.js": "^2.0.4",
|
|
14
15
|
"jose": "^6.0.11",
|
|
15
16
|
"lodash.chunk": "^4.2.0",
|
|
16
17
|
"lodash.clonedeep": "^4.5.0",
|