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.

9.5 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 {
  HStack,
  MENU_ITEM_SHORTCUTS,
  MenuIcon,
  MenuItem,
  useDisclosure
} from "@carbon/react";
import { Trans, useLingui } from "@lingui/react/macro";
import type { ColumnDef } from "@tanstack/react-table";
import type { ReactNode } from "react";
import { memo, useCallback, useMemo, useState } from "react";
import {
  LuBookmark,
  LuCalendar,
  LuCircleDollarSign,
  LuFileText,
  LuPencil,
  LuStar,
  LuTag,
  LuTrash,
  LuUser
} from "react-icons/lu";
import { useNavigate } from "react-router";
import { DateTime, EmployeeAvatar, Hyperlink, Table } from "~/components";
import { Enumerable } from "~/components/Enumerable";
import { JournalEntrySourceTypeIcon } from "~/components/Icons";
import { ConfirmDelete } from "~/components/Modals";
import type { InfiniteOptions } from "~/components/Table";
import { usePermissions, useUser } from "~/hooks";
import { useCurrencyFormatter } from "~/hooks/useCurrencyFormatter";
import { usePeople } from "~/stores/people";
import { path } from "~/utils/path";
import {
  journalEntrySourceTypes,
  journalEntryStatuses
} from "../../accounting.models";
import type { JournalEntryListItem } from "../../types";
import JournalEntryStatus from "./JournalEntryStatus";

type JournalEntriesTableProps = {
  data: JournalEntryListItem[];
  count: number;
  infinite: InfiniteOptions;
  primaryAction?: ReactNode;
};

const defaultColumnVisibility = {
  sourceType: false,
  createdBy: false,
  createdAt: false,
  updatedBy: false,
  updatedAt: false
};

const JournalEntriesTable = memo(
  ({ data, count, primaryAction, infinite }: JournalEntriesTableProps) => {
    const { t } = useLingui();
    const navigate = useNavigate();
    const permissions = usePermissions();
    const { company } = useUser();
    const [people] = usePeople();
    const currencyFormatter = useCurrencyFormatter({
      currency: company.baseCurrencyCode
    });
    const [selectedEntry, setSelectedEntry] =
      useState<JournalEntryListItem | null>(null);
    const deleteModal = useDisclosure();

    const columns = useMemo<ColumnDef<JournalEntryListItem>[]>(() => {
      const defaultColumns: ColumnDef<JournalEntryListItem>[] = [
        {
          accessorKey: "journalEntryId",
          header: t`Journal Entry`,
          cell: ({ row }) => (
            <Hyperlink
              to={path.to.journalEntryDetails(row.original.id?.toString()!)}
            >
              {row.original.journalEntryId}
            </Hyperlink>
          ),
          meta: {
            mobile: "P1",
            icon: <LuBookmark />
          }
        },
        {
          accessorKey: "postingDate",
          header: t`Date`,
          cell: ({ row }) => (
            <DateTime value={row.original.postingDate} variant="date" />
          ),
          meta: {
            filter: {
              type: "dateRange"
            },
            icon: <LuCalendar />
          }
        },
        {
          accessorKey: "description",
          header: t`Description`,
          cell: ({ row }) => (
            <HStack className="py-1" spacing={2}>
              <div className="w-8 h-8 bg-muted rounded-lg flex items-center justify-center flex-shrink-0 p-1">
                <JournalEntrySourceTypeIcon
                  sourceType={row.original.sourceType ?? "Manual"}
                  className="w-4 h-4 text-[#AAAAAA] dark:text-[#444]"
                />
              </div>

              <div className="flex flex-col max-w-[300px] truncate">
                <div className="text-sm line-clamp-1">
                  {row.original.description || "—"}
                </div>
                <div className="text-xs text-muted-foreground">
                  {row.original.sourceType || "—"}
                </div>
              </div>
            </HStack>
          ),
          meta: {
            mobile: "P3",
            // Phones: plain text on the row's context line.
            mobileCell: ({ row }) => row.original.description || "—",
            icon: <LuFileText />
          }
        },
        {
          accessorKey: "sourceType",
          header: t`Source`,
          cell: ({ row }) => <Enumerable value={row.original.sourceType} />,
          meta: {
            icon: <LuTag />,
            filter: {
              type: "static",
              options: journalEntrySourceTypes.map((v) => ({
                label: <Enumerable value={v} />,
                value: v
              }))
            }
          }
        },
        {
          accessorKey: "status",
          header: t`Status`,
          cell: ({ row }) => (
            <JournalEntryStatus
              status={
                row.original.status as (typeof journalEntryStatuses)[number]
              }
            />
          ),
          meta: {
            mobile: "P2",
            filter: {
              type: "static",
              options: journalEntryStatuses.map((v) => ({
                label: <JournalEntryStatus status={v} />,
                value: v
              }))
            },
            icon: <LuStar />
          }
        },
        {
          accessorKey: "totalDebits",
          header: t`Debits`,
          cell: ({ row }) =>
            currencyFormatter.format(Number(row.original.totalDebits)),
          meta: {
            mobile: "P2",
            icon: <LuCircleDollarSign />
          }
        },
        {
          accessorKey: "totalCredits",
          header: t`Credits`,
          cell: ({ row }) =>
            currencyFormatter.format(Number(row.original.totalCredits)),
          meta: {
            icon: <LuCircleDollarSign />
          }
        },
        {
          accessorKey: "createdBy",
          header: t`Created By`,
          cell: ({ row }) => (
            <EmployeeAvatar employeeId={row.original.createdBy} />
          ),
          meta: {
            filter: {
              type: "static",
              options: people.map((employee) => ({
                value: employee.id,
                label: <Enumerable value={employee.name} />
              }))
            },
            icon: <LuUser />
          }
        },
        {
          accessorKey: "createdAt",
          header: t`Created At`,
          cell: ({ row }) => (
            <DateTime value={row.original.createdAt} variant="date" />
          ),
          meta: {
            icon: <LuCalendar />
          }
        },
        {
          accessorKey: "updatedBy",
          header: t`Updated By`,
          cell: ({ row }) => (
            <EmployeeAvatar employeeId={row.original.updatedBy} />
          ),
          meta: {
            filter: {
              type: "static",
              options: people.map((employee) => ({
                value: employee.id,
                label: <Enumerable value={employee.name} />
              }))
            },
            icon: <LuUser />
          }
        },
        {
          accessorKey: "updatedAt",
          header: t`Updated At`,
          cell: ({ row }) => (
            <DateTime value={row.original.updatedAt} variant="date" />
          ),
          meta: {
            icon: <LuCalendar />
          }
        }
      ];
      return defaultColumns;
    }, [currencyFormatter, people, t]);

    const renderContextMenu = useCallback(
      (row: JournalEntryListItem) => {
        const isDraft = row.status === "Draft";
        return (
          <>
            <MenuItem
              shortcut={
                isDraft ? MENU_ITEM_SHORTCUTS.edit : MENU_ITEM_SHORTCUTS.view
              }
              disabled={!permissions.can("view", "accounting")}
              onClick={() => {
                navigate(path.to.journalEntryDetails(row.id?.toString()!));
              }}
            >
              <MenuIcon icon={<LuPencil />} />
              {isDraft ? t`Edit Journal Entry` : t`View Journal Entry`}
            </MenuItem>
            <MenuItem
              shortcut={MENU_ITEM_SHORTCUTS.delete}
              disabled={!isDraft || !permissions.can("delete", "accounting")}
              destructive
              onClick={() => {
                setSelectedEntry(row);
                deleteModal.onOpen();
              }}
            >
              <MenuIcon icon={<LuTrash />} />
              <Trans>Delete Journal Entry</Trans>
            </MenuItem>
          </>
        );
      },
      [deleteModal, navigate, permissions, t]
    );

    return (
      <>
        <Table<JournalEntryListItem>
          infinite={infinite}
          data={data}
          columns={columns}
          count={count}
          defaultColumnVisibility={defaultColumnVisibility}
          primaryAction={primaryAction}
          renderContextMenu={renderContextMenu}
          title={t`Journal Entries`}
          table="journal"
          withSavedView
        />
        {selectedEntry && selectedEntry.id && (
          <ConfirmDelete
            action={path.to.deleteJournalEntry(selectedEntry.id.toString())}
            isOpen={deleteModal.isOpen}
            name={selectedEntry.journalEntryId ?? ""}
            text={t`Are you sure you want to delete ${selectedEntry.journalEntryId}?`}
            onCancel={() => {
              deleteModal.onClose();
              setSelectedEntry(null);
            }}
            onSubmit={() => {
              deleteModal.onClose();
              setSelectedEntry(null);
            }}
          />
        )}
      </>
    );
  }
);

JournalEntriesTable.displayName = "JournalEntriesTable";
export default JournalEntriesTable;