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.

4.8 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 type { Database } from "@carbon/database";
import { formatDate, formatMoney } from "@carbon/utils";
import { useLingui } from "@lingui/react/macro";
import { useLocale } from "@react-aria/i18n";
import type { ColumnDef } from "@tanstack/react-table";
import { memo, useMemo } from "react";
import {
  LuCalendar,
  LuCircleDot,
  LuCoins,
  LuCreditCard,
  LuHash,
  LuTag,
  LuUser
} from "react-icons/lu";
import { EmployeeAvatar, Hyperlink, Table } from "~/components";
import type { InfiniteOptions } from "~/components/Table";
import { useCurrencyDecimalsLookup } from "~/hooks";
import { usePeople } from "~/stores";
import { path } from "~/utils/path";
import { reimbursementStatus } from "../../invoicing.models";
import ReimbursementStatus from "./ReimbursementStatus";

type ReimbursementRow = Database["public"]["Tables"]["reimbursement"]["Row"];

type ReimbursementsTableProps = {
  data: ReimbursementRow[];
  count: number;
  infinite: InfiniteOptions;
};

const ReimbursementsTable = memo(
  ({ data, count, infinite }: ReimbursementsTableProps) => {
    const { t } = useLingui();
    const { locale } = useLocale();
    // Each reimbursement carries its own currencyCode, so the amount is
    // formatted per row from a decimals lookup rather than a single-currency
    // hook — and there is no cross-currency total, which would sum unlike
    // units into a meaningless number.
    const currencyDecimals = useCurrencyDecimalsLookup();
    const [people] = usePeople();

    const columns = useMemo<ColumnDef<ReimbursementRow>[]>(
      () => [
        {
          accessorKey: "reimbursementId",
          header: t`Reimbursement ID`,
          cell: ({ row }) => (
            <Hyperlink to={path.to.reimbursement(row.original.id)}>
              {row.original.reimbursementId}
            </Hyperlink>
          ),
          meta: { icon: <LuHash />, mobile: "P1" }
        },
        {
          accessorKey: "status",
          header: t`Status`,
          cell: ({ row }) => (
            <ReimbursementStatus status={row.original.status} />
          ),
          meta: {
            mobile: "P2",
            icon: <LuCircleDot />,
            filter: {
              type: "static",
              options: reimbursementStatus.map((status) => ({
                value: status,
                label: <ReimbursementStatus status={status} />
              }))
            },
            pluralHeader: t`Statuses`
          }
        },
        {
          accessorKey: "employeeId",
          header: t`Employee`,
          cell: ({ row }) => (
            <EmployeeAvatar employeeId={row.original.employeeId} />
          ),
          meta: {
            mobile: "P3",
            icon: <LuUser />,
            filter: {
              type: "static",
              options: people.map((employee) => ({
                value: employee.id,
                label: employee.name
              }))
            },
            pluralHeader: t`Employees`
          }
        },
        {
          accessorKey: "reimbursementDate",
          header: t`Date`,
          cell: (item) =>
            formatDate(item.getValue<string>(), undefined, locale),
          meta: { filter: { type: "dateRange" }, icon: <LuCalendar /> }
        },
        {
          accessorKey: "reference",
          header: t`Reference`,
          cell: ({ row }) => row.original.reference ?? null,
          meta: { icon: <LuTag /> }
        },
        {
          accessorKey: "amount",
          header: t`Amount`,
          cell: ({ row }) => {
            const code = row.original.currencyCode || "USD";
            return (
              <span className="tabular-nums">
                {formatMoney(
                  row.original.amount,
                  locale,
                  code,
                  currencyDecimals(code)
                )}
              </span>
            );
          },
          meta: { icon: <LuCoins />, mobile: "P2" }
        },
        {
          accessorKey: "journalId",
          header: t`Journal`,
          cell: ({ row }) => row.original.journalId ?? null,
          meta: { icon: <LuCreditCard /> }
        }
      ],
      [t, locale, currencyDecimals, people]
    );

    return (
      <Table<ReimbursementRow>
        infinite={infinite}
        count={count}
        columns={columns}
        data={data}
        defaultColumnPinning={{ left: ["reimbursementId"] }}
        defaultColumnVisibility={{
          journalId: false
        }}
        title={t`Reimbursements`}
        table="reimbursement"
        withSavedView
      />
    );
  }
);

ReimbursementsTable.displayName = "ReimbursementsTable";

export default ReimbursementsTable;