rerobe-js-orm 3.9.8 → 3.9.9
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 +22 -13
- package/package.json +1 -1
|
@@ -355,26 +355,35 @@ 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: Apply the order-wide discount to the pre-tax subtotal, ensuring it doesn't exceed the pre-tax subtotal
|
|
368
|
+
const discountedSubtotal = Math.max(preTaxSubtotal - orderDiscount, 0);
|
|
369
|
+
// Step 3: Calculate the total tax based on the discounted subtotal
|
|
370
|
+
const totalTax = productsFullInfoWithQuantity
|
|
371
|
+
.map((item) => {
|
|
372
|
+
const price = item.product.isOnSale && item.product.clearanceTimestamp ? item.product.salePrice : item.product.price;
|
|
364
373
|
const basePrice = Number(price || 0) * Number(item.quantity);
|
|
365
|
-
|
|
366
|
-
// Apply
|
|
374
|
+
const discountFactor = basePrice / preTaxSubtotal; // Determine how much of the discount applies to this item
|
|
375
|
+
const discountedPriceForItem = basePrice - orderDiscount * discountFactor; // Apply discount proportionally to this item
|
|
376
|
+
const taxableAmount = Math.max(discountedPriceForItem, 0); // Cap the discounted price at 0
|
|
377
|
+
// Calculate tax only for taxable items, based on the discounted price
|
|
367
378
|
if (item.isTaxable && item.taxRate) {
|
|
368
|
-
|
|
369
|
-
totalPrice += taxAmount; // Add tax to the base price
|
|
379
|
+
return taxableAmount * Number(item.taxRate);
|
|
370
380
|
}
|
|
371
|
-
return
|
|
381
|
+
return 0; // No tax for non-taxable items
|
|
372
382
|
})
|
|
373
|
-
.reduce((a, b) => a + b, 0); // Sum up the
|
|
374
|
-
// Step
|
|
375
|
-
const
|
|
376
|
-
|
|
377
|
-
return Number(this.toFixedPointPrice(totalAfterDiscount));
|
|
383
|
+
.reduce((a, b) => a + b, 0); // Sum up the taxes for all items
|
|
384
|
+
// Step 4: Return the final total (discounted subtotal + total tax)
|
|
385
|
+
const totalAfterDiscountAndTax = discountedSubtotal + totalTax;
|
|
386
|
+
return Number(this.toFixedPointPrice(totalAfterDiscountAndTax));
|
|
378
387
|
}
|
|
379
388
|
getSaleDiscountAmount(productsFullInfoWithQuantity) {
|
|
380
389
|
return productsFullInfoWithQuantity
|