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.
TypeScript ai-agentsbill-of-materialserpinventory-managementleanmanufacturingmanufacturing-execution-systemmcp-servermesmrpopen-source-erppostgresqlproduction-planningqmsquality-management-systemreact-routerself-hostedsupabasetraceabilitytypescript
// 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 type { Database } from "@carbon/database";
import type { JSONContent } from "@carbon/react";
import { Fragment } from "react";
import type { DocumentTemplate, ResolvedSection } from "../template";
import {
DEFAULT_HEADER_OPTIONS,
interpolateContent,
resolveTemplate
} from "../template";
import type { AccountsPayableBillingAddress, PDF } from "../types";
import { getPurchaseOrderDisplayId } from "../utils/purchase-order";
import {
getMoneyFormatter,
getRateFormatter,
resolveRegistrationLine
} from "../utils/shared";
import type {
PurchaseOrderApproval,
PurchaseOrderData
} from "./blocks/purchaseOrder";
import {
buildPurchaseOrderVars,
purchaseOrderBlockRegistry
} from "./blocks/purchaseOrder";
import { Template } from "./components";
interface PurchaseOrderPDFProps extends PDF {
purchaseOrder: Database["public"]["Views"]["purchaseOrders"]["Row"];
purchaseOrderLines: Database["public"]["Views"]["purchaseOrderLines"]["Row"][];
purchaseOrderLocations: Database["public"]["Views"]["purchaseOrderLocations"]["Row"];
companySettings?:
| Database["public"]["Tables"]["companySettings"]["Row"]
| null;
accountsPayableBillingAddress?: AccountsPayableBillingAddress | null;
paymentTerms?: { id: string; name: string }[];
terms: JSONContent;
thumbnails?: Record<string, string | null>;
/** Stored layout. When omitted, the default Purchase Order layout is used. */
template?: DocumentTemplate | null;
/** Shared sections referenced by the template, keyed by id. */
sections?: Record<string, ResolvedSection>;
/** Settlement decimals from the document currency's row; null/omitted falls back to 2. */
currencyDecimals?: number | null;
/** The approval that released the order; null when it was not approved. */
approval?: PurchaseOrderApproval | null;
}
const PurchaseOrderPDF = ({
accountsPayableBillingAddress,
approval,
company,
companySettings,
meta,
paymentTerms,
purchaseOrder,
purchaseOrderLines,
purchaseOrderLocations,
terms,
thumbnails,
locale,
currencyDecimals,
template,
sections = {},
title = "Purchase Order"
}: PurchaseOrderPDFProps) => {
const currencyCode =
purchaseOrder.currencyCode ?? company.baseCurrencyCode ?? "USD";
const numberFormatter = getMoneyFormatter(locale, currencyDecimals);
// The unit-price COLUMN is a rate, not a settlement amount — see
// getRateFormatter. Totals/tax/shipping stay on numberFormatter.
const rateFormatter = getRateFormatter(locale, currencyDecimals);
const displayId = purchaseOrder
? getPurchaseOrderDisplayId(purchaseOrder)
: "";
const headerTitle = displayId ? `${title}: ${displayId}` : title;
const { blocks, theme, settings, headerSectionId, footerSectionId } =
resolveTemplate("purchaseOrder", template);
const vars = buildPurchaseOrderVars({
purchaseOrder,
purchaseOrderLocations,
company,
currencyCode,
approval,
locale
});
const registration = resolveRegistrationLine({
company,
footerSectionId,
sections,
settings,
vars
});
const headerOptions = {
...DEFAULT_HEADER_OPTIONS,
...(headerSectionId ? (sections[headerSectionId]?.config ?? {}) : {})
};
const data: PurchaseOrderData = {
company,
companySettings,
locale,
purchaseOrder,
purchaseOrderLines,
purchaseOrderLocations,
accountsPayableBillingAddress,
paymentTerms: paymentTerms ?? [],
terms,
thumbnails,
theme,
sections,
currencyCode,
approval,
numberFormatter,
rateFormatter,
vars,
headerOptions
};
const headerSection = headerSectionId
? sections[headerSectionId]?.content
: undefined;
const footerSection = footerSectionId
? sections[footerSectionId]?.content
: undefined;
const headerContent = headerSection
? interpolateContent(headerSection, vars)
: undefined;
const footerContent = footerSection
? interpolateContent(footerSection, vars)
: undefined;
const showHeader = headerSectionId !== null;
const showFooter = footerSectionId !== null;
const visibleBlocks = blocks.filter(
(block) => block.visible && !(block.type === "header" && !showHeader)
);
return (
<Template
theme={theme}
title={headerTitle}
meta={{
author: meta?.author ?? "Carbon",
keywords: meta?.keywords ?? "purchase order",
subject: meta?.subject ?? "Purchase Order"
}}
footerDocumentId={displayId || undefined}
footerLabel={registration.label}
showFooter={showFooter}
showPageNumbers={settings.showPageNumbers}
pageNumberFormat={settings.pageNumberFormat}
showRegistrationLine={registration.show}
fontFamily={settings.fontFamily}
headerContent={headerContent}
footerContent={footerContent}
>
{visibleBlocks.map((block) => {
const render = purchaseOrderBlockRegistry[block.type];
if (!render) return null;
return <Fragment key={block.id}>{render({ block, data })}</Fragment>;
})}
</Template>
);
};
export default PurchaseOrderPDF;