cd78f72f33
Complete rename of all technical identifiers across the codebase: Package names (11 packages): - @planarchy/* → @capakraken/* in all package.json, tsconfig, imports Import statements: 277 files, 548 occurrences replaced Database & Docker: - PostgreSQL user/db: planarchy → capakraken - Docker volumes: planarchy_pgdata → capakraken_pgdata - Connection strings updated in docker-compose, .env, CI CI/CD: - GitHub Actions workflow: all filter commands updated - Test database credentials updated Infrastructure: - Redis channel: planarchy:sse → capakraken:sse - Logger service name: planarchy-api → capakraken-api - Anonymization seed updated - Start/stop/restart scripts updated Test data: - Seed emails: @planarchy.dev → @capakraken.dev - E2E test credentials: all 11 spec files updated - Email defaults: @planarchy.app → @capakraken.app - localStorage keys: planarchy_* → capakraken_* Documentation: 30+ .md files updated Verification: - pnpm install: workspace resolution works - TypeScript: only pre-existing TS2589 (no new errors) - Engine: 310/310 tests pass - Staffing: 37/37 tests pass Co-Authored-By: claude-flow <ruv@ruv.net>
130 lines
3.7 KiB
TypeScript
130 lines
3.7 KiB
TypeScript
/**
|
|
* Pure commercial-terms calculation engine.
|
|
*
|
|
* Applies contingency, discount, and payment milestone validation
|
|
* to base cost/price totals from demand lines.
|
|
*/
|
|
|
|
import type { CommercialTerms, CommercialTermsSummary, PaymentMilestone } from "@capakraken/shared";
|
|
|
|
export interface CommercialTermsInput {
|
|
baseCostCents: number;
|
|
basePriceCents: number;
|
|
terms: CommercialTerms;
|
|
}
|
|
|
|
/**
|
|
* Compute adjusted totals after applying contingency and discount.
|
|
*
|
|
* Contingency is added to cost (risk buffer on cost side).
|
|
* Discount is subtracted from price (reduction on sell side).
|
|
*
|
|
* adjustedCost = baseCost * (1 + contingency%)
|
|
* adjustedPrice = basePrice * (1 - discount%)
|
|
* margin = adjustedPrice - adjustedCost
|
|
*/
|
|
export function computeCommercialTermsSummary(
|
|
input: CommercialTermsInput,
|
|
): CommercialTermsSummary {
|
|
const { baseCostCents, basePriceCents, terms } = input;
|
|
|
|
const contingencyFactor = terms.contingencyPercent / 100;
|
|
const discountFactor = terms.discountPercent / 100;
|
|
|
|
const contingencyCents = Math.round(baseCostCents * contingencyFactor);
|
|
const discountCents = Math.round(basePriceCents * discountFactor);
|
|
|
|
const adjustedCostCents = baseCostCents + contingencyCents;
|
|
const adjustedPriceCents = basePriceCents - discountCents;
|
|
const adjustedMarginCents = adjustedPriceCents - adjustedCostCents;
|
|
const adjustedMarginPercent =
|
|
adjustedPriceCents > 0
|
|
? (adjustedMarginCents / adjustedPriceCents) * 100
|
|
: 0;
|
|
|
|
return {
|
|
baseCostCents,
|
|
basePriceCents,
|
|
contingencyCents,
|
|
discountCents,
|
|
adjustedCostCents,
|
|
adjustedPriceCents,
|
|
adjustedMarginCents,
|
|
adjustedMarginPercent,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Validate that payment milestones sum to 100%.
|
|
* Returns list of validation warnings (empty = valid).
|
|
*/
|
|
export function validatePaymentMilestones(
|
|
milestones: PaymentMilestone[],
|
|
): string[] {
|
|
const warnings: string[] = [];
|
|
|
|
if (milestones.length === 0) return warnings;
|
|
|
|
const totalPercent = milestones.reduce((sum, m) => sum + m.percent, 0);
|
|
|
|
if (Math.abs(totalPercent - 100) > 0.01) {
|
|
warnings.push(
|
|
`Payment milestones sum to ${totalPercent.toFixed(1)}%, expected 100%`,
|
|
);
|
|
}
|
|
|
|
for (let i = 0; i < milestones.length; i++) {
|
|
const m = milestones[i]!;
|
|
if (m.percent <= 0) {
|
|
warnings.push(`Milestone "${m.label}" has 0% or negative allocation`);
|
|
}
|
|
if (!m.label.trim()) {
|
|
warnings.push(`Milestone at position ${i + 1} has empty label`);
|
|
}
|
|
}
|
|
|
|
// Check chronological order when dates are provided
|
|
const datedMilestones = milestones.filter(
|
|
(m): m is PaymentMilestone & { dueDate: string } =>
|
|
m.dueDate != null && m.dueDate !== "",
|
|
);
|
|
for (let i = 1; i < datedMilestones.length; i++) {
|
|
if (datedMilestones[i]!.dueDate < datedMilestones[i - 1]!.dueDate) {
|
|
warnings.push(
|
|
`Milestone "${datedMilestones[i]!.label}" has an earlier date than "${datedMilestones[i - 1]!.label}"`,
|
|
);
|
|
}
|
|
}
|
|
|
|
return warnings;
|
|
}
|
|
|
|
/**
|
|
* Compute per-milestone payment amounts from adjusted price.
|
|
*/
|
|
export function computeMilestoneAmounts(
|
|
adjustedPriceCents: number,
|
|
milestones: PaymentMilestone[],
|
|
): Array<{ label: string; percent: number; amountCents: number; dueDate?: string | null }> {
|
|
return milestones.map((m) => ({
|
|
label: m.label,
|
|
percent: m.percent,
|
|
amountCents: Math.round(adjustedPriceCents * (m.percent / 100)),
|
|
...(m.dueDate !== undefined ? { dueDate: m.dueDate } : {}),
|
|
}));
|
|
}
|
|
|
|
/**
|
|
* Default commercial terms for a new estimate.
|
|
*/
|
|
export function defaultCommercialTerms(): CommercialTerms {
|
|
return {
|
|
pricingModel: "fixed_price",
|
|
contingencyPercent: 0,
|
|
discountPercent: 0,
|
|
paymentTermDays: 30,
|
|
paymentMilestones: [],
|
|
warrantyMonths: 0,
|
|
};
|
|
}
|