fix(pricing): PriceLists of type Sale should not override lower prices (#10882)
Resolves CMRC-840
This commit is contained in:
+123
@@ -1167,6 +1167,129 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
])
|
||||
})
|
||||
|
||||
it("should return default prices when the price list price is higher than the default price when the price list is of type SALE", async () => {
|
||||
await createPriceLists(service, undefined, undefined, [
|
||||
{
|
||||
amount: 2500,
|
||||
currency_code: "PLN",
|
||||
price_set_id: "price-set-PLN",
|
||||
},
|
||||
{
|
||||
amount: 2500,
|
||||
currency_code: "EUR",
|
||||
price_set_id: "price-set-EUR",
|
||||
},
|
||||
])
|
||||
|
||||
const priceSetsResult = await service.calculatePrices(
|
||||
{ id: ["price-set-EUR", "price-set-PLN"] },
|
||||
{
|
||||
context: {
|
||||
currency_code: "PLN",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
expect(priceSetsResult).toEqual([
|
||||
{
|
||||
id: "price-set-PLN",
|
||||
is_calculated_price_price_list: false,
|
||||
is_calculated_price_tax_inclusive: false,
|
||||
calculated_amount: 1000,
|
||||
raw_calculated_amount: {
|
||||
value: "1000",
|
||||
precision: 20,
|
||||
},
|
||||
is_original_price_price_list: false,
|
||||
is_original_price_tax_inclusive: false,
|
||||
original_amount: 1000,
|
||||
raw_original_amount: {
|
||||
value: "1000",
|
||||
precision: 20,
|
||||
},
|
||||
currency_code: "PLN",
|
||||
calculated_price: {
|
||||
id: expect.any(String),
|
||||
price_list_id: null,
|
||||
price_list_type: null,
|
||||
min_quantity: 1,
|
||||
max_quantity: 10,
|
||||
},
|
||||
original_price: {
|
||||
id: expect.any(String),
|
||||
price_list_id: null,
|
||||
price_list_type: null,
|
||||
min_quantity: 1,
|
||||
max_quantity: 10,
|
||||
},
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
it("should return price list prices even if the price list price is higher than the default price when the price list is of type OVERRIDE", async () => {
|
||||
await createPriceLists(
|
||||
service,
|
||||
{ type: PriceListType.OVERRIDE },
|
||||
{},
|
||||
[
|
||||
{
|
||||
amount: 2500,
|
||||
currency_code: "PLN",
|
||||
price_set_id: "price-set-PLN",
|
||||
},
|
||||
{
|
||||
amount: 2500,
|
||||
currency_code: "EUR",
|
||||
price_set_id: "price-set-EUR",
|
||||
},
|
||||
]
|
||||
)
|
||||
|
||||
const priceSetsResult = await service.calculatePrices(
|
||||
{ id: ["price-set-EUR", "price-set-PLN"] },
|
||||
{
|
||||
context: {
|
||||
currency_code: "PLN",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
expect(priceSetsResult).toEqual([
|
||||
{
|
||||
id: "price-set-PLN",
|
||||
is_calculated_price_price_list: true,
|
||||
is_calculated_price_tax_inclusive: false,
|
||||
calculated_amount: 2500,
|
||||
raw_calculated_amount: {
|
||||
value: "2500",
|
||||
precision: 20,
|
||||
},
|
||||
is_original_price_price_list: true,
|
||||
is_original_price_tax_inclusive: false,
|
||||
original_amount: 2500,
|
||||
raw_original_amount: {
|
||||
value: "2500",
|
||||
precision: 20,
|
||||
},
|
||||
currency_code: "PLN",
|
||||
calculated_price: {
|
||||
id: expect.any(String),
|
||||
price_list_id: expect.any(String),
|
||||
price_list_type: "override",
|
||||
min_quantity: null,
|
||||
max_quantity: null,
|
||||
},
|
||||
original_price: {
|
||||
id: expect.any(String),
|
||||
price_list_id: expect.any(String),
|
||||
price_list_type: "override",
|
||||
min_quantity: null,
|
||||
max_quantity: null,
|
||||
},
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
it("should return price list prices when price list conditions match for override", async () => {
|
||||
await createPriceLists(service, { type: PriceListType.OVERRIDE })
|
||||
|
||||
|
||||
@@ -31,6 +31,7 @@ import {
|
||||
InjectTransactionManager,
|
||||
isPresent,
|
||||
isString,
|
||||
MathBN,
|
||||
MedusaContext,
|
||||
MedusaError,
|
||||
ModulesSdkUtils,
|
||||
@@ -50,10 +51,10 @@ import {
|
||||
PriceSet,
|
||||
} from "@models"
|
||||
|
||||
import { Collection } from "@mikro-orm/core"
|
||||
import { ServiceTypes } from "@types"
|
||||
import { eventBuilders, validatePriceListDates } from "@utils"
|
||||
import { joinerConfig } from "../joiner-config"
|
||||
import { Collection } from "@mikro-orm/core"
|
||||
|
||||
type InjectedDependencies = {
|
||||
baseRepository: DAL.RepositoryService
|
||||
@@ -288,11 +289,32 @@ export default class PricingModuleService
|
||||
let originalPrice: PricingTypes.CalculatedPriceSetDTO | undefined =
|
||||
defaultPrice
|
||||
|
||||
/**
|
||||
* When deciding which price to use we follow the following logic:
|
||||
* - If the price list is of type OVERRIDE, we always use the price list price.
|
||||
* - If the price list is of type SALE, we use the lowest price between the price list price and the default price
|
||||
*/
|
||||
if (priceListPrice) {
|
||||
calculatedPrice = priceListPrice
|
||||
switch (priceListPrice.price_list_type) {
|
||||
case PriceListType.OVERRIDE:
|
||||
calculatedPrice = priceListPrice
|
||||
originalPrice = priceListPrice
|
||||
break
|
||||
case PriceListType.SALE: {
|
||||
let lowestPrice = priceListPrice
|
||||
|
||||
if (priceListPrice.price_list_type === PriceListType.OVERRIDE) {
|
||||
originalPrice = priceListPrice
|
||||
if (defaultPrice?.amount && priceListPrice.amount) {
|
||||
lowestPrice = MathBN.lte(
|
||||
priceListPrice.amount,
|
||||
defaultPrice.amount
|
||||
)
|
||||
? priceListPrice
|
||||
: defaultPrice
|
||||
}
|
||||
|
||||
calculatedPrice = lowestPrice
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user