Преглед изворни кода

fix(core): Fix NaN error when prorating discount over zero-tax line

Michael Bromley пре 5 година
родитељ
комит
51af5a0b3d

+ 22 - 0
packages/core/src/service/helpers/order-calculator/order-calculator.spec.ts

@@ -25,6 +25,7 @@ import {
     MockTaxRateService,
     taxCategoryReduced,
     taxCategoryStandard,
+    taxCategoryZero,
 } from '../../../testing/order-test-utils';
 import { WorkerService } from '../../../worker/worker.service';
 import { ShippingMethodService } from '../../services/shipping-method.service';
@@ -590,6 +591,27 @@ describe('OrderCalculator', () => {
                     expect(order.totalWithTax).toBe(50);
                     assertOrderTotalsAddUp(order);
                 });
+
+                it('prices include tax at 0%', async () => {
+                    const ctx = createRequestContext({ pricesIncludeTax: true });
+                    const order = createOrder({
+                        ctx,
+                        lines: [
+                            {
+                                listPrice: 100,
+                                taxCategory: taxCategoryZero,
+                                quantity: 1,
+                            },
+                        ],
+                    });
+                    await orderCalculator.applyPriceAdjustments(ctx, order, [promotion]);
+
+                    expect(order.subTotal).toBe(50);
+                    expect(order.discounts.length).toBe(1);
+                    expect(order.discounts[0].description).toBe('50% off order');
+                    expect(order.totalWithTax).toBe(50);
+                    assertOrderTotalsAddUp(order);
+                });
             });
         });
 

+ 1 - 1
packages/core/src/service/helpers/order-calculator/order-calculator.ts

@@ -276,7 +276,7 @@ export class OrderCalculator {
                     const adjustment = await promotion.apply(ctx, { order });
                     if (adjustment && adjustment.amount !== 0) {
                         const amount = adjustment.amount;
-                        const weights = order.lines.map(l => l.proratedLinePrice * l.taxRate);
+                        const weights = order.lines.map(l => l.proratedLinePrice * Math.max(l.taxRate, 1));
                         const distribution = prorate(weights, amount);
                         order.lines.forEach((line, i) => {
                             const shareOfAmount = distribution[i];

+ 13 - 0
packages/core/src/testing/order-test-utils.ts

@@ -63,6 +63,10 @@ export const taxCategoryReduced = new TaxCategory({
     id: 'taxCategoryReduced',
     name: 'Reduced Tax',
 });
+export const taxCategoryZero = new TaxCategory({
+    id: 'taxCategoryZero',
+    name: 'Zero Tax',
+});
 export const zoneDefault = new Zone({
     id: 'zoneDefault',
     name: 'Default Zone',
@@ -91,6 +95,14 @@ export const taxRateDefaultReduced = new TaxRate({
     zone: zoneDefault,
     category: taxCategoryReduced,
 });
+export const taxRateDefaultZero = new TaxRate({
+    id: 'taxRateDefaultZero',
+    name: 'Default Zero Tax',
+    value: 0,
+    enabled: true,
+    zone: zoneDefault,
+    category: taxCategoryZero,
+});
 export const taxRateOtherStandard = new TaxRate({
     id: 'taxRateOtherStandard',
     name: 'Other Standard',
@@ -112,6 +124,7 @@ export class MockTaxRateService {
     private activeTaxRates = [
         taxRateDefaultStandard,
         taxRateDefaultReduced,
+        taxRateDefaultZero,
         taxRateOtherStandard,
         taxRateOtherReduced,
     ];