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.

7.5 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 { extensionTableFor } from "../item-tables.ts";
import { assertSingle, insertId, insertMaybe, maybeOne } from "../sql.ts";
import type {
  Ctx,
  ItemRef,
  ItemSpec,
  MethodType,
  OperationType
} from "../types.ts";
import { bootstrapIdByName } from "./bootstrap-lookup.ts";

/**
 * Inserts an item and its positional extension row, then activates the
 * auto-created make method for Part/Tool items. Never inserts itemCost or
 * itemReplenishment (the interceptor already did that — duplicates fan out views).
 */
export async function createItem(ctx: Ctx, spec: ItemSpec): Promise<ItemRef> {
  const { client, companyId, userId } = ctx;

  const revision = spec.revision ?? "0";
  const uom = spec.unitOfMeasureCode ?? "EA";
  const isMake = (spec.replenishment ?? "Buy") === "Make";
  const defaultMethodType =
    spec.defaultMethodType ?? (isMake ? "Make to Order" : "Purchase to Order");

  const existing = await maybeOne<{ id: string }>(
    client,
    `SELECT id FROM item WHERE "readableId" = $1 AND revision = $2 AND "companyId" = $3`,
    [spec.readableId, revision, companyId]
  );
  if (existing) {
    // Already there — collect make method if any
    const mm = await maybeOne<{ id: string }>(
      client,
      `SELECT id FROM "makeMethod" WHERE "itemId" = $1 ORDER BY version LIMIT 1`,
      [existing.id]
    );
    return {
      id: existing.id,
      readableId: spec.readableId,
      revision,
      name: spec.name,
      type: spec.type,
      makeMethodId: mm?.id ?? null,
      isMake,
      unitCost: spec.standardCost ?? 0,
      unitOfMeasureCode: uom
    };
  }

  const itemId = await insertId(ctx, "item", {
    readableId: spec.readableId,
    revision,
    name: spec.name,
    type: spec.type,
    replenishmentSystem: spec.replenishment ?? "Buy",
    defaultMethodType,
    itemTrackingType: spec.trackingType ?? "Inventory",
    unitOfMeasureCode: uom,
    description: spec.description ?? null,
    thumbnailPath: ctx.dataset.industryId
      ? `_templates/${ctx.dataset.industryId}/${spec.thumbnail ?? spec.readableId}.svg`
      : null,
    active: spec.active ?? true,
    // undefined is dropped by insertRow, keeping the column default.
    revisionStatus: spec.revisionStatus
  });

  // The item interceptor creates itemCost, itemReplenishment, itemUnitSalePrice,
  // itemPlanning (one per location). Guard against any double-insert bugs.
  await assertSingle(ctx, "itemCost", { itemId });

  // Positional extension row (part.id = readableId). All revisions share one row.
  const extensionTable = extensionTableFor(spec.type);
  const classification = spec.material;
  const lookup = (table: string, name: string | undefined) =>
    name ? bootstrapIdByName(ctx, table, name) : undefined;
  const taxonomy =
    spec.type === "Material" && classification
      ? {
          materialSubstanceId: await lookup(
            "materialSubstance",
            classification.substance
          ),
          materialFormId: await lookup("materialForm", classification.form),
          materialTypeId: await lookup(
            "materialType",
            classification.materialType
          ),
          gradeId: await lookup("materialGrade", classification.grade),
          finishId: await lookup("materialFinish", classification.finish),
          dimensionId: await lookup(
            "materialDimension",
            classification.dimension
          )
        }
      : {};
  await insertMaybe(ctx, extensionTable, {
    id: spec.readableId,
    approved: true,
    companyId,
    createdBy: userId,
    ...taxonomy
  });

  // Apply cost/price/lead-time by UPDATE (not insert).
  // unitCost too: FIFO valuation and calculateCOGS fall back to it for stock no
  // cost layer covers (opening stock, job output).
  if (spec.standardCost !== undefined) {
    await client.query(
      `UPDATE "itemCost" SET "standardCost" = $1, "unitCost" = $1
       WHERE "itemId" = $2 AND "companyId" = $3`,
      [spec.standardCost, itemId, companyId]
    );
  }
  if (spec.unitSalePrice !== undefined) {
    await client.query(
      `UPDATE "itemUnitSalePrice" SET "unitSalePrice" = $1 WHERE "itemId" = $2`,
      [spec.unitSalePrice, itemId]
    );
  }
  if (spec.leadTime !== undefined) {
    await client.query(
      `UPDATE "itemReplenishment" SET "leadTime" = $1 WHERE "itemId" = $2`,
      [spec.leadTime, itemId]
    );
  }

  // For Part / Tool / Service the interceptor created a Draft makeMethod; tier 02
  // releases the authored ones (Active) once their BOM and BOP are written.
  let makeMethodId: string | null = null;
  if (["Part", "Tool", "Service"].includes(spec.type)) {
    const mmRow = await maybeOne<{ id: string }>(
      client,
      `SELECT id FROM "makeMethod" WHERE "itemId" = $1 ORDER BY version LIMIT 1`,
      [itemId]
    );
    if (!mmRow)
      throw new Error(
        `Seed: no makeMethod created for item ${spec.readableId}`
      );
    makeMethodId = mmRow.id;
  }

  return {
    id: itemId,
    readableId: spec.readableId,
    revision,
    name: spec.name,
    type: spec.type,
    makeMethodId,
    isMake,
    unitCost: spec.standardCost ?? 0,
    unitOfMeasureCode: uom
  };
}

/**
 * A Make component is a subassembly: `methodType` must say so, and
 * `materialMakeMethodId` must point at the component's own active method, or the
 * BOM renders as one flat level and the Subassembly/Kit control never appears.
 */
export async function addBomLine(
  ctx: Ctx,
  makeMethodId: string,
  componentItem: ItemRef,
  quantity: number,
  order: number,
  opts: { methodType?: MethodType; kit?: boolean } = {}
): Promise<string> {
  const methodType =
    opts.methodType ??
    (componentItem.isMake ? "Make to Order" : "Pull from Inventory");
  const isSubassembly = methodType === "Make to Order";

  return insertId(ctx, "methodMaterial", {
    makeMethodId,
    itemId: componentItem.id,
    itemType: componentItem.type,
    unitOfMeasureCode: componentItem.unitOfMeasureCode,
    methodType,
    materialMakeMethodId: isSubassembly ? componentItem.makeMethodId : null,
    kit: opts.kit ?? false,
    quantity,
    order
  });
}

export async function addBopOperation(
  ctx: Ctx,
  makeMethodId: string,
  processId: string,
  workCenterId: string | undefined,
  description: string,
  order: number,
  opts: {
    laborTime?: number;
    laborUnit?: string;
    setupTime?: number;
    machineTime?: number;
    operationType?: OperationType;
    // Required by the UI for an Outside Processing step — without it the job
    // cannot be released and no supplier name renders on the operation.
    operationSupplierProcessId?: string;
    operationLeadTime?: number;
    operationUnitCost?: number;
    procedureId?: string;
    inspectionDocumentId?: string;
  } = {}
): Promise<string> {
  return insertId(ctx, "methodOperation", {
    makeMethodId,
    processId,
    workCenterId: workCenterId ?? null,
    description,
    order,
    laborTime: opts.laborTime ?? 0.5,
    laborUnit: opts.laborUnit ?? "Hours/Piece",
    setupTime: opts.setupTime ?? 0,
    machineTime: opts.machineTime ?? 0,
    operationType: opts.operationType ?? "Process",
    operationSupplierProcessId: opts.operationSupplierProcessId ?? null,
    operationLeadTime: opts.operationLeadTime ?? 0,
    operationUnitCost: opts.operationUnitCost ?? 0,
    procedureId: opts.procedureId ?? null,
    inspectionDocumentId: opts.inspectionDocumentId ?? null
  });
}