Skip to content

Commit

Permalink
Added support for itemCode when handling order/checkout calc taxes …
Browse files Browse the repository at this point in the history
…in AvaTax (#1644)
  • Loading branch information
krzysztofzuraw authored Nov 13, 2024
1 parent a036cce commit 96c3164
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 43 deletions.
5 changes: 5 additions & 0 deletions .changeset/hungry-panthers-yawn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"app-avatax": minor
---

Added AvaTax `itemCode` support when handling order or checkout calculate taxes webhooks. After this change app will send `itemCode` to Avalara based on Saleor variant SKU or variant id.
2 changes: 2 additions & 0 deletions apps/avatax/graphql/fragments/TaxBase.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ fragment TaxBaseLine on TaxableObjectLine {
id
checkoutProductVariant: variant {
id
sku
product {
taxClass {
id
Expand All @@ -17,6 +18,7 @@ fragment TaxBaseLine on TaxableObjectLine {
id
orderProductVariant: variant {
id
sku
product {
taxClass {
id
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, it, test } from "vitest";
import { describe, expect, it } from "vitest";

import { AvataxCalculateTaxesTaxCodeMatcher } from "@/modules/avatax/calculate-taxes/avatax-calculate-taxes-tax-code-matcher";

Expand Down Expand Up @@ -33,20 +33,23 @@ describe("AvataxCalculateTaxesPayloadLinesTransformer", () => {
amount: 60,
quantity: 3,
taxCode: DEFAULT_TAX_CLASS_ID,
itemCode: expect.any(String),
taxIncluded: true,
discounted: false,
},
{
amount: 20,
quantity: 1,
taxCode: DEFAULT_TAX_CLASS_ID,
itemCode: expect.any(String),
taxIncluded: true,
discounted: false,
},
{
amount: 100,
quantity: 2,
taxCode: DEFAULT_TAX_CLASS_ID,
itemCode: expect.any(String),
taxIncluded: true,
discounted: false,
},
Expand Down Expand Up @@ -78,20 +81,23 @@ describe("AvataxCalculateTaxesPayloadLinesTransformer", () => {
amount: 60,
quantity: 3,
taxCode: DEFAULT_TAX_CLASS_ID,
itemCode: expect.any(String),
taxIncluded: true,
discounted: false,
},
{
amount: 20,
quantity: 1,
taxCode: DEFAULT_TAX_CLASS_ID,
itemCode: expect.any(String),
taxIncluded: true,
discounted: false,
},
{
amount: 100,
quantity: 2,
taxCode: DEFAULT_TAX_CLASS_ID,
itemCode: expect.any(String),
taxIncluded: true,
discounted: false,
},
Expand All @@ -114,20 +120,23 @@ describe("AvataxCalculateTaxesPayloadLinesTransformer", () => {
amount: 60,
quantity: 3,
taxCode: DEFAULT_TAX_CLASS_ID,
itemCode: expect.any(String),
taxIncluded: true,
discounted: true,
},
{
amount: 20,
quantity: 1,
taxCode: DEFAULT_TAX_CLASS_ID,
itemCode: expect.any(String),
taxIncluded: true,
discounted: true,
},
{
amount: 100,
quantity: 2,
taxCode: DEFAULT_TAX_CLASS_ID,
itemCode: expect.any(String),
taxIncluded: true,
discounted: true,
},
Expand Down Expand Up @@ -178,20 +187,23 @@ describe("AvataxCalculateTaxesPayloadLinesTransformer", () => {
amount: 60,
quantity: 3,
taxCode: DEFAULT_TAX_CLASS_ID,
itemCode: expect.any(String),
taxIncluded: true,
discounted: false,
},
{
amount: 20,
quantity: 1,
taxCode: DEFAULT_TAX_CLASS_ID,
itemCode: expect.any(String),
taxIncluded: true,
discounted: false,
},
{
amount: 100,
quantity: 2,
taxCode: DEFAULT_TAX_CLASS_ID,
itemCode: expect.any(String),
taxIncluded: true,
discounted: false,
},
Expand Down Expand Up @@ -244,20 +256,23 @@ describe("AvataxCalculateTaxesPayloadLinesTransformer", () => {
amount: 60,
quantity: 3,
taxCode: DEFAULT_TAX_CLASS_ID,
itemCode: expect.any(String),
taxIncluded: true,
discounted: true,
},
{
amount: 20,
quantity: 1,
taxCode: DEFAULT_TAX_CLASS_ID,
itemCode: expect.any(String),
taxIncluded: true,
discounted: true,
},
{
amount: 100,
quantity: 2,
taxCode: DEFAULT_TAX_CLASS_ID,
itemCode: expect.any(String),
taxIncluded: true,
discounted: true,
},
Expand Down Expand Up @@ -322,20 +337,23 @@ describe("AvataxCalculateTaxesPayloadLinesTransformer", () => {
amount: 60,
quantity: 3,
taxCode: DEFAULT_TAX_CLASS_ID,
itemCode: expect.any(String),
taxIncluded: true,
discounted: true,
},
{
amount: 20,
quantity: 1,
taxCode: DEFAULT_TAX_CLASS_ID,
itemCode: expect.any(String),
taxIncluded: true,
discounted: true,
},
{
amount: 100,
quantity: 2,
taxCode: DEFAULT_TAX_CLASS_ID,
itemCode: expect.any(String),
taxIncluded: true,
discounted: true,
},
Expand Down Expand Up @@ -381,20 +399,23 @@ describe("AvataxCalculateTaxesPayloadLinesTransformer", () => {
amount: 60,
quantity: 3,
taxCode: DEFAULT_TAX_CLASS_ID,
itemCode: expect.any(String),
taxIncluded: true,
discounted: true,
},
{
amount: 20,
quantity: 1,
taxCode: DEFAULT_TAX_CLASS_ID,
itemCode: expect.any(String),
taxIncluded: true,
discounted: true,
},
{
amount: 100,
quantity: 2,
taxCode: DEFAULT_TAX_CLASS_ID,
itemCode: expect.any(String),
taxIncluded: true,
discounted: true,
},
Expand Down Expand Up @@ -444,20 +465,23 @@ describe("AvataxCalculateTaxesPayloadLinesTransformer", () => {
amount: 60,
quantity: 3,
taxCode: DEFAULT_TAX_CLASS_ID,
itemCode: expect.any(String),
taxIncluded: true,
discounted: true,
},
{
amount: 20,
quantity: 1,
taxCode: DEFAULT_TAX_CLASS_ID,
itemCode: expect.any(String),
taxIncluded: true,
discounted: true,
},
{
amount: 100,
quantity: 2,
taxCode: DEFAULT_TAX_CLASS_ID,
itemCode: expect.any(String),
taxIncluded: true,
discounted: true,
},
Expand Down Expand Up @@ -503,20 +527,23 @@ describe("AvataxCalculateTaxesPayloadLinesTransformer", () => {
amount: 60,
quantity: 3,
taxCode: DEFAULT_TAX_CLASS_ID,
itemCode: expect.any(String),
taxIncluded: true,
discounted: false,
},
{
amount: 20,
quantity: 1,
taxCode: DEFAULT_TAX_CLASS_ID,
itemCode: expect.any(String),
taxIncluded: true,
discounted: false,
},
{
amount: 100,
quantity: 2,
taxCode: DEFAULT_TAX_CLASS_ID,
itemCode: expect.any(String),
taxIncluded: true,
discounted: false,
},
Expand Down Expand Up @@ -567,20 +594,23 @@ describe("AvataxCalculateTaxesPayloadLinesTransformer", () => {
amount: 60,
quantity: 3,
taxCode: DEFAULT_TAX_CLASS_ID,
itemCode: expect.any(String),
taxIncluded: true,
discounted: false,
},
{
amount: 20,
quantity: 1,
taxCode: DEFAULT_TAX_CLASS_ID,
itemCode: expect.any(String),
taxIncluded: true,
discounted: false,
},
{
amount: 100,
quantity: 2,
taxCode: DEFAULT_TAX_CLASS_ID,
itemCode: expect.any(String),
taxIncluded: true,
discounted: false,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { AvataxConfig } from "../avatax-connection-schema";
import { AutomaticallyDistributedProductLinesDiscountsStrategy } from "../discounts";
import { AvataxTaxCodeMatches } from "../tax-code/avatax-tax-code-match-repository";
import { AvataxCalculateTaxesTaxCodeMatcher } from "./avatax-calculate-taxes-tax-code-matcher";
import { avataxProductLine } from "./avatax-product-line";
import { AvataxProductLineCalculateTaxesFactory } from "./avatax-product-line-calculate-taxes-factory";
import { avataxShippingLine } from "./avatax-shipping-line";

export class AvataxCalculateTaxesPayloadLinesTransformer {
Expand All @@ -34,17 +34,17 @@ export class AvataxCalculateTaxesPayloadLinesTransformer {
matches: AvataxTaxCodeMatches,
discountsStrategy: AutomaticallyDistributedProductLinesDiscountsStrategy,
) {
const avataxProductLine = new AvataxProductLineCalculateTaxesFactory();
const areLinesDiscounted = discountsStrategy.areLinesDiscounted(taxBase.discounts);

// Price reduction discounts - we send totalPrices with or without discounts and let AvaTax calculate the tax
const productLines: LineItemModel[] = taxBase.lines.map((line) => {
const taxCode = this.avataxCalculateTaxesTaxCodeMatcher.match(line, matches);

return avataxProductLine.create({
amount: line.totalPrice.amount,
return avataxProductLine.createFromSaleorLine({
saleorLine: line,
taxIncluded: taxBase.pricesEnteredWithTax,
taxCode,
quantity: line.quantity,
discounted: areLinesDiscounted,
});
});
Expand Down
Loading

0 comments on commit 96c3164

Please sign in to comment.