|
|
@@ -5,6 +5,7 @@ import { beforeAll, describe, expect, it } from 'vitest';
|
|
|
import { ensureConfigLoaded } from '../../config/config-helpers';
|
|
|
import { createOrder, createRequestContext, taxCategoryStandard } from '../../testing/order-test-utils';
|
|
|
import { ShippingLine } from '../shipping-line/shipping-line.entity';
|
|
|
+import { Surcharge } from '../surcharge/surcharge.entity';
|
|
|
|
|
|
import { Order } from './order.entity';
|
|
|
|
|
|
@@ -324,6 +325,73 @@ describe('Order entity methods', () => {
|
|
|
]);
|
|
|
assertOrderTaxesAddsUp(order);
|
|
|
});
|
|
|
+
|
|
|
+ it('with surcharge', () => {
|
|
|
+ const ctx = createRequestContext({ pricesIncludeTax: false });
|
|
|
+ const order = createOrder({
|
|
|
+ ctx,
|
|
|
+ lines: [
|
|
|
+ {
|
|
|
+ listPrice: 300,
|
|
|
+ taxCategory: taxCategoryStandard,
|
|
|
+ quantity: 2,
|
|
|
+ },
|
|
|
+ {
|
|
|
+ listPrice: 1000,
|
|
|
+ taxCategory: taxCategoryStandard,
|
|
|
+ quantity: 1,
|
|
|
+ },
|
|
|
+ ],
|
|
|
+ surcharges: [
|
|
|
+ new Surcharge({
|
|
|
+ description: 'Special surcharge',
|
|
|
+ listPrice: 400,
|
|
|
+ listPriceIncludesTax: ctx.channel.pricesIncludeTax,
|
|
|
+ taxLines: [
|
|
|
+ { description: 'Special surcharge tax', taxRate: 50 },
|
|
|
+ { description: 'Special surcharge second tax', taxRate: 20 },
|
|
|
+ ],
|
|
|
+ sku: 'special-surcharge',
|
|
|
+ }),
|
|
|
+ new Surcharge({
|
|
|
+ description: 'Other surcharge',
|
|
|
+ listPrice: 500,
|
|
|
+ listPriceIncludesTax: ctx.channel.pricesIncludeTax,
|
|
|
+ taxLines: [{ description: 'Other surcharge tax', taxRate: 0 }],
|
|
|
+ sku: 'other-surcharge',
|
|
|
+ }),
|
|
|
+ ],
|
|
|
+ });
|
|
|
+ order.lines.forEach(i => (i.taxLines = [{ taxRate: 5, description: 'tax a' }]));
|
|
|
+
|
|
|
+ expect(order.taxSummary).toEqual([
|
|
|
+ {
|
|
|
+ description: 'tax a',
|
|
|
+ taxRate: 5,
|
|
|
+ taxBase: 1600,
|
|
|
+ taxTotal: 80,
|
|
|
+ },
|
|
|
+ {
|
|
|
+ description: 'Special surcharge tax',
|
|
|
+ taxRate: 50,
|
|
|
+ taxBase: 400,
|
|
|
+ taxTotal: 200,
|
|
|
+ },
|
|
|
+ {
|
|
|
+ description: 'Special surcharge second tax',
|
|
|
+ taxRate: 20,
|
|
|
+ taxBase: 400,
|
|
|
+ taxTotal: 80,
|
|
|
+ },
|
|
|
+ {
|
|
|
+ description: 'Other surcharge tax',
|
|
|
+ taxRate: 0,
|
|
|
+ taxBase: 500,
|
|
|
+ taxTotal: 0,
|
|
|
+ },
|
|
|
+ ]);
|
|
|
+ assertOrderTaxesAddsUp(order);
|
|
|
+ });
|
|
|
});
|
|
|
});
|
|
|
|
|
|
@@ -332,5 +400,9 @@ function assertOrderTaxesAddsUp(order: Order) {
|
|
|
const lineTotal = summate(order.lines, 'proratedLinePrice');
|
|
|
const lineTotalWithTax = summate(order.lines, 'proratedLinePriceWithTax');
|
|
|
const shippingTax = (order.shippingWithTax ?? 0) - (order.shipping ?? 0);
|
|
|
- expect(lineTotalWithTax - lineTotal + shippingTax).toBe(summaryTaxTotal);
|
|
|
+ const surchargesTotal = summate(order.surcharges, 'price');
|
|
|
+ const surchargesTotalWithTax = summate(order.surcharges, 'priceWithTax');
|
|
|
+ expect(lineTotalWithTax - lineTotal + shippingTax + (surchargesTotalWithTax - surchargesTotal)).toBe(
|
|
|
+ summaryTaxTotal,
|
|
|
+ );
|
|
|
}
|