Skip to content

Domain Model

Pricing không sở hữu schema nào. Mọi bảng dưới đây được định nghĩa trong @nx/core dưới packages/core/src/models/schemas/pricing/, trong schema PostgreSQL pricing. Pricing re-export chúng như repository.

1. ERD đầy đủ

2. Entities

Một khối mỗi bảng. Nguồn schema: packages/core/src/models/schemas/pricing/.

FareSet

Thuộc tínhGiá trị
Tablepricing.FareSet
Sourcecore/src/models/schemas/pricing/fare-set/schema.ts
Soft-delete
Owner ID columnproductVariantId (soft ref, không FK)

Trường: id, name (i18n), description (i18n), status (mặc định DEACTIVATED), productVariantId (not null), cột chung.

Index: IDX trên productVariantId, status.

Enum statusFareSetStatuses: ACTIVATED, DEACTIVATED. Một set ACTIVATED mỗi variant là bất biến chọn lựa.

Fare

Thuộc tínhGiá trị
Tablepricing.Fare
Sourcecore/src/models/schemas/pricing/fare/schema.ts
Soft-delete

Trường:

TrườngKiểuBắt buộcMặc địnhMô tả
idtextSnowflakePK
name / descriptioni18n jsonb{ default, en, vi }
statustextACTIVATEDFareStatuses
typetextnullFareTypes.SALE / OVERRIDE / null (child)
effectiveFrom / effectiveTotimestamptzCửa sổ hiệu lực
minQuantity / maxQuantitydecimal(15,4)Cổng số lượng
amountdecimal(15,4)Giá (null cho nhóm OVERRIDE parent)
rulesCount / childrenCountintegerCounter denormalize
fareSetIdtextFareSet sở hữu
parentIdtextnullFare cha (nhóm)

Index: IDX trên fareSetId, parentId, status.

Relations: fareSet (M:1), parent (self M:1), children (1:M), fareRules (1:M qua Rule đa hình).

Enum statusFareStatuses: ACTIVATED, DEACTIVATED, ARCHIVED.

Rule

Thuộc tínhGiá trị
Tablepricing.Rule
Sourcecore/src/models/schemas/pricing/rule/schema.ts
Soft-delete

Trường: principalType / principalId đa hình, cột giá trị theo data-type (tValue/nValue/bValue/jValue/boValue + dataType), attribute (not null), priority (not null), operator (not null).

Khái niệmGiá trị
principalType (RulePrincipalTypes)Fare, Promotion, PromotionMethod
operator (RuleOperators)EQ, NE, NEQ, GT, GTE, LT, LTE, IN, INQ, NIN, CONTAINS
context (RuleContexts, trong metadata)source (rule mua), target (rule giảm giá)

CONTAINS là nghịch đảo của IN — giá trị context là mảng, giá trị rule là scalar. Dùng bởi FBT override (orderProductVariantIds CONTAINS leadVariantId).

TaxSet

Thuộc tínhGiá trị
Tablepricing.TaxSet
Sourcecore/src/models/schemas/pricing/tax-set/schema.ts
Soft-delete

Trường: principalType / principalId đa hình, name (i18n), status (mặc định ACTIVATED), sourceType, sourceId.

Index: unique-partial trên (principalId, principalType, status) where deletedAt IS NULL; IDX trên sourceId, (sourceType, sourceId), status.

principalType='ProductVariant' → thuế cấp item; principalType='Merchant' → thuế cấp order.

Tax

TrườngKiểuBắt buộcMặc địnhMô tả
namei18n jsonb
typetext100_PERCENTAGETaxModes: AMOUNT / PERCENTAGE / PER_UNIT_AMOUNT
valuedecimal(15,4)Tỷ lệ hoặc số cố định
effectiveFromtimestamptz
effectiveTotimestamptz
priorityinteger0Tăng dần; cùng priority chia một base
isInclusivebooleanfalseNhúng trong giá hiển thị (back-calc)
shouldApplyOnDiscountedbooleantrueCơ sở nhận biết giảm giá
isCompoundbooleantrueCộng dồn trên thuế trước
minQuantity / maxQuantitydecimal(15,4)Điều kiện số lượng
usagetext000_SALETaxUsages.SALE / PURCHASE
chargeTargettext000_CUSTOMERCUSTOMER (trên hoá đơn) / MERCHANT (PIT)
taxSetIdtextTaxSet sở hữu
taxTypeIdtextPhân loại
discriminationTypeIdtextRef phân loại thuế VN

Index: IDX trên discriminationTypeId, status, taxSetId, taxTypeId.

TaxType

Thuộc tínhGiá trị
Tablepricing.TaxType
Sourcecore/src/models/schemas/pricing/tax-type/schema.ts

Trường: type (FixedTaxTypes: VAT, EXCISE, ENVIRONMENTAL, LUXURY, PIT, CUSTOM), name/description (i18n), chargeTarget, status, merchantId (nullable → loại hệ thống).

Index: unique trên (type, merchantId); IDX trên merchantId, (merchantId, status).

Cost

Thuộc tínhGiá trị
Tablepricing.Cost
Sourcecore/src/models/schemas/pricing/cost/schema.ts

Trường: principalType / principalId đa hình (getCurrentCost/getEffectiveCost hardcode ProductVariant), cột user-audit, amount (not null), note, effectiveFrom (not null), effectiveTo (nullable = hiện tại/mở).

Chỉ một cost mở (null effectiveTo) mỗi variant. Chưa wire vào calculator nào.

Promotion

TrườngKiểuBắt buộcMặc địnhMô tả
codetextnullNull = chỉ auto-apply
enableStackingbooleantrue
isAutomaticbooleanfalse
isTaxInclusivebooleanfalse
namei18n
effectiveFrom / effectiveTotimestamptz
typetextSTANDARDPromotionTypes
statustextDRAFTPromotionStatuses
usageLimit / usageCountintegerusageCount=0
rulesCountinteger0Denormalize
merchantIdtext

Index: unique-partial trên code where deletedAt IS NULL; IDX trên merchantId, (merchantId, status).

PromotionMethod

Trường: promotionId (not null), type (PromotionMethodType), value (not null), targetType (not null), allocation, currency (mặc định VND), exchangeRate (mặc định 1), maxQuantity, sourceRulesCount/targetRulesCount, trường BuyGet (buyGetTargetQuantity, buyGetSourceMinQuantity).

Index: unique-partial trên promotionId where deletedAt IS NULL (một method mỗi promotion).

3. Bất biến xuyên entity

Bất biếnCách thực thi
Đúng một FareSet ACTIVATED mỗi productVariantIdIdempotency của worker + FareSetStatuses.canDeactivate() luôn false
Fare OVERRIDE thắng default; nếu không, fare giá thấp nhấtChọn lựa PricingFareCalculatorService / FareCalculatorService
Child fare chỉ đánh giá khi mọi AND-rule của nó passPricingRuleEvaluatorService (lodash get() attribute path)
Thuế cùng priority chia một base; priority cao hơn cộng dồnGom theo priority PricingTaxCalculatorService / TaxCalculatorService
Thuế inclusive back-calc ra khỏi giá hiển thịNhánh isInclusive của tax calculator
Một Cost mở (effectiveTo IS NULL) mỗi variantCostService.updateCurrentCost đóng cái trước trước khi mở cái mới
Một PromotionMethod mỗi Promotionunique-partial index trên promotionId
FBT override seed nhiều nhất một lần mỗi (FareSet liên quan, lead variant)PricingWorkerService._isFbtOverrideAlreadySeeded

4. Hành vi Soft-delete

Hành viChi tiết
Mặc định đọcdeletedAt IS NULL (defaultFilter của model)
Hard-deleteMặc định không dùng
Ràng buộc uniquePartial — scope WHERE deletedAt IS NULL (FareSet, TaxSet, Promotion code, PromotionMethod) nên hàng đã soft-delete giải phóng slot

5. Trang liên quan

Proprietary and Confidential. Unauthorized copying, distribution, or use of this software is strictly prohibited.