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.

8.4 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 { JSONContent } from "@carbon/react";
import { formatPercent } from "@carbon/utils";
import { Image, Text, View } from "@react-pdf/renderer";
import {
  DEFAULT_LINE_ITEMS_OPTIONS,
  type LineItemsBlock as LineItemsBlockType
} from "../../template";
import {
  getLineDescription,
  getLineDescriptionDetails,
  getLineTaxableSubtotal,
  getLineTotal
} from "../../utils/sales-invoice";
import { Note } from "../components";
import { itemTextOverflowStyle } from "./itemText";
import { useTw } from "./tw";
import type { SalesInvoiceData } from "./types";

export function LineItemsBlock({
  block,
  data
}: {
  block: LineItemsBlockType;
  data: SalesInvoiceData;
}) {
  const tw = useTw();
  const {
    salesInvoiceLines,
    thumbnails,
    numberFormatter,
    rateFormatter,
    theme,
    locale
  } = data;
  const opts = { ...DEFAULT_LINE_ITEMS_OPTIONS, ...block.options };
  const overflow = itemTextOverflowStyle(opts);
  let rowIndex = 0;
  // Comment lines carry no quantity or price, so they take no line number.
  let lineNumber = 0;

  return (
    <View>
      {/* Header */}
      <View
        fixed
        style={[
          tw("flex flex-row py-2 px-3 text-[9px] font-bold items-center"),
          { backgroundColor: theme.accent, color: theme.accentForeground }
        ]}
      >
        <Text style={tw("w-[6%] text-center")}>#</Text>
        <Text style={tw("w-[44%]")}>Description</Text>
        <Text style={tw("w-1/6 text-center")}>Qty</Text>
        <Text style={tw("w-1/6 text-center")}>Unit Price</Text>
        <Text style={tw("w-1/6 text-center")}>Total</Text>
      </View>

      {/* Rows */}
      {salesInvoiceLines.map((line) => {
        const isEven = rowIndex % 2 === 0;
        rowIndex++;
        const isComment = line.invoiceLineType === "Comment";
        if (!isComment) lineNumber++;

        const lineAddOnCost = line.convertedAddOnCost ?? 0;
        const lineNonTaxableAddOnCost = line.convertedNonTaxableAddOnCost ?? 0;
        const lineShippingCost = line.convertedShippingCost ?? 0;
        const lineTaxPercent = line.taxPercent ?? 0;
        const lineDiscountPercent = line.discountPercent ?? 0;
        const lineTaxAmount = getLineTaxableSubtotal(line) * lineTaxPercent;
        const totalTaxAndFees =
          lineAddOnCost +
          lineNonTaxableAddOnCost +
          lineShippingCost +
          lineTaxAmount;

        return (
          <View key={line.id}>
            <View
              wrap={false}
              style={[
                tw(
                  "flex flex-row py-2 px-3 border-b border-gray-200 text-[10px]"
                ),
                {
                  backgroundColor:
                    opts.zebra && !isEven
                      ? "rgba(249, 250, 251, 0.6)"
                      : "transparent"
                }
              ]}
            >
              <Text style={tw("w-[6%] text-center text-gray-600")}>
                {isComment ? "" : lineNumber}
              </Text>
              <View style={tw("w-[44%] pr-2")}>
                <Text style={{ ...tw("text-gray-800"), ...overflow }}>
                  {getLineDescription(line)}
                </Text>
                <Text
                  style={{
                    ...tw("text-[9px] text-gray-600 mt-0.5"),
                    ...overflow
                  }}
                >
                  {getLineDescriptionDetails(line)}
                </Text>
                {opts.showThumbnails &&
                thumbnails &&
                line.id &&
                line.id in thumbnails &&
                thumbnails[line.id] ? (
                  <View style={tw("mt-1 w-16")}>
                    <Image
                      src={thumbnails[line.id]!}
                      style={tw("w-full h-auto")}
                    />
                  </View>
                ) : null}
                {line.invoiceLineType !== "Comment" && totalTaxAndFees > 0 ? (
                  <View style={tw("mt-1")}>
                    <Text style={tw("text-[9px] text-gray-600 font-bold")}>
                      Tax & Fees
                    </Text>
                    {lineShippingCost > 0 ? (
                      <View style={tw("flex flex-row justify-between")}>
                        <Text
                          style={tw("text-[9px] text-gray-600 flex-1 pr-2")}
                        >
                          - Shipping
                        </Text>
                        <Text style={tw("text-[9px] text-gray-600")}>
                          {numberFormatter.format(lineShippingCost)}
                        </Text>
                      </View>
                    ) : null}
                    {lineAddOnCost > 0 ? (
                      <View style={tw("flex flex-row justify-between")}>
                        <Text
                          style={tw("text-[9px] text-gray-600 flex-1 pr-2")}
                        >
                          - Add-On
                        </Text>
                        <Text style={tw("text-[9px] text-gray-600")}>
                          {numberFormatter.format(lineAddOnCost)}
                        </Text>
                      </View>
                    ) : null}
                    {lineNonTaxableAddOnCost > 0 ? (
                      <View style={tw("flex flex-row justify-between")}>
                        <Text
                          style={tw("text-[9px] text-gray-600 flex-1 pr-2")}
                        >
                          - Non-Taxable Add-On
                        </Text>
                        <Text style={tw("text-[9px] text-gray-600")}>
                          {numberFormatter.format(lineNonTaxableAddOnCost)}
                        </Text>
                      </View>
                    ) : null}
                    {lineTaxPercent > 0 ? (
                      <View style={tw("flex flex-row justify-between")}>
                        <Text
                          style={tw("text-[9px] text-gray-600 flex-1 pr-2")}
                        >
                          - Tax ({formatPercent(lineTaxPercent, locale)})
                        </Text>
                        <Text style={tw("text-[9px] text-gray-600")}>
                          {numberFormatter.format(lineTaxAmount)}
                        </Text>
                      </View>
                    ) : null}
                  </View>
                ) : null}
              </View>
              <Text style={tw("w-1/6 text-center text-gray-600")}>
                {line.invoiceLineType === "Comment"
                  ? ""
                  : `${line.quantity} ${line.unitOfMeasureCode ?? "EA"}`}
              </Text>
              {line.invoiceLineType !== "Comment" && lineDiscountPercent > 0 ? (
                <View style={tw("w-1/6 items-center")}>
                  <Text style={tw("text-gray-600")}>
                    {rateFormatter.format(line.convertedUnitPrice ?? 0)}
                  </Text>
                  <Text style={tw("text-[9px] text-gray-600 mt-0.5")}>
                    {formatPercent(-lineDiscountPercent, locale)}
                  </Text>
                </View>
              ) : (
                <Text style={tw("w-1/6 text-center text-gray-600")}>
                  {line.invoiceLineType === "Comment"
                    ? ""
                    : rateFormatter.format(line.convertedUnitPrice ?? 0)}
                </Text>
              )}
              <Text style={tw("w-1/6 text-center text-gray-800 font-medium")}>
                {line.invoiceLineType === "Comment"
                  ? ""
                  : numberFormatter.format(getLineTotal(line))}
              </Text>
            </View>
            {Object.keys(line.externalNotes ?? {}).length > 0 ? (
              // Indented under the description, past the line-number column.
              <View
                style={tw("flex flex-row px-3 py-2 border-b border-gray-200")}
              >
                <View style={tw("w-[6%]")} />
                <View style={tw("w-[94%]")}>
                  <Note
                    key={`${line.id}-notes`}
                    content={line.externalNotes as JSONContent}
                  />
                </View>
              </View>
            ) : null}
          </View>
        );
      })}
    </View>
  );
}