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.

27.1 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.

import { parseDate } from "@internationalized/date";
import { z } from "zod";
import { zfd } from "zod-form-data";
// Import the constants from the models file directly (not the `../shared` barrel),
// which also re-exports shared.service/shared.server — those transitively pull in
// `@carbon/auth`'s Lingui-macro glossary and break plain unit tests of this module.
import { incoterms, itemType, methodType } from "../shared/shared.models";

export const purchaseInvoiceLineType = [
  "Part",
  "Service",
  "Material",
  "Tool",
  "Consumable",
  // "Fixed Asset",
  "G/L Account",
  "Lease",
  "Comment"
] as const;

export const purchaseInvoiceStatusType = [
  "Draft",
  // "Return",
  "Pending",
  "Partially Paid",
  "Open",
  "Debit Note Issued",
  "Paid",
  "Voided",
  "Overdue"
] as const;

/**
 * Purchase Invoice is locked (non-editable) when status is anything other than Draft.
 * Once posted/confirmed, no edits are allowed regardless of permission level.
 * The only way to make changes is to reopen it to Draft first.
 */
export function isPurchaseInvoiceLocked(
  status: (typeof purchaseInvoiceStatusType)[number] | string | null | undefined
): boolean {
  return status !== null && status !== undefined && status !== "Draft";
}

export const salesInvoiceLineType = [
  "Part",
  "Service",
  "Material",
  "Tool",
  "Consumable",
  "Fixed Asset",
  // "G/L Account",
  "Comment"
] as const;

export const salesInvoiceStatusType = [
  "Draft",
  // "Return",
  "Pending",
  "Partially Paid",
  "Submitted",
  "Credit Note Issued",
  "Paid",
  "Voided",
  "Overdue"
] as const;

/**
 * Sales Invoice is locked (non-editable) when status is anything other than Draft.
 * Once posted/confirmed, no edits are allowed regardless of permission level.
 */
export function isSalesInvoiceLocked(
  status: string | null | undefined
): boolean {
  return status !== null && status !== undefined && status !== "Draft";
}

type InvoiceCurrency = { currencyCode: string; exchangeRate: number };

/**
 * The columns written when a sales invoice's invoice customer changes. It
 * never writes `customerId`, the sold-to customer. A customer with no
 * currency (`currency` null) leaves the invoice's currency as it is.
 */
export function salesInvoiceCustomerChange(
  invoiceCustomerId: string,
  currency: InvoiceCurrency | null
) {
  return {
    invoiceCustomerId,
    invoiceCustomerContactId: null,
    invoiceCustomerLocationId: null,
    ...currency
  };
}

/**
 * The columns written when a purchase invoice's invoice supplier changes. It
 * never writes `supplierId`. A supplier with no currency (`currency` null)
 * leaves the invoice's currency as it is.
 */
export function purchaseInvoiceSupplierChange(
  invoiceSupplierId: string,
  currency: InvoiceCurrency | null
) {
  return {
    invoiceSupplierId,
    invoiceSupplierContactId: null,
    invoiceSupplierLocationId: null,
    ...currency
  };
}

export const purchaseInvoiceValidator = z.object({
  id: zfd.text(z.string().optional()),
  invoiceId: zfd.text(z.string().optional()),
  supplierId: z.string().min(1, { message: "Supplier is required" }),
  supplierReference: zfd.text(z.string().optional()),
  paymentTermId: zfd.text(z.string().optional()),
  currencyCode: zfd.text(z.string().optional()),
  locationId: z.string().min(1, { message: "Location is required" }),
  invoiceSupplierId: zfd.text(z.string().optional()),
  invoiceSupplierContactId: zfd.text(z.string().optional()),
  invoiceSupplierLocationId: zfd.text(z.string().optional()),
  dateIssued: zfd.text(z.string().optional()),
  dateDue: zfd.text(z.string().optional()),
  supplierShippingCost: zfd.numeric(z.number().optional()),
  exchangeRate: zfd.numeric(z.number().optional()),
  exchangeRateUpdatedAt: zfd.text(z.string().optional())
});

export const purchaseInvoiceDeliveryValidator = z.object({
  id: z.string(),
  locationId: zfd.text(z.string().optional()),
  shippingMethodId: zfd.text(z.string().optional()),
  shippingTermId: zfd.text(z.string().optional()),
  supplierShippingCost: zfd.numeric(z.number().optional().default(0)),
  incoterm: zfd.text(z.enum(incoterms).optional()),
  incotermLocation: zfd.text(z.string().optional()),
  customFields: z.any().optional()
});

export const purchaseInvoiceLineValidator = z
  .object({
    id: zfd.text(z.string().optional()),
    invoiceId: z.string().min(1, { message: "Invoice is required" }),
    invoiceLineType: z.enum(
      [
        ...itemType,
        "Fixture",
        "G/L Account",
        "Fixed Asset",
        "Lease",
        "Comment"
      ],

      {
        error: "Type is required"
      }
    ),
    purchaseOrderId: zfd.text(z.string().optional()),
    purchaseOrderLineId: zfd.text(z.string().optional()),
    itemId: zfd.text(z.string().optional()),
    accountId: zfd.text(z.string().optional()),
    costCenterId: zfd.text(z.string().optional()),
    assetId: zfd.text(z.string().optional()),
    // A Lease line names the lease; a drafted one also its payment row.
    leaseId: zfd.text(z.string().optional()),
    leasePaymentId: zfd.text(z.string().optional()),
    description: zfd.text(z.string().optional()),
    quantity: zfd.numeric(z.number().optional()),
    purchaseUnitOfMeasureCode: zfd.text(z.string().optional()),
    inventoryUnitOfMeasureCode: zfd.text(z.string().optional()),
    conversionFactor: zfd.numeric(z.number().optional()),
    supplierUnitPrice: zfd.numeric(z.number().optional()),
    supplierShippingCost: zfd.numeric(z.number().optional().default(0)),
    supplierTaxAmount: zfd.numeric(z.number().optional().default(0)),
    taxPercent: zfd.numeric(z.number().min(0).max(1).optional().default(0)),
    requiredDate: zfd.text(z.string().optional()),
    locationId: zfd.text(z.string().optional()),
    storageUnitId: zfd.text(z.string().optional()),
    exchangeRate: zfd.numeric(z.number().optional())
  })
  .refine(
    (data) =>
      ["Part", "Service", "Material", "Tool", "Consumable"].includes(
        data.invoiceLineType
      )
        ? data.itemId
        : true,
    {
      message: "Item is required",
      path: ["itemId"] // path of error
    }
  )
  .refine(
    (data) =>
      ["Part", "Material", "Tool", "Consumable"].includes(data.invoiceLineType)
        ? data.locationId
        : true,
    {
      message: "Location is required",
      path: ["locationId"]
    }
  )
  .refine(
    (data) => (data.invoiceLineType === "G/L Account" ? data.accountId : true),
    {
      message: "Account is required",
      path: ["accountId"]
    }
  )
  .refine(
    (data) =>
      data.invoiceLineType === "G/L Account" ? data.description : true,
    {
      message: "Description is required",
      path: ["description"]
    }
  )
  .refine((data) => (data.invoiceLineType === "Lease" ? data.leaseId : true), {
    message: "Lease is required",
    path: ["leaseId"]
  })
  .refine(
    (data) => (data.invoiceLineType === "Lease" ? data.description : true),
    {
      message: "Description is required",
      path: ["description"]
    }
  )
  .refine(
    (data) =>
      data.invoiceLineType === "Fixed Asset"
        ? (data.quantity ?? 1) === 1
        : true,
    {
      message: "Fixed Asset quantity must be 1",
      path: ["quantity"]
    }
  );

export const salesInvoiceValidator = z.object({
  id: zfd.text(z.string().optional()),
  invoiceId: zfd.text(z.string().optional()),
  customerId: z.string().min(1, { message: "Customer is required" }),
  customerReference: zfd.text(z.string().optional()),
  paymentTermId: zfd.text(z.string().optional()),
  currencyCode: zfd.text(z.string().optional()),
  locationId: z.string().min(1, { message: "Location is required" }),
  invoiceCustomerId: zfd.text(z.string().optional()),
  invoiceCustomerContactId: zfd.text(z.string().optional()),
  invoiceCustomerLocationId: zfd.text(z.string().optional()),
  dateIssued: zfd.text(z.string().optional()),
  dateDue: zfd.text(z.string().optional()),
  supplierShippingCost: zfd.numeric(z.number().optional()),
  exchangeRate: zfd.numeric(z.number().optional()),
  exchangeRateUpdatedAt: zfd.text(z.string().optional())
});

export const stripeCustomerActions = [
  "use-linked",
  "link-existing",
  "create"
] as const;

/** The fields this panel emits, plus the email its parent commits — for a
 *  form outside the invoice post modal (the contract confirm modal) that
 *  submits the user's Stripe customer choice. The action re-checks it with
 *  `linkStripeCustomerForBilling`. */
export const stripeCustomerChoiceValidator = z.object({
  stripeCustomerAction: z.enum(stripeCustomerActions).optional(),
  stripeCustomerId: zfd.text(z.string().optional()),
  stripeContactEmail: zfd.text(
    z.string().email({ message: "Email is invalid" }).optional()
  )
});

export const salesInvoicePostValidator = z
  .object({
    notification: z.enum(["Email", "Stripe", "None"]).optional(),
    customerContact: zfd.text(z.string().optional()),
    cc: z.array(z.string()).optional(),
    // What the user agreed to do with the connected account's customer list.
    stripeCustomerAction: z.enum(stripeCustomerActions).optional(),
    // The customer to link to, when the user picked one Stripe already had.
    stripeCustomerId: zfd.text(z.string().optional()),
    // Supplied only when the selected contact had no email on file.
    stripeContactEmail: zfd.text(
      z.string().email({ message: "Email is invalid" }).optional()
    ),
    // Supplied only when the invoice's own dateDue wouldn't survive
    // stripeDueDate (missing, on/before today, or too far out) — see the post modal.
    stripeDueDate: zfd.text(z.string().optional())
  })
  .refine(
    (data) =>
      data.notification === "Email" || data.notification === "Stripe"
        ? data.customerContact
        : true,
    {
      message: "Customer contact is required",
      path: ["customerContact"] // path of error
    }
  )
  // The guard that makes the confirmation step structurally mandatory: with no
  // action there is no code path left that creates a customer on a merchant's
  // account, so a stale or hand-rolled form body cannot skip the dialog.
  .refine(
    (data) =>
      data.notification === "Stripe" ? data.stripeCustomerAction : true,
    {
      message: "Confirm the Stripe customer before posting",
      path: ["stripeCustomerAction"]
    }
  )
  .refine(
    (data) =>
      data.stripeCustomerAction === "link-existing"
        ? data.stripeCustomerId
        : true,
    {
      message: "Select the Stripe customer to link",
      path: ["stripeCustomerId"]
    }
  );

export const salesInvoiceShipmentValidator = z.object({
  id: z.string(),
  locationId: zfd.text(z.string().optional()),
  // The customer's ship-to (a `customerLocation` of the invoice's customer).
  // Never the bill-to: sales rules evaluate standalone lines against it.
  customerLocationId: zfd.text(z.string().optional()),
  shippingMethodId: zfd.text(z.string().optional()),
  shippingTermId: zfd.text(z.string().optional()),
  shippingCost: zfd.numeric(z.number().optional().default(0)),
  incoterm: zfd.text(z.enum(incoterms).optional()),
  incotermLocation: zfd.text(z.string().optional()),
  customFields: z.any().optional()
});

/**
 * A service period is both dates or neither, with the end on or after the
 * start. `zfd.text` has already turned an empty submission into undefined.
 * A malformed date fails the check instead of throwing out of the refine.
 */
function isValidServicePeriod(data: {
  serviceStartDate?: string;
  serviceEndDate?: string;
}): boolean {
  const { serviceStartDate, serviceEndDate } = data;
  if (!serviceStartDate && !serviceEndDate) return true;
  if (!serviceStartDate || !serviceEndDate) return false;
  try {
    return parseDate(serviceEndDate).compare(parseDate(serviceStartDate)) >= 0;
  } catch {
    return false;
  }
}

export const salesInvoiceLineValidator = z
  .object({
    id: zfd.text(z.string().optional()),
    invoiceId: z.string().min(1, { message: "Invoice is required" }),
    // "Rental" lines are written by rental invoice generation, never offered as
    // a choice (it is not in `salesInvoiceLineType`); it is accepted here so a
    // rental line's shape validates. The DB requires a rental agreement line on
    // every Rental row, so a hand-posted one is still refused.
    invoiceLineType: z.enum([...itemType, "Fixture", "Fixed Asset", "Rental"], {
      error: "Type is required"
    }),
    // Wrapped in zfd.text so an empty-string submission (the form always posts a
    // hidden methodType) coerces to undefined instead of failing the enum check.
    // Requiredness is enforced conditionally by the refine below, which exempts
    // Fixed Asset and Rental lines.
    methodType: zfd.text(
      z
        .enum(methodType, {
          error: "Method is required"
        })
        .optional()
    ),
    purchaseOrderId: zfd.text(z.string().optional()),
    purchaseOrderLineId: zfd.text(z.string().optional()),
    itemId: zfd.text(z.string().optional()),
    accountId: zfd.text(z.string().optional()),
    assetId: zfd.text(z.string().optional()),
    addOnCost: zfd.numeric(z.number().optional().default(0)),
    nonTaxableAddOnCost: zfd.numeric(z.number().optional().default(0)),
    description: zfd.text(z.string().optional()),
    quantity: zfd.numeric(z.number().optional()),
    unitOfMeasureCode: zfd.text(z.string().default("EA")),
    unitPrice: zfd.numeric(z.number().optional()),
    // Percent points (0–100), as the form types it; the route stores the 0–1
    // fraction the column holds. It discounts the merchandise only.
    discountPercent: zfd.numeric(z.number().min(0).max(100).optional()),
    shippingCost: zfd.numeric(z.number().optional().default(0)),
    taxPercent: zfd.numeric(z.number().optional().default(0)),
    locationId: zfd.text(z.string().optional()),
    storageUnitId: zfd.text(z.string().optional()),
    exchangeRate: zfd.numeric(z.number().optional()),
    serviceStartDate: zfd.text(z.string().optional()),
    serviceEndDate: zfd.text(z.string().optional())
  })
  .refine(
    (data) =>
      ["Part", "Service", "Material", "Tool", "Consumable"].includes(
        data.invoiceLineType
      )
        ? data.itemId
        : true,
    {
      message: "Item is required",
      path: ["itemId"]
    }
  )
  .refine(
    (data) =>
      ["Part", "Material", "Tool", "Consumable"].includes(data.invoiceLineType)
        ? data.locationId
        : true,
    {
      message: "Location is required",
      path: ["locationId"]
    }
  )
  .refine(
    (data) => {
      if (
        data.invoiceLineType === "Fixed Asset" ||
        data.invoiceLineType === "Rental"
      )
        return true;
      return !!data.methodType;
    },
    {
      message: "Method is required",
      path: ["methodType"]
    }
  )
  .refine(
    (data) =>
      data.invoiceLineType === "Fixed Asset"
        ? (data.quantity ?? 1) === 1
        : true,
    {
      message: "Fixed Asset quantity must be 1",
      path: ["quantity"]
    }
  )
  .refine((data) => isValidServicePeriod(data), {
    message: "Service end must be on or after service start",
    path: ["serviceEndDate"]
  })
  // Rental lines carry their billing period in these columns, written by
  // rental invoice generation; every other non-Service type is a physical good.
  .refine(
    (data) =>
      data.invoiceLineType === "Service" ||
      data.invoiceLineType === "Rental" ||
      (!data.serviceStartDate && !data.serviceEndDate),
    {
      message: "Service dates only apply to Service lines",
      path: ["serviceStartDate"]
    }
  );

// ----------------------------------------------------------------------
// Credit / Debit Memos — payment-shaped documents (the `memo` table). A memo is
// a party + amount + reason GL account, applied to invoices via
// invoiceSettlement exactly like a payment, but the offset is a GL account
// (returns/allowance/adjustment) instead of cash. NOT an invoice row.
//
// The four combos = party (customer/supplier) × direction (Credit/Debit):
//   Customer Credit -> AR down,  Customer Debit -> AR up
//   Supplier Debit  -> AP down,  Supplier Credit -> AP up
// ----------------------------------------------------------------------

export const memoDirection = ["Credit", "Debit"] as const;
export const memoStatus = ["Draft", "Posted", "Voided"] as const;

export type MemoDirection = (typeof memoDirection)[number];
export type MemoStatusType = (typeof memoStatus)[number];

export function isMemoLocked(status: string | null | undefined): boolean {
  return status !== null && status !== undefined && status !== "Draft";
}

export const memoValidator = z
  .object({
    id: zfd.text(z.string().optional()),
    memoId: zfd.text(z.string().optional()),
    direction: z.enum(memoDirection, {
      error: "Direction is required"
    }),
    customerId: zfd.text(z.string().optional()),
    supplierId: zfd.text(z.string().optional()),
    memoDate: z.string().min(1, { message: "Date is required" }),
    currencyCode: z.string().min(1, { message: "Currency is required" }),
    exchangeRate: zfd.numeric(z.number().positive().default(1)),
    amount: zfd.numeric(z.number().positive({ message: "Amount must be > 0" })),
    reference: zfd.text(z.string().optional()),
    notes: zfd.text(z.string().optional())
  })
  .refine((d) => Boolean(d.customerId) !== Boolean(d.supplierId), {
    message: "A memo is for exactly one party (customer or supplier)",
    path: ["customerId"]
  });

// ----------------------------------------------------------------------
// Payments (AR receipts + AP disbursements + applications)
// ----------------------------------------------------------------------

export const paymentType = ["Receipt", "Disbursement"] as const;
export const paymentStatus = ["Draft", "Posted", "Voided"] as const;

export type PaymentType = (typeof paymentType)[number];
export type PaymentStatusType = (typeof paymentStatus)[number];

export function isPaymentLocked(status: string | null | undefined): boolean {
  return status !== null && status !== undefined && status !== "Draft";
}

export const paymentValidator = z
  .object({
    id: zfd.text(z.string().optional()),
    paymentId: zfd.text(z.string().optional()),
    paymentType: z.enum(paymentType, {
      error: "Payment type is required"
    }),
    customerId: zfd.text(z.string().optional()),
    supplierId: zfd.text(z.string().optional()),
    // An employee payee settles reimbursements only — it is never a trade
    // party, which is the point of the segregated employee-payable control
    // account. Mirrors the DB's widened `payment_party_check`.
    employeeId: zfd.text(z.string().optional()),
    paymentDate: z.string().min(1, { message: "Payment date is required" }),
    currencyCode: z.string().min(1, { message: "Currency is required" }),
    exchangeRate: zfd.numeric(z.number().positive().default(1)),
    // Cash may be 0: a receipt/payment can be a pure credit-application (apply
    // the party's posted credits to invoices with no cash changing hands).
    totalAmount: zfd.numeric(
      z.number().nonnegative({ message: "Total amount cannot be negative" })
    ),
    bankAccount: z.string().min(1, { message: "Bank account is required" }),
    reference: zfd.text(z.string().optional()),
    memo: zfd.text(z.string().optional()),
    // A customer deposit names the document it is held against — at most one
    // (`payment_deposit_document_check`). post-payment books the unapplied cash
    // of a payment carrying either reference on the prepayment account.
    salesOrderId: zfd.text(z.string().optional()),
    rentalAgreementId: zfd.text(z.string().optional())
  })
  .refine(
    (d) =>
      [d.customerId, d.supplierId, d.employeeId].filter(Boolean).length === 1,
    {
      message:
        "A payment requires exactly one party (customer, supplier, or employee)",
      path: ["customerId"]
    }
  )
  .refine((d) => !(d.salesOrderId && d.rentalAgreementId), {
    message:
      "A deposit is held against a sales order or a rental agreement, not both",
    path: ["rentalAgreementId"]
  })
  .refine(
    (d) => Boolean(d.customerId) || !(d.salesOrderId || d.rentalAgreementId),
    {
      message: "Only a customer payment can be a deposit",
      path: ["rentalAgreementId"]
    }
  );

// A sales order or rental agreement a customer payment can be a deposit for
// (a Receipt) or a refund of (a Disbursement), as the payment form's "Deposit
// for" picker lists them. Loaded company-wide with the customer on each row —
// the form narrows to the selected customer, which can change before the
// payment is saved. `open` is whether a NEW deposit may be taken against it
// (an open order / a Draft or Active agreement); a refund can name any document
// that already holds a deposit, closed or not.
export type DepositDocument = {
  id: string;
  readableId: string;
  customerId: string;
  kind: "salesOrder" | "rentalAgreement";
  status: string;
  open: boolean;
};

// ----------------------------------------------------------------------
// Charges (Ramp spend-management sync)
// ----------------------------------------------------------------------

export const chargeType = [
  "Charge",
  "Credit",
  "Payment",
  "Cashback",
  "Repayment"
] as const;
export const chargeStatus = ["Draft", "Posted", "Voided"] as const;

export type ChargeType = (typeof chargeType)[number];
export type ChargeStatusType = (typeof chargeStatus)[number];

// ----------------------------------------------------------------------
// Reimbursements (employee expense payables — imported from a spend tool,
// then editable in Carbon while Draft; never hand-created)
// ----------------------------------------------------------------------

export const reimbursementStatus = ["Draft", "Posted", "Voided"] as const;
export type ReimbursementStatusType = (typeof reimbursementStatus)[number];

export function isReimbursementLocked(
  status: string | null | undefined
): boolean {
  return status !== null && status !== undefined && status !== "Draft";
}

// Header edit. No create counterpart by design.
export const reimbursementUpdateValidator = z.object({
  id: z.string().min(1),
  reimbursementDate: z
    .string()
    .min(1, { message: "Reimbursement date is required" }),
  currencyCode: z.string().min(1, { message: "Currency is required" }),
  exchangeRate: zfd.numeric(z.number().positive().default(1)),
  amount: zfd.numeric(
    z.number().finite().positive({ message: "Amount must be positive" })
  ),
  reference: zfd.text(z.string().optional()),
  notes: zfd.text(z.string().optional())
});

// One coding line. The five stored columns mirror chargeLine, plus the
// generic dimension pairs DimensionSelector works in. Do NOT add a field per
// dimension concept — that is what `dimensions` is.
//
// These lines arrive as parsed JSON from a hidden field, not as form data,
// so this validator is plain zod (no zfd coercion) — zfd.numeric expects a
// FormData string and would reject an already-numeric amount.
export const reimbursementLineDimensionValidator = z.object({
  dimensionId: z.string().min(1),
  valueId: z.string().min(1)
});

export const reimbursementLineValidator = z.object({
  id: z.string().optional(),
  accountId: z.string().min(1, { message: "Account is required" }),
  costCenterId: z.string().nullish(),
  projectId: z.string().nullish(),
  description: z.string().nullish(),
  amount: z
    .number()
    .finite()
    .positive({ message: "Line amount must be positive" }),
  dimensions: z.array(reimbursementLineDimensionValidator).default([])
});

// The editor submits the whole line set as ONE hidden JSON field, so the
// route parses that string and runs it through this array.
export const reimbursementLinesValidator = z
  .array(reimbursementLineValidator)
  .min(1, { message: "A reimbursement needs at least one line" });

// The "Pay expense" modal. Exactly three fields, and deliberately no more: they
// are a 1:1 match for Rillet's `POST /reimbursements/{id}/payments`
// (`{amount, date, account_code}`), which is what lets the payout sync across
// with no impedance. The amount is in the reimbursement's DOCUMENT currency and
// may be LESS than the balance — a partial payout is supported — so it is not
// pinned to the balance here; the balance ceiling is enforced server-side by
// `replaceInvoiceSettlements`.
export const reimbursementPaymentValidator = z.object({
  amount: zfd.numeric(
    z.number().positive({ message: "Amount must be greater than zero" })
  ),
  paymentDate: z.string().min(1, { message: "Payment date is required" }),
  bankAccount: z.string().min(1, { message: "Bank account is required" })
});

// The raw object schema (no refinements). Routes that need to `.omit()` a source
// key before injecting it from the URL use THIS — peeling `.refine()` layers off
// the refined validator below with `.innerType()` is brittle (it breaks whenever
// a refinement is added/removed).
export const invoiceSettlementBase = z.object({
  id: zfd.text(z.string().optional()),
  // Source: exactly one of a payment or a memo settles the target.
  paymentId: zfd.text(z.string().optional()),
  memoId: zfd.text(z.string().optional()),
  // Target: exactly one of a sales invoice, purchase invoice, memo, or
  // reimbursement.
  targetSalesInvoiceId: zfd.text(z.string().optional()),
  targetPurchaseInvoiceId: zfd.text(z.string().optional()),
  targetMemoId: zfd.text(z.string().optional()),
  targetReimbursementId: zfd.text(z.string().optional()),
  sourceAmount: zfd.numeric(z.number().finite().nonnegative().optional()),
  appliedAmount: zfd.numeric(z.number().finite().nonnegative().default(0)),
  discountAmount: zfd.numeric(z.number().nonnegative().default(0)),
  writeOffAmount: zfd.numeric(z.number().nonnegative().default(0)),
  targetExchangeRate: zfd.numeric(
    z.number().positive({ message: "Target exchange rate must be > 0" })
  ),
  sourceExchangeRate: zfd.numeric(
    z.number().positive({ message: "Source exchange rate must be > 0" })
  ),
  appliedDate: z.string().min(1, { message: "Applied date is required" })
});

export const invoiceSettlementValidator = invoiceSettlementBase
  .refine((d) => Boolean(d.paymentId) !== Boolean(d.memoId), {
    message: "A settlement must have exactly one source (payment or memo)",
    path: ["paymentId"]
  })
  .refine(
    (d) =>
      [
        d.targetSalesInvoiceId,
        d.targetPurchaseInvoiceId,
        d.targetMemoId,
        d.targetReimbursementId
      ].filter(Boolean).length === 1,
    {
      message:
        "Application must target exactly one document (sales invoice, purchase invoice, memo, or reimbursement)",
      path: ["targetSalesInvoiceId"]
    }
  )
  .refine(
    (d) =>
      Number(d.appliedAmount) +
        Number(d.discountAmount) +
        Number(d.writeOffAmount) +
        Number(d.sourceAmount ?? 0) >
      0,
    {
      message: "At least one of applied / discount / write-off must be > 0",
      path: ["appliedAmount"]
    }
  );

// The balance views preserve any remaining document minor unit, even when its
// base equivalent is smaller than a base currency cent.
export function isInvoicePayable(
  status: string | null | undefined,
  balance: number | null | undefined
): boolean {
  return (
    !["Voided", "Draft", "Pending", "Paid"].includes(status ?? "") &&
    Number.isFinite(Number(balance)) &&
    Number(balance ?? 0) > 0
  );
}