@eristack/qups
Version:
Quantity / unit price / subtotal (QUPS) with 2-of-3 sources of truth, plus modifiers and tax — business line pricing on @eristack/money
119 lines • 4.07 kB
TypeScript
import type { ModifierKind } from "./modifier.js";
import type { QupsTruthMode, QuantityRatio } from "./qups.js";
import { qupsRolesFor } from "./stores/fields.js";
/**
* Plain string input for forms / HTTP / BE — no Money objects required.
* Exactly two of quantity / unitPrice / subtotal must be present per `truth`.
*/
export type CalculateLineInput = {
truth: QupsTruthMode;
/** Currency code for all money fields on the line. */
currency: string;
quantity?: string;
unitPrice?: string;
subtotal?: string;
modifiers?: readonly CalculateModifierInput[];
taxRatePercent?: string;
taxMode?: "exclusive" | "inclusive";
/** Round money fields with `Rounding.currencyDefault()` (typical before display/persist). */
round?: boolean;
};
export type CalculateModifierInput = {
id?: string;
kind: ModifierKind;
type: "percent";
percent: string;
} | {
id?: string;
kind: ModifierKind;
type: "nominal";
amount: string;
currency?: string;
};
/**
* Flat snapshot for TanStack Form state, API responses, or DB inserts.
* All money/qty are decimal strings.
*/
export type CalculatedLine = {
truth: QupsTruthMode;
currency: string;
quantity: string;
quantityRatio: QuantityRatio | null;
unitPrice: string;
subtotal: string;
discountTotal: string;
surchargeTotal: string;
net: string;
taxAmount: string;
taxRatePercent: string | null;
taxMode: "exclusive" | "inclusive" | null;
/** Payable gross. */
total: string;
/** Echo modifiers as plain strings (for form round-trip). */
modifiers: CalculateModifierInput[];
/** Which QUPS fields are editable vs derived for the current truth. */
roles: ReturnType<typeof qupsRolesFor>;
/**
* Values shaped for injectable Drizzle columns / SQL insert
* (`currency`, `unit_price_amount`, …).
*/
columns: QupsColumnValues;
};
/** CamelCase keys matching `qupsLineColumns()` drizzle properties. */
export type QupsColumnValues = {
truth: QupsTruthMode;
quantity: string;
quantityRatioNumerator: string | null;
quantityRatioDenominator: string | null;
currency: string;
unitPriceAmount: string;
subtotalAmount: string;
taxRatePercent: string | null;
taxMode: "exclusive" | "inclusive" | null;
taxAmount: string | null;
netAmount: string;
grossAmount: string;
};
export type PatchLineInput = {
quantity?: string;
unitPrice?: string;
subtotal?: string;
truth?: QupsTruthMode;
currency?: string;
modifiers?: readonly CalculateModifierInput[];
taxRatePercent?: string | null;
taxMode?: "exclusive" | "inclusive" | null;
/**
* When only `subtotal` changes, keep this SoT partner
* (same as `Qups.edit` prefer).
*/
prefer?: "quantity" | "unitPrice";
round?: boolean;
};
/**
* Recalculate a full pricing line from plain strings.
* Use in TanStack Form listeners and BE insert/update handlers — same function both sides.
*
* ```ts
* const line = calculateLine({
* truth: "quantity+unitPrice",
* currency: "USD",
* quantity: "2",
* unitPrice: "50",
* modifiers: [{ kind: "discount", type: "percent", percent: "5" }],
* taxRatePercent: "11",
* round: true,
* });
* // form: line.quantity, line.unitPrice, line.subtotal, line.total
* // db: { itemId, ...line.columns }
* ```
*/
export declare function calculateLine(input: CalculateLineInput): CalculatedLine;
/**
* Patch one or more fields and recalculate (form onChange / BE partial update).
* Uses the same SoT edit rules as `Qups.edit` when quantity/unitPrice/subtotal change.
*/
export declare function patchLine(current: CalculatedLine | CalculateLineInput, patch: PatchLineInput): CalculatedLine;
/** Merge calculated QUPS columns into an insert/update payload (with itemId, …). */
export declare function withQupsColumns<T extends Record<string, unknown>>(row: T, line: CalculatedLine | QupsColumnValues): T & QupsColumnValues;
//# sourceMappingURL=calculate.d.ts.map