Kaizen
Browse modulesPricingpricing/sharedFunctions

Function: calculatePrices()

function calculatePrices<TContext extends EvaluatePricingContext>(partitioned: PricingEvaluationResult<TContext>, options?: CalculatePricesOptions<TContext>): PricingCalculation<TContext["line_items"][number]>;

Defined in: shared/pricing/calculator.ts:146

Turn a partitioned PricingEvaluationResult into per-line and per-order subtotals with a traceable, ordered breakdown. Pure — no IO, no globals, deterministic given inputs.

Pipeline per line item (in order):

  1. Base subtotal. Find the single base-phase rule on this line and sum every BASE_PRICE action's amount_cents. If more than one base-phase rule matched, throw MultipleBasePriceRulesError. If zero matched, fall back to line_item.base_price_cents, then to 0. Multiply by quantity — the line's own unit axis, times the base rule's optional per (duration) axis when it declares one (see resolveDurationAxis).
  2. non_compounding pass. Each matched rule's actions apply independently against base_cents — deltas are computed off the base, not the running subtotal, and summed in. Multiple non_compounding rules therefore do not stack with each other.
  3. compounding pass. Sequential application against the running subtotal in priority ascending order (matching loadRules).
  4. post pass. Same mechanics as compounding.

Then each line's subtotal_cents is summed into lines_total_cents, and the same four-phase pipeline runs against the order-level rules to produce total_cents.

Subtotals are clamped at zero: a discount that would drive the running subtotal negative records delta_cents = -before_cents instead of the action's full negative value.

Rounding: half-away-from-zero, applied at each multiplication step (percentage applications and per_unit scaling). Sub-cent values are never surfaced.

Type Parameters

Type Parameter
TContext extends EvaluatePricingContext

Parameters

ParameterType
partitionedPricingEvaluationResult<TContext>
optionsCalculatePricesOptions<TContext>

Returns

PricingCalculation<TContext["line_items"][number]>

On this page