manicanldes-backend/docs/batch-contribution-api.md

5.4 KiB
Raw Blame History

Indicative batch contribution (Phase 5E)

Staff can compare a completed batch's sealed standard material/packaging cost with an explicit net unit-price assumption. The report reuses immutable actual-consumption/wastage and packaging cost evidence from Phase 2M, including frozen standard rate revisions. It does not infer costs from current catalog prices, pending orders, or unrelated batches.

GET /api/v1/profitability/batches/:batchId?currency=INR&unitNetPrice=5.00 requires profitability.read. The active session, user/organization scope, and permission are rechecked inside the read transaction. cost.read alone does not grant access, and a profitability-only reader can see this summary without receiving detailed material/rate evidence. Existing system Owners gain the new permission; custom roles remain explicit.

Both query fields are required. Currency uses the existing supported currency set and must match the sealed snapshot; there is no FX conversion. unitNetPrice is a nonnegative two-decimal string up to 9999999999.99, interpreted as a staff-selected selling-price assumption after discounts and excluding selling taxes/freight charges. No price is taken automatically from the catalog or an order. Negative, imprecise, oversized, missing, or extra fields return 400. Missing sessions return 401, insufficient permission 403, and currency mismatch PROFITABILITY_CURRENCY (409).

Only a sealed cost snapshot attached to a completed batch in the caller's organization is eligible. Missing, unsealed, unknown and foreign-scope evidence returns BATCH_COST_NOT_FOUND (404). Snapshot creation stays in the existing cost workflow; this read does not seal, recost, or rewrite any batch. The scoped response includes batch/variant/snapshot IDs, seal date, currency, good output quantity, explicit price, production/packaging subtotals, and basis: "SEALED_BATCH_STANDARD_MATERIAL_COST".

The returned calculations are:

Field Definition
comparisonNetValue Price assumption multiplied by all good units.
standardMaterialCost Sealed production plus packaging material total, including recorded wastage.
materialContribution Comparison value minus sealed material cost; negative amounts are retained.
contributionRateBps Contribution/value × 10,000, nearest integer with half away from zero; signed integer string, or null when comparison value is zero.
breakEvenUnitNetPrice Sealed cost divided by good units, rounded up to a whole cent; null when no good units exist.
comparisonState ABOVE_MATERIAL_BREAK_EVEN, AT_MATERIAL_BREAK_EVEN, BELOW_MATERIAL_BREAK_EVEN, or NO_GOOD_OUTPUT.

Money uses exact integer cents and two-decimal strings. Scenario totals can exceed the per-order money limit without floating-point loss. Basis-point rates use strings so large negative values remain exact. A zero-price scenario returns the signed material shortfall and null rate. A batch with zero good output returns zero comparison value, negative captured material cost, null break-even/rate, and NO_GOOD_OUTPUT, avoiding division by zero. Break-even is calculated from the full batch cost; rounding a display unit cost down must not understate it.

The summary lists excluded categories: labour, overhead, fulfillment, channel fees, marketing and finance. These costs are not recorded here and are not silently estimated or treated as zero in a net-profit claim. The report is an indicative material contribution scenario, not realized revenue, recognized net profit, a complete margin statement, cost-of-goods-sold allocation, or evidence that all good units were sold. No sale-to-batch attribution, tax treatment, inventory valuation policy, or external ledger posting is invented.

Sealed rate changes and later catalog repricing do not change cost evidence or the chosen price assumption. Staff must supply a new price to evaluate another scenario. No customer data, supplier contact/evidence, creator identities, hashes or keys are returned. No audit event or report table is created for this read. Full cost lines remain behind their existing permission.

Deploy the additive Owner-permission migration before the application. No schema table, dependency, pricing policy, source amount or historical migration changes. Older code ignores the permission on rollback. This branch is stacked on the pushed Phase 5D accounting source branch; merge that prerequisite first or review the stacked diff accordingly.

Verification covers positive/negative/zero contribution, exact large values, upward break-even rounding, signed basis-point rounding, zero price/output, sealed evidence, frozen rates and ignored catalog changes, explicit currency matching, independent permission, organization isolation, strict price input and migration drift. Realized profitability, complete expense/COGS attribution, margin dashboards and native multi-connection PostgreSQL checks remain separate work.