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.

14.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 { useCallback, useMemo } from "react";
import { useParams } from "react-router";
import type { Tree } from "~/components/TreeView";
import type { SupplierPriceMap } from "~/modules/shared/shared.models";
import { resolveBuyUnitCost } from "~/modules/shared/shared.utils";
import type {
  CostEffects,
  Costs,
  QuotationLine,
  QuotationOperation,
  QuoteMethod
} from "../../types";

const defaultEffects: CostEffects = {
  consumableCost: [],
  laborCost: [],
  laborHours: [],
  machineCost: [],
  machineHours: [],
  materialCost: [],
  outsideCost: [],
  overheadCost: [],
  partCost: [],
  serviceCost: [],
  setupHours: [],
  toolCost: []
};

type EnhancedTree = Tree<QuoteMethod & { operations?: QuotationOperation[] }>;

export function useLineCosts({
  methodTree: originalMethodTree,
  operations,
  line,
  supplierPriceMap = {}
}: {
  methodTree?: Tree<QuoteMethod>;
  operations: QuotationOperation[];
  line: QuotationLine;
  supplierPriceMap?: SupplierPriceMap;
}): (quantity: number) => Costs {
  const { quoteId, lineId } = useParams();
  if (!quoteId) throw new Error("Could not find quoteId");
  if (!lineId) throw new Error("Could not find lineId");

  // TODO: instead of walking the tree twice (once for the quantities/operations and once for the effects)
  // we could do it all in one pass

  const methodTree = useMemo<EnhancedTree | undefined>(() => {
    if (!originalMethodTree || !originalMethodTree.id) {
      return undefined;
    }

    const tree = structuredClone(originalMethodTree);

    function traverseTree(tree: EnhancedTree, parentQuantity: number) {
      // multiply quantity by parent quantity
      tree.data.quantity = tree.data.quantity * parentQuantity;
      tree.data.operations = operations.filter(
        (o) => o.quoteMakeMethodId === tree.data.quoteMaterialMakeMethodId
      );

      if (tree.children) {
        for (const child of tree.children) {
          traverseTree(child, tree.data.quantity);
        }
      }
    }

    traverseTree(tree, 1);

    return tree;
  }, [operations, originalMethodTree]);

  const costEffects = useMemo<CostEffects>(() => {
    const effects = structuredClone(defaultEffects);

    function pushBuyCostEffect(
      itemId: string,
      itemType: string,
      quantity: number,
      unitCost: number,
      unitCostSource: string | null,
      supplierPriceMap: SupplierPriceMap
    ) {
      const costFn = (outerQty: number) => {
        const requestedQty = quantity * outerQty;
        const resolved = resolveBuyUnitCost(
          { itemId, unitCost, unitCostSource },
          requestedQty,
          supplierPriceMap
        );
        return resolved * requestedQty;
      };
      switch (itemType) {
        case "Material":
          effects.materialCost.push(costFn);
          break;
        case "Part":
          effects.partCost.push(costFn);
          break;
        case "Tool":
          effects.toolCost.push(costFn);
          break;
        case "Consumable":
          effects.consumableCost.push(costFn);
          break;
        case "Service":
          effects.serviceCost.push(costFn);
          break;
        default:
          break;
      }
    }

    function walkTree(tree: EnhancedTree) {
      const { data } = tree;

      if (data.methodType === "Purchase to Order") {
        pushBuyCostEffect(
          data.itemId,
          data.itemType,
          data.quantity,
          data.unitCost,
          data.unitCostSource,
          supplierPriceMap
        );
      } else if (data.methodType === "Pull from Inventory") {
        // Pick items use static average cost
        const costFn = (quantity: number) =>
          data.unitCost * data.quantity * quantity;
        switch (data.itemType) {
          case "Material":
            effects.materialCost.push(costFn);
            break;
          case "Part":
            effects.partCost.push(costFn);
            break;
          case "Tool":
            effects.toolCost.push(costFn);
            break;
          case "Consumable":
            effects.consumableCost.push(costFn);
            break;
          case "Service":
            effects.serviceCost.push(costFn);
            break;
          default:
            break;
        }
      }

      data.operations?.forEach((operation: QuotationOperation) => {
        if (operation.operationType !== "Outside Processing") {
          if (operation.setupTime) {
            // normalize production standard to hours
            let hoursPerUnit = 0;
            let fixedHours = 0;
            switch (operation.setupUnit) {
              case "Total Hours":
                fixedHours = operation.setupTime;
                break;
              case "Total Minutes":
                fixedHours = operation.setupTime / 60;
                break;
              case "Hours/Piece":
                hoursPerUnit = operation.setupTime;
                break;
              case "Hours/100 Pieces":
                hoursPerUnit = operation.setupTime / 100;
                break;
              case "Hours/1000 Pieces":
                hoursPerUnit = operation.setupTime / 1000;
                break;

              case "Minutes/Piece":
                hoursPerUnit = operation.setupTime / 60;
                break;
              case "Minutes/100 Pieces":
                hoursPerUnit = operation.setupTime / 100 / 60;
                break;
              case "Minutes/1000 Pieces":
                hoursPerUnit = operation.setupTime / 1000 / 60;
                break;
              case "Pieces/Hour":
                hoursPerUnit = 1 / operation.setupTime;
                break;
              case "Pieces/Minute":
                hoursPerUnit = 1 / (operation.setupTime / 60);
                break;
              case "Seconds/Piece":
                hoursPerUnit = operation.setupTime / 3600;
                break;
              default:
                break;
            }

            effects.setupHours.push((quantity) => {
              return hoursPerUnit * quantity * data.quantity + fixedHours;
            });

            effects.laborCost.push((quantity) => {
              return (
                hoursPerUnit *
                  quantity *
                  data.quantity *
                  (operation.laborRate ?? 0) +
                fixedHours * (operation.laborRate ?? 0)
              );
            });

            effects.overheadCost.push((quantity) => {
              return (
                hoursPerUnit *
                  quantity *
                  data.quantity *
                  (operation.overheadRate ?? 0) +
                fixedHours * (operation.overheadRate ?? 0)
              );
            });
          }

          let laborFixedHours = 0;
          let laborHoursPerUnit = 0;
          let machineFixedHours = 0;
          let machineHoursPerUnit = 0;

          if (operation.laborTime) {
            // normalize production standard to hours

            switch (operation.laborUnit) {
              case "Total Hours":
                laborFixedHours = operation.laborTime;
                break;
              case "Total Minutes":
                laborFixedHours = operation.laborTime / 60;
                break;
              case "Hours/Piece":
                laborHoursPerUnit = operation.laborTime;
                break;
              case "Hours/100 Pieces":
                laborHoursPerUnit = operation.laborTime / 100;
                break;
              case "Hours/1000 Pieces":
                laborHoursPerUnit = operation.laborTime / 1000;
                break;

              case "Minutes/Piece":
                laborHoursPerUnit = operation.laborTime / 60;
                break;
              case "Minutes/100 Pieces":
                laborHoursPerUnit = operation.laborTime / 100 / 60;
                break;
              case "Minutes/1000 Pieces":
                laborHoursPerUnit = operation.laborTime / 1000 / 60;
                break;
              case "Pieces/Hour":
                laborHoursPerUnit = 1 / operation.laborTime;
                break;
              case "Pieces/Minute":
                laborHoursPerUnit = 1 / (operation.laborTime / 60);
                break;
              case "Seconds/Piece":
                laborHoursPerUnit = operation.laborTime / 3600;
                break;
              default:
                break;
            }

            effects.laborHours.push((quantity) => {
              return (
                laborHoursPerUnit * quantity * data.quantity + laborFixedHours
              );
            });

            effects.laborCost.push((quantity) => {
              return (
                laborHoursPerUnit *
                  quantity *
                  data.quantity *
                  (operation.laborRate ?? 0) +
                laborFixedHours * (operation.laborRate ?? 0)
              );
            });
          }

          if (operation.machineTime) {
            // normalize production standard to hours

            switch (operation.machineUnit) {
              case "Total Hours":
                machineFixedHours = operation.machineTime;
                break;
              case "Total Minutes":
                machineFixedHours = operation.machineTime / 60;
                break;
              case "Hours/Piece":
                machineHoursPerUnit = operation.machineTime;
                break;
              case "Hours/100 Pieces":
                machineHoursPerUnit = operation.machineTime / 100;
                break;
              case "Hours/1000 Pieces":
                machineHoursPerUnit = operation.machineTime / 1000;
                break;

              case "Minutes/Piece":
                machineHoursPerUnit = operation.machineTime / 60;
                break;
              case "Minutes/100 Pieces":
                machineHoursPerUnit = operation.machineTime / 100 / 60;
                break;
              case "Minutes/1000 Pieces":
                machineHoursPerUnit = operation.machineTime / 1000 / 60;
                break;
              case "Pieces/Hour":
                machineHoursPerUnit = 1 / operation.machineTime;
                break;
              case "Pieces/Minute":
                machineHoursPerUnit = 1 / (operation.machineTime / 60);
                break;
              case "Seconds/Piece":
                machineHoursPerUnit = operation.machineTime / 3600;
                break;
              default:
                break;
            }

            effects.machineHours.push((quantity) => {
              return (
                machineHoursPerUnit * quantity * data.quantity +
                machineFixedHours
              );
            });

            effects.machineCost.push((quantity) => {
              return (
                machineHoursPerUnit *
                  quantity *
                  data.quantity *
                  (operation.machineRate ?? 0) +
                machineFixedHours * (operation.machineRate ?? 0)
              );
            });
          }

          const hoursPerUnit = Math.max(laborHoursPerUnit, machineHoursPerUnit);
          const fixedHours = Math.max(laborFixedHours, machineFixedHours);

          effects.overheadCost.push((quantity) => {
            if (hoursPerUnit * quantity * data.quantity > fixedHours) {
              return (
                hoursPerUnit *
                quantity *
                data.quantity *
                (operation.overheadRate ?? 0)
              );
            } else {
              return fixedHours * (operation.overheadRate ?? 0);
            }
          });
        } else {
          effects.outsideCost.push((quantity) => {
            const unitCost =
              operation.operationUnitCost * data.quantity * quantity;
            return Math.max(operation.operationMinimumCost, unitCost);
          });
        }
      });

      if (tree.children) {
        for (const child of tree.children) {
          walkTree(child);
        }
      }
    }

    if (methodTree && line.methodType === "Make to Order") {
      walkTree(methodTree);
    } else if (line.methodType === "Purchase to Order") {
      pushBuyCostEffect(
        line.itemId ?? "",
        "Material",
        1,
        line.unitCost ?? 0,
        // The quote LINE's own cost, not a BOM material's. Only quoteMaterial
        // carries a typed/calculated flag, so this always resolves from the
        // supplier's price breaks, exactly as it did before.
        null,
        supplierPriceMap
      );
    } else {
      effects.materialCost.push((quantity) => (line.unitCost ?? 0) * quantity);
    }

    return effects;
  }, [
    methodTree,
    line.methodType,
    line.unitCost,
    line.itemId,
    supplierPriceMap
  ]);

  const getCosts = useCallback(
    (quantity: number) => {
      const materialCost = costEffects.materialCost.reduce(
        (acc, effect) => acc + effect(quantity),
        0
      );

      const partCost = costEffects.partCost.reduce(
        (acc, effect) => acc + effect(quantity),
        0
      );

      const toolCost = costEffects.toolCost.reduce(
        (acc, effect) => acc + effect(quantity),
        0
      );

      const consumableCost = costEffects.consumableCost.reduce(
        (acc, effect) => acc + effect(quantity),
        0
      );

      const serviceCost = costEffects.serviceCost.reduce(
        (acc, effect) => acc + effect(quantity),
        0
      );

      const laborCost = costEffects.laborCost.reduce(
        (acc, effect) => acc + effect(quantity),
        0
      );

      const overheadCost = costEffects.overheadCost.reduce(
        (acc, effect) => acc + effect(quantity),
        0
      );

      const outsideCost = costEffects.outsideCost.reduce(
        (acc, effect) => acc + effect(quantity),
        0
      );

      const setupHours = costEffects.setupHours.reduce(
        (acc, effect) => acc + effect(quantity),
        0
      );

      const laborHours = costEffects.laborHours.reduce(
        (acc, effect) => acc + effect(quantity),
        0
      );

      const machineCost = costEffects.machineCost.reduce(
        (acc, effect) => acc + effect(quantity),
        0
      );

      const machineHours = costEffects.machineHours.reduce(
        (acc, effect) => acc + effect(quantity),
        0
      );

      return {
        consumableCost,
        laborCost,
        laborHours,
        machineCost,
        machineHours,
        materialCost,
        outsideCost,
        overheadCost,
        partCost,
        serviceCost,
        setupHours,
        toolCost
      };
    },
    [costEffects]
  );

  return getCosts;
}