@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
79 lines • 2.51 kB
TypeScript
import { Money, type CurrencyUnit } from "@eristack/money";
import type { QupsTruthMode } from "./truth-mode.js";
export type { QupsTruthMode } from "./truth-mode.js";
export { QUPS_TRUTH_MODES, isQupsTruthMode } from "./truth-mode.js";
export type QupsInput = {
truth: "quantity+unitPrice";
quantity: string;
unitPrice: Money;
} | {
truth: "quantity+subtotal";
quantity: string;
subtotal: Money;
} | {
truth: "unitPrice+subtotal";
unitPrice: Money;
subtotal: Money;
};
/** Exact quantity as a ratio of decimal strings (avoids losing 10/3). */
export type QuantityRatio = {
numerator: string;
denominator: string;
};
/**
* Quantity · Unit price · Subtotal triad with exactly two sources of truth.
*
* When truth is `unitPrice+subtotal` (e.g. UP=3, S=10), quantity is kept as
* the ratio `10/3` so the true qty is not collapsed to a rounded 3.33.
*/
export declare class Qups {
readonly truth: QupsTruthMode;
readonly unitPrice: Money;
readonly subtotal: Money;
/**
* Decimal string view of quantity (high precision).
* Prefer `quantityRatio` when truth is `unitPrice+subtotal`.
*/
readonly quantity: string;
/** Exact ratio when quantity was derived from unitPrice+subtotal; else null. */
readonly quantityRatio: QuantityRatio | null;
private constructor();
get currency(): CurrencyUnit;
static of(input: QupsInput): Qups;
withTruth(input: QupsInput): Qups;
/**
* `quantity × unitPrice` using SoT when possible so UP+S never drifts.
*/
product(): Money;
/**
* Common UI edits. Single-field defaults:
* - qty or unitPrice → recompute subtotal (`quantity+unitPrice`)
* - subtotal → keep unitPrice, recompute qty (`unitPrice+subtotal`)
* unless `prefer: "quantity"`
*/
edit(patch: {
quantity?: string;
unitPrice?: Money;
subtotal?: Money;
}, options?: {
prefer?: "quantity" | "unitPrice";
}): Qups;
asTruth(truth: QupsTruthMode): Qups;
withRounding(round: {
apply: (m: Money) => Money;
}): Qups;
toJSON(): {
truth: "quantity+subtotal" | "quantity+unitPrice" | "unitPrice+subtotal";
quantity: string;
quantityRatio: QuantityRatio | null;
unitPrice: {
currency: string;
amount: string;
};
subtotal: {
currency: string;
amount: string;
};
};
}
//# sourceMappingURL=qups.d.ts.map