fix: quantity not respected (#460)

* fix:  fix failing map tests

* fix: 🐛 use totalPrice instead of unitPrice to fix quantity

* build: 👷 add changeset
This commit is contained in:
Adrian Pilarczyk 2023-05-12 14:49:27 +02:00 committed by GitHub
parent 0c039f59dc
commit b4ddb02efb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 64 additions and 30 deletions

View file

@ -0,0 +1,5 @@
---
"saleor-app-taxes": patch
---
Fix quantity not respected when calculating taxes.

View file

@ -8,6 +8,9 @@ fragment OrderLine on OrderLine {
} }
} }
totalPrice { totalPrice {
net {
amount
}
tax { tax {
amount amount
} }

View file

@ -8,6 +8,9 @@ fragment OrderLine on OrderLine {
} }
} }
totalPrice { totalPrice {
net {
amount
}
tax { tax {
amount amount
} }

View file

@ -34,12 +34,12 @@ const MOCKED_CALCULATE_TAXES_ARGS: AvataxCalculateTaxesMapPayloadArgs = {
lines: [ lines: [
{ {
chargeTaxes: true, chargeTaxes: true,
quantity: 1, quantity: 3,
unitPrice: { unitPrice: {
amount: 84, amount: 84,
}, },
totalPrice: { totalPrice: {
amount: 84, amount: 252,
}, },
sourceLine: { sourceLine: {
__typename: "OrderLine", __typename: "OrderLine",
@ -114,17 +114,36 @@ describe("avataxCalculateTaxesMaps", () => {
it.todo("rounding of numbers"); it.todo("rounding of numbers");
}); });
describe("mapLines", () => { describe("mapLines", () => {
it("includes shipping as a line", () => { const lines = avataxCalculateTaxesMaps.mapLines(
const lines = avataxCalculateTaxesMaps.mapLines( MOCKED_CALCULATE_TAXES_ARGS.taxBase,
MOCKED_CALCULATE_TAXES_ARGS.taxBase, MOCKED_CALCULATE_TAXES_ARGS.config
MOCKED_CALCULATE_TAXES_ARGS.config );
);
it("includes shipping as a line", () => {
expect(lines).toContainEqual({ expect(lines).toContainEqual({
itemCode: avataxCalculateTaxesMaps.shippingItemCode, itemCode: avataxCalculateTaxesMaps.shippingItemCode,
quantity: 1, quantity: 1,
amount: 48.33, amount: 48.33,
taxCode: MOCKED_CALCULATE_TAXES_ARGS.config.shippingTaxCode, taxCode: MOCKED_CALCULATE_TAXES_ARGS.config.shippingTaxCode,
taxIncluded: true,
});
});
it("returns the correct quantity of individual lines", () => {
expect(lines).toContainEqual({
quantity: 3,
amount: 252,
taxCode: "",
taxIncluded: true,
});
});
it("returns the correct quantity of individual lines", () => {
expect(lines).toContainEqual({
quantity: 3,
amount: 252,
taxCode: "",
taxIncluded: true,
}); });
}); });
}); });

View file

@ -18,7 +18,7 @@ const SHIPPING_ITEM_CODE = "Shipping";
function mapLines(taxBase: TaxBaseFragment, config: AvataxConfig): LineItemModel[] { function mapLines(taxBase: TaxBaseFragment, config: AvataxConfig): LineItemModel[] {
const productLines = taxBase.lines.map((line) => ({ const productLines = taxBase.lines.map((line) => ({
amount: line.unitPrice.amount, amount: line.totalPrice.amount,
taxIncluded: line.chargeTaxes, taxIncluded: line.chargeTaxes,
// todo: get from tax code matcher // todo: get from tax code matcher
taxCode: "", taxCode: "",
@ -32,6 +32,7 @@ function mapLines(taxBase: TaxBaseFragment, config: AvataxConfig): LineItemModel
itemCode: SHIPPING_ITEM_CODE, itemCode: SHIPPING_ITEM_CODE,
taxCode: config.shippingTaxCode, taxCode: config.shippingTaxCode,
quantity: 1, quantity: 1,
taxIncluded: true,
}; };
return [...productLines, shippingLine]; return [...productLines, shippingLine];

View file

@ -59,13 +59,16 @@ const MOCKED_ARGS: CreateTransactionMapPayloadArgs = {
{ {
productSku: "328223581", productSku: "328223581",
productName: "Monospace Tee", productName: "Monospace Tee",
quantity: 1, quantity: 3,
unitPrice: { unitPrice: {
net: { net: {
amount: 90, amount: 90,
}, },
}, },
totalPrice: { totalPrice: {
net: {
amount: 270,
},
tax: { tax: {
amount: 8.55, amount: 8.55,
}, },
@ -80,7 +83,11 @@ const MOCKED_ARGS: CreateTransactionMapPayloadArgs = {
amount: 45, amount: 45,
}, },
}, },
totalPrice: { totalPrice: {
net: {
amount: 45,
},
tax: { tax: {
amount: 4.28, amount: 4.28,
}, },
@ -156,6 +163,7 @@ describe("avataxOrderCreatedMaps", () => {
taxCode: MOCKED_ARGS.config.shippingTaxCode, taxCode: MOCKED_ARGS.config.shippingTaxCode,
quantity: 1, quantity: 1,
amount: 48.33, amount: 48.33,
taxIncluded: true,
}); });
}); });
@ -165,8 +173,8 @@ describe("avataxOrderCreatedMaps", () => {
expect(first).toContain({ expect(first).toContain({
itemCode: "328223581", itemCode: "328223581",
description: "Monospace Tee", description: "Monospace Tee",
quantity: 1, quantity: 3,
amount: 90, amount: 270,
}); });
expect(second).toContain({ expect(second).toContain({
itemCode: "328223580", itemCode: "328223580",

View file

@ -16,7 +16,7 @@ const SHIPPING_ITEM_CODE = "Shipping";
function mapLines(order: OrderCreatedSubscriptionFragment, config: AvataxConfig): LineItemModel[] { function mapLines(order: OrderCreatedSubscriptionFragment, config: AvataxConfig): LineItemModel[] {
const productLines: LineItemModel[] = order.lines.map((line) => ({ const productLines: LineItemModel[] = order.lines.map((line) => ({
amount: line.unitPrice.net.amount, amount: line.totalPrice.net.amount,
// todo: get from tax code matcher // todo: get from tax code matcher
taxCode: "", taxCode: "",
quantity: line.quantity, quantity: line.quantity,

View file

@ -66,6 +66,9 @@ const MOCKED_MAP_PAYLOAD_ARGS: CommitTransactionMapPayloadArgs = {
}, },
}, },
totalPrice: { totalPrice: {
net: {
amount: 90,
},
tax: { tax: {
amount: 8.55, amount: 8.55,
}, },
@ -81,6 +84,9 @@ const MOCKED_MAP_PAYLOAD_ARGS: CommitTransactionMapPayloadArgs = {
}, },
}, },
totalPrice: { totalPrice: {
net: {
amount: 45,
},
tax: { tax: {
amount: 4.28, amount: 4.28,
}, },

View file

@ -66,6 +66,9 @@ const MOCKED_ORDER: TaxJarOrderCreatedMapPayloadArgs = {
}, },
}, },
totalPrice: { totalPrice: {
net: {
amount: 90,
},
tax: { tax: {
amount: 8.55, amount: 8.55,
}, },
@ -81,6 +84,9 @@ const MOCKED_ORDER: TaxJarOrderCreatedMapPayloadArgs = {
}, },
}, },
totalPrice: { totalPrice: {
net: {
amount: 45,
},
tax: { tax: {
amount: 4.28, amount: 4.28,
}, },
@ -165,22 +171,5 @@ describe("taxJarOrderCreatedMaps", () => {
expect(result).toBe(412.1); expect(result).toBe(412.1);
}); });
it("returns the rounded sum of all line items when line items n of decimals > 2", () => {
const result = taxJarOrderCreatedMaps.sumLines([
{
quantity: 3,
unit_price: 10.256,
product_identifier: "328223581",
},
{
quantity: 2,
unit_price: 50.512,
product_identifier: "328223580",
},
]);
expect(result).toBe(131.79);
});
}); });
}); });

View file

@ -32,7 +32,7 @@ export type TaxJarOrderCreatedMapPayloadArgs = {
const mapPayload = ({ order, channel }: TaxJarOrderCreatedMapPayloadArgs): CreateOrderArgs => { const mapPayload = ({ order, channel }: TaxJarOrderCreatedMapPayloadArgs): CreateOrderArgs => {
const lineItems = mapLines(order.lines); const lineItems = mapLines(order.lines);
const lineSum = sumLines(lineItems); const lineSum = sumLines(lineItems);
const shippingAmount = order.shippingPrice.net.amount; const shippingAmount = order.shippingPrice.gross.amount;
/** /**
* "The TaxJar API performs arbitrary-precision decimal arithmetic for accurately calculating sales tax." * "The TaxJar API performs arbitrary-precision decimal arithmetic for accurately calculating sales tax."
* but we want to round to 2 decimals for consistency * but we want to round to 2 decimals for consistency