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.0 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 { requirePermissions } from "@carbon/auth/auth.server";
import type { Database } from "@carbon/database";
import type { LoaderFunctionArgs } from "react-router";
import { flattenTree } from "~/components/TreeView";
import type { BomItemAttributes } from "~/modules/items/items.service";
import { getBomItemAttributes } from "~/modules/items/items.service";
import { getQuoteMethodTrees } from "~/modules/sales/sales.service";
import type { BomOperation } from "~/utils/bom";
import {
  calculateMadePartCosts,
  calculateTotalQuantity,
  generateBomIds,
  stripCsvFormulaPrefix
} from "~/utils/bom";
import { makeDurations } from "~/utils/duration";

const bomHeaders = [
  "ID",
  "Item ID",
  "Revision",
  "Description",
  "Quantity",
  "Total",
  "Unit Cost",
  "Total Cost",
  "Method Type",
  "Item Type",
  "Tracking Type",
  "Replenishment",
  "Item Group",
  "Batch Size",
  "Lead Time",
  "Level",
  "Version"
];

const operationHeaders = [
  "Operation",
  "Process",
  "Work Center",
  "Operation Type",
  "Setup Time",
  "Setup Unit",
  "Labor Time",
  "Labor Unit",
  "Machine Time",
  "Machine Unit"
];

export async function loader({ request, params }: LoaderFunctionArgs) {
  const { client, companyId } = await requirePermissions(request, {
    view: "sales"
  });

  const { id } = params;
  const withOperations = request.url.includes("withOperations=true");

  if (!id) {
    return new Response(bomHeaders.join(",") + "\n", {
      headers: {
        "Content-Type": "text/csv",
        "Content-Disposition": "attachment; filename=bom.csv"
      }
    });
  }

  const quote = await client
    .from("quoteLines")
    .select("quoteId, quantity, itemReadableId")
    .eq("id", id)
    .single();
  const fileName = `${quote.data?.itemReadableId}-bom.csv`;
  if (quote.error) {
    return new Response(bomHeaders.join(",") + "\n", {
      headers: {
        "Content-Type": "text/csv",
        "Content-Disposition": `attachment; filename=${fileName}`
      }
    });
  }

  if (!quote.data?.quoteId) {
    throw new Error("Failed to fetch quote");
  }
  const methodTrees = await getQuoteMethodTrees(client, quote.data?.quoteId);

  if (methodTrees.error) {
    return new Response(bomHeaders.join(",") + "\n", {
      headers: {
        "Content-Type": "text/csv",
        "Content-Disposition": "attachment; filename=bom.csv"
      }
    });
  }

  const methodTree = methodTrees.data.find((m) => m.data.quoteLineId === id);
  const flattenedMethods = methodTree ? flattenTree(methodTree) : [];

  const makeMethodIds = [
    ...new Set(flattenedMethods.map((method) => method.data.quoteMakeMethodId))
  ];

  const itemIds = [
    ...new Set(flattenedMethods.map((method) => method.data.itemId))
  ];

  const [quoteOperations, itemAttributesResult] = await Promise.all([
    client
      .from("quoteOperation")
      .select(
        "*, ...process(processName:name), ...workCenter(workCenterName:name)"
      )
      .in("quoteMakeMethodId", makeMethodIds)
      .eq("companyId", companyId),
    getBomItemAttributes(client, companyId, itemIds)
  ]);

  const itemAttributes =
    itemAttributesResult.data ?? new Map<string, BomItemAttributes>();

  let operationsByMakeMethodId: Record<
    string,
    Array<
      Database["public"]["Tables"]["quoteOperation"]["Row"] & {
        processName: string;
        workCenterName: string | null;
      }
    >
  > = {};

  if (quoteOperations.data) {
    operationsByMakeMethodId = quoteOperations.data.reduce(
      (acc, operation) => {
        acc[operation.quoteMakeMethodId ?? ""] = [
          ...(acc[operation.quoteMakeMethodId ?? ""] || []),
          operation
        ];
        return acc;
      },
      {} as typeof operationsByMakeMethodId
    );
  }

  // Build BomOperation map for cost calculation
  const bomOperationsByKey: Record<string, BomOperation[]> = {};
  for (const [key, ops] of Object.entries(operationsByMakeMethodId)) {
    bomOperationsByKey[key] = ops.map((op) => ({
      operationType: op.operationType,
      setupTime: op.setupTime,
      setupUnit: op.setupUnit,
      laborTime: op.laborTime,
      laborUnit: op.laborUnit,
      machineTime: op.machineTime,
      machineUnit: op.machineUnit,
      operationUnitCost: op.operationUnitCost,
      operationMinimumCost: op.operationMinimumCost,
      laborRate: op.laborRate ?? 0,
      machineRate: op.machineRate ?? 0,
      overheadRate: op.overheadRate ?? 0
    }));
  }

  // Use the first quote quantity as the batch size for the root item
  const batchSizesByItemId = new Map<string, number>();
  const rootQuoteNode = flattenedMethods[0];
  const firstQuantity = quote.data?.quantity?.[0];
  if (rootQuoteNode && firstQuantity && firstQuantity > 1) {
    batchSizesByItemId.set(rootQuoteNode.data.itemId, firstQuantity);
  }

  const computedCosts = calculateMadePartCosts(
    flattenedMethods,
    bomOperationsByKey,
    (node) => node.data.quoteMaterialMakeMethodId,
    batchSizesByItemId
  );

  const bomIds = generateBomIds(flattenedMethods);

  // Build headers including duration columns for each quantity
  let headers = bomHeaders.join(",");
  if (withOperations) {
    headers += "," + operationHeaders.join(",");
    // Add duration column for each quantity
    if (quote.data?.quantity) {
      quote.data.quantity.forEach((qty) => {
        headers += `,Total Duration x ${qty} (ms)`;
      });
    }
  }
  headers += "\n";

  let csv = headers;

  flattenedMethods.forEach((node, index) => {
    const total = calculateTotalQuantity(node, flattenedMethods);
    const unitCost = computedCosts.get(node.id) ?? node.data.unitCost ?? 0;
    const totalCost = total * unitCost;

    const attrs = itemAttributes.get(node.data.itemId);
    const revision = attrs?.revision || "0";
    const itemGroup = stripCsvFormulaPrefix(
      attrs?.itemPostingGroup ?? ""
    ).replace(/"/g, '""');

    csv += `${bomIds[index]},${
      attrs?.readableId ?? node.data.itemReadableId
    },${revision},"${node.data.description?.replace(/"/g, '""')}",${
      node.data.quantity
    },${total},${unitCost},${totalCost},${node.data.methodType},${
      node.data.itemType
    },${attrs?.itemTrackingType ?? ""},${
      attrs?.replenishmentSystem ?? ""
    },"${itemGroup}",${attrs?.lotSize ?? ""},${
      attrs?.leadTime ?? ""
    },${node.level},${node.data.version || ""}\n`;

    if (withOperations) {
      const operations =
        operationsByMakeMethodId[node.data.quoteMaterialMakeMethodId];
      if (operations) {
        operations.forEach((operation) => {
          const durations: Record<string, number> = (quote.data?.quantity ?? [])
            .map((quantity) => {
              const duration = makeDurations({
                ...operation,
                operationQuantity: quantity
              });
              return [quantity, duration.duration];
            })
            .reduce(
              (acc, [quantity, duration]) => {
                acc[`totalDuration${quantity}`] = duration;
                return acc;
              },
              {} as Record<string, number>
            );

          csv += Array(bomHeaders.length).fill(",").join("");
          csv += `${operation.description},${operation.processName},${
            operation.workCenterName ?? ""
          },${operation.operationType},${operation.setupTime},${
            operation.setupUnit
          },${operation.laborTime},${operation.laborUnit},${
            operation.machineTime
          },${operation.machineUnit}`;

          // Add duration values for each quantity
          if (quote.data?.quantity) {
            quote.data.quantity.forEach((qty) => {
              csv += `,${durations[`totalDuration${qty}`] || 0}`;
            });
          }
          csv += "\n";
        });
      }
    }
  });

  return new Response(csv, {
    headers: {
      "Content-Type": "text/csv",
      "Content-Disposition": `attachment; filename=${fileName}`
    }
  });
}