rerobe-js-orm 3.9.7 → 3.9.8

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.
@@ -26,7 +26,7 @@ export default class OrderHelpers {
26
26
  currency: string;
27
27
  }): string;
28
28
  getSubtotalWithoutSalePrice(productsFullInfoWithQuantity?: ProductsFullInfoWithQuantity): number;
29
- getSubtotalWithSalePriceIncluded(productsFullInfoWithQuantity?: ProductsFullInfoWithQuantity): number;
29
+ getSubtotalWithSalePriceIncluded(productsFullInfoWithQuantity?: ProductsFullInfoWithQuantity, orderDiscount?: number): number;
30
30
  getSaleDiscountAmount(productsFullInfoWithQuantity: ProductsFullInfoWithQuantity): number;
31
31
  preparePresentmentData({ merchantName, merchantCurrency, presentmentCurrencyCode, productsFullInfoWithQuantity, currencyConverter, saleDiscountAmount, creditDiscountAmount, additionalDiscount, shippingPrice, itemsTax, shippingFeeTax, subTotalWithSalePriceIncluded, totalIncludingShipping, totalIncludingShippingAndTaxes, }: {
32
32
  merchantName: string;
@@ -351,23 +351,30 @@ class OrderHelpers {
351
351
  .reduce((a, b) => a + b, 0);
352
352
  return Number(this.toFixedPointPrice(cumsum));
353
353
  }
354
- getSubtotalWithSalePriceIncluded(productsFullInfoWithQuantity) {
354
+ getSubtotalWithSalePriceIncluded(productsFullInfoWithQuantity, orderDiscount = 0) {
355
355
  if (!productsFullInfoWithQuantity || productsFullInfoWithQuantity.length === 0) {
356
356
  return 0;
357
357
  }
358
- const cumsum = productsFullInfoWithQuantity
358
+ // Step 1: Calculate the total before applying the order-wide discount
359
+ const totalBeforeDiscount = productsFullInfoWithQuantity
359
360
  .map((item) => {
360
361
  // Determine if the product is on sale and set the price accordingly
361
362
  const price = item.product.isOnSale && item.product.clearanceTimestamp ? item.product.salePrice : item.product.price;
363
+ // Base price for this item before tax and discounts
362
364
  const basePrice = Number(price || 0) * Number(item.quantity);
365
+ let totalPrice = basePrice;
366
+ // Apply tax if the item is taxable
363
367
  if (item.isTaxable && item.taxRate) {
364
- const taxAmount = basePrice * Number(item.taxRate); // Calculate the tax based on the base price
365
- return basePrice + taxAmount; // Return the total including tax
368
+ const taxAmount = basePrice * Number(item.taxRate); // Calculate tax based on base price
369
+ totalPrice += taxAmount; // Add tax to the base price
366
370
  }
367
- return basePrice; // Return the price without tax for non-taxable items
371
+ return totalPrice; // Return the total price (including tax if applicable)
368
372
  })
369
- .reduce((a, b) => a + b, 0);
370
- return Number(this.toFixedPointPrice(cumsum));
373
+ .reduce((a, b) => a + b, 0); // Sum up the totals for all items
374
+ // Step 2: Apply the order-wide discount, ensuring it doesn't exceed the total before discount
375
+ const totalAfterDiscount = Math.max(totalBeforeDiscount - orderDiscount, 0);
376
+ // Step 3: Return the final subtotal, rounded to fixed decimal points
377
+ return Number(this.toFixedPointPrice(totalAfterDiscount));
371
378
  }
372
379
  getSaleDiscountAmount(productsFullInfoWithQuantity) {
373
380
  return productsFullInfoWithQuantity
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rerobe-js-orm",
3
- "version": "3.9.7",
3
+ "version": "3.9.8",
4
4
  "description": "ReRobe's Javascript ORM Framework",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",