rerobe-js-orm 3.9.8 → 4.0.0
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/lib/helpers/OrderHelpers.js +28 -13
- package/package.json +1 -1
|
@@ -355,26 +355,41 @@ class OrderHelpers {
|
|
|
355
355
|
if (!productsFullInfoWithQuantity || productsFullInfoWithQuantity.length === 0) {
|
|
356
356
|
return 0;
|
|
357
357
|
}
|
|
358
|
-
// Step 1: Calculate the
|
|
359
|
-
const
|
|
358
|
+
// Step 1: Calculate the pre-tax subtotal (no taxes yet)
|
|
359
|
+
const preTaxSubtotal = productsFullInfoWithQuantity
|
|
360
360
|
.map((item) => {
|
|
361
361
|
// Determine if the product is on sale and set the price accordingly
|
|
362
362
|
const price = item.product.isOnSale && item.product.clearanceTimestamp ? item.product.salePrice : item.product.price;
|
|
363
|
-
// Base price for this item before tax
|
|
363
|
+
// Base price for this item before tax
|
|
364
|
+
return Number(price || 0) * Number(item.quantity);
|
|
365
|
+
})
|
|
366
|
+
.reduce((a, b) => a + b, 0); // Sum up the pre-tax prices of all items
|
|
367
|
+
// Step 2: Calculate the total tax based on the pre-tax subtotal
|
|
368
|
+
const totalTax = productsFullInfoWithQuantity
|
|
369
|
+
.map((item) => {
|
|
370
|
+
const price = item.product.isOnSale && item.product.clearanceTimestamp ? item.product.salePrice : item.product.price;
|
|
364
371
|
const basePrice = Number(price || 0) * Number(item.quantity);
|
|
365
|
-
|
|
366
|
-
// Apply tax if the item is taxable
|
|
372
|
+
// Calculate tax only for taxable items
|
|
367
373
|
if (item.isTaxable && item.taxRate) {
|
|
368
|
-
|
|
369
|
-
totalPrice += taxAmount; // Add tax to the base price
|
|
374
|
+
return basePrice * Number(item.taxRate);
|
|
370
375
|
}
|
|
371
|
-
return
|
|
376
|
+
return 0; // No tax for non-taxable items
|
|
372
377
|
})
|
|
373
|
-
.reduce((a, b) => a + b, 0); // Sum up the
|
|
374
|
-
// Step
|
|
375
|
-
const
|
|
376
|
-
// Step
|
|
377
|
-
|
|
378
|
+
.reduce((a, b) => a + b, 0); // Sum up the taxes for all items
|
|
379
|
+
// Step 3: Calculate subtotal with tax
|
|
380
|
+
const subtotalWithTax = preTaxSubtotal + totalTax;
|
|
381
|
+
// Step 4: Apply discount logic
|
|
382
|
+
let finalSubtotal;
|
|
383
|
+
if (orderDiscount >= preTaxSubtotal) {
|
|
384
|
+
// If the discount is greater than or equal to the pre-tax subtotal, the subtotal is 0 and no tax is applied
|
|
385
|
+
finalSubtotal = 0;
|
|
386
|
+
}
|
|
387
|
+
else {
|
|
388
|
+
// If the discount is less than the pre-tax subtotal, apply it to the subtotal with tax
|
|
389
|
+
finalSubtotal = Math.max(subtotalWithTax - orderDiscount, 0);
|
|
390
|
+
}
|
|
391
|
+
// Step 5: Return the final total, rounded to the correct decimal places
|
|
392
|
+
return Number(this.toFixedPointPrice(finalSubtotal));
|
|
378
393
|
}
|
|
379
394
|
getSaleDiscountAmount(productsFullInfoWithQuantity) {
|
|
380
395
|
return productsFullInfoWithQuantity
|