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 {
Badge,
HStack,
Tooltip,
TooltipContent,
TooltipTrigger
} from "@carbon/react";
import type { I18n } from "@lingui/core";
import { msg } from "@lingui/core/macro";
import type { ColumnDef } from "@tanstack/react-table";
import {
LuBlocks,
LuBox,
LuCalendarClock,
LuCalendarRange,
LuChartNoAxesColumn,
LuCircleCheck,
LuGroup,
LuTrendingDown
} from "react-icons/lu";
import { exportOnlyColumn, MethodItemTypeIcon } from "~/components";
import { Enumerable } from "~/components/Enumerable";
import { inventoryItemTypes } from "~/modules/inventory/inventory.models";
import { itemReorderingPolicies } from "~/modules/items/items.models";
import {
getReorderPolicyDescription,
ItemReorderPolicy
} from "~/modules/items/ui/Item/ItemReorderPolicy";
import type { ProductionPlanningItem } from "~/modules/production/types";
import {
LatestOrderDateCell,
latestOrderDateExportValue
} from "~/modules/production/ui/Planning/LatestOrderDate";
import {
FirstNegativeDateCell,
TimeFenceCell,
type useTimeFenceOverrides
} from "~/modules/production/ui/Planning/PlanningFence";
import {
PlanningWeekStrip,
planningWeekStripSize,
planningWeekValues
} from "~/modules/production/ui/Planning/PlanningWeekStrip";
import type { PurchasingPlanningItem } from "~/modules/purchasing/types";
// Labels as descriptors: this factory is not a component, so it cannot use
// `useLingui`'s `t`; the grid's `i18n` renders them.
const LABELS = {
stockAvailability: msg`Stock Availability`,
unitOfMeasure: msg`Unit of Measure`,
itemGroup: msg`Item Group`,
reorderPolicy: msg`Reorder Policy`,
onHand: msg`On Hand`,
firstNegativeDate: msg`1st Negative On Hand`,
latestOrderDate: msg`Latest Order Date`,
timeFence: msg`Planning Horizon`,
type: msg`Type`
};
type PlanningRow = ProductionPlanningItem | PurchasingPlanningItem;
type Option = { value: string; label: string };
/**
* The columns the production and purchasing planning grids share, built once.
* Each grid keeps its own order and its own columns (item link, supplier,
* lead time, quantity to order, the Order button) around these. They were two
* copies that differed only in the row type.
*
* `accessorKey` is typed against the union of the two row types, which the
* grid's own row type narrows; the cast at the end is that narrowing.
*
* Labels come from `LABELS` and render through the grid's `i18n`; a week's
* heading comes from `weekLabel` (`usePlanningWeekLabel`).
*/
export function planningColumns<T extends PlanningRow>(ctx: {
i18n: I18n;
periods: { id: string; startDate: string; endDate: string }[];
weekLabel: (startDate: string, index: number) => string;
locationToday: string;
formatQuantity: (quantity: number) => string;
unitOfMeasures: Option[];
itemPostingGroups: Option[];
timeFence: ReturnType<typeof useTimeFenceOverrides>;
onFenceChange: (itemId: string, date: string | null) => void;
}) {
const {
i18n,
periods,
weekLabel,
locationToday,
formatQuantity,
unitOfMeasures,
itemPostingGroups,
timeFence,
onFenceChange
} = ctx;
const columns = {
// The grid shows every week as one bar in a strip; the CSV keeps a column
// per week so an export still carries the numbers.
periods: [
{
id: "stockAvailability",
header: i18n._(LABELS.stockAvailability),
cell: ({ row }) => (
<PlanningWeekStrip
periods={periods}
values={planningWeekValues(row.original, periods)}
weekLabel={weekLabel}
/>
),
size: planningWeekStripSize(periods.length),
meta: {
icon: <LuChartNoAxesColumn />
}
},
...periods.map((period, index) => {
const weekKey = `week${index + 1}` as keyof PlanningRow;
return exportOnlyColumn<PlanningRow>({
id: weekKey,
header: weekLabel(period.startDate, index),
value: (row) => {
const value = row[weekKey] as number | undefined;
return value === undefined ? null : value;
}
});
})
] as ColumnDef<PlanningRow>[],
unitOfMeasure: {
accessorKey: "unitOfMeasureCode",
header: "",
cell: ({ row }) => (
<Enumerable
value={
unitOfMeasures.find(
(uom) => uom.value === row.original.unitOfMeasureCode
)?.label ?? null
}
/>
),
meta: {
filterHeader: i18n._(LABELS.unitOfMeasure),
exportValue: (row: PlanningRow) =>
unitOfMeasures.find((uom) => uom.value === row.unitOfMeasureCode)
?.label ?? null
}
} as ColumnDef<PlanningRow>,
itemGroup: {
accessorKey: "itemPostingGroupId",
header: i18n._(LABELS.itemGroup),
cell: ({ row }) => {
const label = itemPostingGroups.find(
(group) => group.value === row.original.itemPostingGroupId
)?.label;
return label ? <Badge variant="secondary">{label}</Badge> : null;
},
meta: {
filter: {
type: "static",
options: itemPostingGroups.map((group) => ({
value: group.value,
label: <Badge variant="secondary">{group.label}</Badge>
}))
},
icon: <LuGroup />,
exportValue: (row: PlanningRow) =>
itemPostingGroups.find(
(group) => group.value === row.itemPostingGroupId
)?.label ?? null
}
} as ColumnDef<PlanningRow>,
reorderPolicy: {
accessorKey: "reorderingPolicy",
header: i18n._(LABELS.reorderPolicy),
cell: ({ row }) => (
<HStack>
<Tooltip>
<TooltipTrigger>
<ItemReorderPolicy
reorderingPolicy={row.original.reorderingPolicy}
/>
</TooltipTrigger>
<TooltipContent>
{getReorderPolicyDescription(row.original)}
</TooltipContent>
</Tooltip>
</HStack>
),
meta: {
filter: {
type: "static",
options: itemReorderingPolicies.map((policy) => ({
label: <ItemReorderPolicy reorderingPolicy={policy} />,
value: policy
}))
},
icon: <LuCircleCheck />
}
} as ColumnDef<PlanningRow>,
onHand: {
accessorKey: "quantityOnHand",
header: i18n._(LABELS.onHand),
cell: ({ row }) => formatQuantity(row.original.quantityOnHand),
meta: {
icon: <LuBlocks />,
renderTotal: true
}
} as ColumnDef<PlanningRow>,
firstNegativeDate: {
accessorKey: "firstNegativeDate",
header: i18n._(LABELS.firstNegativeDate),
cell: ({ row }) => (
<FirstNegativeDateCell
date={row.original.firstNegativeDate}
todayIso={locationToday}
/>
),
meta: {
icon: <LuTrendingDown />
}
} as ColumnDef<PlanningRow>,
latestOrderDate: {
// The order-by date MRP stored on the item's open new-supply actions —
// the same actions the order drawer lists — so cell, sort and drawer
// agree.
accessorKey: "latestOrderDate",
header: i18n._(LABELS.latestOrderDate),
cell: ({ row }) => (
<LatestOrderDateCell
itemPlanning={row.original}
todayIso={locationToday}
/>
),
meta: {
icon: <LuCalendarClock />,
exportValue: latestOrderDateExportValue
}
} as ColumnDef<PlanningRow>,
timeFence: {
accessorKey: "timeFenceDate",
header: i18n._(LABELS.timeFence),
cell: ({ row }) => (
// The row toggles its expanded actions on click. React events bubble
// through the calendar's portal, so every click in the picker would
// reach the row without this.
<div onClick={(event) => event.stopPropagation()}>
<TimeFenceCell
fenceDate={timeFence.fenceDateFor(row.original)}
isOverridden={timeFence.isOverridden(row.original)}
onChange={(date) => onFenceChange(row.original.id, date)}
/>
</div>
),
meta: {
icon: <LuCalendarRange />,
exportValue: (row: PlanningRow) => timeFence.fenceDateFor(row)
}
} as ColumnDef<PlanningRow>,
type: {
accessorKey: "type",
header: i18n._(LABELS.type),
cell: ({ row }) =>
row.original.type && (
<HStack>
<MethodItemTypeIcon type={row.original.type} />
<span>{row.original.type}</span>
</HStack>
),
meta: {
filter: {
type: "static",
options: inventoryItemTypes
.filter((type) => ["Part", "Tool"].includes(type))
.map((type) => ({
label: (
<HStack spacing={2}>
<MethodItemTypeIcon type={type} />
<span>{type}</span>
</HStack>
),
value: type
}))
},
icon: <LuBox />
}
} as ColumnDef<PlanningRow>
};
return columns as unknown as {
[K in keyof typeof columns]: (typeof columns)[K] extends unknown[]
? ColumnDef<T>[]
: ColumnDef<T>;
};
}