Skip to content

reset tax rate to 0 if taxable amount is zero #1726

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/gold-dryers-talk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"app-avatax": patch
---

Changed behavior how tax rate is calculated. It was possible that AvaTax return a non-zero rate, while the actual tax was 0 (for example - product is normally taxable, but this transaction has tax exemption).

Previously, app returned original, non-zero tax rate as the response together with 0 tax.

Now, if taxableAmount or total net amount is 0, rate will be also zero
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { TransactionModel } from "avatax/lib/models/TransactionModel";
import { describe, expect, it } from "vitest";

import { avataxMockFactory } from "../avatax-mock-factory";
import { AvataxCalculateTaxesResponseLinesTransformer } from "./avatax-calculate-taxes-response-lines-transformer";

const transformer = new AvataxCalculateTaxesResponseLinesTransformer();

const TAXABLE_TRANSACTION_MOCK = avataxMockFactory.createMockTransaction("taxIncludedShipping");
const NON_TAXABLE_TRANSACTION_MOCK = avataxMockFactory.createMockTransaction("nonTaxable");
const NON_TAXABLE_TRANSACTION_MOCK_WITH_DISCOUNT =
avataxMockFactory.createMockTransaction("nonTaxableWithDiscount");
Expand Down Expand Up @@ -85,4 +87,38 @@ describe("AvataxCalculateTaxesResponseLinesTransformer", () => {
},
]);
});

/**
* AvaTax will return non-zero rate even if item is not taxable or there is some tax exemption.
* That's why we overwrite the rate to be effectively zero too.
*/
describe("GIVEN non-zero tax rate from AvaTax", () => {
const transaction = structuredClone(TAXABLE_TRANSACTION_MOCK);

// Assert only first line
transaction.lines![0].details![0].rate = 0.1;

describe("AND calculated tax from AvaTax is zero", () => {
const localTransaction = structuredClone(transaction);

localTransaction!.lines![0].taxCalculated = 0;

it("Should return tax rate as zero", () => {
const result = transformer.transform(localTransaction);

expect(result[0].tax_rate).toBe(0);
});
});
describe("AND taxableAmount from AvaTax is zero", () => {
const localTransaction = structuredClone(transaction);

localTransaction!.lines![0].taxableAmount = 0;

it("Should return tax rate as zero", () => {
const result = transformer.transform(localTransaction);

expect(result[0].tax_rate).toBe(0);
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class AvataxCalculateTaxesResponseLinesTransformer {
};
}

const rate = extractIntegerRateFromTaxDetailsRates(
let rate = extractIntegerRateFromTaxDetailsRates(
line.details?.map((details) => details.rate),
);

Expand All @@ -69,6 +69,18 @@ export class AvataxCalculateTaxesResponseLinesTransformer {
.toDecimalPlaces(2)
.toNumber();

/**
* Avalara will return non-zero rate as a standard rate, but
* it's possible that there is a tax exemption. So the tax effectively
* is zero, but rate is not.
*
* In this scenario, we reset rate to 0, so Saleor properly apply such rate in further
* calculations
*/
if (lineTaxCalculated === 0 || lineTotalNetAmount === 0) {
rate = 0;
}

this.logger.info(
"Transforming taxable product line from AvaTax to Saleor CalculateTaxesResponse",
{
Expand Down
Loading