feat: Add support for managing tax inclusivity (#7943)

UI / HTTP / Workflows will come in separate PRs

REF CORE-2376
This commit is contained in:
Stevche Radevski authored and GitHub committed 2024-07-04 14:50:09 +00:00
1 parent 20be64e799
commit 5544303b91
17 files changed
+970 -66

No files matched your search

@@ -1,5 +1,5 @@
import { defineJoinerConfig, Modules } from "@medusajs/utils"
import { Price, PriceList, PriceSet } from "@models"
import { Price, PriceList, PricePreference, PriceSet } from "@models"
export const joinerConfig = defineJoinerConfig(Modules.PRICING, {
models: [PriceSet, PriceList, Price],
@@ -7,5 +7,6 @@ export const joinerConfig = defineJoinerConfig(Modules.PRICING, {
price_set_id: PriceSet.name,
price_list_id: PriceList.name,
price_id: Price.name,
price_preference_id: PricePreference.name,
},
})
@@ -271,6 +271,112 @@
}
}
},
{
"columns": {
"id": {
"name": "id",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"attribute": {
"name": "attribute",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"value": {
"name": "value",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"mappedType": "text"
},
"is_tax_inclusive": {
"name": "is_tax_inclusive",
"type": "boolean",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"default": "false",
"mappedType": "boolean"
},
"created_at": {
"name": "created_at",
"type": "timestamptz",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"length": 6,
"default": "now()",
"mappedType": "datetime"
},
"updated_at": {
"name": "updated_at",
"type": "timestamptz",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"length": 6,
"default": "now()",
"mappedType": "datetime"
},
"deleted_at": {
"name": "deleted_at",
"type": "timestamptz",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"length": 6,
"mappedType": "datetime"
}
},
"name": "price_preference",
"schema": "public",
"indexes": [
{
"keyName": "IDX_price_preference_deleted_at",
"columnNames": [
"deleted_at"
],
"composite": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_preference_deleted_at\" ON \"price_preference\" (deleted_at) WHERE deleted_at IS NOT NULL"
},
{
"keyName": "IDX_price_preference_attribute_value",
"columnNames": [],
"composite": false,
"primary": false,
"unique": false,
"expression": "CREATE UNIQUE INDEX IF NOT EXISTS \"IDX_price_preference_attribute_value\" ON \"price_preference\" (attribute, value) WHERE deleted_at IS NULL"
},
{
"keyName": "price_preference_pkey",
"columnNames": [
"id"
],
"composite": false,
"primary": true,
"unique": true
}
],
"checks": [],
"foreignKeys": {}
},
{
"columns": {
"id": {
@@ -0,0 +1,15 @@
import { Migration } from '@mikro-orm/migrations';
export class Migration20240704094505 extends Migration {
async up(): Promise<void> {
this.addSql('create table if not exists "price_preference" ("id" text not null, "attribute" text not null, "value" text null, "is_tax_inclusive" boolean not null default false, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "price_preference_pkey" primary key ("id"));');
this.addSql('CREATE INDEX IF NOT EXISTS "IDX_price_preference_deleted_at" ON "price_preference" (deleted_at) WHERE deleted_at IS NOT NULL;');
this.addSql('CREATE UNIQUE INDEX IF NOT EXISTS "IDX_price_preference_attribute_value" ON "price_preference" (attribute, value) WHERE deleted_at IS NULL;');
}
async down(): Promise<void> {
this.addSql('drop table if exists "price_preference" cascade;');
}
}
@@ -3,3 +3,4 @@ export { default as PriceList } from "./price-list"
export { default as PriceListRule } from "./price-list-rule"
export { default as PriceRule } from "./price-rule"
export { default as PriceSet } from "./price-set"
export { default as PricePreference } from "./price-preference"
@@ -0,0 +1,75 @@
import {
createPsqlIndexStatementHelper,
DALUtils,
generateEntityId,
} from "@medusajs/utils"
import {
BeforeCreate,
Entity,
Filter,
OnInit,
PrimaryKey,
Property,
} from "@mikro-orm/core"
export const uniquePreferenceRuleIndexName =
"IDX_price_preference_attribute_value"
const UniquePreferenceRuleIndexStatement = createPsqlIndexStatementHelper({
name: uniquePreferenceRuleIndexName,
tableName: "price_preference",
columns: ["attribute", "value"],
unique: true,
where: "deleted_at IS NULL",
})
const DeletedAtIndex = createPsqlIndexStatementHelper({
tableName: "price_preference",
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
})
@Entity()
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
@UniquePreferenceRuleIndexStatement.MikroORMIndex()
export default class PricePreference {
@PrimaryKey({ columnType: "text" })
id: string
@Property({ columnType: "text" })
attribute: string
@Property({ columnType: "text", nullable: true })
value: string | null = null
@Property({ default: false })
is_tax_inclusive: boolean
@Property({
onCreate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
created_at: Date
@Property({
onCreate: () => new Date(),
onUpdate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
updated_at: Date
@DeletedAtIndex.MikroORMIndex()
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "prpref")
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "prpref")
}
}
@@ -83,10 +83,6 @@ export class PricingRepository
)
})
.leftJoin("price_list_rule as plr", "plr.price_list_id", "pl.id")
.orderBy([
{ column: "rules_count", order: "desc" },
{ column: "pl.rules_count", order: "desc" },
])
.groupBy("price.id", "pl.id")
.having(
knex.raw(
@@ -168,7 +164,6 @@ export class PricingRepository
),
})
.join(priceSubQueryKnex.as("price"), "price.price_set_id", "ps.id")
.leftJoin("price_rule as pr", "pr.price_id", "price.id")
.whereIn("ps.id", pricingFilters.id)
.andWhere("price.currency_code", "=", currencyCode)
@@ -1,6 +1,7 @@
import {
AddPricesDTO,
Context,
CreatePricePreferenceDTO,
CreatePriceRuleDTO,
CreatePricesDTO,
CreatePriceSetDTO,
@@ -9,15 +10,18 @@ import {
InternalModuleDeclaration,
ModuleJoinerConfig,
ModulesSdkTypes,
PricePreferenceDTO,
PriceSetDTO,
PricingContext,
PricingFilters,
PricingRepositoryService,
PricingTypes,
UpsertPricePreferenceDTO,
UpsertPriceSetDTO,
} from "@medusajs/types"
import {
arrayDifference,
deduplicate,
EmitEvents,
GetIsoStringFromDate,
groupBy,
@@ -34,7 +38,14 @@ import {
simpleHash,
} from "@medusajs/utils"
import { Price, PriceList, PriceListRule, PriceRule, PriceSet } from "@models"
import {
Price,
PriceList,
PriceListRule,
PriceRule,
PriceSet,
PricePreference,
} from "@models"
import { ServiceTypes } from "@types"
import { eventBuilders, validatePriceListDates } from "@utils"
@@ -48,6 +59,7 @@ type InjectedDependencies = {
priceRuleService: ModulesSdkTypes.IMedusaInternalService<any>
priceService: ModulesSdkTypes.IMedusaInternalService<any>
priceListService: ModulesSdkTypes.IMedusaInternalService<any>
pricePreferenceService: ModulesSdkTypes.IMedusaInternalService<any>
priceListRuleService: ModulesSdkTypes.IMedusaInternalService<any>
}
@@ -57,6 +69,7 @@ const generateMethodForModels = {
PriceListRule,
PriceRule,
Price,
PricePreference,
}
export default class PricingModuleService
@@ -70,6 +83,8 @@ export default class PricingModuleService
}
PriceList: { dto: PricingTypes.PriceListDTO }
PriceListRule: { dto: PricingTypes.PriceListRuleDTO }
// PricePreference: { dto: PricingTypes.PricePreferenceDTO }
PricePreference: { dto: any }
}>(generateMethodForModels)
implements PricingTypes.IPricingModuleService
{
@@ -80,6 +95,7 @@ export default class PricingModuleService
protected readonly priceService_: ModulesSdkTypes.IMedusaInternalService<Price>
protected readonly priceListService_: ModulesSdkTypes.IMedusaInternalService<PriceList>
protected readonly priceListRuleService_: ModulesSdkTypes.IMedusaInternalService<PriceListRule>
protected readonly pricePreferenceService_: ModulesSdkTypes.IMedusaInternalService<PricePreference>
constructor(
{
@@ -88,6 +104,7 @@ export default class PricingModuleService
priceSetService,
priceRuleService,
priceService,
pricePreferenceService,
priceListService,
priceListRuleService,
}: InjectedDependencies,
@@ -101,6 +118,7 @@ export default class PricingModuleService
this.priceSetService_ = priceSetService
this.priceRuleService_ = priceRuleService
this.priceService_ = priceService
this.pricePreferenceService_ = pricePreferenceService
this.priceListService_ = priceListService
this.priceListRuleService_ = priceListRuleService
}
@@ -240,41 +258,93 @@ export default class PricingModuleService
)
const pricesSetPricesMap = groupBy(results, "price_set_id")
const priceIds: string[] = []
pricesSetPricesMap.forEach(
(prices: PricingTypes.CalculatedPriceSetDTO[], key) => {
const priceListPrice = prices.find((p) => p.price_list_id)
const defaultPrice = prices?.find((p) => !p.price_list_id)
if (!prices.length || (!priceListPrice && !defaultPrice)) {
pricesSetPricesMap.delete(key)
return
}
let calculatedPrice: PricingTypes.CalculatedPriceSetDTO | undefined =
defaultPrice
let originalPrice: PricingTypes.CalculatedPriceSetDTO | undefined =
defaultPrice
if (priceListPrice) {
calculatedPrice = priceListPrice
if (priceListPrice.price_list_type === PriceListType.OVERRIDE) {
originalPrice = priceListPrice
}
}
pricesSetPricesMap.set(key, { calculatedPrice, originalPrice })
priceIds.push(
...(deduplicate(
[calculatedPrice?.id, originalPrice?.id].filter(Boolean)
) as string[])
)
}
)
// We use the price rules to get the right preferences for the price
const priceRulesForPrices = await this.priceRuleService_.list(
{ price_id: priceIds },
{ take: null }
)
const priceRulesPriceMap = groupBy(priceRulesForPrices, "price_id")
// Note: For now the preferences are intentionally kept very simple and explicit - they use either the region or currency,
// so we hard-code those as the possible filters here. This can be made more flexible if needed later on.
const pricingPreferences = await this.pricePreferenceService_.list(
{
$or: Object.entries(pricingContext)
.filter(([key, val]) => {
return key === "region_id" || key === "currency_code"
})
.map(([key, val]) => ({
attribute: key,
value: val,
})),
},
{},
sharedContext
)
const calculatedPrices: PricingTypes.CalculatedPriceSet[] =
pricingFilters.id
.map((priceSetId: string): PricingTypes.CalculatedPriceSet | null => {
// This is where we select prices, for now we just do a first match based on the database results
// which is prioritized by rules_count first for exact match and then deafult_priority of the rule_type
// TODO: inject custom price selection here
const prices = pricesSetPricesMap.get(priceSetId) || []
if (!prices.length) {
const prices = pricesSetPricesMap.get(priceSetId)
if (!prices) {
return null
}
const priceListPrice = prices.find((p) => p.price_list_id)
const defaultPrice = prices?.find((p) => !p.price_list_id)
let calculatedPrice: PricingTypes.CalculatedPriceSetDTO = defaultPrice
let originalPrice: PricingTypes.CalculatedPriceSetDTO = defaultPrice
if (priceListPrice) {
calculatedPrice = priceListPrice
if (priceListPrice.price_list_type === PriceListType.OVERRIDE) {
originalPrice = priceListPrice
}
}
const {
calculatedPrice,
originalPrice,
}: {
calculatedPrice: PricingTypes.CalculatedPriceSetDTO
originalPrice: PricingTypes.CalculatedPriceSetDTO | undefined
} = prices
return {
id: priceSetId,
is_calculated_price_price_list: !!calculatedPrice?.price_list_id,
is_calculated_price_tax_inclusive: isTaxInclusive(
priceRulesPriceMap.get(calculatedPrice.id),
pricingPreferences
),
calculated_amount: parseInt(calculatedPrice?.amount || "") || null,
is_original_price_price_list: !!originalPrice?.price_list_id,
is_original_price_tax_inclusive: originalPrice?.id
? isTaxInclusive(
priceRulesPriceMap.get(originalPrice.id),
pricingPreferences
)
: false,
original_amount: parseInt(originalPrice?.amount || "") || null,
currency_code: calculatedPrice?.currency_code || null,
@@ -641,6 +711,102 @@ export default class PricingModuleService
)
}
// @ts-expect-error
async createPricePreferences(
data: PricingTypes.CreatePricePreferenceDTO,
sharedContext?: Context
): Promise<PricePreferenceDTO>
async createPricePreferences(
data: PricingTypes.CreatePricePreferenceDTO[],
sharedContext?: Context
): Promise<PricePreferenceDTO[]>
@InjectManager("baseRepository_")
@EmitEvents()
async createPricePreferences(
data:
| PricingTypes.CreatePricePreferenceDTO
| PricingTypes.CreatePricePreferenceDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<PricePreferenceDTO | PricePreferenceDTO[]> {
const preferences = await this.pricePreferenceService_.create(
data,
sharedContext
)
return await this.baseRepository_.serialize<any[]>(preferences)
}
async upsertPricePreferences(
data: UpsertPricePreferenceDTO[],
sharedContext?: Context
): Promise<PricePreferenceDTO[]>
async upsertPricePreferences(
data: UpsertPricePreferenceDTO,
sharedContext?: Context
): Promise<PricePreferenceDTO>
@InjectManager("baseRepository_")
async upsertPricePreferences(
data: UpsertPricePreferenceDTO | UpsertPricePreferenceDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<PricePreferenceDTO | PricePreferenceDTO[]> {
const input = Array.isArray(data) ? data : [data]
const forUpdate = input.filter(
(
pricePreference
): pricePreference is ServiceTypes.UpdatePricePreferenceInput =>
!!pricePreference.id
)
const forCreate = input.filter(
(pricePreference): pricePreference is CreatePricePreferenceDTO =>
!pricePreference.id
)
const operations: Promise<PricePreference[]>[] = []
if (forCreate.length) {
operations.push(
this.pricePreferenceService_.create(forCreate, sharedContext)
)
}
if (forUpdate.length) {
operations.push(
this.pricePreferenceService_.update(forUpdate, sharedContext)
)
}
const result = (await promiseAll(operations)).flat()
return await this.baseRepository_.serialize<
PricePreferenceDTO[] | PricePreferenceDTO
>(Array.isArray(data) ? result : result[0])
}
// @ts-expect-error
async updatePricePreferences(
id: string,
data: PricingTypes.UpdatePricePreferenceDTO,
sharedContext?: Context
): Promise<PricePreferenceDTO>
async updatePricePreferences(
selector: PricingTypes.FilterablePricePreferenceProps,
data: PricingTypes.UpdatePricePreferenceDTO,
sharedContext?: Context
): Promise<PricePreferenceDTO[]>
@InjectManager("baseRepository_")
async updatePricePreferences(
idOrSelector: string | PricingTypes.FilterablePricePreferenceProps,
data: PricingTypes.UpdatePricePreferenceDTO,
@MedusaContext() sharedContext: Context = {}
): Promise<PricePreferenceDTO | PricePreferenceDTO[]> {
const preferences = await this.pricePreferenceService_.update(
data,
sharedContext
)
return await this.baseRepository_.serialize<any[]>(preferences)
}
@InjectTransactionManager("baseRepository_")
protected async createPriceSets_(
data: PricingTypes.CreatePriceSetDTO[],
@@ -1253,6 +1419,31 @@ export default class PricingModuleService
}
}
const isTaxInclusive = (
priceRules: PriceRule[],
preferences: PricePreference[]
) => {
const regionPreference = preferences.find((p) => p.attribute === "region_id")
const currencyPreference = preferences.find(
(p) => p.attribute === "currency_code"
)
const regionRule = priceRules?.find((rule) => rule.attribute === "region_id")
if (
regionRule &&
regionPreference &&
regionRule.value === regionPreference.value
) {
return regionPreference.is_tax_inclusive
}
if (currencyPreference) {
return currencyPreference.is_tax_inclusive
}
return false
}
const hashPrice = (
price: PricingTypes.PriceDTO | PricingTypes.CreatePricesDTO
): string => {
@@ -1,3 +1,36 @@
export * from "./price-list"
export * from "./price-set"
export * from "./price"
import { PriceListStatus, PricingTypes } from "@medusajs/types"
export interface CreatePriceListDTO extends PricingTypes.CreatePriceListDTO {
rules_count?: number
price_list_rules?: {
attribute: string
value: string
}[]
prices?: PricingTypes.CreatePriceListPriceDTO[]
}
export interface UpdatePriceListDTO {
id: string
title?: string
description?: string | null
starts_at?: string | null
ends_at?: string | null
status?: PriceListStatus
number_rules?: number
}
export interface UpdatePriceSetInput extends PricingTypes.UpdatePriceSetDTO {
id: string
}
export interface UpsertPriceDTO
extends Omit<PricingTypes.CreatePriceDTO, "rules"> {
id?: string
price_list_id?: string
price_rules: PricingTypes.CreatePriceRuleDTO[]
}
export interface UpdatePricePreferenceInput
extends PricingTypes.UpdatePricePreferenceDTO {
id: string
}
@@ -1,20 +0,0 @@
import { PriceListStatus, PricingTypes } from "@medusajs/types"
export interface CreatePriceListDTO extends PricingTypes.CreatePriceListDTO {
rules_count?: number
price_list_rules?: {
attribute: string
value: string
}[]
prices?: PricingTypes.CreatePriceListPriceDTO[]
}
export interface UpdatePriceListDTO {
id: string
title?: string
description?: string | null
starts_at?: string | null
ends_at?: string | null
status?: PriceListStatus
number_rules?: number
}
@@ -1,5 +0,0 @@
import { UpdatePriceSetDTO } from "@medusajs/types"
export interface UpdatePriceSetInput extends UpdatePriceSetDTO {
id: string
}
@@ -1,8 +0,0 @@
import { PricingTypes } from "@medusajs/types"
export interface UpsertPriceDTO
extends Omit<PricingTypes.CreatePriceDTO, "rules"> {
id?: string
price_list_id?: string
price_rules: PricingTypes.CreatePriceRuleDTO[]
}