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.

feat: add batches route and table

Brad Barbin76550a5c536f
diff --git a/apps/erp/app/modules/inventory/Batches/BatchesTable.tsx b/apps/erp/app/modules/inventory/Batches/BatchesTable.tsx
new file mode 100644
index 0000000000..219b412e1b
--- /dev/null
+++ b/apps/erp/app/modules/inventory/Batches/BatchesTable.tsx
@@ -0,0 +1,146 @@
+import { MenuIcon, MenuItem, VStack } from "@carbon/react";
+import { formatDate } from "@carbon/utils";
+import { useNavigate } from "@remix-run/react";
+import type { ColumnDef } from "@tanstack/react-table";
+import { memo, useCallback, useMemo } from "react";
+import { AiOutlinePartition } from "react-icons/ai";
+import {
+ LuBookMarked,
+ LuCalendar,
+ LuContainer,
+ LuPencil,
+} from "react-icons/lu";
+import { Hyperlink, New, SupplierAvatar, Table } from "~/components";
+import { usePermissions } from "~/hooks";
+import type { BatchTableRow } from "~/modules/inventory";
+import { useItems, useSuppliers } from "~/stores";
+import { path } from "~/utils/path";
+
+type BatchesTableProps = {
+ data: BatchTableRow[];
+ count: number;
+};
+
+const BatchesTable = memo(({ data, count }: BatchesTableProps) => {
+ const navigate = useNavigate();
+ const permissions = usePermissions();
+ const [items] = useItems();
+ const [suppliers] = useSuppliers();
+
+ const columns = useMemo<ColumnDef<BatchTableRow>[]>(() => {
+ const result: ColumnDef<BatchTableRow>[] = [
+ {
+ accessorKey: "batchId",
+ header: "Batch ID",
+ cell: ({ row }) => (
+ <Hyperlink to={path.to.batch(row.original.id!)}>
+ {row.original.number}
+ </Hyperlink>
+ ),
+ meta: {
+ icon: <LuBookMarked />,
+ },
+ },
+ {
+ accessorKey: "itemId",
+ header: "Item",
+ cell: ({ row }) => (
+ <VStack spacing={0}>
+ <span className="text-sm font-medium">{row.original.itemName}</span>
+ <span className="text-sm text-muted-foreground">
+ {row.original.itemReadableId}
+ </span>
+ </VStack>
+ ),
+ meta: {
+ icon: <AiOutlinePartition />,
+ filter: {
+ type: "static",
+ options: items.map((item) => ({
+ value: item.id,
+ label: item.readableId,
+ helperText: item.name,
+ })),
+ },
+ },
+ },
+ {
+ accessorKey: "supplierId",
+ header: "Supplier",
+ cell: ({ row }) => (
+ <SupplierAvatar supplierId={row.original.supplierId} />
+ ),
+ meta: {
+ icon: <LuContainer />,
+ filter: {
+ type: "static",
+ options: suppliers.map((supplier) => ({
+ value: supplier.id,
+ label: supplier.name,
+ })),
+ },
+ },
+ },
+ {
+ accessorKey: "manufacturingDate",
+ header: "Manufactured Date",
+ cell: (item) => formatDate(item.getValue<string>()),
+ meta: {
+ icon: <LuCalendar />,
+ },
+ },
+ {
+ accessorKey: "expirationDate",
+ header: "Expiry Date",
+ cell: (item) => formatDate(item.getValue<string>()),
+ meta: {
+ icon: <LuCalendar />,
+ },
+ },
+ ];
+
+ return result;
+ }, [items, suppliers]);
+
+ const renderContextMenu = useCallback(
+ (row: BatchTableRow) => {
+ return (
+ <>
+ <MenuItem
+ disabled={!permissions.can("update", "inventory")}
+ onClick={() => {
+ navigate(`${path.to.batch(row.id!)}`);
+ }}
+ >
+ <MenuIcon icon={<LuPencil />} />
+ View Batch
+ </MenuItem>
+ </>
+ );
+ },
+ [navigate, permissions]
+ );
+
+ return (
+ <>
+ <Table<BatchTableRow>
+ data={data}
+ columns={columns}
+ count={count}
+ defaultColumnPinning={{
+ left: ["batchId"],
+ }}
+ primaryAction={
+ permissions.can("create", "inventory") && (
+ <New label="Batch" to={path.to.newBatch} />
+ )
+ }
+ renderContextMenu={renderContextMenu}
+ title="Batches"
+ />
+ </>
+ );
+});
+
+BatchesTable.displayName = "BatchesTable";
+export default BatchesTable;
diff --git a/apps/erp/app/routes/x+/inventory+/batches.tsx b/apps/erp/app/routes/x+/inventory+/batches.tsx
new file mode 100644
index 0000000000..bf0f94c677
--- /dev/null
+++ b/apps/erp/app/routes/x+/inventory+/batches.tsx
@@ -0,0 +1,62 @@
+import { error } from "@carbon/auth";
+import { requirePermissions } from "@carbon/auth/auth.server";
+import { flash } from "@carbon/auth/session.server";
+import { VStack } from "@carbon/react";
+import { Outlet, useLoaderData } from "@remix-run/react";
+import type { LoaderFunctionArgs } from "@vercel/remix";
+import { json, redirect } from "@vercel/remix";
+import { getBatches } from "~/modules/inventory";
+import BatchesTable from "~/modules/inventory/Batches/BatchesTable";
+import type { Handle } from "~/utils/handle";
+import { path } from "~/utils/path";
+import { getGenericQueryFilters } from "~/utils/query";
+
+export const handle: Handle = {
+ breadcrumb: "Batches",
+ to: path.to.batches,
+};
+
+export async function loader({ request }: LoaderFunctionArgs) {
+ const { client, companyId } = await requirePermissions(request, {
+ view: "inventory",
+ });
+
+ const url = new URL(request.url);
+ const searchParams = new URLSearchParams(url.search);
+ const search = searchParams.get("search");
+ const { limit, offset, sorts, filters } =
+ getGenericQueryFilters(searchParams);
+
+ const [batches] = await Promise.all([
+ getBatches(client, companyId, {
+ search,
+ limit,
+ offset,
+ sorts,
+ filters,
+ }),
+ ]);
+
+ if (batches.error) {
+ throw redirect(
+ path.to.authenticatedRoot,
+ await flash(request, error(null, "Error loading batches"))
+ );
+ }
+
+ return json({
+ batches: batches.data ?? [],
+ count: batches.count ?? 0,
+ });
+}
+
+export default function ReceiptsRoute() {
+ const { batches, count } = useLoaderData<typeof loader>();
+
+ return (
+ <VStack spacing={0} className="h-full">
+ <BatchesTable data={batches} count={count ?? 0} />
+ <Outlet />
+ </VStack>
+ );
+}