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.

5.7 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 { useAction } from "@carbon/query";
import {
  Alert,
  AlertDescription,
  AlertTitle,
  Button,
  HStack,
  Modal,
  ModalBody,
  ModalContent,
  ModalFooter,
  ModalHeader,
  ModalTitle,
  Table,
  Tbody,
  Td,
  Th,
  Thead,
  Tr,
  VStack
} from "@carbon/react";

import { distinctItemText, getItemReadableId } from "@carbon/utils";
import { Trans } from "@lingui/react/macro";
import { useMemo } from "react";
import { LuTriangleAlert } from "react-icons/lu";
import { useNavigate } from "react-router";
import type { action as postAction } from "~/routes/x+/purchase-invoice+/$invoiceId.post";
import { useItems } from "~/stores";
import { path } from "~/utils/path";

type PurchaseInvoicePostModalProps = {
  isOpen: boolean;
  onClose: () => void;
  invoiceId: string;
  linesToReceive: {
    itemId: string | null;
    description: string | null;
    quantity: number;
  }[];
};

const PurchaseInvoicePostModal = ({
  isOpen,
  onClose,
  invoiceId,
  linesToReceive
}: PurchaseInvoicePostModalProps) => {
  const [items] = useItems();
  const navigate = useNavigate();
  const hasLinesToReceive = linesToReceive.length > 0;

  const hasTrackedItems = useMemo(() => {
    return linesToReceive.some((line) => {
      const item = items.find((i) => i.id === line.itemId);
      return (
        item?.itemTrackingType === "Serial" ||
        item?.itemTrackingType === "Batch"
      );
    });
  }, [linesToReceive, items]);

  // A failed post is toasted by useAction.
  const fetcher = useAction<typeof postAction>({
    onSuccess: ({ data }) => {
      // A tracked item's receipt is created unposted: open it.
      if (data?.receiptId) navigate(path.to.receipt(data.receiptId));
      onClose();
    }
  });

  return (
    <Modal
      open={isOpen}
      onOpenChange={(open) => {
        if (!open) onClose();
      }}
    >
      <ModalContent>
        <ModalHeader>
          <ModalTitle>
            <Trans>Post Invoice</Trans>
          </ModalTitle>
        </ModalHeader>
        <ModalBody>
          {hasLinesToReceive ? (
            <div className="gap-4 w-full flex flex-col">
              {hasTrackedItems ? (
                <>
                  <p className="text-sm text-muted-foreground">
                    Are you sure you want to post this invoice? A receipt will
                    be created for:
                  </p>
                  <Alert variant="destructive">
                    <LuTriangleAlert className="h-4 w-4" />
                    <AlertTitle>
                      <Trans>Serial or Batch Tracking Required</Trans>
                    </AlertTitle>
                    <AlertDescription>
                      Some items require serial or batch tracking. The receipt
                      will be created but not posted. You will be redirected to
                      the receipt to enter tracking information.
                    </AlertDescription>
                  </Alert>
                </>
              ) : (
                <p className="text-sm text-muted-foreground">
                  Are you sure you want to post this invoice? A receipt will be
                  automatically created and posted for:
                </p>
              )}
              <Table>
                <Thead>
                  <Tr>
                    <Th>
                      <Trans>Item</Trans>
                    </Th>
                    <Th className="text-right">Quantity</Th>
                  </Tr>
                </Thead>
                <Tbody>
                  {linesToReceive.map((line) => (
                    <Tr key={line.itemId} className="text-sm">
                      <Td>
                        <VStack spacing={0}>
                          <span>
                            {getItemReadableId(items, line.itemId) ?? ""}
                          </span>
                          {distinctItemText(
                            getItemReadableId(items, line.itemId) ?? "",
                            line.description
                          ) && (
                            <span className="text-xs text-muted-foreground">
                              {line.description}
                            </span>
                          )}
                        </VStack>
                      </Td>
                      <Td className="text-right">{line.quantity}</Td>
                    </Tr>
                  ))}
                </Tbody>
              </Table>
            </div>
          ) : (
            <p className="text-sm text-muted-foreground">
              Are you sure you want to post this invoice?
            </p>
          )}
        </ModalBody>
        <ModalFooter>
          <HStack>
            <Button variant="solid" onClick={onClose}>
              <Trans>Cancel</Trans>
            </Button>
            <fetcher.Form
              method="post"
              action={path.to.purchaseInvoicePost(invoiceId)}
            >
              {hasTrackedItems && (
                <input type="hidden" name="skipReceiptPost" value="true" />
              )}
              <Button
                isDisabled={fetcher.state !== "idle"}
                isLoading={fetcher.state !== "idle"}
                type="submit"
              >
                {hasLinesToReceive
                  ? hasTrackedItems
                    ? "Post and Create Receipt"
                    : "Post and Receive Invoice"
                  : "Post Invoice"}
              </Button>
            </fetcher.Form>
          </HStack>
        </ModalFooter>
      </ModalContent>
    </Modal>
  );
};

export default PurchaseInvoicePostModal;