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, success } 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 { validationError, validator } from "@carbon/form";
import type { ActionFunctionArgs } from "react-router";
import { data } from "react-router";
import { erpPosting } from "~/services/erp";
import { scrapQuantityValidator } from "~/services/models";
import { abilityBlockRefusal } from "~/services/operations.service";
export async function action({ request }: ActionFunctionArgs) {
assertIsPost(request);
const { companyId, userId } = await requirePermissions(request, {});
const formData = await request.formData();
const validation = await validator(scrapQuantityValidator).validate(formData);
if (validation.error) {
return validationError(validation.error);
}
const {
trackedEntityId,
trackingType,
jobOperationId,
quantity,
scrapReasonId,
notes,
setupProductionEventId,
laborProductionEventId,
machineProductionEventId
} = validation.data;
const serviceRole = await getCarbonServiceRole();
const refusal = await abilityBlockRefusal(serviceRole, {
operationId: jobOperationId,
employeeId: userId,
companyId
});
if (refusal) {
return data({}, await flash(request, error(null, refusal)));
}
// One transactional posting: Scrap productionQuantity row, BOM
// backflush, tracked-entity terminal status + replacement serial spawn
// (serial parents), Done-operation reopen / capacity top-up beyond the
// planned allowance, and the WIP→scrap journal.
const issued = await erpPosting(
"issue",
{ companyId, userId },
{
type: "jobOperationScrap",
jobOperationId,
quantity,
scrapReasonId,
notes,
setupProductionEventId,
laborProductionEventId,
machineProductionEventId,
trackedEntityId: trackingType === "Serial" ? trackedEntityId : undefined
}
);
if (issued.error) {
return data(
{},
await flash(
request,
error(
issued.error,
issued.error.message || "Failed to record scrap quantity"
)
)
);
}
// The client (useOperation / AssemblyView) advances to the spawned
// replacement serial the same way the complete flow does.
return data(
{ scrapped: true, newTrackedEntityId: issued.data?.newTrackedEntityId },
await flash(request, success("Scrap quantity recorded successfully"))
);
}