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 {
|
||||
chargeTaxes
|
||||
sourceLine {
|
||||
__typename
|
||||
... on CheckoutLine {
|
||||
|
@ -43,6 +42,7 @@ fragment TaxDiscount on TaxableObjectDiscount {
|
|||
}
|
||||
|
||||
fragment TaxBase on TaxableObject {
|
||||
pricesEnteredWithTax
|
||||
currency
|
||||
channel {
|
||||
slug
|
||||
|
|
|
@ -7,6 +7,7 @@ import {
|
|||
// * Mocked payload data, channel config and avatax config
|
||||
const MOCKED_CALCULATE_TAXES_ARGS: AvataxCalculateTaxesMapPayloadArgs = {
|
||||
taxBase: {
|
||||
pricesEnteredWithTax: false,
|
||||
currency: "PLN",
|
||||
channel: {
|
||||
slug: "channel-pln",
|
||||
|
@ -33,7 +34,6 @@ const MOCKED_CALCULATE_TAXES_ARGS: AvataxCalculateTaxesMapPayloadArgs = {
|
|||
},
|
||||
lines: [
|
||||
{
|
||||
chargeTaxes: true,
|
||||
quantity: 3,
|
||||
unitPrice: {
|
||||
amount: 84,
|
||||
|
@ -56,7 +56,6 @@ const MOCKED_CALCULATE_TAXES_ARGS: AvataxCalculateTaxesMapPayloadArgs = {
|
|||
},
|
||||
},
|
||||
{
|
||||
chargeTaxes: true,
|
||||
quantity: 1,
|
||||
unitPrice: {
|
||||
amount: 5.99,
|
||||
|
@ -125,7 +124,7 @@ describe("avataxCalculateTaxesMaps", () => {
|
|||
quantity: 1,
|
||||
amount: 48.33,
|
||||
taxCode: MOCKED_CALCULATE_TAXES_ARGS.config.shippingTaxCode,
|
||||
taxIncluded: true,
|
||||
taxIncluded: false,
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -134,16 +133,7 @@ describe("avataxCalculateTaxesMaps", () => {
|
|||
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,
|
||||
taxIncluded: false,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -19,7 +19,7 @@ const SHIPPING_ITEM_CODE = "Shipping";
|
|||
function mapLines(taxBase: TaxBaseFragment, config: AvataxConfig): LineItemModel[] {
|
||||
const productLines = taxBase.lines.map((line) => ({
|
||||
amount: line.totalPrice.amount,
|
||||
taxIncluded: line.chargeTaxes,
|
||||
taxIncluded: taxBase.pricesEnteredWithTax,
|
||||
// todo: get from tax code matcher
|
||||
taxCode: "",
|
||||
quantity: line.quantity,
|
||||
|
@ -32,7 +32,7 @@ function mapLines(taxBase: TaxBaseFragment, config: AvataxConfig): LineItemModel
|
|||
itemCode: SHIPPING_ITEM_CODE,
|
||||
taxCode: config.shippingTaxCode,
|
||||
quantity: 1,
|
||||
taxIncluded: true,
|
||||
taxIncluded: taxBase.pricesEnteredWithTax,
|
||||
};
|
||||
|
||||
return [...productLines, shippingLine];
|
||||
|
@ -70,12 +70,14 @@ const mapPayload = (props: AvataxCalculateTaxesMapPayloadArgs): CreateTransactio
|
|||
|
||||
const mapResponse = (transaction: TransactionModel): CalculateTaxesResponse => {
|
||||
const shippingLine = transaction.lines?.find((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 shippingNetAmount = numbers.roundFloatToTwoDecimals(
|
||||
shippingGrossAmount - shippingTaxCalculated
|
||||
const shippingTaxableAmount = shippingLine?.taxableAmount ?? 0;
|
||||
const shippingGrossAmount = numbers.roundFloatToTwoDecimals(
|
||||
shippingTaxableAmount + shippingTaxCalculated
|
||||
);
|
||||
const shippingNetAmount = shippingGrossAmount;
|
||||
|
||||
return {
|
||||
shipping_price_gross_amount: shippingGrossAmount,
|
||||
|
|
|
@ -174,13 +174,13 @@ describe("avataxOrderCreatedMaps", () => {
|
|||
itemCode: "328223581",
|
||||
description: "Monospace Tee",
|
||||
quantity: 3,
|
||||
amount: 270,
|
||||
amount: 278.55,
|
||||
});
|
||||
expect(second).toContain({
|
||||
itemCode: "328223580",
|
||||
description: "Polyspace Tee",
|
||||
quantity: 1,
|
||||
amount: 45,
|
||||
amount: 49.28,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -7,6 +7,7 @@ import { CreateOrderResponse } from "../../taxes/tax-provider-webhook";
|
|||
import { CreateTransactionArgs } from "../avatax-client";
|
||||
import { AvataxConfig } from "../avatax-config";
|
||||
import { avataxAddressFactory } from "./address-factory";
|
||||
import { numbers } from "../../taxes/numbers";
|
||||
|
||||
/**
|
||||
* * Shipping is a regular line item in Avatax
|
||||
|
@ -16,7 +17,10 @@ const SHIPPING_ITEM_CODE = "Shipping";
|
|||
|
||||
function mapLines(order: OrderCreatedSubscriptionFragment, config: AvataxConfig): LineItemModel[] {
|
||||
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
|
||||
taxCode: "",
|
||||
quantity: line.quantity,
|
||||
|
|
|
@ -1,9 +1,5 @@
|
|||
import { TaxForOrderRes } from "taxjar/dist/types/returnTypes";
|
||||
import {
|
||||
TaxBaseFragment,
|
||||
TaxBaseLineFragment,
|
||||
TaxDiscountFragment,
|
||||
} from "../../../../generated/graphql";
|
||||
import { TaxBaseFragment } from "../../../../generated/graphql";
|
||||
import { ChannelConfig } from "../../channels-configuration/channels-config";
|
||||
import { taxLineResolver } from "../../taxes/tax-line-resolver";
|
||||
import { CalculateTaxesResponse } from "../../taxes/tax-provider-webhook";
|
||||
|
@ -25,9 +21,9 @@ type FetchTaxesLinePayload = {
|
|||
};
|
||||
|
||||
const prepareLinesWithDiscountPayload = (
|
||||
lines: Array<TaxBaseLineFragment>,
|
||||
discounts: Array<TaxDiscountFragment>
|
||||
taxBase: TaxBaseFragment
|
||||
): Array<FetchTaxesLinePayload> => {
|
||||
const { lines, discounts } = taxBase;
|
||||
const allLinesTotal = lines.reduce(
|
||||
(total, current) => total + Number(current.totalPrice.amount),
|
||||
0
|
||||
|
@ -47,7 +43,7 @@ const prepareLinesWithDiscountPayload = (
|
|||
|
||||
return {
|
||||
id: line.sourceLine.id,
|
||||
chargeTaxes: line.chargeTaxes,
|
||||
chargeTaxes: taxBase.pricesEnteredWithTax,
|
||||
// todo: get from tax code matcher
|
||||
taxCode: "",
|
||||
quantity: line.quantity,
|
||||
|
@ -62,7 +58,7 @@ const mapResponse = (
|
|||
payload: TaxBaseFragment,
|
||||
response: TaxForOrderRes
|
||||
): CalculateTaxesResponse => {
|
||||
const linesWithDiscount = prepareLinesWithDiscountPayload(payload.lines, payload.discounts);
|
||||
const linesWithDiscount = prepareLinesWithDiscountPayload(payload);
|
||||
const linesWithChargeTaxes = linesWithDiscount.filter((line) => line.chargeTaxes === true);
|
||||
|
||||
const taxResponse = linesWithChargeTaxes.length !== 0 ? response : undefined;
|
||||
|
@ -108,7 +104,7 @@ const mapResponse = (
|
|||
};
|
||||
|
||||
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 taxParams = {
|
||||
|
|
Loading…
Reference in a new issue