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.

6.4 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 { Badge } from "@carbon/react";
import { getItemReadableId } from "@carbon/utils";
import { useLingui } from "@lingui/react/macro";
import type { ColumnDef } from "@tanstack/react-table";
import { memo, useMemo } from "react";
import {
  LuBlocks,
  LuBookMarked,
  LuCalendar,
  LuClipboardCheck,
  LuHash,
  LuTruck
} from "react-icons/lu";
import {
  DateTime,
  EmployeeAvatar,
  exportOnlyColumn,
  Hyperlink,
  Table
} from "~/components";
import type { InfiniteOptions } from "~/components/Table";
import { useUrlParams } from "~/hooks";
import {
  inspectionSourceDocuments,
  inspectionStatusType
} from "~/modules/quality/quality.models";
import type { Inspection } from "~/modules/quality/types";
import { useItems } from "~/stores/items";
import { path } from "~/utils/path";
import { getInspectionStatusVariant } from "./InspectionStatus";

type InspectionsTableProps = {
  data: Inspection[];
  count: number;
  infinite: InfiniteOptions;
};

function computeProgress(row: Inspection): {
  inspected: number;
  total: number;
} {
  // The list loader selects `inspectionSample(status)` as an array of
  // child rows; count the non-Pending ones.
  const samples: { status: string }[] =
    ((row as any).inspectionSample as { status: string }[]) ?? [];
  const inspected = samples.filter((s) => s.status !== "Pending").length;
  return { inspected, total: (row as any).sampleSize ?? 0 };
}

const InspectionsTable = memo((props: InspectionsTableProps) => {
  const { data, count, infinite } = props;
  const { t } = useLingui();
  const [params] = useUrlParams();
  const [items] = useItems();

  const columns = useMemo<ColumnDef<Inspection>[]>(() => {
    return [
      {
        accessorKey: "inspectionId",
        header: t`Inspection`,
        cell: ({ row }) => (
          <Hyperlink
            to={`${path.to.inspection(row.original.id!)}?${params.toString()}`}
          >
            {(row.original as any).inspectionId}
          </Hyperlink>
        ),
        meta: {
          mobile: "P1",
          icon: <LuBookMarked />
        }
      },
      {
        accessorKey: "itemId",
        header: t`Item`,
        cell: ({ row }) => (
          <div className="flex flex-col gap-0">
            <span className="text-sm font-medium max-md:text-[13px] max-md:font-normal">
              {getItemReadableId(items, (row.original as any).itemId) ??
                (row.original as any).itemReadableId ??
                ""}
            </span>
            <span className="text-xs text-muted-foreground">
              {(row.original as any).item?.name}
            </span>
          </div>
        ),
        meta: {
          mobile: "P3",
          icon: <LuBookMarked />,
          filter: {
            type: "static",
            options: items.map((item) => ({
              value: item.id,
              label: item.readableIdWithRevision
            }))
          },
          // Without this the exporter substitutes the item's name for the id
          // (Download.tsx idNameMaps), losing the readable id the cell shows.
          exportValue: (row) =>
            getItemReadableId(items, (row as any).itemId) ??
            (row as any).itemReadableId ??
            null
        }
      },
      exportOnlyColumn<Inspection>({
        id: "itemName",
        header: t`Item Name`,
        value: (row) => (row as any).item?.name ?? null
      }),
      {
        accessorKey: "sourceDocument",
        header: t`Source`,
        cell: ({ row }) => (
          <div className="flex flex-col gap-0 text-sm">
            <span>
              {(row.original as any).sourceDocumentReadableId ??
                (row.original as any).sourceDocument}
            </span>
            <span className="text-xs text-muted-foreground">
              {(row.original as any).supplier?.name ??
                (row.original as any).sourceDocument}
            </span>
          </div>
        ),
        meta: {
          icon: <LuTruck />,
          filter: {
            type: "static",
            options: inspectionSourceDocuments.map((source) => ({
              value: source,
              label: source
            }))
          },
          exportValue: (row) =>
            (row as any).sourceDocumentReadableId ??
            (row as any).sourceDocument ??
            null
        }
      },
      {
        accessorKey: "lotSize",
        header: t`Lot Size`,
        cell: ({ row }) => (
          <span className="text-sm">{(row.original as any).lotSize ?? 0}</span>
        ),
        meta: { mobile: "P2", mobileLabel: true, icon: <LuBlocks /> }
      },
      {
        accessorKey: "sampleSize",
        header: t`Sample`,
        cell: ({ row }) => {
          const p = computeProgress(row.original);
          return (
            <span className="text-sm">
              {p.inspected} / {p.total}
            </span>
          );
        },
        meta: { icon: <LuHash /> }
      },
      {
        accessorKey: "status",
        header: t`Status`,
        cell: ({ row }) => (
          <Badge variant={getInspectionStatusVariant(row.original.status)}>
            {row.original.status}
          </Badge>
        ),
        meta: {
          mobile: "P2",
          icon: <LuClipboardCheck />,
          filter: {
            type: "static",
            options: inspectionStatusType.map((s) => ({
              value: s,
              label: <Badge variant={getInspectionStatusVariant(s)}>{s}</Badge>
            }))
          }
        }
      },
      {
        accessorKey: "createdBy",
        header: t`Received By`,
        cell: ({ row }) => (
          <EmployeeAvatar employeeId={row.original.createdBy} />
        ),
        meta: { icon: <LuBlocks /> }
      },
      {
        accessorKey: "createdAt",
        header: t`Received At`,
        cell: ({ row }) => (
          <DateTime value={row.original.createdAt} variant="date" />
        ),
        meta: { icon: <LuCalendar /> }
      }
    ];
  }, [items, t, params]);

  return (
    <Table<Inspection>
      data={data}
      infinite={infinite}
      columns={columns}
      count={count ?? 0}
      title={t`Inspections`}
      table="inspection"
      withSavedView
    />
  );
});

InspectionsTable.displayName = "InspectionsTable";
export default InspectionsTable;