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, notFound } from "@carbon/auth";
import { requirePermissions } from "@carbon/auth/auth.server";
import { flash } from "@carbon/auth/session.server";
import { validationError, validator } from "@carbon/form";
import { getLogger } from "@carbon/logger";
import type { JSONContent } from "@carbon/react";
import { RecordOutlet } from "@carbon/react";
import { redirect } from "@carbon/utils";
import { Fragment } from "react/jsx-runtime";
import type { ActionFunctionArgs, LoaderFunctionArgs } from "react-router";
import { useLoaderData, useParams } from "react-router";
import { CadModel, DeferredFiles } from "~/components";
import { usePermissions, useRouteData } from "~/hooks";
import {
isPurchaseOrderLocked,
purchaseOrderLineValidator
} from "~/modules/purchasing/purchasing.models";
import {
getPurchaseOrder,
getPurchaseOrderLine,
getSupplierInteractionLineDocuments,
upsertPurchaseOrderLine
} from "~/modules/purchasing/purchasing.service";
import { PurchaseOrderLineForm } from "~/modules/purchasing/ui/PurchaseOrder";
import {
SupplierInteractionLineDocuments,
SupplierInteractionLineNotes
} from "~/modules/purchasing/ui/SupplierInteraction";
import { getCustomFields, setCustomFields } from "~/utils/form";
import { requireUnlocked } from "~/utils/lockedGuard.server";
import { path } from "~/utils/path";
const logger = getLogger("erp", "purchase-order-line-details");
export async function loader({ request, params }: LoaderFunctionArgs) {
const { client, companyId } = await requirePermissions(request, {
view: "purchasing",
role: "employee",
bypassRls: true
});
const { orderId, lineId } = params;
if (!orderId) throw notFound("orderId not found");
if (!lineId) throw notFound("lineId not found");
const line = await getPurchaseOrderLine(client, lineId);
if (line.error) {
throw redirect(
path.to.purchaseOrderDetails(orderId),
await flash(request, error(line.error, "Failed to load sales order line"))
);
}
// bypassRls hands back the service role and lineId comes from the URL: the
// line must belong to this company and to the order in the URL.
if (
line.data.companyId !== companyId ||
line.data.purchaseOrderId !== orderId
) {
logger.error("Purchase order line not found for company", {
companyId,
orderId,
lineId
});
throw notFound("Purchase order line not found");
}
return {
line: line?.data ?? null,
files: getSupplierInteractionLineDocuments(client, companyId, lineId)
};
}
export async function action({ request, params }: ActionFunctionArgs) {
assertIsPost(request);
const { orderId, lineId } = params;
if (!orderId) throw new Error("Could not find orderId");
if (!lineId) throw new Error("Could not find lineId");
// First check with view permission to get the PO status
const { client: viewClient } = await requirePermissions(request, {
view: "purchasing"
});
// Get PO status and current line data
const [purchaseOrder, currentLine] = await Promise.all([
getPurchaseOrder(viewClient, orderId),
getPurchaseOrderLine(viewClient, lineId)
]);
if (purchaseOrder.error) {
throw redirect(
path.to.purchaseOrderLine(orderId, lineId),
await flash(
request,
error(purchaseOrder.error, "Failed to load purchase order")
)
);
}
if (currentLine.error || !currentLine.data) {
throw redirect(
path.to.purchaseOrderLine(orderId, lineId),
await flash(
request,
error(currentLine.error, "Failed to load purchase order line")
)
);
}
await requireUnlocked({
request,
isLocked: isPurchaseOrderLocked(purchaseOrder.data?.status),
redirectTo: path.to.purchaseOrderLine(orderId, lineId),
message: "Cannot modify a confirmed purchase order."
});
const { client, companyId, userId } = await requirePermissions(request, {
update: "purchasing"
});
const formData = await request.formData();
const validation = await validator(purchaseOrderLineValidator).validate(
formData
);
if (validation.error) {
return validationError(validation.error);
}
// Dates save on their own (the Dates section); sent empty here, the
// update would clear them.
const {
id: _id,
requiredDate: _requiredDate,
promisedDate: _promisedDate,
...d
} = validation.data;
const updatePurchaseOrderLine = await upsertPurchaseOrderLine(client, {
id: lineId,
...d,
companyId,
updatedBy: userId,
customFields: setCustomFields(formData)
});
if (updatePurchaseOrderLine.error) {
throw redirect(
path.to.purchaseOrderLine(orderId, lineId),
await flash(
request,
error(
updatePurchaseOrderLine.error,
"Failed to update purchase order line"
)
)
);
}
throw redirect(path.to.purchaseOrderLine(orderId, lineId));
}
export default function EditPurchaseOrderLineRoute() {
const { orderId, lineId } = useParams();
if (!orderId) throw new Error("orderId not found");
if (!lineId) throw new Error("lineId not found");
const permissions = usePermissions();
const routeData = useRouteData<{
purchaseOrder: { status: string };
}>(path.to.purchaseOrder(orderId));
const isReadOnly = isPurchaseOrderLocked(routeData?.purchaseOrder?.status);
const { line, files } = useLoaderData<typeof loader>();
const initialValues = {
id: line?.id ?? undefined,
purchaseOrderId: line?.purchaseOrderId ?? "",
purchaseOrderLineType: (line?.purchaseOrderLineType ?? "Part") as "Part",
itemId: line?.itemId ?? "",
accountId: line?.accountId ?? "",
assetId: line?.assetId ?? "",
conversionFactor: line?.conversionFactor ?? 1,
description: line?.description ?? "",
exchangeRate: line?.exchangeRate ?? 1,
inventoryUnitOfMeasureCode: line?.inventoryUnitOfMeasureCode ?? "",
jobId: line?.jobId ?? "",
jobOperationId: line?.jobOperationId ?? "",
locationId: line?.locationId ?? "",
purchaseQuantity: line?.purchaseQuantity ?? 1,
purchaseUnitOfMeasureCode: line?.purchaseUnitOfMeasureCode ?? "",
storageUnitId: line?.storageUnitId ?? "",
supplierPartId: line?.supplierPartId ?? "",
supplierShippingCost: line?.supplierShippingCost ?? 0,
supplierTaxAmount: line?.supplierTaxAmount ?? 0,
supplierUnitPrice: line?.supplierUnitPrice ?? 0,
costCenterId: line?.costCenterId ?? "",
taxPercent: line?.taxPercent ?? 0,
assetReadableId: (line as any)?.assetReadableId ?? "",
assetName: (line as any)?.assetName ?? "",
...getCustomFields(line?.customFields)
};
return (
<Fragment key={lineId}>
<PurchaseOrderLineForm
key={initialValues.id}
initialValues={initialValues}
lineDates={{
receivedComplete: line.receivedComplete ?? false,
requiredDate: line.requiredDate,
promisedDate: line.promisedDate,
receivedDate: line.receivedDate
}}
/>
<SupplierInteractionLineNotes
id={line?.id ?? ""}
table="purchaseOrderLine"
title="Notes"
subTitle={
line.purchaseOrderLineType === "Fixed Asset"
? (line.assetName ?? line.description ?? "")
: line.purchaseOrderLineType === "G/L Account"
? (line.description ?? "")
: (line.itemReadableId ?? "")
}
internalNotes={line.internalNotes as JSONContent}
externalNotes={line.externalNotes as JSONContent}
/>
<DeferredFiles resolve={files}>
{(resolvedFiles) => (
<SupplierInteractionLineDocuments
files={resolvedFiles ?? []}
id={orderId}
lineId={lineId}
type="Purchase Order"
/>
)}
</DeferredFiles>
<CadModel
isReadOnly={isReadOnly || !permissions.can("update", "purchasing")}
metadata={{
itemId: line?.itemId ?? undefined
}}
modelUpload={line ?? null}
title="CAD Model"
uploadClassName="aspect-square min-h-[420px] max-h-[70vh]"
viewerClassName="aspect-square min-h-[420px] max-h-[70vh]"
/>
<RecordOutlet />
</Fragment>
);
}