Skip to content

Commit

Permalink
fix(core): Fix NaN error when prorating discount over zero-tax line
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbromley committed Jan 13, 2021
1 parent a2e34ec commit 51af5a0
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
});
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
13 changes: 13 additions & 0 deletions packages/core/src/testing/order-test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand All @@ -112,6 +124,7 @@ export class MockTaxRateService {
private activeTaxRates = [
taxRateDefaultStandard,
taxRateDefaultReduced,
taxRateDefaultZero,
taxRateOtherStandard,
taxRateOtherReduced,
];
Expand Down

0 comments on commit 51af5a0

Please sign in to comment.