Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

1.4 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.

// Which drafted contract invoices a person must review before invoice
// automation posts them. Pure, so the generator and its tests agree.
// Spec: `.ai/specs/implemented/2026-09-22-revenue-recognition-rentals-and-contracts.md` Part III

import {
  type InvoiceAutomation,
  rentalHoldRebill
} from "./rental-invoice-plan";

export { rentalHoldRebill as recurringHoldRebill } from "./rental-invoice-plan";

export const CONTRACT_HOLD_ADJUSTMENT =
  "Includes a credit for a contract change";

/** The hold for one drafted contract invoice. Draft Only → null (nothing is
 *  automated). Re-bill of a voided invoice (any row with
 *  voidedInvoiceReadableId) wins over a negative adjustment row. */
export function contractInvoiceHold(
  mode: InvoiceAutomation,
  rows: {
    amount: number;
    isAdjustment: boolean;
    voidedInvoiceReadableId: string | null;
  }[]
): string | null {
  if (mode === "Draft Only") return null;

  const voided = [
    ...new Set(
      rows
        .map((row) => row.voidedInvoiceReadableId)
        .filter((id): id is string => !!id)
    )
  ];
  if (voided.length > 0) return rentalHoldRebill(voided);

  return rows.some((row) => row.isAdjustment && row.amount < 0)
    ? CONTRACT_HOLD_ADJUSTMENT
    : null;
}