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.
TypeScript ai-agentsbill-of-materialserpinventory-managementleanmanufacturingmanufacturing-execution-systemmcp-servermesmrpopen-source-erppostgresqlproduction-planningqmsquality-management-systemreact-routerself-hostedsupabasetraceabilitytypescript
// 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 { getCarbonServiceRole } from "@carbon/auth/client.server";
import { validationError, validator } from "@carbon/form";
import { redirect } from "@carbon/utils";
import type { ActionFunctionArgs } from "react-router";
import { data } from "react-router";
import { getMethodValidator } from "~/modules/sales/sales.models";
import {
upsertMakeMethodFromQuoteLine,
upsertMakeMethodFromQuoteMethod
} from "~/modules/sales/sales.service";
import { getDatabaseClient } from "~/services/database.server";
import { path, requestReferrer } from "~/utils/path";
export async function action({ request }: ActionFunctionArgs) {
const { companyId, userId } = await requirePermissions(request, {
update: "sales"
});
const formData = await request.formData();
const type = formData.get("type") as string;
const serviceRole = getCarbonServiceRole();
if (type === "item") {
const validation = await validator(getMethodValidator).validate(formData);
if (validation.error) {
return validationError(validation.error);
}
const [quoteId, quoteLineId] = validation.data.sourceId.split(":");
const itemId = validation.data.targetId;
const lineMethod = await upsertMakeMethodFromQuoteLine(
serviceRole,
getDatabaseClient(),
{
quoteId,
quoteLineId,
itemId,
companyId,
userId,
parts: {
billOfMaterial: validation.data.billOfMaterial,
billOfProcess: validation.data.billOfProcess,
parameters: validation.data.parameters,
tools: validation.data.tools,
steps: validation.data.steps,
workInstructions: validation.data.workInstructions
}
}
);
return {
error: lineMethod.error
? "Failed to save quote method to make method"
: null
};
}
if (type === "method") {
const validation = await validator(getMethodValidator).validate(formData);
if (validation.error) {
return validationError(validation.error);
}
const makeMethod = await upsertMakeMethodFromQuoteMethod(
serviceRole,
getDatabaseClient(),
{
...validation.data,
companyId,
userId,
parts: {
billOfMaterial: validation.data.billOfMaterial,
billOfProcess: validation.data.billOfProcess,
parameters: validation.data.parameters,
tools: validation.data.tools,
steps: validation.data.steps,
workInstructions: validation.data.workInstructions
}
}
);
if (makeMethod.error) {
return {
error: makeMethod.error
? "Failed to save quote method to make method"
: null
};
}
throw redirect(requestReferrer(request) ?? path.to.quotes);
}
return data({ error: "Invalid type" }, { status: 400 });
}