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.

chore(wip): added approval workflow UI components

Ayush Soni9d977c3f2d71
diff --git a/apps/erp/app/modules/approvals/ui/ApprovalDecisionModal.tsx b/apps/erp/app/modules/approvals/ui/ApprovalDecisionModal.tsx
new file mode 100644
index 0000000000..e779d24f60
--- /dev/null
+++ b/apps/erp/app/modules/approvals/ui/ApprovalDecisionModal.tsx
@@ -0,0 +1,105 @@
+import {
+ Button,
+ HStack,
+ Modal,
+ ModalBody,
+ ModalClose,
+ ModalContent,
+ ModalDescription,
+ ModalFooter,
+ ModalHeader,
+ ModalTitle,
+ Textarea,
+ VStack
+} from "@carbon/react";
+import { useState } from "react";
+import { useFetcher } from "react-router";
+import type { ApprovalRequest } from "~/modules/approvals";
+import { path } from "~/utils/path";
+
+type ApprovalDecisionModalProps = {
+ approval: ApprovalRequest;
+ decisionType: "approve" | "reject";
+ isOpen: boolean;
+ onClose: () => void;
+};
+
+const ApprovalDecisionModal = ({
+ approval,
+ decisionType,
+ isOpen,
+ onClose
+}: ApprovalDecisionModalProps) => {
+ const fetcher = useFetcher();
+ const [notes, setNotes] = useState("");
+
+ const isApproving = decisionType === "approve";
+ const isSubmitting = fetcher.state !== "idle";
+
+ const handleSubmit = () => {
+ fetcher.submit(
+ {
+ id: approval.id!,
+ decision: isApproving ? "Approved" : "Rejected",
+ decisionNotes: notes
+ },
+ {
+ method: "post",
+ action: path.to.approvalDecision
+ }
+ );
+ };
+
+ return (
+ <Modal open={isOpen}>
+ <ModalContent>
+ <ModalHeader>
+ <ModalTitle>
+ {isApproving ? "Approve Request" : "Reject Request"}
+ </ModalTitle>
+ <ModalDescription>
+ {isApproving
+ ? `Are you sure you want to approve ${approval.documentReadableId}?`
+ : `Are you sure you want to reject ${approval.documentReadableId}?`}
+ </ModalDescription>
+ <ModalClose onClick={onClose} />
+ </ModalHeader>
+ <ModalBody>
+ <VStack spacing={4}>
+ <div className="w-full">
+ <label className="text-sm font-medium text-muted-foreground mb-2 block">
+ Notes (optional)
+ </label>
+ <Textarea
+ value={notes}
+ onChange={(e) => setNotes(e.target.value)}
+ placeholder="Add any notes about your decision..."
+ rows={4}
+ />
+ </div>
+ </VStack>
+ </ModalBody>
+ <ModalFooter>
+ <HStack spacing={2}>
+ <Button
+ variant="secondary"
+ onClick={onClose}
+ disabled={isSubmitting}
+ >
+ Cancel
+ </Button>
+ <Button
+ variant={isApproving ? "primary" : "destructive"}
+ onClick={handleSubmit}
+ isLoading={isSubmitting}
+ >
+ {isApproving ? "Approve" : "Reject"}
+ </Button>
+ </HStack>
+ </ModalFooter>
+ </ModalContent>
+ </Modal>
+ );
+};
+
+export default ApprovalDecisionModal;
diff --git a/apps/erp/app/modules/approvals/ui/ApprovalHistory.tsx b/apps/erp/app/modules/approvals/ui/ApprovalHistory.tsx
new file mode 100644
index 0000000000..31fc97026a
--- /dev/null
+++ b/apps/erp/app/modules/approvals/ui/ApprovalHistory.tsx
@@ -0,0 +1,77 @@
+import {
+ Card,
+ CardContent,
+ CardHeader,
+ CardTitle,
+ VStack
+} from "@carbon/react";
+import { formatDate } from "@carbon/utils";
+import { EmployeeAvatar } from "~/components";
+import type { ApprovalHistory as ApprovalHistoryType } from "~/modules/approvals";
+import ApprovalStatus from "./ApprovalStatus";
+
+type ApprovalHistoryProps = {
+ history: ApprovalHistoryType;
+};
+
+const ApprovalHistory = ({ history }: ApprovalHistoryProps) => {
+ if (!history || history.length === 0) {
+ return (
+ <Card>
+ <CardHeader>
+ <CardTitle>Approval History</CardTitle>
+ </CardHeader>
+ <CardContent>
+ <p className="text-muted-foreground text-sm">No approval history</p>
+ </CardContent>
+ </Card>
+ );
+ }
+
+ return (
+ <Card>
+ <CardHeader>
+ <CardTitle>Approval History</CardTitle>
+ </CardHeader>
+ <CardContent>
+ <VStack spacing={4} className="w-full">
+ {history.map((item) => (
+ <div key={item.id} className="border rounded-lg p-4 space-y-2">
+ <div className="flex items-center justify-between">
+ <ApprovalStatus status={item.status} />
+ <span className="text-sm text-muted-foreground">
+ {formatDate(item.requestedAt)}
+ </span>
+ </div>
+ <div className="flex items-center gap-2">
+ <span className="text-sm text-muted-foreground">
+ Requested by:
+ </span>
+ <EmployeeAvatar employeeId={item.requestedBy} />
+ </div>
+ {item.decisionBy && (
+ <div className="flex items-center gap-2">
+ <span className="text-sm text-muted-foreground">
+ Decision by:
+ </span>
+ <EmployeeAvatar employeeId={item.decisionBy} />
+ <span className="text-sm text-muted-foreground">
+ on {formatDate(item.decisionAt)}
+ </span>
+ </div>
+ )}
+ {item.decisionNotes && (
+ <div className="text-sm">
+ <span className="text-muted-foreground">Notes: </span>
+ {item.decisionNotes}
+ </div>
+ )}
+ </div>
+ ))}
+ </VStack>
+ </CardContent>
+ </Card>
+ );
+};
+
+export default ApprovalHistory;
diff --git a/apps/erp/app/modules/approvals/ui/ApprovalStatus.tsx b/apps/erp/app/modules/approvals/ui/ApprovalStatus.tsx
new file mode 100644
index 0000000000..3a1952ee54
--- /dev/null
+++ b/apps/erp/app/modules/approvals/ui/ApprovalStatus.tsx
@@ -0,0 +1,23 @@
+import { Status } from "@carbon/react";
+import type { approvalStatusType } from "../approvals.models";
+
+type ApprovalStatusProps = {
+ status: (typeof approvalStatusType)[number] | null;
+};
+
+const ApprovalStatus = ({ status }: ApprovalStatusProps) => {
+ switch (status) {
+ case "Pending":
+ return <Status color="orange">Pending</Status>;
+ case "Approved":
+ return <Status color="green">Approved</Status>;
+ case "Rejected":
+ return <Status color="red">Rejected</Status>;
+ case "Cancelled":
+ return <Status color="gray">Cancelled</Status>;
+ default:
+ return null;
+ }
+};
+
+export default ApprovalStatus;
diff --git a/apps/erp/app/modules/approvals/ui/ApprovalsTable.tsx b/apps/erp/app/modules/approvals/ui/ApprovalsTable.tsx
new file mode 100644
index 0000000000..1511562d3b
--- /dev/null
+++ b/apps/erp/app/modules/approvals/ui/ApprovalsTable.tsx
@@ -0,0 +1,278 @@
+import {
+ Badge,
+ HStack,
+ MenuIcon,
+ MenuItem,
+ useDisclosure
+} from "@carbon/react";
+import { formatDate } from "@carbon/utils";
+import type { ColumnDef } from "@tanstack/react-table";
+import { memo, useCallback, useMemo, useState } from "react";
+import {
+ LuCalendar,
+ LuCheck,
+ LuFileText,
+ LuStar,
+ LuUser,
+ LuX
+} from "react-icons/lu";
+import { EmployeeAvatar, Hyperlink, Table } from "~/components";
+import type { ApprovalRequest } from "~/modules/approvals";
+import { approvalDocumentType, approvalStatusType } from "~/modules/approvals";
+import { usePeople } from "~/stores";
+import { path } from "~/utils/path";
+import ApprovalDecisionModal from "./ApprovalDecisionModal";
+import ApprovalStatus from "./ApprovalStatus";
+
+type ApprovalsTableProps = {
+ data: ApprovalRequest[];
+ count: number;
+};
+
+const ApprovalsTable = memo(({ data, count }: ApprovalsTableProps) => {
+ const [people] = usePeople();
+
+ const [selectedApproval, setSelectedApproval] =
+ useState<ApprovalRequest | null>(null);
+ const [decisionType, setDecisionType] = useState<"approve" | "reject">(
+ "approve"
+ );
+
+ const decisionModal = useDisclosure();
+
+ const columns = useMemo<ColumnDef<ApprovalRequest>[]>(() => {
+ const defaultColumns: ColumnDef<ApprovalRequest>[] = [
+ {
+ accessorKey: "documentReadableId",
+ header: "Document",
+ cell: ({ row }) => {
+ const docType = row.original.documentType;
+ const docId = row.original.documentId;
+ const readableId = row.original.documentReadableId;
+ const label = readableId ?? docId ?? "—";
+
+ if (!docId) {
+ return (
+ <HStack>
+ <span>{label}</span>
+ </HStack>
+ );
+ }
+
+ const link =
+ docType === "purchaseOrder"
+ ? path.to.purchaseOrderDetails(docId)
+ : path.to.qualityDocument(docId);
+
+ return (
+ <HStack>
+ <Hyperlink to={link}>{label}</Hyperlink>
+ </HStack>
+ );
+ },
+ meta: {
+ icon: <LuFileText />
+ }
+ },
+ {
+ accessorKey: "documentType",
+ header: "Type",
+ cell: ({ row }) => {
+ const type = row.original.documentType;
+ return (
+ <Badge variant="secondary">
+ {type === "purchaseOrder" ? "Purchase Order" : "Quality Document"}
+ </Badge>
+ );
+ },
+ meta: {
+ filter: {
+ type: "static",
+ options: approvalDocumentType.map((type) => ({
+ value: type,
+ label:
+ type === "purchaseOrder" ? "Purchase Order" : "Quality Document"
+ }))
+ },
+ pluralHeader: "Document Types",
+ icon: <LuFileText />
+ }
+ },
+ {
+ accessorKey: "documentDescription",
+ header: "Description",
+ cell: (item) => item.getValue(),
+ meta: {
+ icon: <LuFileText />
+ }
+ },
+ {
+ accessorKey: "status",
+ header: "Status",
+ cell: ({ row }) => {
+ return <ApprovalStatus status={row.original.status} />;
+ },
+ meta: {
+ filter: {
+ type: "static",
+ options: approvalStatusType.map((status) => ({
+ value: status,
+ label: <ApprovalStatus status={status} />
+ }))
+ },
+ pluralHeader: "Statuses",
+ icon: <LuStar />
+ }
+ },
+ {
+ id: "requestedByName",
+ header: "Requested By",
+ cell: ({ row }) => (
+ <EmployeeAvatar employeeId={row.original.requestedBy} />
+ ),
+ meta: {
+ filter: {
+ type: "static",
+ options: people.map((employee) => ({
+ value: employee.id,
+ label: employee.name
+ }))
+ },
+ icon: <LuUser />
+ }
+ },
+ {
+ accessorKey: "requestedAt",
+ header: "Requested Date",
+ cell: (item) => formatDate(item.getValue<string>()),
+ meta: {
+ icon: <LuCalendar />
+ }
+ },
+ {
+ id: "approverName",
+ header: "Approver",
+ cell: ({ row }) => {
+ const groupNames = row.original.approverGroupNames;
+ if (
+ groupNames &&
+ Array.isArray(groupNames) &&
+ groupNames.length > 0
+ ) {
+ return (
+ <HStack spacing={1}>
+ {groupNames.map((name, idx) => (
+ <Badge key={idx} variant="outline">
+ {name}
+ </Badge>
+ ))}
+ </HStack>
+ );
+ }
+ if (row.original.approverId) {
+ return <EmployeeAvatar employeeId={row.original.approverId} />;
+ }
+ return null;
+ },
+ meta: {
+ icon: <LuUser />
+ }
+ },
+ {
+ accessorKey: "decisionAt",
+ header: "Decision Date",
+ cell: (item) => formatDate(item.getValue<string>()),
+ meta: {
+ icon: <LuCalendar />
+ }
+ },
+ {
+ id: "decisionByName",
+ header: "Decided By",
+ cell: ({ row }) => (
+ <EmployeeAvatar employeeId={row.original.decisionBy} />
+ ),
+ meta: {
+ icon: <LuUser />
+ }
+ },
+ {
+ accessorKey: "decisionNotes",
+ header: "Notes",
+ cell: (item) => item.getValue(),
+ meta: {
+ icon: <LuFileText />
+ }
+ }
+ ];
+
+ return defaultColumns;
+ }, [people]);
+
+ const renderContextMenu = useCallback(
+ (row: ApprovalRequest) => (
+ <>
+ <MenuItem
+ disabled={row.status !== "Pending"}
+ onClick={() => {
+ setSelectedApproval(row);
+ setDecisionType("approve");
+ decisionModal.onOpen();
+ }}
+ >
+ <MenuIcon icon={<LuCheck />} />
+ Approve
+ </MenuItem>
+ <MenuItem
+ disabled={row.status !== "Pending"}
+ onClick={() => {
+ setSelectedApproval(row);
+ setDecisionType("reject");
+ decisionModal.onOpen();
+ }}
+ >
+ <MenuIcon icon={<LuX />} />
+ Reject
+ </MenuItem>
+ </>
+ ),
+ [decisionModal]
+ );
+
+ return (
+ <>
+ <Table<ApprovalRequest>
+ count={count}
+ columns={columns}
+ data={data}
+ defaultColumnPinning={{
+ left: ["documentReadableId"]
+ }}
+ defaultColumnVisibility={{
+ decisionNotes: false,
+ decisionAt: false,
+ decisionByName: false
+ }}
+ renderContextMenu={renderContextMenu}
+ title="Approvals"
+ table="approvalRequest"
+ withSavedView
+ />
+
+ {selectedApproval?.id && (
+ <ApprovalDecisionModal
+ approval={selectedApproval}
+ decisionType={decisionType}
+ isOpen={decisionModal.isOpen}
+ onClose={() => {
+ decisionModal.onClose();
+ setSelectedApproval(null);
+ }}
+ />
+ )}
+ </>
+ );
+});
+ApprovalsTable.displayName = "ApprovalsTable";
+
+export default ApprovalsTable;
diff --git a/apps/erp/app/modules/approvals/ui/index.ts b/apps/erp/app/modules/approvals/ui/index.ts
new file mode 100644
index 0000000000..61a1b40a39
--- /dev/null
+++ b/apps/erp/app/modules/approvals/ui/index.ts
@@ -0,0 +1,4 @@
+export { default as ApprovalDecisionModal } from "./ApprovalDecisionModal";
+export { default as ApprovalHistory } from "./ApprovalHistory";
+export { default as ApprovalStatus } from "./ApprovalStatus";
+export { default as ApprovalsTable } from "./ApprovalsTable";
diff --git a/apps/erp/app/modules/purchasing/ui/PurchaseOrder/PurchaseOrderHeader.tsx b/apps/erp/app/modules/purchasing/ui/PurchaseOrder/PurchaseOrderHeader.tsx
index 2485fa5837..2533a9ca6a 100644
--- a/apps/erp/app/modules/purchasing/ui/PurchaseOrder/PurchaseOrderHeader.tsx
+++ b/apps/erp/app/modules/purchasing/ui/PurchaseOrder/PurchaseOrderHeader.tsx
@@ -27,6 +27,7 @@ import {
LuLoaderCircle,
LuPanelLeft,
LuPanelRight,
+ LuSend,
LuTrash,
LuTruck
} from "react-icons/lu";
@@ -187,6 +188,31 @@ const PurchaseOrderHeader = () => {
>
Finalize
</SplitButton>
+ <statusFetcher.Form
+ method="post"
+ action={path.to.purchaseOrderSubmitForApproval(orderId)}
+ >
+ <Button
+ type="submit"
+ isLoading={
+ statusFetcher.state !== "idle" &&
+ statusFetcher.formAction?.includes("submit-for-approval")
+ }
+ isDisabled={
+ !["Planned"].includes(
+ routeData?.purchaseOrder?.status ?? ""
+ ) || !permissions.can("update", "purchasing")
+ }
+ leftIcon={<LuSend />}
+ variant={
+ ["Planned"].includes(routeData?.purchaseOrder?.status ?? "")
+ ? "primary"
+ : "secondary"
+ }
+ >
+ Submit for Approval
+ </Button>
+ </statusFetcher.Form>
{routeData?.purchaseOrder?.purchaseOrderType ===
"Outside Processing" &&
(shipments.length > 0 ? (