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: get method for item to item and item to job

Brad Barbina095d77822ba
diff --git a/apps/carbon/app/modules/items/ui/Item/BillOfProcess.tsx b/apps/carbon/app/modules/items/ui/Item/BillOfProcess.tsx
index d711829b8b..6d3fe70821 100644
--- a/apps/carbon/app/modules/items/ui/Item/BillOfProcess.tsx
+++ b/apps/carbon/app/modules/items/ui/Item/BillOfProcess.tsx
@@ -123,7 +123,6 @@ function makeItem(operation: Operation): ItemWithData {
{operation.laborTime} {operation.laborUnit}
</Badge>
)}
-
{(operation?.machineTime ?? 0) > 0 && (
<Badge variant="secondary">
<TimeTypeIcon type="Machine" className="h-3 w-3 mr-1" />
@@ -611,14 +610,9 @@ function OperationForm({
userId,
]);
- const [showMachine, setShowMachine] = useState(
- (item.data.machineTime ?? 0) > 0
- );
- const [showLabor, setShowLabor] = useState((item.data.laborTime ?? 0) > 0);
- const [showSetup, setShowSetup] = useState(
- (item.data.setupTime ?? 0) > 0 ||
- (showLabor === false && showMachine === false)
- );
+ const [showMachine, setShowMachine] = useState(false);
+ const [showLabor, setShowLabor] = useState(false);
+ const [showSetup, setShowSetup] = useState(false);
const [processData, setProcessData] = useState<{
description: string;
@@ -811,19 +805,27 @@ function OperationForm({
<TimeTypeIcon type="Setup" />
<Label>Setup</Label>
</HStack>
- <IconButton
- icon={<LuChevronDown />}
- aria-label={showSetup ? "Collapse Setup" : "Expand Setup"}
- variant="ghost"
- size="sm"
- onClick={(e) => {
- e.stopPropagation();
- setShowSetup(!showSetup);
- }}
- className={`transition-transform ${
- showSetup ? "rotate-180" : ""
- }`}
- />
+ <HStack>
+ {(processData.setupTime ?? 0) > 0 && (
+ <Badge variant="secondary">
+ <TimeTypeIcon type="Setup" className="h-3 w-3 mr-1" />
+ {processData.setupTime} {processData.setupUnit}
+ </Badge>
+ )}
+ <IconButton
+ icon={<LuChevronDown />}
+ aria-label={showSetup ? "Collapse Setup" : "Expand Setup"}
+ variant="ghost"
+ size="sm"
+ onClick={(e) => {
+ e.stopPropagation();
+ setShowSetup(!showSetup);
+ }}
+ className={`transition-transform ${
+ showSetup ? "rotate-180" : ""
+ }`}
+ />
+ </HStack>
</HStack>
<div
className={`grid w-full gap-x-8 gap-y-4 grid-cols-1 lg:grid-cols-3 pb-4 ${
@@ -879,19 +881,27 @@ function OperationForm({
<TimeTypeIcon type="Labor" />
<Label>Labor</Label>
</HStack>
- <IconButton
- icon={<LuChevronDown />}
- aria-label={showLabor ? "Collapse Labor" : "Expand Labor"}
- variant="ghost"
- size="sm"
- onClick={(e) => {
- e.stopPropagation();
- setShowLabor(!showLabor);
- }}
- className={`transition-transform ${
- showLabor ? "rotate-180" : ""
- }`}
- />
+ <HStack>
+ {(processData.laborTime ?? 0) > 0 && (
+ <Badge variant="secondary">
+ <TimeTypeIcon type="Labor" className="h-3 w-3 mr-1" />
+ {processData.laborTime} {processData.laborUnit}
+ </Badge>
+ )}
+ <IconButton
+ icon={<LuChevronDown />}
+ aria-label={showLabor ? "Collapse Labor" : "Expand Labor"}
+ variant="ghost"
+ size="sm"
+ onClick={(e) => {
+ e.stopPropagation();
+ setShowLabor(!showLabor);
+ }}
+ className={`transition-transform ${
+ showLabor ? "rotate-180" : ""
+ }`}
+ />
+ </HStack>
</HStack>
<div
className={`grid w-full gap-x-8 gap-y-4 grid-cols-1 lg:grid-cols-3 pb-4 ${
@@ -946,19 +956,29 @@ function OperationForm({
<TimeTypeIcon type="Machine" />
<Label>Machine</Label>
</HStack>
- <IconButton
- icon={<LuChevronDown />}
- aria-label={showMachine ? "Collapse Machine" : "Expand Machine"}
- variant="ghost"
- size="sm"
- onClick={(e) => {
- e.stopPropagation();
- setShowMachine(!showMachine);
- }}
- className={`transition-transform ${
- showMachine ? "rotate-180" : ""
- }`}
- />
+ <HStack>
+ {(processData?.machineTime ?? 0) > 0 && (
+ <Badge variant="secondary">
+ <TimeTypeIcon type="Machine" className="h-3 w-3 mr-1" />
+ {processData.machineTime} {processData.machineUnit}
+ </Badge>
+ )}
+ <IconButton
+ icon={<LuChevronDown />}
+ aria-label={
+ showMachine ? "Collapse Machine" : "Expand Machine"
+ }
+ variant="ghost"
+ size="sm"
+ onClick={(e) => {
+ e.stopPropagation();
+ setShowMachine(!showMachine);
+ }}
+ className={`transition-transform ${
+ showMachine ? "rotate-180" : ""
+ }`}
+ />
+ </HStack>
</HStack>
<div
className={`grid w-full gap-x-8 gap-y-4 grid-cols-1 lg:grid-cols-3 pb-4 ${
diff --git a/apps/carbon/app/modules/production/ui/Jobs/JobBillOfProcess.tsx b/apps/carbon/app/modules/production/ui/Jobs/JobBillOfProcess.tsx
index 2970dd096d..a231a8fce3 100644
--- a/apps/carbon/app/modules/production/ui/Jobs/JobBillOfProcess.tsx
+++ b/apps/carbon/app/modules/production/ui/Jobs/JobBillOfProcess.tsx
@@ -529,7 +529,7 @@ const JobBillOfProcess = ({
},
{
id: 3,
- label: "Realtime Events",
+ label: "Events",
content: (
<div className="flex w-full flex-col pr-2 py-6 min-h-[300px]">
<motion.div
@@ -775,15 +775,9 @@ function OperationForm({
userId,
]);
- const [showMachine, setShowMachine] = useState(
- (item.data.machineTime ?? 0) > 0
- );
- const [showLabor, setShowLabor] = useState((item.data.laborTime ?? 0) > 0);
- const [showSetup, setShowSetup] = useState(
- (item.data.setupTime ?? 0) > 0 ||
- (showLabor === false && showMachine === false)
- );
-
+ const [showMachine, setShowMachine] = useState(false);
+ const [showLabor, setShowLabor] = useState(false);
+ const [showSetup, setShowSetup] = useState(false);
const [showCost, setShowCost] = useState(false);
const [processData, setProcessData] = useState<{
@@ -1104,19 +1098,27 @@ function OperationForm({
<TimeTypeIcon type="Setup" />
<Label>Setup</Label>
</HStack>
- <IconButton
- icon={<LuChevronDown />}
- aria-label={showSetup ? "Collapse Setup" : "Expand Setup"}
- variant="ghost"
- size="sm"
- onClick={(e) => {
- e.stopPropagation();
- setShowSetup(!showSetup);
- }}
- className={`transition-transform ${
- showSetup ? "rotate-180" : ""
- }`}
- />
+ <HStack>
+ {(processData.setupTime ?? 0) > 0 && (
+ <Badge variant="secondary">
+ <TimeTypeIcon type="Setup" className="h-3 w-3 mr-1" />
+ {processData.setupTime} {processData.setupUnit}
+ </Badge>
+ )}
+ <IconButton
+ icon={<LuChevronDown />}
+ aria-label={showSetup ? "Collapse Setup" : "Expand Setup"}
+ variant="ghost"
+ size="sm"
+ onClick={(e) => {
+ e.stopPropagation();
+ setShowSetup(!showSetup);
+ }}
+ className={`transition-transform ${
+ showSetup ? "rotate-180" : ""
+ }`}
+ />
+ </HStack>
</HStack>
<div
className={`grid w-full gap-x-8 gap-y-4 grid-cols-1 lg:grid-cols-3 pb-4 ${
@@ -1171,19 +1173,27 @@ function OperationForm({
<TimeTypeIcon type="Labor" />
<Label>Labor</Label>
</HStack>
- <IconButton
- icon={<LuChevronDown />}
- aria-label={showLabor ? "Collapse Labor" : "Expand Labor"}
- variant="ghost"
- size="sm"
- onClick={(e) => {
- e.stopPropagation();
- setShowLabor(!showLabor);
- }}
- className={`transition-transform ${
- showLabor ? "rotate-180" : ""
- }`}
- />
+ <HStack>
+ {(processData.laborTime ?? 0) > 0 && (
+ <Badge variant="secondary">
+ <TimeTypeIcon type="Labor" className="h-3 w-3 mr-1" />
+ {processData.laborTime} {processData.laborUnit}
+ </Badge>
+ )}
+ <IconButton
+ icon={<LuChevronDown />}
+ aria-label={showLabor ? "Collapse Labor" : "Expand Labor"}
+ variant="ghost"
+ size="sm"
+ onClick={(e) => {
+ e.stopPropagation();
+ setShowLabor(!showLabor);
+ }}
+ className={`transition-transform ${
+ showLabor ? "rotate-180" : ""
+ }`}
+ />
+ </HStack>
</HStack>
<div
className={`grid w-full gap-x-8 gap-y-4 grid-cols-1 lg:grid-cols-3 pb-4 ${
@@ -1239,19 +1249,29 @@ function OperationForm({
<TimeTypeIcon type="Machine" />
<Label>Machine</Label>
</HStack>
- <IconButton
- icon={<LuChevronDown />}
- aria-label={showMachine ? "Collapse Machine" : "Expand Machine"}
- variant="ghost"
- size="sm"
- onClick={(e) => {
- e.stopPropagation();
- setShowMachine(!showMachine);
- }}
- className={`transition-transform ${
- showMachine ? "rotate-180" : ""
- }`}
- />
+ <HStack>
+ {(processData.machineTime ?? 0) > 0 && (
+ <Badge variant="secondary">
+ <TimeTypeIcon type="Machine" className="h-3 w-3 mr-1" />
+ {processData.machineTime} {processData.machineUnit}
+ </Badge>
+ )}
+ <IconButton
+ icon={<LuChevronDown />}
+ aria-label={
+ showMachine ? "Collapse Machine" : "Expand Machine"
+ }
+ variant="ghost"
+ size="sm"
+ onClick={(e) => {
+ e.stopPropagation();
+ setShowMachine(!showMachine);
+ }}
+ className={`transition-transform ${
+ showMachine ? "rotate-180" : ""
+ }`}
+ />
+ </HStack>
</HStack>
<div
className={`grid w-full gap-x-8 gap-y-4 grid-cols-1 lg:grid-cols-3 pb-4 ${
@@ -1307,19 +1327,21 @@ function OperationForm({
<LuDollarSign />
<Label>Costing</Label>
</HStack>
- <IconButton
- icon={<LuChevronDown />}
- aria-label={showCost ? "Collapse Costing" : "Expand Costing"}
- variant="ghost"
- size="sm"
- onClick={(e) => {
- e.stopPropagation();
- setShowCost(!showCost);
- }}
- className={`transition-transform ${
- showCost ? "rotate-180" : ""
- }`}
- />
+ <HStack>
+ <IconButton
+ icon={<LuChevronDown />}
+ aria-label={showCost ? "Collapse Costing" : "Expand Costing"}
+ variant="ghost"
+ size="sm"
+ onClick={(e) => {
+ e.stopPropagation();
+ setShowCost(!showCost);
+ }}
+ className={`transition-transform ${
+ showCost ? "rotate-180" : ""
+ }`}
+ />
+ </HStack>
</HStack>
<div
className={`grid w-full gap-x-8 gap-y-4 grid-cols-1 lg:grid-cols-3 pb-4 ${
diff --git a/apps/carbon/app/modules/sales/ui/Quotes/QuoteBillOfProcess.tsx b/apps/carbon/app/modules/sales/ui/Quotes/QuoteBillOfProcess.tsx
index 25aefa94d0..6de96c9246 100644
--- a/apps/carbon/app/modules/sales/ui/Quotes/QuoteBillOfProcess.tsx
+++ b/apps/carbon/app/modules/sales/ui/Quotes/QuoteBillOfProcess.tsx
@@ -641,15 +641,9 @@ function OperationForm({
userId,
]);
- const [showMachine, setShowMachine] = useState(
- (item.data.machineTime ?? 0) > 0
- );
- const [showLabor, setShowLabor] = useState((item.data.laborTime ?? 0) > 0);
- const [showSetup, setShowSetup] = useState(
- (item.data.setupTime ?? 0) > 0 ||
- (showLabor === false && showMachine === false)
- );
-
+ const [showMachine, setShowMachine] = useState(false);
+ const [showLabor, setShowLabor] = useState(false);
+ const [showSetup, setShowSetup] = useState(false);
const [showCost, setShowCost] = useState(false);
const [processData, setProcessData] = useState<{
@@ -968,19 +962,27 @@ function OperationForm({
<TimeTypeIcon type="Setup" />
<Label>Setup</Label>
</HStack>
- <IconButton
- icon={<LuChevronDown />}
- aria-label={showSetup ? "Collapse Setup" : "Expand Setup"}
- variant="ghost"
- size="sm"
- onClick={(e) => {
- e.stopPropagation();
- setShowSetup(!showSetup);
- }}
- className={`transition-transform ${
- showSetup ? "rotate-180" : ""
- }`}
- />
+ <HStack>
+ {(processData.setupTime ?? 0) > 0 && (
+ <Badge variant="secondary">
+ <TimeTypeIcon type="Setup" className="h-3 w-3 mr-1" />
+ {processData.setupTime} {processData.setupUnit}
+ </Badge>
+ )}
+ <IconButton
+ icon={<LuChevronDown />}
+ aria-label={showSetup ? "Collapse Setup" : "Expand Setup"}
+ variant="ghost"
+ size="sm"
+ onClick={(e) => {
+ e.stopPropagation();
+ setShowSetup(!showSetup);
+ }}
+ className={`transition-transform ${
+ showSetup ? "rotate-180" : ""
+ }`}
+ />
+ </HStack>
</HStack>
<div
className={`grid w-full gap-x-8 gap-y-4 grid-cols-1 lg:grid-cols-3 pb-4 ${
@@ -1035,19 +1037,27 @@ function OperationForm({
<TimeTypeIcon type="Labor" />
<Label>Labor</Label>
</HStack>
- <IconButton
- icon={<LuChevronDown />}
- aria-label={showLabor ? "Collapse Labor" : "Expand Labor"}
- variant="ghost"
- size="sm"
- onClick={(e) => {
- e.stopPropagation();
- setShowLabor(!showLabor);
- }}
- className={`transition-transform ${
- showLabor ? "rotate-180" : ""
- }`}
- />
+ <HStack>
+ {(processData.laborTime ?? 0) > 0 && (
+ <Badge variant="secondary">
+ <TimeTypeIcon type="Labor" className="h-3 w-3 mr-1" />
+ {processData.laborTime} {processData.laborUnit}
+ </Badge>
+ )}
+ <IconButton
+ icon={<LuChevronDown />}
+ aria-label={showLabor ? "Collapse Labor" : "Expand Labor"}
+ variant="ghost"
+ size="sm"
+ onClick={(e) => {
+ e.stopPropagation();
+ setShowLabor(!showLabor);
+ }}
+ className={`transition-transform ${
+ showLabor ? "rotate-180" : ""
+ }`}
+ />
+ </HStack>
</HStack>
<div
className={`grid w-full gap-x-8 gap-y-4 grid-cols-1 lg:grid-cols-3 pb-4 ${
@@ -1103,19 +1113,29 @@ function OperationForm({
<TimeTypeIcon type="Machine" />
<Label>Machine</Label>
</HStack>
- <IconButton
- icon={<LuChevronDown />}
- aria-label={showMachine ? "Collapse Machine" : "Expand Machine"}
- variant="ghost"
- size="sm"
- onClick={(e) => {
- e.stopPropagation();
- setShowMachine(!showMachine);
- }}
- className={`transition-transform ${
- showMachine ? "rotate-180" : ""
- }`}
- />
+ <HStack>
+ {(processData.machineTime ?? 0) > 0 && (
+ <Badge variant="secondary">
+ <TimeTypeIcon type="Machine" className="h-3 w-3 mr-1" />
+ {processData.machineTime} {processData.machineUnit}
+ </Badge>
+ )}
+ <IconButton
+ icon={<LuChevronDown />}
+ aria-label={
+ showMachine ? "Collapse Machine" : "Expand Machine"
+ }
+ variant="ghost"
+ size="sm"
+ onClick={(e) => {
+ e.stopPropagation();
+ setShowMachine(!showMachine);
+ }}
+ className={`transition-transform ${
+ showMachine ? "rotate-180" : ""
+ }`}
+ />
+ </HStack>
</HStack>
<div
className={`grid w-full gap-x-8 gap-y-4 grid-cols-1 lg:grid-cols-3 pb-4 ${
diff --git a/packages/database/supabase/functions/get-method/index.ts b/packages/database/supabase/functions/get-method/index.ts
index 34b707cce7..35827404c1 100644
--- a/packages/database/supabase/functions/get-method/index.ts
+++ b/packages/database/supabase/functions/get-method/index.ts
@@ -86,7 +86,9 @@ serve(async (req: Request) => {
.eq("makeMethodId", sourceMakeMethod.data.id),
client
.from("methodOperation")
- .select("*")
+ .select(
+ "*, methodOperationTool(id, operationId, toolId, quantity, createdBy, createdAt, updatedBy, updatedAt)"
+ )
.eq("makeMethodId", sourceMakeMethod.data.id),
]);
@@ -125,17 +127,43 @@ serve(async (req: Request) => {
// Copy operations from source to target
if (sourceOperations.data && sourceOperations.data.length > 0) {
- await trx
+ const operationIds = await trx
.insertInto("methodOperation")
.values(
- sourceOperations.data.map((operation) => ({
- ...operation,
- id: undefined, // Let the database generate a new ID
- makeMethodId: targetMakeMethod.data.id,
- createdBy: userId,
- }))
+ sourceOperations.data.map(
+ ({ methodOperationTool: _tools, ...operation }) => ({
+ ...operation,
+ id: undefined, // Let the database generate a new ID
+ makeMethodId: targetMakeMethod.data.id,
+ createdBy: userId,
+ })
+ )
)
+ .returning(["id"])
.execute();
+
+ for await (const [
+ index,
+ operation,
+ ] of sourceOperations.data.entries()) {
+ const { methodOperationTool } = operation;
+ const operationId = operationIds[index].id;
+
+ if (operationId && Array.isArray(methodOperationTool)) {
+ await trx
+ .insertInto("methodOperationTool")
+ .values(
+ methodOperationTool.map((tool) => ({
+ toolId: tool.toolId,
+ quantity: tool.quantity,
+ operationId,
+ companyId,
+ createdBy: userId,
+ }))
+ )
+ .execute();
+ }
+ }
}
});
@@ -220,7 +248,9 @@ serve(async (req: Request) => {
) {
const relatedOperations = await client
.from("methodOperation")
- .select("*")
+ .select(
+ "*, methodOperationTool(id, operationId, toolId, quantity, createdBy, createdAt, updatedBy, updatedAt)"
+ )
.eq("makeMethodId", node.data.materialMakeMethodId);
const jobOperations =
@@ -259,6 +289,33 @@ serve(async (req: Request) => {
.returning(["id"])
.execute();
+ for (const [index, operation] of (
+ relatedOperations.data ?? []
+ ).entries()) {
+ const operationId = operationIds[index].id;
+
+ if (operationId) {
+ const { methodOperationTool } = operation;
+ if (
+ Array.isArray(methodOperationTool) &&
+ methodOperationTool.length > 0
+ ) {
+ await trx
+ .insertInto("jobOperationTool")
+ .values(
+ methodOperationTool.map((tool) => ({
+ toolId: tool.toolId,
+ quantity: tool.quantity,
+ operationId,
+ companyId,
+ createdBy: userId,
+ }))
+ )
+ .execute();
+ }
+ }
+ }
+
methodOperationsToJobOperations =
relatedOperations.data?.reduce<Record<string, string>>(
(acc, op, index) => {