fix(medusa): Add tax inclusive flag to return lines from line item (#2909)
This commit is contained in:
1 parent
90d774f563
commit
eda26f6e81
4 files changed
+133
-28
No files matched your search
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/medusa": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
fix(medusa): Add tax inclusive flag to return lines from line item
|
||||||
@@ -11,10 +11,17 @@ const {
|
|||||||
simpleRegionFactory,
|
simpleRegionFactory,
|
||||||
simpleShippingOptionFactory,
|
simpleShippingOptionFactory,
|
||||||
simpleOrderFactory,
|
simpleOrderFactory,
|
||||||
|
simpleProductFactory,
|
||||||
} = require("../../../factories")
|
} = require("../../../factories")
|
||||||
|
|
||||||
jest.setTimeout(30000)
|
jest.setTimeout(30000)
|
||||||
|
|
||||||
|
const adminReqConfig = {
|
||||||
|
headers: {
|
||||||
|
Authorization: "Bearer test_token",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
describe("[MEDUSA_FF_TAX_INCLUSIVE_PRICING] /admin/orders", () => {
|
describe("[MEDUSA_FF_TAX_INCLUSIVE_PRICING] /admin/orders", () => {
|
||||||
let medusaProcess
|
let medusaProcess
|
||||||
let dbConnection
|
let dbConnection
|
||||||
@@ -83,11 +90,7 @@ describe("[MEDUSA_FF_TAX_INCLUSIVE_PRICING] /admin/orders", () => {
|
|||||||
option_id: includesTaxShippingOption.id,
|
option_id: includesTaxShippingOption.id,
|
||||||
price: 10,
|
price: 10,
|
||||||
},
|
},
|
||||||
{
|
adminReqConfig
|
||||||
headers: {
|
|
||||||
Authorization: "Bearer test_token",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
expect(orderWithShippingMethodRes.status).toEqual(200)
|
expect(orderWithShippingMethodRes.status).toEqual(200)
|
||||||
@@ -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)
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
@@ -2,20 +2,23 @@ import { MedusaError } from "medusa-core-utils"
|
|||||||
import { EntityManager, In } from "typeorm"
|
import { EntityManager, In } from "typeorm"
|
||||||
import { DeepPartial } from "typeorm/common/DeepPartial"
|
import { DeepPartial } from "typeorm/common/DeepPartial"
|
||||||
|
|
||||||
import { CartRepository } from "../repositories/cart"
|
import { TransactionBaseService } from "../interfaces"
|
||||||
import { LineItemRepository } from "../repositories/line-item"
|
import OrderEditingFeatureFlag from "../loaders/feature-flags/order-editing"
|
||||||
import { LineItemTaxLineRepository } from "../repositories/line-item-tax-line"
|
import TaxInclusivePricingFeatureFlag from "../loaders/feature-flags/tax-inclusive-pricing"
|
||||||
import {
|
import {
|
||||||
LineItem,
|
LineItem,
|
||||||
LineItemAdjustment,
|
LineItemAdjustment,
|
||||||
LineItemTaxLine,
|
LineItemTaxLine,
|
||||||
ProductVariant,
|
ProductVariant,
|
||||||
} from "../models"
|
} 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 { 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 { 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 {
|
import {
|
||||||
PricingService,
|
PricingService,
|
||||||
ProductService,
|
ProductService,
|
||||||
@@ -23,10 +26,7 @@ import {
|
|||||||
RegionService,
|
RegionService,
|
||||||
TaxProviderService,
|
TaxProviderService,
|
||||||
} from "./index"
|
} from "./index"
|
||||||
import { buildQuery, isString, setMetadata } from "../utils"
|
import LineItemAdjustmentService from "./line-item-adjustment"
|
||||||
import { TransactionBaseService } from "../interfaces"
|
|
||||||
import { GenerateInputData, GenerateLineItemContext } from "../types/line-item"
|
|
||||||
import { ProductVariantPricing } from "../types/pricing"
|
|
||||||
|
|
||||||
type InjectedDependencies = {
|
type InjectedDependencies = {
|
||||||
manager: EntityManager
|
manager: EntityManager
|
||||||
@@ -159,6 +159,7 @@ class LineItemService extends TransactionBaseService {
|
|||||||
unit_price: -1 * lineItem.unit_price,
|
unit_price: -1 * lineItem.unit_price,
|
||||||
quantity: lineItem.return_item.quantity,
|
quantity: lineItem.return_item.quantity,
|
||||||
allow_discounts: lineItem.allow_discounts,
|
allow_discounts: lineItem.allow_discounts,
|
||||||
|
includes_tax: !!lineItem.includes_tax,
|
||||||
tax_lines: lineItem.tax_lines.map((taxLine) => {
|
tax_lines: lineItem.tax_lines.map((taxLine) => {
|
||||||
return itemTaxLineRepo.create({
|
return itemTaxLineRepo.create({
|
||||||
name: taxLine.name,
|
name: taxLine.name,
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { isDefined, MedusaError } from "medusa-core-utils"
|
import { isDefined, MedusaError } from "medusa-core-utils"
|
||||||
|
import { EntityManager } from "typeorm"
|
||||||
import {
|
import {
|
||||||
ITaxCalculationStrategy,
|
ITaxCalculationStrategy,
|
||||||
TaxCalculationContext,
|
TaxCalculationContext,
|
||||||
@@ -25,14 +26,10 @@ import {
|
|||||||
LineDiscountAmount,
|
LineDiscountAmount,
|
||||||
SubtotalOptions,
|
SubtotalOptions,
|
||||||
} from "../types/totals"
|
} from "../types/totals"
|
||||||
import {
|
import { NewTotalsService, TaxProviderService } from "./index"
|
||||||
TaxProviderService,
|
|
||||||
NewTotalsService,
|
|
||||||
} from "./index"
|
|
||||||
import { EntityManager } from "typeorm"
|
|
||||||
|
|
||||||
import { calculatePriceTaxAmount } from "../utils"
|
|
||||||
import TaxInclusivePricingFeatureFlag from "../loaders/feature-flags/tax-inclusive-pricing"
|
import TaxInclusivePricingFeatureFlag from "../loaders/feature-flags/tax-inclusive-pricing"
|
||||||
|
import { calculatePriceTaxAmount } from "../utils"
|
||||||
import { FlagRouter } from "../utils/flag-router"
|
import { FlagRouter } from "../utils/flag-router"
|
||||||
|
|
||||||
type ShippingMethodTotals = {
|
type ShippingMethodTotals = {
|
||||||
@@ -806,7 +803,7 @@ class TotalsService extends TransactionBaseService {
|
|||||||
|
|
||||||
// Tax Information
|
// Tax Information
|
||||||
if (options.include_tax) {
|
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
|
// order from the old tax system. The following is a backward compat
|
||||||
// calculation.
|
// calculation.
|
||||||
if (isOrder(cartOrOrder) && cartOrOrder.tax_rate != null) {
|
if (isOrder(cartOrOrder) && cartOrOrder.tax_rate != null) {
|
||||||
@@ -979,14 +976,11 @@ class TotalsService extends TransactionBaseService {
|
|||||||
giftCardable = subtotal - discountTotal
|
giftCardable = subtotal - discountTotal
|
||||||
}
|
}
|
||||||
|
|
||||||
return await this.newTotalsService_.getGiftCardTotals(
|
return await this.newTotalsService_.getGiftCardTotals(giftCardable, {
|
||||||
giftCardable,
|
|
||||||
{
|
|
||||||
region: cartOrOrder.region,
|
region: cartOrOrder.region,
|
||||||
giftCards: cartOrOrder.gift_cards || [],
|
giftCards: cartOrOrder.gift_cards || [],
|
||||||
giftCardTransactions: cartOrOrder['gift_card_transactions'] || []
|
giftCardTransactions: cartOrOrder["gift_card_transactions"] || [],
|
||||||
}
|
})
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in new issue
Block a user