rerobe-js-orm 3.9.9 → 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.
@@ -364,26 +364,32 @@ class OrderHelpers {
364
364
  return Number(price || 0) * Number(item.quantity);
365
365
  })
366
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
367
+ // Step 2: Calculate the total tax based on the pre-tax subtotal
370
368
  const totalTax = productsFullInfoWithQuantity
371
369
  .map((item) => {
372
370
  const price = item.product.isOnSale && item.product.clearanceTimestamp ? item.product.salePrice : item.product.price;
373
371
  const basePrice = Number(price || 0) * Number(item.quantity);
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
372
+ // Calculate tax only for taxable items
378
373
  if (item.isTaxable && item.taxRate) {
379
- return taxableAmount * Number(item.taxRate);
374
+ return basePrice * Number(item.taxRate);
380
375
  }
381
376
  return 0; // No tax for non-taxable items
382
377
  })
383
378
  .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));
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));
387
393
  }
388
394
  getSaleDiscountAmount(productsFullInfoWithQuantity) {
389
395
  return productsFullInfoWithQuantity
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rerobe-js-orm",
3
- "version": "3.9.9",
3
+ "version": "4.0.0",
4
4
  "description": "ReRobe's Javascript ORM Framework",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",