@shushed/helpers 0.0.223-main-20251222110224 → 0.0.223-redis-fix-20251222110722
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/src-public/bcOrder.js +42 -104
- package/dist/cjs/src-public/env.js +6 -3
- package/package.json +1 -1
|
@@ -400,15 +400,18 @@ function transformMasterOrder(payload) {
|
|
|
400
400
|
const desc = String(line?.description || '').trim().toLowerCase();
|
|
401
401
|
return desc === 'delivery';
|
|
402
402
|
};
|
|
403
|
+
let invoicesNetMinor = 0;
|
|
403
404
|
let invoicesGrossMinor = 0;
|
|
404
405
|
(payload.invoices || []).forEach((inv) => {
|
|
405
406
|
(inv.invoice_lines || []).forEach((line) => {
|
|
406
407
|
if (!isDeliveryItem(line)) {
|
|
408
|
+
invoicesNetMinor += toMinorUnits(Number(line?.amount || line?.amount_excl_vat || 0));
|
|
407
409
|
invoicesGrossMinor += toMinorUnits(Number(line?.amount_including_vat || line?.amount_incl_vat || 0));
|
|
408
410
|
}
|
|
409
411
|
});
|
|
410
412
|
});
|
|
411
413
|
if (invoicesGrossMinor === 0 && (payload.invoices || []).length > 0) {
|
|
414
|
+
invoicesNetMinor = toMinorUnits((payload.invoices || []).reduce((acc, inv) => acc + (inv.amount_excl_vat || 0), 0));
|
|
412
415
|
invoicesGrossMinor = toMinorUnits((payload.invoices || []).reduce((acc, inv) => acc + (inv.amount_incl_vat || 0), 0));
|
|
413
416
|
}
|
|
414
417
|
if (invoicesGrossMinor === 0 && invoicedLineNos.size > 0 && sale0?.sale_lines) {
|
|
@@ -420,8 +423,10 @@ function transformMasterOrder(payload) {
|
|
|
420
423
|
const lineNo = Number(l?.line_no || -1);
|
|
421
424
|
return invoicedLineNos.has(lineNo);
|
|
422
425
|
});
|
|
426
|
+
invoicesNetMinor = toMinorUnits(invoicedSaleLines.reduce((acc, l) => acc + (Number(l?.amount || 0)), 0));
|
|
423
427
|
invoicesGrossMinor = toMinorUnits(invoicedSaleLines.reduce((acc, l) => acc + (Number(l?.amount_including_vat || 0)), 0));
|
|
424
428
|
}
|
|
429
|
+
let nonInvSalesNetMinor = 0;
|
|
425
430
|
let nonInvSalesGrossMinor = 0;
|
|
426
431
|
if (sale0?.sale_lines && sale0.sale_lines.length > 0) {
|
|
427
432
|
const allSaleLines = (sale0.sale_lines || []).filter((l) => {
|
|
@@ -431,20 +436,26 @@ function transformMasterOrder(payload) {
|
|
|
431
436
|
return false;
|
|
432
437
|
return true;
|
|
433
438
|
});
|
|
439
|
+
const totalSaleNetMinor = toMinorUnits(allSaleLines.reduce((acc, l) => acc + (Number(l?.amount || 0)), 0));
|
|
434
440
|
const totalSaleGrossMinor = toMinorUnits(allSaleLines.reduce((acc, l) => acc + (Number(l?.amount_including_vat || 0)), 0));
|
|
435
441
|
if (invoicesGrossMinor > 0) {
|
|
442
|
+
nonInvSalesNetMinor = Math.max(0, totalSaleNetMinor - invoicesNetMinor);
|
|
436
443
|
nonInvSalesGrossMinor = Math.max(0, totalSaleGrossMinor - invoicesGrossMinor);
|
|
437
444
|
}
|
|
438
445
|
else {
|
|
446
|
+
nonInvSalesNetMinor = totalSaleNetMinor;
|
|
439
447
|
nonInvSalesGrossMinor = totalSaleGrossMinor;
|
|
440
448
|
}
|
|
441
449
|
}
|
|
442
450
|
else if (sale0 && (sale0.amount_excl_vat || sale0.amount_incl_vat)) {
|
|
451
|
+
const saleNetMinor = toMinorUnits(sale0.amount_excl_vat || 0);
|
|
443
452
|
const saleGrossMinor = toMinorUnits(sale0.amount_incl_vat || 0);
|
|
444
453
|
if (invoicesGrossMinor > 0) {
|
|
454
|
+
nonInvSalesNetMinor = Math.max(0, saleNetMinor - invoicesNetMinor);
|
|
445
455
|
nonInvSalesGrossMinor = Math.max(0, saleGrossMinor - invoicesGrossMinor);
|
|
446
456
|
}
|
|
447
457
|
else {
|
|
458
|
+
nonInvSalesNetMinor = saleNetMinor;
|
|
448
459
|
nonInvSalesGrossMinor = saleGrossMinor;
|
|
449
460
|
}
|
|
450
461
|
}
|
|
@@ -457,57 +468,32 @@ function transformMasterOrder(payload) {
|
|
|
457
468
|
const lineNo = Number(l?.line_no || -1);
|
|
458
469
|
return !invoicedLineNos.has(lineNo);
|
|
459
470
|
});
|
|
471
|
+
nonInvSalesNetMinor = toMinorUnits(nonInvoicedSales.reduce((acc, l) => acc + (Number(l?.amount || 0)), 0));
|
|
460
472
|
nonInvSalesGrossMinor = toMinorUnits(nonInvoicedSales.reduce((acc, l) => acc + (Number(l?.amount_including_vat || 0)), 0));
|
|
461
473
|
}
|
|
474
|
+
const returnsNetMinor = toMinorUnits((payload.credit_memos || []).reduce((acc, cm) => acc + (cm.amount_excl_vat || 0), 0));
|
|
475
|
+
const returnsGrossMinor = toMinorUnits((payload.credit_memos || []).reduce((acc, cm) => acc + (cm.amount_incl_vat || 0), 0));
|
|
476
|
+
const totalNetMinor = invoicesNetMinor + nonInvSalesNetMinor;
|
|
462
477
|
const totalGrossMinor = invoicesGrossMinor + nonInvSalesGrossMinor;
|
|
463
|
-
const invoicesNetMinorWithDelivery = toMinorUnits((payload.invoices || []).reduce((acc, inv) => acc + (inv.amount_excl_vat || 0), 0));
|
|
464
|
-
const invoicesGrossMinorWithDelivery = toMinorUnits((payload.invoices || []).reduce((acc, inv) => acc + (inv.amount_incl_vat || 0), 0));
|
|
465
|
-
let nonInvSalesNetMinorWithDelivery = 0;
|
|
466
|
-
let nonInvSalesGrossMinorWithDelivery = 0;
|
|
467
|
-
if (sale0?.sale_lines && sale0.sale_lines.length > 0) {
|
|
468
|
-
const allSaleLinesWithDelivery = (sale0.sale_lines || []).filter((l) => {
|
|
469
|
-
if (String(l?.type || '').toLowerCase() !== 'item')
|
|
470
|
-
return false;
|
|
471
|
-
return true;
|
|
472
|
-
});
|
|
473
|
-
const totalSaleNetMinorWithDelivery = toMinorUnits(allSaleLinesWithDelivery.reduce((acc, l) => acc + (Number(l?.amount || 0)), 0));
|
|
474
|
-
const totalSaleGrossMinorWithDelivery = toMinorUnits(allSaleLinesWithDelivery.reduce((acc, l) => acc + (Number(l?.amount_including_vat || 0)), 0));
|
|
475
|
-
if (invoicesGrossMinorWithDelivery > 0) {
|
|
476
|
-
nonInvSalesNetMinorWithDelivery = Math.max(0, totalSaleNetMinorWithDelivery - invoicesNetMinorWithDelivery);
|
|
477
|
-
nonInvSalesGrossMinorWithDelivery = Math.max(0, totalSaleGrossMinorWithDelivery - invoicesGrossMinorWithDelivery);
|
|
478
|
-
}
|
|
479
|
-
else {
|
|
480
|
-
nonInvSalesNetMinorWithDelivery = totalSaleNetMinorWithDelivery;
|
|
481
|
-
nonInvSalesGrossMinorWithDelivery = totalSaleGrossMinorWithDelivery;
|
|
482
|
-
}
|
|
483
|
-
}
|
|
484
|
-
else if (sale0 && (sale0.amount_excl_vat || sale0.amount_incl_vat)) {
|
|
485
|
-
const saleNetMinor = toMinorUnits(sale0.amount_excl_vat || 0);
|
|
486
|
-
const saleGrossMinor = toMinorUnits(sale0.amount_incl_vat || 0);
|
|
487
|
-
if (invoicesGrossMinorWithDelivery > 0) {
|
|
488
|
-
nonInvSalesNetMinorWithDelivery = Math.max(0, saleNetMinor - invoicesNetMinorWithDelivery);
|
|
489
|
-
nonInvSalesGrossMinorWithDelivery = Math.max(0, saleGrossMinor - invoicesGrossMinorWithDelivery);
|
|
490
|
-
}
|
|
491
|
-
else {
|
|
492
|
-
nonInvSalesNetMinorWithDelivery = saleNetMinor;
|
|
493
|
-
nonInvSalesGrossMinorWithDelivery = saleGrossMinor;
|
|
494
|
-
}
|
|
495
|
-
}
|
|
496
|
-
else {
|
|
497
|
-
const nonInvoicedSalesWithDelivery = (sale0?.sale_lines || []).filter((l) => {
|
|
498
|
-
if (String(l?.type || '').toLowerCase() !== 'item')
|
|
499
|
-
return false;
|
|
500
|
-
const lineNo = Number(l?.line_no || -1);
|
|
501
|
-
return !invoicedLineNos.has(lineNo);
|
|
502
|
-
});
|
|
503
|
-
nonInvSalesNetMinorWithDelivery = toMinorUnits(nonInvoicedSalesWithDelivery.reduce((acc, l) => acc + (Number(l?.amount || 0)), 0));
|
|
504
|
-
nonInvSalesGrossMinorWithDelivery = toMinorUnits(nonInvoicedSalesWithDelivery.reduce((acc, l) => acc + (Number(l?.amount_including_vat || 0)), 0));
|
|
505
|
-
}
|
|
506
|
-
const totalNetMinorWithDelivery = invoicesNetMinorWithDelivery + nonInvSalesNetMinorWithDelivery;
|
|
507
|
-
const totalGrossMinorWithDelivery = invoicesGrossMinorWithDelivery + nonInvSalesGrossMinorWithDelivery;
|
|
508
478
|
const total = {
|
|
509
|
-
amount_net: moneyFromMinor(
|
|
510
|
-
amount_gross: moneyFromMinor(
|
|
479
|
+
amount_net: moneyFromMinor(totalNetMinor),
|
|
480
|
+
amount_gross: moneyFromMinor(totalGrossMinor),
|
|
481
|
+
discount_amount_net: money(0),
|
|
482
|
+
discount_amount_gross: money(0),
|
|
483
|
+
discount_amount_percent: 0,
|
|
484
|
+
};
|
|
485
|
+
const return_total = (payload.credit_memos && payload.credit_memos.length > 0)
|
|
486
|
+
? {
|
|
487
|
+
amount_net: moneyFromMinor(returnsNetMinor),
|
|
488
|
+
amount_gross: moneyFromMinor(returnsGrossMinor),
|
|
489
|
+
discount_amount_net: money(0),
|
|
490
|
+
discount_amount_gross: money(0),
|
|
491
|
+
discount_amount_percent: 0,
|
|
492
|
+
}
|
|
493
|
+
: undefined;
|
|
494
|
+
const net_total = {
|
|
495
|
+
amount_net: moneyFromMinor(Math.max(0, totalNetMinor - returnsNetMinor)),
|
|
496
|
+
amount_gross: moneyFromMinor(Math.max(0, totalGrossMinor - returnsGrossMinor)),
|
|
511
497
|
discount_amount_net: money(0),
|
|
512
498
|
discount_amount_gross: money(0),
|
|
513
499
|
discount_amount_percent: 0,
|
|
@@ -617,7 +603,7 @@ function transformMasterOrder(payload) {
|
|
|
617
603
|
const merchReturnGrossFromCreditMemos = (payload.credit_memos || []).reduce((acc, cm) => {
|
|
618
604
|
const lines = cm?.credit_memo_lines || [];
|
|
619
605
|
let hasQualifying = false;
|
|
620
|
-
const
|
|
606
|
+
const sum = lines.reduce((s, l) => {
|
|
621
607
|
const itemNo = String(l?.item_no || l?.no || "");
|
|
622
608
|
const desc = String(l?.description || "");
|
|
623
609
|
const looksProduct = itemNo.includes('-') || desc.includes(' - ');
|
|
@@ -630,36 +616,24 @@ function transformMasterOrder(payload) {
|
|
|
630
616
|
}
|
|
631
617
|
return s;
|
|
632
618
|
}, 0);
|
|
633
|
-
const sumNet = lines.reduce((s, l) => {
|
|
634
|
-
const itemNo = String(l?.item_no || l?.no || "");
|
|
635
|
-
const desc = String(l?.description || "");
|
|
636
|
-
const looksProduct = itemNo.includes('-') || desc.includes(' - ');
|
|
637
|
-
const typeStr = String(l?.type || "").toLowerCase();
|
|
638
|
-
const isItemish = typeStr.includes('item') || looksProduct;
|
|
639
|
-
const isRefund = itemNo.toUpperCase() === 'REFUND';
|
|
640
|
-
if ((l?.quantity || 0) > 0 && isItemish && !isRefund && !isDeliveryItem(l)) {
|
|
641
|
-
return s + (l?.amount || l?.amount_excl_vat || 0);
|
|
642
|
-
}
|
|
643
|
-
return s;
|
|
644
|
-
}, 0);
|
|
645
619
|
if (!hasQualifying)
|
|
646
620
|
return acc;
|
|
647
621
|
if (cm?.amount_incl_vat != null && cm.amount_incl_vat > 0) {
|
|
648
|
-
return
|
|
622
|
+
return acc + cm.amount_incl_vat;
|
|
649
623
|
}
|
|
650
|
-
return
|
|
651
|
-
},
|
|
624
|
+
return acc + sum;
|
|
625
|
+
}, 0);
|
|
652
626
|
const merchReturnGrossFromReturnSales = (payload.sales || []).reduce((acc, sale) => {
|
|
653
627
|
const docTypeRaw = String(sale?.document_type || sale?.documentType || "");
|
|
654
628
|
const isReturnOrder = docTypeRaw.toLowerCase().includes('return');
|
|
655
629
|
if (!isReturnOrder)
|
|
656
630
|
return acc;
|
|
657
631
|
if (sale?.amount_incl_vat != null && sale.amount_incl_vat > 0) {
|
|
658
|
-
return
|
|
632
|
+
return acc + sale.amount_incl_vat;
|
|
659
633
|
}
|
|
660
634
|
const lines = sale?.sale_lines || [];
|
|
661
635
|
let hasQualifying = false;
|
|
662
|
-
const
|
|
636
|
+
const sum = lines.reduce((s, l) => {
|
|
663
637
|
const itemNo = String(l?.item_no || l?.no || "");
|
|
664
638
|
const desc = String(l?.description || "");
|
|
665
639
|
const looksProduct = itemNo.includes('-') || desc.includes(' - ');
|
|
@@ -672,46 +646,10 @@ function transformMasterOrder(payload) {
|
|
|
672
646
|
}
|
|
673
647
|
return s;
|
|
674
648
|
}, 0);
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
const looksProduct = itemNo.includes('-') || desc.includes(' - ');
|
|
679
|
-
const typeStr = String(l?.type || "").toLowerCase();
|
|
680
|
-
const isItemish = typeStr.includes('item') || looksProduct;
|
|
681
|
-
const isRefund = itemNo.toUpperCase() === 'REFUND';
|
|
682
|
-
if ((l?.quantity || 0) > 0 && isItemish && !isRefund && !isDeliveryItem(l)) {
|
|
683
|
-
return s + (l?.amount || l?.amount_excl_vat || 0);
|
|
684
|
-
}
|
|
685
|
-
return s;
|
|
686
|
-
}, 0);
|
|
687
|
-
if (sumGross > 0) {
|
|
688
|
-
return { gross: acc.gross + sumGross, net: acc.net + sumNet };
|
|
689
|
-
}
|
|
690
|
-
if (hasQualifying) {
|
|
691
|
-
return { gross: acc.gross + (sale?.amount_incl_vat || sale?.amount_including_vat || 0), net: acc.net + (sale?.amount_excl_vat || sale?.amount || 0) };
|
|
692
|
-
}
|
|
693
|
-
return acc;
|
|
694
|
-
}, { gross: 0, net: 0 });
|
|
695
|
-
const merchReturnGross = merchReturnGrossFromCreditMemos.gross + merchReturnGrossFromReturnSales.gross;
|
|
649
|
+
return acc + (sum > 0 ? sum : (hasQualifying ? (sale?.amount_incl_vat || sale?.amount_including_vat || 0) : 0));
|
|
650
|
+
}, 0);
|
|
651
|
+
const merchReturnGross = merchReturnGrossFromCreditMemos + merchReturnGrossFromReturnSales;
|
|
696
652
|
const hasMerchReturn = merchReturnGross > 0;
|
|
697
|
-
const returnsNetMinor = toMinorUnits((payload.credit_memos || []).reduce((acc, cm) => acc + (cm.amount_excl_vat || 0), 0));
|
|
698
|
-
const returnsGrossMinor = toMinorUnits((payload.credit_memos || []).reduce((acc, cm) => acc + (cm.amount_incl_vat || 0), 0));
|
|
699
|
-
const return_total = (returnsGrossMinor > 0)
|
|
700
|
-
? {
|
|
701
|
-
amount_net: moneyFromMinor(returnsNetMinor),
|
|
702
|
-
amount_gross: moneyFromMinor(returnsGrossMinor),
|
|
703
|
-
discount_amount_net: money(0),
|
|
704
|
-
discount_amount_gross: money(0),
|
|
705
|
-
discount_amount_percent: 0,
|
|
706
|
-
}
|
|
707
|
-
: undefined;
|
|
708
|
-
const net_total = {
|
|
709
|
-
amount_net: moneyFromMinor(Math.max(0, totalNetMinorWithDelivery - returnsNetMinor)),
|
|
710
|
-
amount_gross: moneyFromMinor(Math.max(0, totalGrossMinorWithDelivery - returnsGrossMinor)),
|
|
711
|
-
discount_amount_net: money(0),
|
|
712
|
-
discount_amount_gross: money(0),
|
|
713
|
-
discount_amount_percent: 0,
|
|
714
|
-
};
|
|
715
653
|
const totalOrderGrossMinor = totalGrossMinor;
|
|
716
654
|
const sourceLineMap = new Map();
|
|
717
655
|
sourceLines.forEach((line) => {
|
|
@@ -155,12 +155,15 @@ class EnvEngine extends runtime_1.default {
|
|
|
155
155
|
Object.assign(this.globalInMemoryRef[level], Object.fromEntries(Object.entries(objEncrypted).map(([k, v]) => [k, `${expirseAt}/${now}/${v}`])));
|
|
156
156
|
}
|
|
157
157
|
if (!options.ignoreStores?.includes('redis')) {
|
|
158
|
-
|
|
158
|
+
try {
|
|
159
159
|
const redisClient = await (0, redisClient_1.getConnectedRedisClient)(this.systemEnvName).catch(() => null);
|
|
160
160
|
if (redisClient) {
|
|
161
|
-
Promise.allSettled(Object.entries(objEncrypted).map(([k, v]) => redisClient.set(k, `${expirseAt}/${now}/${v}`, { PX: expirseAt - Date.now() })));
|
|
161
|
+
await Promise.allSettled(Object.entries(objEncrypted).map(([k, v]) => redisClient.set(k, `${expirseAt}/${now}/${v}`, { PX: expirseAt - Date.now() })));
|
|
162
162
|
}
|
|
163
|
-
}
|
|
163
|
+
}
|
|
164
|
+
catch {
|
|
165
|
+
this.logging.error(`Failed to set the ${envs.map(x => x.name).join(', ')} in Redis`);
|
|
166
|
+
}
|
|
164
167
|
}
|
|
165
168
|
}
|
|
166
169
|
else {
|