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.

6.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 { formatDate } from "@carbon/utils";
import { Image, Text, View } from "@react-pdf/renderer";
import { DEFAULT_THEME } from "../template";
import type { Company } from "../types";
import { tw } from "./blocks/jobTraveler/tw";
import { Header, Template } from "./components";
import { breakLongNotesWord } from "./notesText";

// Shared column widths so the header and body rows can never drift out of
// alignment (the MaterialsBlock convention: widths sum to 12/12; spacing via
// in-cell right padding, never a flex gap).
const COL_THUMBNAIL = "w-1/12 pr-2";
const COL_JOB = "w-2/12 text-left pr-4";
const COL_ITEM = "w-2/12 text-left pr-4";
const COL_DESCRIPTION = "w-5/12 text-left pr-4";
const COL_QUANTITY = "w-2/12 text-right";

// The traveler's Job Notes look (components/Note.tsx): a ruled uppercase
// title over 9pt body text, in the default document theme's colors.
const NOTES_TITLE = `flex flex-row items-center mt-[20px] mb-[10px] py-[6px] px-[3px] border-t border-b border-[#CCCCCC] text-[9px] font-bold uppercase text-[${DEFAULT_THEME.heading}]`;
const NOTES_BODY = `text-[9px] text-[${DEFAULT_THEME.text}]`;

export interface BatchListMember {
  id: string;
  jobReadableId: string | null;
  itemReadableId: string | null;
  itemDescription: string | null;
  operationDescription: string | null;
  quantity: number | null;
  // The make-method item's thumbnail (base64 data URL), NOT the job's parent
  // part — resolved by the route. Null when the item has no thumbnail.
  thumbnail: string | null;
}

interface BatchListPDFProps {
  company: Company;
  batch: {
    readableId: string | null;
    status: string | null;
    createdAt: string | null;
  };
  processName: string | null;
  workCenterName: string | null;
  members: BatchListMember[];
  // The planner's plain-text notes from the batch builder.
  notes?: string | null;
  locale?: string;
}

/**
 * The batch list an operator works a shared run from: which jobs' parts
 * are in this furnace load / laser nest / plating rack, and how many of each.
 * Hand-built (no template engine) — a fixed operational document, not a
 * customer-facing one.
 */
export const BatchListPDF = ({
  company,
  batch,
  processName,
  workCenterName,
  members,
  notes,
  locale
}: BatchListPDFProps) => {
  const totalQuantity = members.reduce((sum, m) => sum + (m.quantity ?? 0), 0);

  const tableHeader = (
    <View
      style={tw(
        "flex flex-row justify-between items-center py-3 px-[6px] border-t border-b border-gray-300 font-bold uppercase"
      )}
    >
      <Text style={tw(COL_JOB)}>Job</Text>
      <Text style={tw(COL_THUMBNAIL)} />
      <Text style={tw(COL_ITEM)}>Item</Text>
      <Text style={tw(COL_DESCRIPTION)}>Description</Text>
      <Text style={tw(COL_QUANTITY)}>Quantity</Text>
    </View>
  );

  return (
    <Template
      title="Batch List"
      meta={{
        author: "Carbon",
        keywords: "batch list",
        subject: "Batch List"
      }}
      footerDocumentId={batch.readableId}
    >
      <Header
        company={company}
        title="Batch List"
        documentId={batch.readableId}
      />

      <View style={tw("flex flex-row gap-8 mb-4 text-xs")}>
        {processName ? (
          <View style={tw("flex flex-col")}>
            <Text style={tw("font-bold text-gray-500 uppercase text-[8px]")}>
              Process
            </Text>
            <Text>{processName}</Text>
          </View>
        ) : null}
        {workCenterName ? (
          <View style={tw("flex flex-col")}>
            <Text style={tw("font-bold text-gray-500 uppercase text-[8px]")}>
              Work Center
            </Text>
            <Text>{workCenterName}</Text>
          </View>
        ) : null}
        {batch.createdAt ? (
          <View style={tw("flex flex-col")}>
            <Text style={tw("font-bold text-gray-500 uppercase text-[8px]")}>
              Created
            </Text>
            <Text>
              {formatDate(batch.createdAt.slice(0, 10), undefined, locale)}
            </Text>
          </View>
        ) : null}
        <View style={tw("flex flex-col")}>
          <Text style={tw("font-bold text-gray-500 uppercase text-[8px]")}>
            Jobs
          </Text>
          <Text>{members.length}</Text>
        </View>
      </View>

      <View style={tw("mb-6 text-xs")}>
        {members.map((member, index) => {
          const row = (
            <View
              style={tw(
                "flex flex-row justify-between items-start border-b border-gray-300 py-3 px-[6px]"
              )}
              wrap={false}
            >
              <Text style={tw(`${COL_JOB} font-bold`)}>
                {member.jobReadableId ?? ""}
              </Text>
              <View style={tw(COL_THUMBNAIL)}>
                {member.thumbnail ? (
                  <Image
                    src={member.thumbnail}
                    style={tw("w-full h-auto border rounded border-gray-300")}
                  />
                ) : null}
              </View>
              <Text style={tw(COL_ITEM)}>{member.itemReadableId ?? ""}</Text>
              <Text style={tw(COL_DESCRIPTION)}>
                {member.itemDescription ?? member.operationDescription ?? ""}
              </Text>
              <Text style={tw(COL_QUANTITY)}>{member.quantity ?? 0}</Text>
            </View>
          );

          // Bind the table header to the first row so it can never be orphaned
          // at the bottom of a page (the MaterialsBlock trick).
          if (index === 0) {
            return (
              <View key={member.id} wrap={false} minPresenceAhead={80}>
                {tableHeader}
                {row}
              </View>
            );
          }

          return <View key={member.id}>{row}</View>;
        })}
        <View
          style={tw(
            "flex flex-row justify-between items-center py-3 px-[6px] font-bold"
          )}
          wrap={false}
        >
          <Text style={tw(COL_JOB)} />
          <Text style={tw(COL_THUMBNAIL)} />
          <Text style={tw(COL_ITEM)} />
          <Text style={tw(COL_DESCRIPTION)}>Total</Text>
          <Text style={tw(COL_QUANTITY)}>{totalQuantity}</Text>
        </View>
      </View>

      {/* Placed like the traveler's Job Notes, after the table. Plain text,
          so its line breaks print as typed. */}
      {notes?.trim() ? (
        <View style={tw("mb-6")}>
          <View style={tw(NOTES_TITLE)} minPresenceAhead={30}>
            <Text>Batch Notes</Text>
          </View>
          <Text style={tw(NOTES_BODY)} hyphenationCallback={breakLongNotesWord}>
            {notes}
          </Text>
        </View>
      ) : null}
    </Template>
  );
};