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.8 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 { formatDate } from "@carbon/utils";
import { getPurchaseOrderDisplayId } from "../../../utils/purchase-order";
import type { PurchaseOrderData } from "./types";

/** Merge-field variable map for a Purchase Order. */
export function buildPurchaseOrderVars(
  data: Pick<
    PurchaseOrderData,
    | "purchaseOrder"
    | "purchaseOrderLocations"
    | "company"
    | "currencyCode"
    | "approval"
    | "locale"
  >
): Record<string, string> {
  const po = data.purchaseOrder;
  const loc = data.purchaseOrderLocations;
  const str = (v: unknown): string => (v == null ? "" : String(v));

  return {
    "order.number": po ? getPurchaseOrderDisplayId(po) : "",
    "order.revision": str(po?.revisionId ?? 0),
    "order.date": str(po?.orderDate),
    "order.supplierReference": str(po?.supplierReference),
    "order.currency": str(data.currencyCode),
    "order.approvedBy": str(data.approval?.approvedBy),
    "order.approvedDate": formatDate(
      data.approval?.approvedDate,
      undefined,
      data.locale
    ),
    "supplier.name": str(loc?.supplierName),
    "supplier.addressLine1": str(loc?.supplierAddressLine1),
    "supplier.city": str(loc?.supplierCity),
    "supplier.country": str(loc?.supplierCountryName),
    "company.name": str(data.company?.name),
    "company.city": str(data.company?.city),
    "company.country": str(data.company?.countryCode),
    "company.taxId": str(data.company?.taxId),
    "company.eori": str(data.company?.eori),
    "company.vatNumber": str(data.company?.vatNumber),
    "company.registrationNumber": str(data.company?.registrationNumber)
  };
}