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.6 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,
  LuStore,
  LuUser
} from "react-icons/lu";
import { Hyperlink, Table } from "~/components";
import { Enumerable } from "~/components/Enumerable";
import type { InfiniteOptions } from "~/components/Table";
import { useCurrencyDecimalsLookup } from "~/hooks";
import { path } from "~/utils/path";
import { chargeStatus, chargeType } from "../../invoicing.models";
import ChargeStatus from "./ChargeStatus";

type ChargeRow = Database["public"]["Tables"]["charge"]["Row"];

type ChargesTableProps = {
  data: ChargeRow[];
  count: number;
  infinite: InfiniteOptions;
};

const ChargesTable = memo((props: ChargesTableProps) => {
  const { data, count, infinite } = props;
  const { t } = useLingui();
  const { locale } = useLocale();
  // Each transaction carries its own currencyCode (a company can hold cards in
  // several currencies), 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 columns = useMemo<ColumnDef<ChargeRow>[]>(
    () => [
      {
        accessorKey: "chargeId",
        header: t`Transaction ID`,
        cell: ({ row }) => (
          <Hyperlink to={path.to.charge(row.original.id)}>
            {row.original.chargeId}
          </Hyperlink>
        ),
        meta: { icon: <LuHash />, mobile: "P1" }
      },
      {
        accessorKey: "type",
        header: t`Type`,
        cell: ({ row }) => <Enumerable value={row.original.type} />,
        meta: {
          mobile: "P2",
          icon: <LuCircleDot />,
          filter: {
            type: "static",
            options: chargeType.map((type) => ({
              value: type,
              label: <Enumerable value={type} />
            }))
          },
          pluralHeader: t`Types`
        }
      },
      {
        accessorKey: "status",
        header: t`Status`,
        cell: ({ row }) => <ChargeStatus status={row.original.status} />,
        meta: {
          mobile: "P2",
          icon: <LuCircleDot />,
          filter: {
            type: "static",
            options: chargeStatus.map((status) => ({
              value: status,
              label: <ChargeStatus status={status} />
            }))
          },
          pluralHeader: t`Statuses`
        }
      },
      {
        accessorKey: "transactionDate",
        header: t`Transaction Date`,
        cell: (item) => formatDate(item.getValue<string>(), undefined, locale),
        meta: { filter: { type: "dateRange" }, icon: <LuCalendar /> }
      },
      {
        accessorKey: "merchantName",
        header: t`Merchant`,
        cell: ({ row }) => row.original.merchantName ?? null,
        meta: { icon: <LuStore />, mobile: "P3" }
      },
      {
        accessorKey: "cardHolderName",
        header: t`Card Holder`,
        cell: ({ row }) => row.original.cardHolderName ?? null,
        meta: { icon: <LuUser /> }
      },
      {
        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: {
          mobile: "P2",
          icon: <LuCoins />
        }
      },
      {
        accessorKey: "journalId",
        header: t`Journal`,
        cell: ({ row }) => row.original.journalId ?? null,
        meta: { icon: <LuCreditCard /> }
      }
    ],
    [t, locale, currencyDecimals]
  );

  return (
    <Table<ChargeRow>
      infinite={infinite}
      count={count}
      columns={columns}
      data={data}
      defaultColumnPinning={{ left: ["chargeId"] }}
      defaultColumnVisibility={{
        journalId: false
      }}
      title={t`Charges`}
      table="charge"
      withSavedView
    />
  );
});

ChargesTable.displayName = "ChargesTable";

export default ChargesTable;