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: add structured scrap reason and unstructured scrap notes to MES

Rob Carrington9d2ba074a293
diff --git a/apps/carbon/app/components/Form/ScrapReason.tsx b/apps/carbon/app/components/Form/ScrapReason.tsx
index be30133ec9..caff6ad88f 100644
--- a/apps/carbon/app/components/Form/ScrapReason.tsx
+++ b/apps/carbon/app/components/Form/ScrapReason.tsx
@@ -1,50 +1,25 @@
import type { ComboboxProps } from "@carbon/form";
-import { CreatableCombobox } from "@carbon/form";
-import { useDisclosure, useMount } from "@carbon/react";
+import { Combobox } from "@carbon/form";
+import { useMount } from "@carbon/react";
import { useFetcher } from "@remix-run/react";
-import { useMemo, useRef, useState } from "react";
+import { useMemo } from "react";
import { useRouteData } from "~/hooks";
import type {
getScrapReasonsList,
ScrapReason as ScrapReasonType,
} from "~/modules/production";
-import ScrapReasonForm from "~/modules/production/ui/ScrapReasonCodes/ScrapReasonForm";
import { path } from "~/utils/path";
type ScrapReasonSelectProps = Omit<ComboboxProps, "options">;
const ScrapReason = (props: ScrapReasonSelectProps) => {
- const newScrapReasonModal = useDisclosure();
- const [created, setCreated] = useState<string>("");
- const triggerRef = useRef<HTMLButtonElement>(null);
-
const options = useScrapReasons();
return (
- <>
- <CreatableCombobox
- ref={triggerRef}
- options={options}
- {...props}
- label={props?.label ?? "CustomerStatus"}
- onCreateOption={(option) => {
- newScrapReasonModal.onOpen();
- setCreated(option);
- }}
- />
- {newScrapReasonModal.isOpen && (
- <ScrapReasonForm
- type="modal"
- onClose={() => {
- setCreated("");
- newScrapReasonModal.onClose();
- triggerRef.current?.click();
- }}
- initialValues={{
- name: created,
- }}
- />
- )}
- </>
+ <Combobox
+ options={options}
+ {...props}
+ label={props?.label ?? "Scrap Reason"}
+ />
);
};
diff --git a/apps/carbon/app/modules/production/ui/Jobs/ProductionQuantityForm.tsx b/apps/carbon/app/modules/production/ui/Jobs/ProductionQuantityForm.tsx
index 0876f7ce19..a1fc834607 100644
--- a/apps/carbon/app/modules/production/ui/Jobs/ProductionQuantityForm.tsx
+++ b/apps/carbon/app/modules/production/ui/Jobs/ProductionQuantityForm.tsx
@@ -21,6 +21,7 @@ import {
Submit,
TextArea,
} from "~/components/Form";
+import ScrapReason from "~/components/Form/ScrapReason";
import { usePermissions } from "~/hooks";
import { productionQuantityValidator } from "~/modules/production";
@@ -68,7 +69,8 @@ const ProductionQuantityForm = ({
{ label: "Rework", value: "Rework" },
]}
/>
- <TextArea name="scrapReason" label="Scrap Reason" />
+ <ScrapReason name="scrapReasonId" label="Scrap Reason" />
+ <TextArea name="notes" label="Notes" />
</VStack>
</DrawerBody>
<DrawerFooter>
diff --git a/apps/carbon/app/routes/api+/production.scrap-reason-codes.ts b/apps/carbon/app/routes/api+/production.scrap-reasons.ts
similarity index 100%
rename from apps/carbon/app/routes/api+/production.scrap-reason-codes.ts
rename to apps/carbon/app/routes/api+/production.scrap-reasons.ts
diff --git a/apps/carbon/app/routes/x+/job+/$jobId.quantities.$id.tsx b/apps/carbon/app/routes/x+/job+/$jobId.quantities.$id.tsx
index 39a7eb2626..f49c760569 100644
--- a/apps/carbon/app/routes/x+/job+/$jobId.quantities.$id.tsx
+++ b/apps/carbon/app/routes/x+/job+/$jobId.quantities.$id.tsx
@@ -81,6 +81,8 @@ export default function EditProductionQuantityRoute() {
type: productionQuantity?.type ?? ("Scrap" as "Scrap"),
jobOperationId: productionQuantity?.jobOperationId ?? "",
quantity: productionQuantity?.quantity ?? 0,
+ scrapReasonId: productionQuantity?.scrapReasonId ?? "",
+ notes: productionQuantity?.notes ?? "",
createdBy: productionQuantity?.createdBy ?? "",
};
diff --git a/apps/carbon/app/utils/path.ts b/apps/carbon/app/utils/path.ts
index 4b13f39c1e..345c2c6af8 100644
--- a/apps/carbon/app/utils/path.ts
+++ b/apps/carbon/app/utils/path.ts
@@ -46,7 +46,7 @@ export const path = {
generatePath(
`${api}/settings/sequence/rollback?table=${table}&currentSequence=${id}`
),
- scrapReasons: `${api}/production/scrap-reason-codes`,
+ scrapReasons: `${api}/production/scrap-reasons`,
sequences: (table: string) => `${api}/settings/sequences?table=${table}`,
services: `${api}/items/services`,
diff --git a/apps/mes/app/components/JobOperation.tsx b/apps/mes/app/components/JobOperation.tsx
index 403f65adf7..d00c020c11 100644
--- a/apps/mes/app/components/JobOperation.tsx
+++ b/apps/mes/app/components/JobOperation.tsx
@@ -115,6 +115,7 @@ import {
LuTimer,
} from "react-icons/lu";
import { MethodIcon, MethodItemTypeIcon } from "~/components/Icons";
+import ScrapReason from "./ScrapReason";
type JobOperationProps = {
backPath: string;
@@ -1242,7 +1243,6 @@ function QuantityModal({
type: "scrap" | "rework" | "complete" | "finish";
}) {
const fetcher = useFetcher<ProductionQuantity>();
- const [reason, setReason] = useState("");
const [quantity, setQuantity] = useState(type === "finish" ? 0 : 1);
const titleMap = {
@@ -1332,35 +1332,8 @@ function QuantityModal({
)}
{type === "scrap" ? (
<>
- <TextArea
- label="Scrap Reason"
- name="scrapReason"
- value={reason}
- onChange={(e) => setReason(e.target.value)}
- />
- <div className="col-span-2 flex gap-2 mt-2">
- <Badge
- variant="secondary"
- className="cursor-pointer"
- onClick={() => setReason("Defective")}
- >
- Defective
- </Badge>
- <Badge
- variant="secondary"
- className="cursor-pointer"
- onClick={() => setReason("Damaged")}
- >
- Damaged
- </Badge>
- <Badge
- variant="secondary"
- className="cursor-pointer"
- onClick={() => setReason("Quality Control")}
- >
- Quality Control
- </Badge>
- </div>
+ <ScrapReason name="scrapReasonId" label="Scrap Reason" />
+ <TextArea label="Notes" name="notes" />
</>
) : (
<NumberControlled
diff --git a/apps/mes/app/components/ScrapReason.tsx b/apps/mes/app/components/ScrapReason.tsx
new file mode 100644
index 0000000000..35f4a2aaac
--- /dev/null
+++ b/apps/mes/app/components/ScrapReason.tsx
@@ -0,0 +1,44 @@
+import type { ComboboxProps } from "@carbon/form";
+import { Combobox } from "@carbon/form";
+import { useMount } from "@carbon/react";
+import { useFetcher } from "@remix-run/react";
+import { useMemo } from "react";
+import type { getScrapReasonsList } from "~/services/jobs.service";
+import { path } from "~/utils/path";
+type ScrapReasonSelectProps = Omit<ComboboxProps, "options">;
+
+const ScrapReason = (props: ScrapReasonSelectProps) => {
+ const options = useScrapReasons();
+
+ return (
+ <Combobox
+ options={options}
+ {...props}
+ label={props?.label ?? "Scrap Reason"}
+ />
+ );
+};
+
+ScrapReason.displayName = "ScrapReason";
+
+export default ScrapReason;
+
+export const useScrapReasons = () => {
+ const scrapReasonFetcher =
+ useFetcher<Awaited<ReturnType<typeof getScrapReasonsList>>>();
+
+ useMount(() => {
+ scrapReasonFetcher.load(path.to.scrapReasons);
+ });
+
+ const options = useMemo(() => {
+ const dataSource = scrapReasonFetcher.data?.data ?? [];
+
+ return dataSource.map((c) => ({
+ value: c.id,
+ label: c.name,
+ }));
+ }, [scrapReasonFetcher.data?.data]);
+
+ return options;
+};
diff --git a/apps/mes/app/routes/api+/scrap-reasons.ts b/apps/mes/app/routes/api+/scrap-reasons.ts
new file mode 100644
index 0000000000..8f33275895
--- /dev/null
+++ b/apps/mes/app/routes/api+/scrap-reasons.ts
@@ -0,0 +1,12 @@
+import { requirePermissions } from "@carbon/auth/auth.server";
+import type { LoaderFunctionArgs } from "@vercel/remix";
+import { json } from "@vercel/remix";
+import { getScrapReasonsList } from "~/services/jobs.service";
+
+export async function loader({ request }: LoaderFunctionArgs) {
+ const { client, companyId } = await requirePermissions(request, {
+ view: "production",
+ });
+
+ return json(await getScrapReasonsList(client, companyId));
+}
diff --git a/apps/mes/app/services/jobs.service.ts b/apps/mes/app/services/jobs.service.ts
index 87ca758395..5fcfc04df5 100644
--- a/apps/mes/app/services/jobs.service.ts
+++ b/apps/mes/app/services/jobs.service.ts
@@ -109,7 +109,8 @@ export const nonScrapQuantityValidator = finishValidator.extend({
});
export const scrapQuantityValidator = nonScrapQuantityValidator.extend({
- scrapReason: z.string().min(1, { message: "Scrap reason is required" }),
+ scrapReasonId: zfd.text(z.string()),
+ notes: zfd.text(z.string().optional()),
});
export async function finishJobOperation(
@@ -321,6 +322,17 @@ export async function getRecentJobOperationsByEmployee(
});
}
+export async function getScrapReasonsList(
+ client: SupabaseClient<Database>,
+ companyId: string
+) {
+ return client
+ .from("scrapReason")
+ .select("id, name")
+ .eq("companyId", companyId)
+ .order("name");
+}
+
export async function getWorkCenter(
client: SupabaseClient<Database>,
workCenterId: string
diff --git a/apps/mes/app/utils/path.ts b/apps/mes/app/utils/path.ts
index e73c3cdad8..c8e0a6c577 100644
--- a/apps/mes/app/utils/path.ts
+++ b/apps/mes/app/utils/path.ts
@@ -39,6 +39,7 @@ export const path = {
rework: `${x}/rework`,
root: "/",
scrap: `${x}/scrap`,
+ scrapReasons: `${api}/scrap-reasons`,
switchCompany: (companyId: string) =>
generatePath(`${x}/company/switch/${companyId}`),
workCenter: (workCenter: string) => generatePath(`${x}/jobs/${workCenter}`),
diff --git a/packages/database/src/swagger-docs-schema.ts b/packages/database/src/swagger-docs-schema.ts
index 420eb7243d..9c9293419a 100644
--- a/packages/database/src/swagger-docs-schema.ts
+++ b/packages/database/src/swagger-docs-schema.ts
@@ -3151,7 +3151,7 @@ export default {
$ref: "#/parameters/rowFilter.productionQuantity.machineProductionEventId",
},
{
- $ref: "#/parameters/rowFilter.productionQuantity.scrapReason",
+ $ref: "#/parameters/rowFilter.productionQuantity.notes",
},
{
$ref: "#/parameters/rowFilter.productionQuantity.companyId",
@@ -3168,6 +3168,9 @@ export default {
{
$ref: "#/parameters/rowFilter.productionQuantity.updatedBy",
},
+ {
+ $ref: "#/parameters/rowFilter.productionQuantity.scrapReasonId",
+ },
{
$ref: "#/parameters/select",
},
@@ -3249,7 +3252,7 @@ export default {
$ref: "#/parameters/rowFilter.productionQuantity.machineProductionEventId",
},
{
- $ref: "#/parameters/rowFilter.productionQuantity.scrapReason",
+ $ref: "#/parameters/rowFilter.productionQuantity.notes",
},
{
$ref: "#/parameters/rowFilter.productionQuantity.companyId",
@@ -3266,6 +3269,9 @@ export default {
{
$ref: "#/parameters/rowFilter.productionQuantity.updatedBy",
},
+ {
+ $ref: "#/parameters/rowFilter.productionQuantity.scrapReasonId",
+ },
{
$ref: "#/parameters/preferReturn",
},
@@ -3301,7 +3307,7 @@ export default {
$ref: "#/parameters/rowFilter.productionQuantity.machineProductionEventId",
},
{
- $ref: "#/parameters/rowFilter.productionQuantity.scrapReason",
+ $ref: "#/parameters/rowFilter.productionQuantity.notes",
},
{
$ref: "#/parameters/rowFilter.productionQuantity.companyId",
@@ -3318,6 +3324,9 @@ export default {
{
$ref: "#/parameters/rowFilter.productionQuantity.updatedBy",
},
+ {
+ $ref: "#/parameters/rowFilter.productionQuantity.scrapReasonId",
+ },
{
$ref: "#/parameters/body.productionQuantity",
},
@@ -48794,6 +48803,111 @@ export default {
format: "text",
type: "string",
},
+ postingDate: {
+ default: "CURRENT_DATE",
+ format: "date",
+ type: "string",
+ },
+ createdAt: {
+ default: "now()",
+ format: "timestamp with time zone",
+ type: "string",
+ },
+ customFields: {
+ format: "jsonb",
+ },
+ },
+ type: "object",
+ },
+ documentLabel: {
+ required: ["documentId", "userId", "label"],
+ properties: {
+ documentId: {
+ description:
+ "Note:\nThis is a Primary Key.<pk/>\nThis is a Foreign Key to `document.id`.<fk table='document' column='id'/>",
+ format: "text",
+ type: "string",
+ },
+ userId: {
+ description:
+ "Note:\nThis is a Primary Key.<pk/>\nThis is a Foreign Key to `user.id`.<fk table='user' column='id'/>",
+ format: "text",
+ type: "string",
+ },
+ label: {
+ description: "Note:\nThis is a Primary Key.<pk/>",
+ format: "text",
+ type: "string",
+ },
+ },
+ type: "object",
+ },
+ productionQuantity: {
+ required: [
+ "id",
+ "jobOperationId",
+ "type",
+ "quantity",
+ "companyId",
+ "createdAt",
+ "createdBy",
+ ],
+ properties: {
+ id: {
+ default: "public.xid()",
+ description: "Note:\nThis is a Primary Key.<pk/>",
+ format: "text",
+ type: "string",
+ },
+ jobOperationId: {
+ description:
+ "Note:\nThis is a Foreign Key to `jobOperation.id`.<fk table='jobOperation' column='id'/>",
+ format: "text",
+ type: "string",
+ },
+ type: {
+ default: "Production",
+ enum: ["Rework", "Scrap", "Production"],
+ format: 'public."productionQuantityType"',
+ type: "string",
+ },
+ quantity: {
+ format: "integer",
+ type: "integer",
+ },
+ setupProductionEventId: {
+ description:
+ "Note:\nThis is a Foreign Key to `productionEvent.id`.<fk table='productionEvent' column='id'/>",
+ format: "text",
+ type: "string",
+ },
+ laborProductionEventId: {
+ description:
+ "Note:\nThis is a Foreign Key to `productionEvent.id`.<fk table='productionEvent' column='id'/>",
+ format: "text",
+ type: "string",
+ },
+ machineProductionEventId: {
+ description:
+ "Note:\nThis is a Foreign Key to `productionEvent.id`.<fk table='productionEvent' column='id'/>",
+ format: "text",
+ type: "string",
+ },
+ notes: {
+ format: "text",
+ type: "string",
+ },
+ companyId: {
+ description:
+ "Note:\nThis is a Foreign Key to `company.id`.<fk table='company' column='id'/>",
+ format: "text",
+ type: "string",
+ },
+ createdAt: {
+ default: "now()",
+ format: "timestamp with time zone",
+ type: "string",
+ },
createdBy: {
description:
"Note:\nThis is a Foreign Key to `user.id`.<fk table='user' column='id'/>",
@@ -48810,10 +48924,28 @@ export default {
format: "text",
type: "string",
},
- customFields: {
- format: "jsonb",
+ scrapReasonId: {
+ description:
+ "Note:\nThis is a Foreign Key to `scrapReason.id`.<fk table='scrapReason' column='id'/>",
+ format: "text",
+ type: "string",
},
- modelUploadId: {
+ },
+ type: "object",
+ },
+ receipt: {
+ required: [
+ "id",
+ "receiptId",
+ "status",
+ "companyId",
+ "createdAt",
+ "createdBy",
+ ],
+ properties: {
+ id: {
+ default: "public.xid()",
+ description: "Note:\nThis is a Primary Key.<pk/>",
format: "text",
type: "string",
},
@@ -63320,8 +63452,8 @@ export default {
in: "query",
type: "string",
},
- "rowFilter.productionQuantity.scrapReason": {
- name: "scrapReason",
+ "rowFilter.productionQuantity.notes": {
+ name: "notes",
required: false,
format: "text",
in: "query",
@@ -63362,6 +63494,13 @@ export default {
in: "query",
type: "string",
},
+ "rowFilter.productionQuantity.scrapReasonId": {
+ name: "scrapReasonId",
+ required: false,
+ format: "text",
+ in: "query",
+ type: "string",
+ },
"body.receipt": {
name: "receipt",
description: "receipt",
diff --git a/packages/database/src/types.ts b/packages/database/src/types.ts
index 79d2c95603..7be0da0994 100644
--- a/packages/database/src/types.ts
+++ b/packages/database/src/types.ts
@@ -11774,8 +11774,9 @@ export type Database = {
jobOperationId: string
laborProductionEventId: string | null
machineProductionEventId: string | null
+ notes: string | null
quantity: number
- scrapReason: string | null
+ scrapReasonId: string | null
setupProductionEventId: string | null
type: Database["public"]["Enums"]["productionQuantityType"]
updatedAt: string | null
@@ -11789,8 +11790,9 @@ export type Database = {
jobOperationId: string
laborProductionEventId?: string | null
machineProductionEventId?: string | null
+ notes?: string | null
quantity: number
- scrapReason?: string | null
+ scrapReasonId?: string | null
setupProductionEventId?: string | null
type?: Database["public"]["Enums"]["productionQuantityType"]
updatedAt?: string | null
@@ -11804,8 +11806,9 @@ export type Database = {
jobOperationId?: string
laborProductionEventId?: string | null
machineProductionEventId?: string | null
+ notes?: string | null
quantity?: number
- scrapReason?: string | null
+ scrapReasonId?: string | null
setupProductionEventId?: string | null
type?: Database["public"]["Enums"]["productionQuantityType"]
updatedAt?: string | null
@@ -11868,6 +11871,13 @@ export type Database = {
referencedRelation: "productionEvent"
referencedColumns: ["id"]
},
+ {
+ foreignKeyName: "productionQuantity_scrapReasonId_fkey"
+ columns: ["scrapReasonId"]
+ isOneToOne: false
+ referencedRelation: "scrapReason"
+ referencedColumns: ["id"]
+ },
{
foreignKeyName: "productionQuantity_setupProductionEventId_fkey"
columns: ["setupProductionEventId"]
diff --git a/packages/database/supabase/functions/lib/types.ts b/packages/database/supabase/functions/lib/types.ts
index 79d2c95603..7be0da0994 100644
--- a/packages/database/supabase/functions/lib/types.ts
+++ b/packages/database/supabase/functions/lib/types.ts
@@ -11774,8 +11774,9 @@ export type Database = {
jobOperationId: string
laborProductionEventId: string | null
machineProductionEventId: string | null
+ notes: string | null
quantity: number
- scrapReason: string | null
+ scrapReasonId: string | null
setupProductionEventId: string | null
type: Database["public"]["Enums"]["productionQuantityType"]
updatedAt: string | null
@@ -11789,8 +11790,9 @@ export type Database = {
jobOperationId: string
laborProductionEventId?: string | null
machineProductionEventId?: string | null
+ notes?: string | null
quantity: number
- scrapReason?: string | null
+ scrapReasonId?: string | null
setupProductionEventId?: string | null
type?: Database["public"]["Enums"]["productionQuantityType"]
updatedAt?: string | null
@@ -11804,8 +11806,9 @@ export type Database = {
jobOperationId?: string
laborProductionEventId?: string | null
machineProductionEventId?: string | null
+ notes?: string | null
quantity?: number
- scrapReason?: string | null
+ scrapReasonId?: string | null
setupProductionEventId?: string | null
type?: Database["public"]["Enums"]["productionQuantityType"]
updatedAt?: string | null
@@ -11868,6 +11871,13 @@ export type Database = {
referencedRelation: "productionEvent"
referencedColumns: ["id"]
},
+ {
+ foreignKeyName: "productionQuantity_scrapReasonId_fkey"
+ columns: ["scrapReasonId"]
+ isOneToOne: false
+ referencedRelation: "scrapReason"
+ referencedColumns: ["id"]
+ },
{
foreignKeyName: "productionQuantity_setupProductionEventId_fkey"
columns: ["setupProductionEventId"]
diff --git a/packages/database/supabase/migrations/20241023151209_scrap-reason-codes.sql b/packages/database/supabase/migrations/20241023151209_scrap-reasons.sql
similarity index 73%
rename from packages/database/supabase/migrations/20241023151209_scrap-reason-codes.sql
rename to packages/database/supabase/migrations/20241023151209_scrap-reasons.sql
index 3b23e2d58c..d2de4ab9fe 100644
--- a/packages/database/supabase/migrations/20241023151209_scrap-reason-codes.sql
+++ b/packages/database/supabase/migrations/20241023151209_scrap-reasons.sql
@@ -16,3 +16,7 @@ CREATE TABLE "scrapReason" (
);
CREATE INDEX "scrapReason_companyId_fkey" ON "scrapReason"("companyId");
+
+ALTER TABLE "productionQuantity" RENAME COLUMN "scrapReason" TO "notes";
+ALTER TABLE "productionQuantity" ADD COLUMN "scrapReasonId" TEXT;
+ALTER TABLE "productionQuantity" ADD CONSTRAINT "productionQuantity_scrapReasonId_fkey" FOREIGN KEY ("scrapReasonId") REFERENCES "scrapReason"("id") ON UPDATE CASCADE ON DELETE SET NULL;