Просмотр исходного кода

fix(core): Include shipping tax in Order.taxSummary

Relates to #729

BREAKING CHANGE: `Order.taxSummary` now includes shipping taxes
Michael Bromley 4 лет назад
Родитель
Сommit
cf5aa31550

+ 54 - 1
packages/core/src/entity/order/order.entity.spec.ts

@@ -2,6 +2,7 @@ import { AdjustmentType } from '@vendure/common/lib/generated-types';
 import { summate } from '@vendure/common/lib/shared-utils';
 
 import { createOrder, createRequestContext, taxCategoryStandard } from '../../testing/order-test-utils';
+import { ShippingLine } from '../shipping-line/shipping-line.entity';
 
 import { Order } from './order.entity';
 
@@ -280,6 +281,57 @@ describe('Order entity methods', () => {
             ]);
             assertOrderTaxesAddsUp(order);
         });
+
+        it('with shipping', () => {
+            const ctx = createRequestContext({ pricesIncludeTax: false });
+            const order = createOrder({
+                ctx,
+                lines: [
+                    {
+                        listPrice: 300,
+                        taxCategory: taxCategoryStandard,
+                        quantity: 2,
+                    },
+                    {
+                        listPrice: 1000,
+                        taxCategory: taxCategoryStandard,
+                        quantity: 1,
+                    },
+                ],
+            });
+            order.lines[0].items.forEach(i => (i.taxLines = [{ taxRate: 5, description: 'tax a' }]));
+            order.lines[1].items.forEach(i => (i.taxLines = [{ taxRate: 5, description: 'tax a' }]));
+            order.shippingLines = [
+                new ShippingLine({
+                    listPrice: 500,
+                    listPriceIncludesTax: false,
+                    taxLines: [
+                        {
+                            taxRate: 20,
+                            description: 'shipping tax',
+                        },
+                    ],
+                }),
+            ];
+            order.shipping = 500;
+            order.shippingWithTax = 600;
+
+            expect(order.taxSummary).toEqual([
+                {
+                    description: 'tax a',
+                    taxRate: 5,
+                    taxBase: 1600,
+                    taxTotal: 80,
+                },
+                {
+                    description: 'shipping tax',
+                    taxRate: 20,
+                    taxBase: 500,
+                    taxTotal: 100,
+                },
+            ]);
+            assertOrderTaxesAddsUp(order);
+        });
     });
 });
 
@@ -287,5 +339,6 @@ function assertOrderTaxesAddsUp(order: Order) {
     const summaryTaxTotal = summate(order.taxSummary, 'taxTotal');
     const lineTotal = summate(order.lines, 'proratedLinePrice');
     const lineTotalWithTax = summate(order.lines, 'proratedLinePriceWithTax');
-    expect(lineTotalWithTax - lineTotal).toBe(summaryTaxTotal);
+    const shippingTax = (order.shippingWithTax ?? 0) - (order.shipping ?? 0);
+    expect(lineTotalWithTax - lineTotal + shippingTax).toBe(summaryTaxTotal);
 }

+ 9 - 6
packages/core/src/entity/order/order.entity.ts

@@ -174,22 +174,25 @@ export class Order extends VendureEntity implements ChannelAware, HasCustomField
             { rate: number; base: number; tax: number; description: string }
         >();
         const taxId = (taxLine: TaxLine): string => `${taxLine.description}:${taxLine.taxRate}`;
-        for (const line of this.lines) {
+        const taxableLines = [...this.lines, ...this.shippingLines];
+        for (const line of taxableLines) {
             const taxRateTotal = summate(line.taxLines, 'taxRate');
             for (const taxLine of line.taxLines) {
                 const id = taxId(taxLine);
                 const row = taxRateMap.get(id);
                 const proportionOfTotalRate = 0 < taxLine.taxRate ? taxLine.taxRate / taxRateTotal : 0;
-                const amount = Math.round(
-                    (line.proratedLinePriceWithTax - line.proratedLinePrice) * proportionOfTotalRate,
-                );
+
+                const lineBase = line instanceof OrderLine ? line.proratedLinePrice : line.discountedPrice;
+                const lineWithTax =
+                    line instanceof OrderLine ? line.proratedLinePriceWithTax : line.discountedPriceWithTax;
+                const amount = Math.round((lineWithTax - lineBase) * proportionOfTotalRate);
                 if (row) {
                     row.tax += amount;
-                    row.base += line.proratedLinePrice;
+                    row.base += lineBase;
                 } else {
                     taxRateMap.set(id, {
                         tax: amount,
-                        base: line.proratedLinePrice,
+                        base: lineBase,
                         description: taxLine.description,
                         rate: taxLine.taxRate,
                     });

+ 5 - 5
packages/dev-server/dev-config.ts

@@ -80,12 +80,12 @@ export const devConfig: VendureConfig = {
             route: 'assets',
             assetUploadDir: path.join(__dirname, 'assets'),
         }),
-        // DefaultSearchPlugin,
+        DefaultSearchPlugin,
         DefaultJobQueuePlugin,
-        ElasticsearchPlugin.init({
-            host: 'http://localhost',
-            port: 9200,
-        }),
+        // ElasticsearchPlugin.init({
+        //     host: 'http://localhost',
+        //     port: 9200,
+        // }),
         EmailPlugin.init({
             devMode: true,
             route: 'mailbox',

Разница между файлами не показана из-за своего большого размера
+ 0 - 0
schema-admin.json


Разница между файлами не показана из-за своего большого размера
+ 0 - 0
schema-shop.json


Некоторые файлы не были показаны из-за большого количества измененных файлов