Skip to content

Commit

Permalink
fix(medusa): Add tax inclusive flag to return lines from line item (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
olivermrbl authored Dec 29, 2022
1 parent 90d774f commit eda26f6
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 30 deletions.
5 changes: 5 additions & 0 deletions .changeset/quiet-lobsters-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/medusa": patch
---

fix(medusa): Add tax inclusive flag to return lines from line item
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,17 @@ const {
simpleRegionFactory,
simpleShippingOptionFactory,
simpleOrderFactory,
simpleProductFactory,
} = require("../../../factories")

jest.setTimeout(30000)

const adminReqConfig = {
headers: {
Authorization: "Bearer test_token",
},
}

describe("[MEDUSA_FF_TAX_INCLUSIVE_PRICING] /admin/orders", () => {
let medusaProcess
let dbConnection
Expand Down Expand Up @@ -83,11 +90,7 @@ describe("[MEDUSA_FF_TAX_INCLUSIVE_PRICING] /admin/orders", () => {
option_id: includesTaxShippingOption.id,
price: 10,
},
{
headers: {
Authorization: "Bearer test_token",
},
}
adminReqConfig
)

expect(orderWithShippingMethodRes.status).toEqual(200)
Expand All @@ -101,4 +104,106 @@ describe("[MEDUSA_FF_TAX_INCLUSIVE_PRICING] /admin/orders", () => {
)
})
})

describe("POST /admin/orders/:id/swaps", () => {
const prodId = "prod_1234"
const variant1 = "variant_1234"
const variant2 = "variant_5678"
const regionId = "test-region"
const lineItemId = "litem_1234"
const orderId = "order_1234"

beforeEach(async () => {
await adminSeeder(dbConnection)

await simpleRegionFactory(dbConnection, {
id: regionId,
includes_tax: true,
currency_code: "usd",
tax_rate: 10,
})

await simpleProductFactory(dbConnection, {
id: prodId,
variants: [
{
id: variant1,
prices: [{ currency: "usd", amount: 1000, region_id: regionId }],
},
{
id: variant2,
prices: [{ currency: "usd", amount: 1000, region_id: regionId }],
},
],
})

await simpleOrderFactory(dbConnection, {
id: orderId,
email: "test@testson.com",
fulfillment_status: "fulfilled",
payment_status: "captured",
region: regionId,
currency_code: "usd",
line_items: [
{
id: lineItemId,
variant_id: variant1,
tax_lines: [
{
rate: 10,
name: "VAT",
code: "vat",
},
],
unit_price: 1000,
includes_tax: true,
quantity: 1,
fulfilled_quantity: 1,
shipped_quantity: 1,
},
],
})
})

afterEach(async () => {
const db = useDb()
await db.teardown()
})

it("creates a swap with tax inclusive return lines", async () => {
const api = useApi()

const response = await api.post(
`/admin/orders/${orderId}/swaps`,
{
return_items: [
{
item_id: lineItemId,
quantity: 1,
},
],
additional_items: [{ variant_id: variant2, quantity: 1 }],
},
adminReqConfig
)

let swap = response.data.order.swaps[0]
const order = response.data.order

swap = await api.get(`/admin/swaps/${swap.id}`, adminReqConfig)

swap = swap.data.swap

let swapCart = await api.get(`/store/carts/${swap.cart_id}`)

swapCart = swapCart.data.cart

const returnedItemOnSwap = swapCart.items.find((itm) => itm.is_return)
const returnedItemOnOrder = order.items[0]

expect(returnedItemOnSwap.total).toEqual(-1000)
expect(returnedItemOnOrder.total).toEqual(1000)
expect(response.status).toEqual(200)
})
})
})
21 changes: 11 additions & 10 deletions packages/medusa/src/services/line-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,31 @@ import { MedusaError } from "medusa-core-utils"
import { EntityManager, In } from "typeorm"
import { DeepPartial } from "typeorm/common/DeepPartial"

import { CartRepository } from "../repositories/cart"
import { LineItemRepository } from "../repositories/line-item"
import { LineItemTaxLineRepository } from "../repositories/line-item-tax-line"
import { TransactionBaseService } from "../interfaces"
import OrderEditingFeatureFlag from "../loaders/feature-flags/order-editing"
import TaxInclusivePricingFeatureFlag from "../loaders/feature-flags/tax-inclusive-pricing"
import {
LineItem,
LineItemAdjustment,
LineItemTaxLine,
ProductVariant,
} from "../models"
import { CartRepository } from "../repositories/cart"
import { LineItemRepository } from "../repositories/line-item"
import { LineItemTaxLineRepository } from "../repositories/line-item-tax-line"
import { FindConfig, Selector } from "../types/common"
import { GenerateInputData, GenerateLineItemContext } from "../types/line-item"
import { ProductVariantPricing } from "../types/pricing"
import { buildQuery, isString, setMetadata } from "../utils"
import { FlagRouter } from "../utils/flag-router"
import LineItemAdjustmentService from "./line-item-adjustment"
import OrderEditingFeatureFlag from "../loaders/feature-flags/order-editing"
import TaxInclusivePricingFeatureFlag from "../loaders/feature-flags/tax-inclusive-pricing"
import {
PricingService,
ProductService,
ProductVariantService,
RegionService,
TaxProviderService,
} from "./index"
import { buildQuery, isString, setMetadata } from "../utils"
import { TransactionBaseService } from "../interfaces"
import { GenerateInputData, GenerateLineItemContext } from "../types/line-item"
import { ProductVariantPricing } from "../types/pricing"
import LineItemAdjustmentService from "./line-item-adjustment"

type InjectedDependencies = {
manager: EntityManager
Expand Down Expand Up @@ -159,6 +159,7 @@ class LineItemService extends TransactionBaseService {
unit_price: -1 * lineItem.unit_price,
quantity: lineItem.return_item.quantity,
allow_discounts: lineItem.allow_discounts,
includes_tax: !!lineItem.includes_tax,
tax_lines: lineItem.tax_lines.map((taxLine) => {
return itemTaxLineRepo.create({
name: taxLine.name,
Expand Down
24 changes: 9 additions & 15 deletions packages/medusa/src/services/totals.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { isDefined, MedusaError } from "medusa-core-utils"
import { EntityManager } from "typeorm"
import {
ITaxCalculationStrategy,
TaxCalculationContext,
Expand All @@ -25,14 +26,10 @@ import {
LineDiscountAmount,
SubtotalOptions,
} from "../types/totals"
import {
TaxProviderService,
NewTotalsService,
} from "./index"
import { EntityManager } from "typeorm"
import { NewTotalsService, TaxProviderService } from "./index"

import { calculatePriceTaxAmount } from "../utils"
import TaxInclusivePricingFeatureFlag from "../loaders/feature-flags/tax-inclusive-pricing"
import { calculatePriceTaxAmount } from "../utils"
import { FlagRouter } from "../utils/flag-router"

type ShippingMethodTotals = {
Expand Down Expand Up @@ -806,7 +803,7 @@ class TotalsService extends TransactionBaseService {

// Tax Information
if (options.include_tax) {
// When we have an order with a nulled or undefined tax rate we know that it is an
// When we have an order with a tax rate we know that it is an
// order from the old tax system. The following is a backward compat
// calculation.
if (isOrder(cartOrOrder) && cartOrOrder.tax_rate != null) {
Expand Down Expand Up @@ -979,14 +976,11 @@ class TotalsService extends TransactionBaseService {
giftCardable = subtotal - discountTotal
}

return await this.newTotalsService_.getGiftCardTotals(
giftCardable,
{
region: cartOrOrder.region,
giftCards: cartOrOrder.gift_cards || [],
giftCardTransactions: cartOrOrder['gift_card_transactions'] || []
}
)
return await this.newTotalsService_.getGiftCardTotals(giftCardable, {
region: cartOrOrder.region,
giftCards: cartOrOrder.gift_cards || [],
giftCardTransactions: cartOrOrder["gift_card_transactions"] || [],
})
}

/**
Expand Down

0 comments on commit eda26f6

Please sign in to comment.