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.

feat: require work for released job and auto-select work center if there's only one

Brad Barbin1a0948511a67
diff --git a/apps/erp/app/components/Form/WorkCenter.tsx b/apps/erp/app/components/Form/WorkCenter.tsx
index 5056b38f1f..263e65cc38 100644
--- a/apps/erp/app/components/Form/WorkCenter.tsx
+++ b/apps/erp/app/components/Form/WorkCenter.tsx
@@ -9,6 +9,7 @@ import WorkCenterForm from "~/modules/resources/ui/WorkCenters/WorkCenterForm";
import { path } from "~/utils/path";
type WorkCenterSelectProps = Omit<ComboboxProps, "options"> & {
+ autoSelectSingleOption?: boolean;
processId?: string;
locationId?: string;
isConfigured?: boolean;
@@ -21,7 +22,7 @@ const WorkCenter = (props: WorkCenterSelectProps) => {
const [created, setCreated] = useState<string>("");
const triggerRef = useRef<HTMLButtonElement>(null);
- const options = useWorkCenters({
+ const { options, workCenterFetcher } = useWorkCenters({
processId: props?.processId,
locationId: props?.locationId,
});
@@ -29,6 +30,12 @@ const WorkCenter = (props: WorkCenterSelectProps) => {
return (
<>
<CreatableCombobox
+ autoSelectSingleOption={
+ props?.autoSelectSingleOption &&
+ Boolean(props?.processId) &&
+ options.length === 1 &&
+ workCenterFetcher.state === "idle"
+ }
ref={triggerRef}
options={options}
{...props}
@@ -110,5 +117,5 @@ export const useWorkCenters = (args: {
[workCenterFetcher.data, processId, locationId]
);
- return options;
+ return { options, workCenterFetcher };
};
diff --git a/apps/erp/app/modules/production/production.models.ts b/apps/erp/app/modules/production/production.models.ts
index ddc3d10a00..c424562613 100644
--- a/apps/erp/app/modules/production/production.models.ts
+++ b/apps/erp/app/modules/production/production.models.ts
@@ -121,55 +121,211 @@ export const salesOrderToJobValidator = baseJobValidator
}
);
-export const jobOperationValidator = z
- .object({
- id: zfd.text(z.string().optional()),
- jobMakeMethodId: z
- .string()
- .min(1, { message: "Quote Make Method is required" }),
- order: zfd.numeric(z.number().min(0)),
- operationOrder: z.enum(methodOperationOrders, {
- errorMap: (issue, ctx) => ({
- message: "Operation order is required",
- }),
+export const baseJobOperationValidator = z.object({
+ id: zfd.text(z.string().optional()),
+ jobMakeMethodId: z
+ .string()
+ .min(1, { message: "Quote Make Method is required" }),
+ order: zfd.numeric(z.number().min(0)),
+ operationOrder: z.enum(methodOperationOrders, {
+ errorMap: (issue, ctx) => ({
+ message: "Operation order is required",
}),
- operationType: z.enum(operationTypes, {
- errorMap: (issue, ctx) => ({
- message: "Operation type is required",
- }),
+ }),
+ operationType: z.enum(operationTypes, {
+ errorMap: (issue, ctx) => ({
+ message: "Operation type is required",
}),
- processId: z.string().min(20, { message: "Process is required" }),
- procedureId: zfd.text(z.string().optional()),
- workCenterId: zfd.text(z.string().optional()),
- description: zfd.text(
- z.string().min(0, { message: "Description is required" })
- ),
- setupUnit: z
- .enum(standardFactorType, {
- errorMap: () => ({ message: "Setup unit is required" }),
- })
- .optional(),
- setupTime: zfd.numeric(z.number().min(0).optional()),
- laborUnit: z
- .enum(standardFactorType, {
- errorMap: () => ({ message: "Labor unit is required" }),
- })
- .optional(),
- laborTime: zfd.numeric(z.number().min(0).optional()),
- machineUnit: z
- .enum(standardFactorType, {
- errorMap: () => ({ message: "Machine unit is required" }),
- })
- .optional(),
- machineTime: zfd.numeric(z.number().min(0).optional()),
- machineRate: zfd.numeric(z.number().min(0).optional()),
- overheadRate: zfd.numeric(z.number().min(0).optional()),
- laborRate: zfd.numeric(z.number().min(0).optional()),
- operationSupplierProcessId: zfd.text(z.string().optional()),
- operationMinimumCost: zfd.numeric(z.number().min(0).optional()),
- operationUnitCost: zfd.numeric(z.number().min(0).optional()),
- operationLeadTime: zfd.numeric(z.number().min(0).optional()),
- })
+ }),
+ processId: z.string().min(20, { message: "Process is required" }),
+ procedureId: zfd.text(z.string().optional()),
+ description: zfd.text(
+ z.string().min(0, { message: "Description is required" })
+ ),
+ setupUnit: z
+ .enum(standardFactorType, {
+ errorMap: () => ({ message: "Setup unit is required" }),
+ })
+ .optional(),
+ setupTime: zfd.numeric(z.number().min(0).optional()),
+ laborUnit: z
+ .enum(standardFactorType, {
+ errorMap: () => ({ message: "Labor unit is required" }),
+ })
+ .optional(),
+ laborTime: zfd.numeric(z.number().min(0).optional()),
+ machineUnit: z
+ .enum(standardFactorType, {
+ errorMap: () => ({ message: "Machine unit is required" }),
+ })
+ .optional(),
+ machineTime: zfd.numeric(z.number().min(0).optional()),
+ machineRate: zfd.numeric(z.number().min(0).optional()),
+ overheadRate: zfd.numeric(z.number().min(0).optional()),
+ laborRate: zfd.numeric(z.number().min(0).optional()),
+ operationSupplierProcessId: zfd.text(z.string().optional()),
+ operationMinimumCost: zfd.numeric(z.number().min(0).optional()),
+ operationUnitCost: zfd.numeric(z.number().min(0).optional()),
+ operationLeadTime: zfd.numeric(z.number().min(0).optional()),
+});
+
+export const jobOperationValidator = baseJobOperationValidator
+ .merge(
+ z.object({
+ workCenterId: zfd.text(z.string().optional()),
+ })
+ )
+ .refine(
+ (data) => {
+ if (data.operationType === "Outside") {
+ return Number.isFinite(data.operationMinimumCost);
+ }
+ return true;
+ },
+ {
+ message: "Minimum is required",
+ path: ["operationMinimumCost"],
+ }
+ )
+ .refine(
+ (data) => {
+ if (data.operationType === "Outside") {
+ return Number.isFinite(data.operationUnitCost);
+ }
+ return true;
+ },
+ {
+ message: "Unit cost is required",
+ path: ["operationUnitCost"],
+ }
+ )
+ .refine(
+ (data) => {
+ if (data.operationType === "Outside") {
+ return Number.isFinite(data.operationLeadTime);
+ }
+ return true;
+ },
+ {
+ message: "Lead time is required",
+ path: ["operationLeadTime"],
+ }
+ )
+ .refine(
+ (data) => {
+ if (data.operationType === "Inside") {
+ return !!data.setupUnit;
+ }
+ return true;
+ },
+ {
+ message: "Setup unit is required",
+ path: ["setupUnit"],
+ }
+ )
+ .refine(
+ (data) => {
+ if (data.operationType === "Inside") {
+ return !!data.laborUnit;
+ }
+ return true;
+ },
+ {
+ message: "Labor unit is required",
+ path: ["laborUnit"],
+ }
+ )
+ .refine(
+ (data) => {
+ if (data.operationType === "Inside") {
+ return !!data.laborUnit;
+ }
+ return true;
+ },
+ {
+ message: "Machine unit is required",
+ path: ["machineUnit"],
+ }
+ )
+ .refine(
+ (data) => {
+ if (data.operationType === "Inside") {
+ return Number.isFinite(data.setupTime);
+ }
+ return true;
+ },
+ {
+ message: "Setup time is required",
+ path: ["setupTime"],
+ }
+ )
+ .refine(
+ (data) => {
+ if (data.operationType === "Inside") {
+ return Number.isFinite(data.laborTime);
+ }
+ return true;
+ },
+ {
+ message: "Labor time is required",
+ path: ["laborTime"],
+ }
+ )
+ .refine(
+ (data) => {
+ if (data.operationType === "Inside") {
+ return Number.isFinite(data.machineTime);
+ }
+ return true;
+ },
+ {
+ message: "Machine time is required",
+ path: ["machineTime"],
+ }
+ )
+ .refine(
+ (data) => {
+ if (data.operationType === "Inside") {
+ return Number.isFinite(data.machineRate);
+ }
+ return true;
+ },
+ {
+ message: "Machine rate is required",
+ path: ["machineRate"],
+ }
+ )
+ .refine(
+ (data) => {
+ if (data.operationType === "Inside") {
+ return Number.isFinite(data.overheadRate);
+ }
+ return true;
+ },
+ {
+ message: "Overhead rate is required",
+ path: ["overheadRate"],
+ }
+ )
+ .refine(
+ (data) => {
+ if (data.operationType === "Inside") {
+ return Number.isFinite(data.laborRate);
+ }
+ return true;
+ },
+ {
+ message: "Labor rate is required",
+ path: ["laborRate"],
+ }
+ );
+
+export const jobOperationValidatorForReleasedJob = baseJobOperationValidator
+ .merge(
+ z.object({
+ workCenterId: z.string().min(1, { message: "Work center is required" }),
+ })
+ )
.refine(
(data) => {
if (data.operationType === "Outside") {
@@ -314,6 +470,7 @@ export const jobOperationValidator = z
path: ["laborRate"],
}
);
+
export const jobMaterialValidator = z
.object({
id: zfd.text(z.string().optional()),
diff --git a/apps/erp/app/modules/production/ui/Jobs/JobBillOfProcess.tsx b/apps/erp/app/modules/production/ui/Jobs/JobBillOfProcess.tsx
index 534f3dea2b..0a66449c5d 100644
--- a/apps/erp/app/modules/production/ui/Jobs/JobBillOfProcess.tsx
+++ b/apps/erp/app/modules/production/ui/Jobs/JobBillOfProcess.tsx
@@ -132,6 +132,7 @@ import { usePeople, useTools } from "~/stores";
import { getPrivateUrl, path } from "~/utils/path";
import {
jobOperationValidator,
+ jobOperationValidatorForReleasedJob,
procedureSyncValidator,
} from "../../production.models";
import { getProductionEventsPage } from "../../production.service";
@@ -692,6 +693,7 @@ const JobBillOfProcess = ({
<OperationForm
item={item}
isDisabled={isDisabled}
+ job={jobData?.job}
locationId={locationId}
workInstruction={workInstructions[item.id] ?? {}}
setWorkInstructions={setWorkInstructions}
@@ -1691,6 +1693,7 @@ function ParametersListItem({
function OperationForm({
item,
isDisabled,
+ job,
locationId,
workInstruction,
setWorkInstructions,
@@ -1699,6 +1702,7 @@ function OperationForm({
}: {
item: ItemWithData;
isDisabled: boolean;
+ job?: Job;
locationId: string;
workInstruction: JSONContent;
setWorkInstructions: Dispatch<SetStateAction<PendingWorkInstructions>>;
@@ -1914,7 +1918,11 @@ function OperationForm({
}
method="post"
defaultValues={item.data}
- validator={jobOperationValidator}
+ validator={
+ job?.status === "Draft"
+ ? jobOperationValidator
+ : jobOperationValidatorForReleasedJob
+ }
className="w-full flex flex-col gap-y-4"
fetcher={fetcher}
onSubmit={() => {
@@ -2036,8 +2044,9 @@ function OperationForm({
<WorkCenter
name="workCenterId"
label="Work Center"
+ autoSelectSingleOption={Boolean(processData.processId)}
locationId={locationId}
- isOptional
+ isOptional={job?.status === "Draft"}
processId={processData.processId}
onChange={(value) => {
if (value) {
diff --git a/packages/form/src/components/CreatableCombobox.tsx b/packages/form/src/components/CreatableCombobox.tsx
index 938f42c20d..b031c8cc57 100644
--- a/packages/form/src/components/CreatableCombobox.tsx
+++ b/packages/form/src/components/CreatableCombobox.tsx
@@ -15,6 +15,7 @@ export type CreatableComboboxProps = Omit<
CreatableComboboxBaseProps,
"onChange"
> & {
+ autoSelectSingleOption?: boolean;
name: string;
label?: string;
helperText?: string;
@@ -31,6 +32,7 @@ export type CreatableComboboxProps = Omit<
const CreatableCombobox = forwardRef<HTMLButtonElement, CreatableComboboxProps>(
(
{
+ autoSelectSingleOption = false,
name,
label,
helperText,
@@ -49,6 +51,16 @@ const CreatableCombobox = forwardRef<HTMLButtonElement, CreatableComboboxProps>(
setValue(props.value);
}, [props.value, setValue]);
+ useEffect(() => {
+ if (
+ autoSelectSingleOption &&
+ props.options.length === 1 &&
+ !value // Only auto-select if no value is already set
+ ) {
+ setValue(props.options[0].value);
+ }
+ }, [autoSelectSingleOption, props.options, setValue, value]);
+
const onChange = (value: string) => {
if (value) {
props?.onChange?.(props.options.find((o) => o.value === value) ?? null);