fix: charge taxes setting (#466)
* fix: 🐛 respect pricesEnteredWithTax setting * build: 👷 add changeset
This commit is contained in:
parent
1fd0960562
commit
70bf546e28
7 changed files with 29 additions and 32 deletions
5
.changeset/silly-grapes-double.md
Normal file
5
.changeset/silly-grapes-double.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
"saleor-app-taxes": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fix the calculations of shipping price and line prices based on the `pricesEnteredWithTax` value. Before, the Tax App didn't consider the `pricesEnteredWithTax` setting. Now, it will return different values for `pricesEnteredWithTax` true/false.
|
|
@ -1,5 +1,4 @@
|
||||||
fragment TaxBaseLine on TaxableObjectLine {
|
fragment TaxBaseLine on TaxableObjectLine {
|
||||||
chargeTaxes
|
|
||||||
sourceLine {
|
sourceLine {
|
||||||
__typename
|
__typename
|
||||||
... on CheckoutLine {
|
... on CheckoutLine {
|
||||||
|
@ -43,6 +42,7 @@ fragment TaxDiscount on TaxableObjectDiscount {
|
||||||
}
|
}
|
||||||
|
|
||||||
fragment TaxBase on TaxableObject {
|
fragment TaxBase on TaxableObject {
|
||||||
|
pricesEnteredWithTax
|
||||||
currency
|
currency
|
||||||
channel {
|
channel {
|
||||||
slug
|
slug
|
||||||
|
|
|
@ -7,6 +7,7 @@ import {
|
||||||
// * Mocked payload data, channel config and avatax config
|
// * Mocked payload data, channel config and avatax config
|
||||||
const MOCKED_CALCULATE_TAXES_ARGS: AvataxCalculateTaxesMapPayloadArgs = {
|
const MOCKED_CALCULATE_TAXES_ARGS: AvataxCalculateTaxesMapPayloadArgs = {
|
||||||
taxBase: {
|
taxBase: {
|
||||||
|
pricesEnteredWithTax: false,
|
||||||
currency: "PLN",
|
currency: "PLN",
|
||||||
channel: {
|
channel: {
|
||||||
slug: "channel-pln",
|
slug: "channel-pln",
|
||||||
|
@ -33,7 +34,6 @@ const MOCKED_CALCULATE_TAXES_ARGS: AvataxCalculateTaxesMapPayloadArgs = {
|
||||||
},
|
},
|
||||||
lines: [
|
lines: [
|
||||||
{
|
{
|
||||||
chargeTaxes: true,
|
|
||||||
quantity: 3,
|
quantity: 3,
|
||||||
unitPrice: {
|
unitPrice: {
|
||||||
amount: 84,
|
amount: 84,
|
||||||
|
@ -56,7 +56,6 @@ const MOCKED_CALCULATE_TAXES_ARGS: AvataxCalculateTaxesMapPayloadArgs = {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
chargeTaxes: true,
|
|
||||||
quantity: 1,
|
quantity: 1,
|
||||||
unitPrice: {
|
unitPrice: {
|
||||||
amount: 5.99,
|
amount: 5.99,
|
||||||
|
@ -125,7 +124,7 @@ describe("avataxCalculateTaxesMaps", () => {
|
||||||
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,
|
taxIncluded: false,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -134,16 +133,7 @@ describe("avataxCalculateTaxesMaps", () => {
|
||||||
quantity: 3,
|
quantity: 3,
|
||||||
amount: 252,
|
amount: 252,
|
||||||
taxCode: "",
|
taxCode: "",
|
||||||
taxIncluded: true,
|
taxIncluded: false,
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it("returns the correct quantity of individual lines", () => {
|
|
||||||
expect(lines).toContainEqual({
|
|
||||||
quantity: 3,
|
|
||||||
amount: 252,
|
|
||||||
taxCode: "",
|
|
||||||
taxIncluded: true,
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -19,7 +19,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.totalPrice.amount,
|
amount: line.totalPrice.amount,
|
||||||
taxIncluded: line.chargeTaxes,
|
taxIncluded: taxBase.pricesEnteredWithTax,
|
||||||
// todo: get from tax code matcher
|
// todo: get from tax code matcher
|
||||||
taxCode: "",
|
taxCode: "",
|
||||||
quantity: line.quantity,
|
quantity: line.quantity,
|
||||||
|
@ -32,7 +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,
|
taxIncluded: taxBase.pricesEnteredWithTax,
|
||||||
};
|
};
|
||||||
|
|
||||||
return [...productLines, shippingLine];
|
return [...productLines, shippingLine];
|
||||||
|
@ -70,12 +70,14 @@ const mapPayload = (props: AvataxCalculateTaxesMapPayloadArgs): CreateTransactio
|
||||||
|
|
||||||
const mapResponse = (transaction: TransactionModel): CalculateTaxesResponse => {
|
const mapResponse = (transaction: TransactionModel): CalculateTaxesResponse => {
|
||||||
const shippingLine = transaction.lines?.find((line) => line.itemCode === SHIPPING_ITEM_CODE);
|
const shippingLine = transaction.lines?.find((line) => line.itemCode === SHIPPING_ITEM_CODE);
|
||||||
|
|
||||||
const productLines = transaction.lines?.filter((line) => line.itemCode !== SHIPPING_ITEM_CODE);
|
const productLines = transaction.lines?.filter((line) => line.itemCode !== SHIPPING_ITEM_CODE);
|
||||||
const shippingGrossAmount = shippingLine?.taxableAmount ?? 0;
|
|
||||||
const shippingTaxCalculated = shippingLine?.taxCalculated ?? 0;
|
const shippingTaxCalculated = shippingLine?.taxCalculated ?? 0;
|
||||||
const shippingNetAmount = numbers.roundFloatToTwoDecimals(
|
const shippingTaxableAmount = shippingLine?.taxableAmount ?? 0;
|
||||||
shippingGrossAmount - shippingTaxCalculated
|
const shippingGrossAmount = numbers.roundFloatToTwoDecimals(
|
||||||
|
shippingTaxableAmount + shippingTaxCalculated
|
||||||
);
|
);
|
||||||
|
const shippingNetAmount = shippingGrossAmount;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
shipping_price_gross_amount: shippingGrossAmount,
|
shipping_price_gross_amount: shippingGrossAmount,
|
||||||
|
|
|
@ -174,13 +174,13 @@ describe("avataxOrderCreatedMaps", () => {
|
||||||
itemCode: "328223581",
|
itemCode: "328223581",
|
||||||
description: "Monospace Tee",
|
description: "Monospace Tee",
|
||||||
quantity: 3,
|
quantity: 3,
|
||||||
amount: 270,
|
amount: 278.55,
|
||||||
});
|
});
|
||||||
expect(second).toContain({
|
expect(second).toContain({
|
||||||
itemCode: "328223580",
|
itemCode: "328223580",
|
||||||
description: "Polyspace Tee",
|
description: "Polyspace Tee",
|
||||||
quantity: 1,
|
quantity: 1,
|
||||||
amount: 45,
|
amount: 49.28,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -7,6 +7,7 @@ import { CreateOrderResponse } from "../../taxes/tax-provider-webhook";
|
||||||
import { CreateTransactionArgs } from "../avatax-client";
|
import { CreateTransactionArgs } from "../avatax-client";
|
||||||
import { AvataxConfig } from "../avatax-config";
|
import { AvataxConfig } from "../avatax-config";
|
||||||
import { avataxAddressFactory } from "./address-factory";
|
import { avataxAddressFactory } from "./address-factory";
|
||||||
|
import { numbers } from "../../taxes/numbers";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* * Shipping is a regular line item in Avatax
|
* * Shipping is a regular line item in Avatax
|
||||||
|
@ -16,7 +17,10 @@ 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.totalPrice.net.amount,
|
taxIncluded: true,
|
||||||
|
amount: numbers.roundFloatToTwoDecimals(
|
||||||
|
line.totalPrice.net.amount + line.totalPrice.tax.amount
|
||||||
|
),
|
||||||
// todo: get from tax code matcher
|
// todo: get from tax code matcher
|
||||||
taxCode: "",
|
taxCode: "",
|
||||||
quantity: line.quantity,
|
quantity: line.quantity,
|
||||||
|
|
|
@ -1,9 +1,5 @@
|
||||||
import { TaxForOrderRes } from "taxjar/dist/types/returnTypes";
|
import { TaxForOrderRes } from "taxjar/dist/types/returnTypes";
|
||||||
import {
|
import { TaxBaseFragment } from "../../../../generated/graphql";
|
||||||
TaxBaseFragment,
|
|
||||||
TaxBaseLineFragment,
|
|
||||||
TaxDiscountFragment,
|
|
||||||
} from "../../../../generated/graphql";
|
|
||||||
import { ChannelConfig } from "../../channels-configuration/channels-config";
|
import { ChannelConfig } from "../../channels-configuration/channels-config";
|
||||||
import { taxLineResolver } from "../../taxes/tax-line-resolver";
|
import { taxLineResolver } from "../../taxes/tax-line-resolver";
|
||||||
import { CalculateTaxesResponse } from "../../taxes/tax-provider-webhook";
|
import { CalculateTaxesResponse } from "../../taxes/tax-provider-webhook";
|
||||||
|
@ -25,9 +21,9 @@ type FetchTaxesLinePayload = {
|
||||||
};
|
};
|
||||||
|
|
||||||
const prepareLinesWithDiscountPayload = (
|
const prepareLinesWithDiscountPayload = (
|
||||||
lines: Array<TaxBaseLineFragment>,
|
taxBase: TaxBaseFragment
|
||||||
discounts: Array<TaxDiscountFragment>
|
|
||||||
): Array<FetchTaxesLinePayload> => {
|
): Array<FetchTaxesLinePayload> => {
|
||||||
|
const { lines, discounts } = taxBase;
|
||||||
const allLinesTotal = lines.reduce(
|
const allLinesTotal = lines.reduce(
|
||||||
(total, current) => total + Number(current.totalPrice.amount),
|
(total, current) => total + Number(current.totalPrice.amount),
|
||||||
0
|
0
|
||||||
|
@ -47,7 +43,7 @@ const prepareLinesWithDiscountPayload = (
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: line.sourceLine.id,
|
id: line.sourceLine.id,
|
||||||
chargeTaxes: line.chargeTaxes,
|
chargeTaxes: taxBase.pricesEnteredWithTax,
|
||||||
// todo: get from tax code matcher
|
// todo: get from tax code matcher
|
||||||
taxCode: "",
|
taxCode: "",
|
||||||
quantity: line.quantity,
|
quantity: line.quantity,
|
||||||
|
@ -62,7 +58,7 @@ const mapResponse = (
|
||||||
payload: TaxBaseFragment,
|
payload: TaxBaseFragment,
|
||||||
response: TaxForOrderRes
|
response: TaxForOrderRes
|
||||||
): CalculateTaxesResponse => {
|
): CalculateTaxesResponse => {
|
||||||
const linesWithDiscount = prepareLinesWithDiscountPayload(payload.lines, payload.discounts);
|
const linesWithDiscount = prepareLinesWithDiscountPayload(payload);
|
||||||
const linesWithChargeTaxes = linesWithDiscount.filter((line) => line.chargeTaxes === true);
|
const linesWithChargeTaxes = linesWithDiscount.filter((line) => line.chargeTaxes === true);
|
||||||
|
|
||||||
const taxResponse = linesWithChargeTaxes.length !== 0 ? response : undefined;
|
const taxResponse = linesWithChargeTaxes.length !== 0 ? response : undefined;
|
||||||
|
@ -108,7 +104,7 @@ const mapResponse = (
|
||||||
};
|
};
|
||||||
|
|
||||||
const mapPayload = (taxBase: TaxBaseFragment, channel: ChannelConfig): FetchTaxForOrderArgs => {
|
const mapPayload = (taxBase: TaxBaseFragment, channel: ChannelConfig): FetchTaxForOrderArgs => {
|
||||||
const linesWithDiscount = prepareLinesWithDiscountPayload(taxBase.lines, taxBase.discounts);
|
const linesWithDiscount = prepareLinesWithDiscountPayload(taxBase);
|
||||||
const linesWithChargeTaxes = linesWithDiscount.filter((line) => line.chargeTaxes === true);
|
const linesWithChargeTaxes = linesWithDiscount.filter((line) => line.chargeTaxes === true);
|
||||||
|
|
||||||
const taxParams = {
|
const taxParams = {
|
||||||
|
|
Loading…
Reference in a new issue