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 { assertIsPost, error } from "@carbon/auth";
import { requirePermissions } from "@carbon/auth/auth.server";
import { getCarbonServiceRole } from "@carbon/auth/client.server";
import { flash } from "@carbon/auth/session.server";
import { evaluateLinesForSurface, isBlocked } from "@carbon/ee/rules.server";
import { validationError, validator } from "@carbon/form";
import { getLogger } from "@carbon/logger";
import { redirect } from "@carbon/utils";
import type { ActionFunctionArgs } from "react-router";
import { erpPosting } from "~/services/erp";
import { issueValidator } from "~/services/models";
import { abilityBlockRefusal } from "~/services/operations.service";
import { path, requestReferrer } from "~/utils/path";
const logger = getLogger("mes", "issue");
export async function action({ request }: ActionFunctionArgs) {
assertIsPost(request);
const { userId, companyId } = await requirePermissions(request, {});
const formData = await request.formData();
const validation = await validator(issueValidator).validate(formData);
if (validation.error) {
return validationError(validation.error);
}
const {
jobOperationId,
materialId,
jobOperationStepId,
itemId,
quantity,
adjustmentType
} = validation.data;
const serviceRole = await getCarbonServiceRole();
const acknowledged = formData.get("acknowledged") === "true";
// Resolve workCenter context off the operation so workCenter-scoped rules
// can evaluate against this materialIssue.
// `workInstructionId` is in the runtime row but absent from the generated
// DB types (stale until next regen). Select * and let the manual cast
// below resolve the field; type only `workCenterId` directly off the row.
const { data: jobOpRow } = await serviceRole
.from("jobOperation")
.select("workCenterId")
.eq("id", jobOperationId)
.eq("companyId", companyId)
.maybeSingle();
if (!jobOpRow) {
logger.warn("Job operation not found for company", {
companyId,
jobOperationId
});
throw redirect(
requestReferrer(request) ?? path.to.operations,
await flash(request, error(null, "Job operation not found"))
);
}
const refusal = await abilityBlockRefusal(serviceRole, {
operationId: jobOperationId,
employeeId: userId,
companyId
});
if (refusal) {
throw redirect(
requestReferrer(request) ?? path.to.operations,
await flash(request, error(null, refusal))
);
}
if (jobOpRow.workCenterId) {
const ruleEval = await evaluateLinesForSurface({
client: serviceRole,
companyId,
userId,
targetType: "workCenter",
surface: "materialIssue",
lines: [
{
lineId: jobOperationId,
itemId,
workCenterId: jobOpRow.workCenterId,
operation: {
id: jobOperationId,
itemId,
quantity,
workInstructionId:
(jobOpRow as { workInstructionId?: string | null })
.workInstructionId ?? null
},
quantity
}
]
});
if (
ruleEval.violations.length > 0 &&
isBlocked(ruleEval.violations, acknowledged)
) {
return {
error: null,
data: null,
violations: ruleEval.violations,
ruleNames: ruleEval.ruleNames
};
}
}
const issued = await erpPosting(
"issue",
{ companyId, userId },
{
id: jobOperationId,
type: "partToOperation",
itemId,
materialId,
jobOperationStepId,
quantity,
adjustmentType
}
);
if (issued.error) {
throw redirect(
requestReferrer(request) ?? path.to.operations,
await flash(
request,
error(issued.error, issued.error.message || "Failed to issue material")
)
);
}
throw redirect(requestReferrer(request) ?? path.to.operations);
}