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: estimates vs actuals

Brad Barbin328aedfde521
diff --git a/apps/carbon/app/hooks/index.ts b/apps/carbon/app/hooks/index.ts
index e2bf0d6cac..31d541464c 100644
--- a/apps/carbon/app/hooks/index.ts
+++ b/apps/carbon/app/hooks/index.ts
@@ -4,6 +4,7 @@ import { useNanoStore } from "./useNanoStore";
import { useNextItemId } from "./useNextItemId";
import { useOnboarding } from "./useOnboarding";
import { useOptimisticLocation } from "./useOptimisticLocation";
+import { usePercentFormatter } from "./usePercentFormatter";
import { usePermissions } from "./usePermissions";
import { useRealtime } from "./useRealtime";
import { useRouteData } from "./useRouteData";
@@ -18,6 +19,7 @@ export {
useNextItemId,
useOnboarding,
useOptimisticLocation,
+ usePercentFormatter,
usePermissions,
useRealtime,
useRouteData,
diff --git a/apps/carbon/app/hooks/usePercentFormatter.tsx b/apps/carbon/app/hooks/usePercentFormatter.tsx
new file mode 100644
index 0000000000..58022a0d9a
--- /dev/null
+++ b/apps/carbon/app/hooks/usePercentFormatter.tsx
@@ -0,0 +1,17 @@
+import { useLocale } from "@react-aria/i18n";
+import { useMemo } from "react";
+
+export function usePercentFormatter() {
+ const { locale } = useLocale();
+
+ const formatter = useMemo(
+ () =>
+ new Intl.NumberFormat(locale, {
+ style: "percent",
+
+ maximumFractionDigits: 2,
+ }),
+ [locale]
+ );
+ return formatter;
+}
diff --git a/apps/carbon/app/modules/production/production.service.ts b/apps/carbon/app/modules/production/production.service.ts
index 768b5985f3..33b54f6faa 100644
--- a/apps/carbon/app/modules/production/production.service.ts
+++ b/apps/carbon/app/modules/production/production.service.ts
@@ -407,6 +407,19 @@ export async function getProductionEventsPage(
};
}
+export async function getProductionEventsByOperations(
+ client: SupabaseClient<Database>,
+ jobOperationIds: string[]
+) {
+ return client
+ .from("productionEvent")
+ .select(
+ "*, jobOperation(description, jobMakeMethod(parentMaterialId, item(readableId)))"
+ )
+ .in("jobOperationId", jobOperationIds)
+ .order("startTime", { ascending: true });
+}
+
export async function getProductionQuantity(
client: SupabaseClient<Database>,
id: string
@@ -446,6 +459,31 @@ export async function getProductionQuantities(
return query;
}
+export async function getProductionDataByOperations(
+ client: SupabaseClient<Database>,
+ jobOperationIds: string[]
+) {
+ const [quantities, events] = await Promise.all([
+ client
+ .from("productionQuantity")
+ .select(
+ "*, jobOperation(description, jobMakeMethod(parentMaterialId, item(readableId)))"
+ )
+ .in("jobOperationId", jobOperationIds),
+ client
+ .from("productionEvent")
+ .select(
+ "*, jobOperation(description, jobMakeMethod(parentMaterialId, item(readableId)))"
+ )
+ .in("jobOperationId", jobOperationIds),
+ ]);
+
+ return {
+ quantities: quantities.data ?? [],
+ events: events.data ?? [],
+ };
+}
+
export async function getScrapReasonsList(
client: SupabaseClient<Database>,
companyId: string
diff --git a/apps/carbon/app/modules/production/ui/Jobs/JobEstimatesVsActuals.tsx b/apps/carbon/app/modules/production/ui/Jobs/JobEstimatesVsActuals.tsx
new file mode 100644
index 0000000000..4920b4f68c
--- /dev/null
+++ b/apps/carbon/app/modules/production/ui/Jobs/JobEstimatesVsActuals.tsx
@@ -0,0 +1,279 @@
+import {
+ Card,
+ CardAction,
+ CardContent,
+ CardHeader,
+ CardTitle,
+ cn,
+ HStack,
+ Switch,
+ Table,
+ Tbody,
+ Td,
+ Tfoot,
+ Th,
+ Thead,
+ Tooltip,
+ TooltipContent,
+ TooltipTrigger,
+ Tr,
+ useDisclosure,
+} from "@carbon/react";
+import { useParams } from "@remix-run/react";
+import type { z } from "zod";
+import { OperationStatusIcon, TimeTypeIcon } from "~/components/Icons";
+
+import { formatDurationMilliseconds } from "@carbon/utils";
+import {
+ getLocalTimeZone,
+ now,
+ parseAbsolute,
+ toZoned,
+} from "@internationalized/date";
+import { usePercentFormatter } from "~/hooks";
+import { makeDurations } from "~/utils/duration";
+import type { jobOperationValidator } from "../../production.models";
+import type {
+ JobOperation,
+ ProductionEvent,
+ ProductionQuantity,
+} from "../../types";
+
+type Operation = z.infer<typeof jobOperationValidator> & {
+ status: JobOperation["status"];
+ operationQuantity: number | null;
+};
+
+const timeTypes = ["Setup", "Labor", "Machine"] as const;
+
+const JobEstimatesVsActuals = ({
+ operations,
+ productionEvents,
+ productionQuantities,
+}: {
+ operations: Operation[];
+ productionEvents: ProductionEvent[];
+ productionQuantities: ProductionQuantity[];
+}) => {
+ const { jobId } = useParams();
+ if (!jobId) throw new Error("Could not find jobId");
+
+ const percentFormatter = usePercentFormatter();
+ const detailsDisclosure = useDisclosure();
+
+ const getEstimatedTime = (operation: Operation) => {
+ const op = makeDurations(operation);
+
+ return {
+ total: op.duration,
+ setup: op.setupDuration,
+ labor: op.laborDuration,
+ machine: op.machineDuration,
+ };
+ };
+
+ const getActualTime = (operation: Operation) => {
+ const operationEvents = productionEvents.filter(
+ (pe) => pe.jobOperationId === operation.id
+ );
+ const timeNow = now(getLocalTimeZone());
+ const actualTimes = operationEvents.reduce(
+ (acc, event) => {
+ if (event.endTime && event.type) {
+ acc[event.type.toLowerCase() as keyof typeof acc] +=
+ (event.duration ?? 0) * 1000;
+ } else if (event.startTime && event.type) {
+ const startTime = toZoned(
+ parseAbsolute(event.startTime, getLocalTimeZone()),
+ getLocalTimeZone()
+ );
+
+ const difference = timeNow.compare(startTime);
+
+ if (difference > 0) {
+ acc[event.type.toLowerCase() as keyof typeof acc] += difference;
+ }
+ }
+ return acc;
+ },
+ {
+ setup: 0,
+ labor: 0,
+ machine: 0,
+ }
+ );
+
+ return {
+ total: actualTimes.setup + actualTimes.labor + actualTimes.machine,
+ ...actualTimes,
+ };
+ };
+
+ const getCompleteQuantity = (operation: Operation) => {
+ const quantity = productionQuantities
+ .filter(
+ (pq) => pq.jobOperationId === operation.id && pq.type === "Production"
+ )
+ .reduce((acc, pq) => acc + pq.quantity, 0);
+ return quantity ?? 0;
+ };
+
+ const getScrapQuantity = (operation: Operation) => {
+ const quantity = productionQuantities
+ .filter((pq) => pq.jobOperationId === operation.id && pq.type === "Scrap")
+ .reduce((acc, pq) => acc + pq.quantity, 0);
+ return quantity ?? 0;
+ };
+
+ return (
+ <Card>
+ <HStack className="justify-between items-start">
+ <CardHeader>
+ <CardTitle>Estimates vs Actual</CardTitle>
+ </CardHeader>
+ <CardAction>
+ <div className="flex items-center space-x-2 py-2">
+ <Switch
+ variant="small"
+ checked={detailsDisclosure.isOpen}
+ onCheckedChange={detailsDisclosure.onToggle}
+ id="cost-details"
+ />
+ <label className="text-sm" htmlFor="cost-details">
+ Show Details
+ </label>
+ </div>
+ </CardAction>
+ </HStack>
+ <CardContent>
+ <Table>
+ <Thead>
+ <Tr>
+ <Th />
+ <Th>Estimated</Th>
+ <Th>Actual</Th>
+ <Th>%</Th>
+ <Th>Complete</Th>
+ <Th>Scrap</Th>
+ </Tr>
+ </Thead>
+ <Tbody>
+ {operations.map((operation) => {
+ const estimated = getEstimatedTime(operation);
+ const actual = getActualTime(operation);
+ return (
+ <>
+ <Tr key={operation.id}>
+ <Td className="border-r border-border min-w-[200px]">
+ <HStack className="w-full justify-between ">
+ <span>{operation.description}</span>
+ <Tooltip>
+ <TooltipTrigger>
+ <OperationStatusIcon status={operation.status} />
+ </TooltipTrigger>
+ <TooltipContent>{operation.status}</TooltipContent>
+ </Tooltip>
+ </HStack>
+ </Td>
+ <Td>
+ <span className="line-clamp-1">
+ {formatDurationMilliseconds(estimated.total)}
+ </span>
+ </Td>
+ <Td>
+ <span
+ className={cn(
+ "line-clamp-1",
+ actual.total > estimated.total && "text-destructive"
+ )}
+ >
+ {formatDurationMilliseconds(actual.total)}
+ </span>
+ </Td>
+ <Td>
+ <span
+ className={cn(
+ "line-clamp-1",
+ actual.total > estimated.total && "text-destructive"
+ )}
+ >
+ {percentFormatter.format(
+ actual.total / estimated.total
+ )}
+ </span>
+ </Td>
+ <Td>{`${getCompleteQuantity(operation)}/${
+ operation.operationQuantity
+ }`}</Td>
+ <Td>{getScrapQuantity(operation)}</Td>
+ </Tr>
+ {detailsDisclosure.isOpen && (
+ <>
+ {timeTypes.map((type) => {
+ if (
+ estimated[
+ type.toLowerCase() as keyof typeof estimated
+ ] === 0
+ ) {
+ return null;
+ }
+ return (
+ <Tr key={type}>
+ <Td className="border-r border-border pl-8">
+ <HStack>
+ <TimeTypeIcon type={type} />
+ <span>{type}</span>
+ </HStack>
+ </Td>
+ <Td>
+ {formatDurationMilliseconds(
+ estimated[
+ type.toLowerCase() as keyof typeof estimated
+ ]
+ )}
+ </Td>
+ <Td>
+ {formatDurationMilliseconds(
+ actual[
+ type.toLowerCase() as keyof typeof actual
+ ]
+ )}
+ </Td>
+ <Td>
+ {percentFormatter.format(
+ actual[
+ type.toLowerCase() as keyof typeof actual
+ ] /
+ estimated[
+ type.toLowerCase() as keyof typeof estimated
+ ]
+ )}
+ </Td>
+ <Td />
+ <Td />
+ </Tr>
+ );
+ })}
+ </>
+ )}
+ </>
+ );
+ })}
+ </Tbody>
+ <Tfoot>
+ {/* <Tr className="font-bold">
+ <Td className="border-r border-border" />
+ {types.map((type) => (
+ <Td key={type}>
+ <Button variant="secondary">Add</Button>
+ </Td>
+ ))}
+ </Tr> */}
+ </Tfoot>
+ </Table>
+ </CardContent>
+ </Card>
+ );
+};
+
+export default JobEstimatesVsActuals;
diff --git a/apps/carbon/app/modules/production/ui/Jobs/index.ts b/apps/carbon/app/modules/production/ui/Jobs/index.ts
index 535a53bc38..b1b9489ae2 100644
--- a/apps/carbon/app/modules/production/ui/Jobs/index.ts
+++ b/apps/carbon/app/modules/production/ui/Jobs/index.ts
@@ -2,6 +2,7 @@ import { getDeadlineIcon, getDeadlineText } from "./Deadline";
import JobBillOfMaterial from "./JobBillOfMaterial";
import JobBillOfProcess from "./JobBillOfProcess";
import JobBoMExplorer from "./JobBoMExplorer";
+import JobEstimatesVsActuals from "./JobEstimatesVsActuals";
import JobForm from "./JobForm";
import JobHeader from "./JobHeader";
import JobMaterialForm from "./JobMaterialForm";
@@ -21,6 +22,7 @@ export {
JobBillOfMaterial,
JobBillOfProcess,
JobBoMExplorer,
+ JobEstimatesVsActuals,
JobForm,
JobHeader,
JobMaterialForm,
diff --git a/apps/carbon/app/routes/x+/job+/$jobId.method.$methodId.tsx b/apps/carbon/app/routes/x+/job+/$jobId.method.$methodId.tsx
index b4c11c4d25..e877f7618f 100644
--- a/apps/carbon/app/routes/x+/job+/$jobId.method.$methodId.tsx
+++ b/apps/carbon/app/routes/x+/job+/$jobId.method.$methodId.tsx
@@ -1,18 +1,34 @@
import type { JSONContent } from "@carbon/react";
-import { VStack } from "@carbon/react";
-import { json, redirect, useLoaderData, useParams } from "@remix-run/react";
+import {
+ Card,
+ CardContent,
+ CardHeader,
+ CardTitle,
+ Spinner,
+ VStack,
+} from "@carbon/react";
+import {
+ Await,
+ defer,
+ redirect,
+ useLoaderData,
+ useParams,
+} from "@remix-run/react";
import type { LoaderFunctionArgs } from "@vercel/remix";
import { error } from "@carbon/auth";
import { requirePermissions } from "@carbon/auth/auth.server";
import { flash } from "@carbon/auth/session.server";
+import { Suspense } from "react";
import { useRouteData } from "~/hooks";
import type { Job } from "~/modules/production";
import {
getJobMaterialsByMethodId,
getJobOperationsByMethodId,
+ getProductionDataByOperations,
JobBillOfMaterial,
JobBillOfProcess,
+ JobEstimatesVsActuals,
} from "~/modules/production";
import JobBreadcrumbs from "~/modules/production/ui/Jobs/JobBreadcrumbs";
import { path } from "~/utils/path";
@@ -51,7 +67,7 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
);
}
- return json({
+ return defer({
materials:
materials?.data.map((m) => ({
...m,
@@ -70,6 +86,10 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
jobMakeMethodId: o.jobMakeMethodId ?? methodId,
workInstruction: o.workInstruction as JSONContent,
})) ?? [],
+ productionData: getProductionDataByOperations(
+ client,
+ operations?.data?.map((o) => o.id)
+ ),
});
}
@@ -80,23 +100,48 @@ export default function JobMakeMethodRoute() {
const routeData = useRouteData<{ job: Job }>(path.to.job(jobId));
const loaderData = useLoaderData<typeof loader>();
- const { materials, operations } = loaderData;
+ const { materials, operations, productionData } = loaderData;
return (
- <VStack spacing={2} className="p-2">
- <JobBreadcrumbs />
- <JobBillOfProcess
- key={`bop:${methodId}:${operations.length}:${operations[0]?.workCenterId}`}
- jobMakeMethodId={methodId}
- operations={operations}
- locationId={routeData?.job?.locationId ?? ""}
- />
- <JobBillOfMaterial
- key={`bom:${methodId}:${materials.length}`}
- jobMakeMethodId={methodId}
- materials={materials}
- operations={operations}
- />
- </VStack>
+ <div className="h-[calc(100vh-49px)] w-full items-start overflow-y-auto">
+ <VStack spacing={2} className="p-2">
+ <JobBreadcrumbs />
+ <JobBillOfProcess
+ key={`bop:${methodId}:${operations.length}:${operations[0]?.workCenterId}`}
+ jobMakeMethodId={methodId}
+ operations={operations}
+ locationId={routeData?.job?.locationId ?? ""}
+ />
+ <JobBillOfMaterial
+ key={`bom:${methodId}:${materials.length}`}
+ jobMakeMethodId={methodId}
+ materials={materials}
+ operations={operations}
+ />
+
+ <Suspense
+ fallback={
+ <Card>
+ <CardHeader>
+ <CardTitle>Estimates vs Actual</CardTitle>
+ </CardHeader>
+ <CardContent className="flex items-center justify-center min-h-[200px]">
+ <Spinner />
+ </CardContent>
+ </Card>
+ }
+ >
+ <Await resolve={productionData}>
+ {(resolvedProductionData) => (
+ <JobEstimatesVsActuals
+ operations={operations}
+ productionEvents={resolvedProductionData.events}
+ productionQuantities={resolvedProductionData.quantities}
+ />
+ )}
+ </Await>
+ </Suspense>
+ </VStack>
+ </div>
);
}
diff --git a/apps/carbon/app/utils/duration.ts b/apps/carbon/app/utils/duration.ts
index 96efa94bf3..bed7db8142 100644
--- a/apps/carbon/app/utils/duration.ts
+++ b/apps/carbon/app/utils/duration.ts
@@ -1,11 +1,11 @@
export function makeDurations<
T extends {
- setupTime: number;
- setupUnit: string;
- laborTime: number;
- laborUnit: string;
- machineTime: number;
- machineUnit: string;
+ setupTime?: number;
+ setupUnit?: string;
+ laborTime?: number;
+ laborUnit?: string;
+ machineTime?: number;
+ machineUnit?: string;
operationQuantity: number | null;
}
>(
@@ -23,54 +23,58 @@ export function makeDurations<
// Calculate setup duration
switch (operation.setupUnit) {
case "Total Hours":
- setupDuration = operation.setupTime * 3600000; // Convert hours to milliseconds
+ setupDuration = (operation.setupTime ?? 0) * 3600000; // Convert hours to milliseconds
break;
case "Total Minutes":
- setupDuration = operation.setupTime * 60000; // Convert minutes to milliseconds
+ setupDuration = (operation.setupTime ?? 0) * 60000; // Convert minutes to milliseconds
break;
case "Hours/Piece":
setupDuration =
- operation.setupTime * (operation.operationQuantity ?? 0) * 3600000;
+ (operation.setupTime ?? 0) *
+ (operation.operationQuantity ?? 0) *
+ 3600000;
break;
case "Hours/100 Pieces":
setupDuration =
- (operation.setupTime / 100) *
+ ((operation.setupTime ?? 0) / 100) *
(operation.operationQuantity ?? 0) *
3600000;
break;
case "Hours/1000 Pieces":
setupDuration =
- (operation.setupTime / 1000) *
+ ((operation.setupTime ?? 0) / 1000) *
(operation.operationQuantity ?? 0) *
3600000;
break;
case "Minutes/Piece":
setupDuration =
- operation.setupTime * (operation.operationQuantity ?? 0) * 60000;
+ (operation.setupTime ?? 0) * (operation.operationQuantity ?? 0) * 60000;
break;
case "Minutes/100 Pieces":
setupDuration =
- (operation.setupTime / 100) *
+ ((operation.setupTime ?? 0) / 100) *
(operation.operationQuantity ?? 0) *
60000;
break;
case "Minutes/1000 Pieces":
setupDuration =
- (operation.setupTime / 1000) *
+ ((operation.setupTime ?? 0) / 1000) *
(operation.operationQuantity ?? 0) *
60000;
break;
case "Pieces/Hour":
setupDuration =
- ((operation.operationQuantity ?? 0) / operation.setupTime) * 3600000;
+ ((operation.operationQuantity ?? 0) / (operation.setupTime ?? 0)) *
+ 3600000;
break;
case "Pieces/Minute":
setupDuration =
- ((operation.operationQuantity ?? 0) / operation.setupTime) * 60000;
+ ((operation.operationQuantity ?? 0) / (operation.setupTime ?? 0)) *
+ 60000;
break;
case "Seconds/Piece":
setupDuration =
- operation.setupTime * (operation.operationQuantity ?? 0) * 1000;
+ (operation.setupTime ?? 0) * (operation.operationQuantity ?? 0) * 1000;
break;
}
@@ -78,47 +82,51 @@ export function makeDurations<
switch (operation.laborUnit) {
case "Hours/Piece":
laborDuration =
- operation.laborTime * (operation.operationQuantity ?? 0) * 3600000;
+ (operation.laborTime ?? 0) *
+ (operation.operationQuantity ?? 0) *
+ 3600000;
break;
case "Hours/100 Pieces":
laborDuration =
- (operation.laborTime / 100) *
+ ((operation.laborTime ?? 0) / 100) *
(operation.operationQuantity ?? 0) *
3600000;
break;
case "Hours/1000 Pieces":
laborDuration =
- (operation.laborTime / 1000) *
+ ((operation.laborTime ?? 0) / 1000) *
(operation.operationQuantity ?? 0) *
3600000;
break;
case "Minutes/Piece":
laborDuration =
- operation.laborTime * (operation.operationQuantity ?? 0) * 60000;
+ (operation.laborTime ?? 0) * (operation.operationQuantity ?? 0) * 60000;
break;
case "Minutes/100 Pieces":
laborDuration =
- (operation.laborTime / 100) *
+ ((operation.laborTime ?? 0) / 100) *
(operation.operationQuantity ?? 0) *
60000;
break;
case "Minutes/1000 Pieces":
laborDuration =
- (operation.laborTime / 1000) *
+ ((operation.laborTime ?? 0) / 1000) *
(operation.operationQuantity ?? 0) *
60000;
break;
case "Pieces/Hour":
laborDuration =
- ((operation.operationQuantity ?? 0) / operation.laborTime) * 3600000;
+ ((operation.operationQuantity ?? 0) / (operation.laborTime ?? 0)) *
+ 3600000;
break;
case "Pieces/Minute":
laborDuration =
- ((operation.operationQuantity ?? 0) / operation.laborTime) * 60000;
+ ((operation.operationQuantity ?? 0) / (operation.laborTime ?? 0)) *
+ 60000;
break;
case "Seconds/Piece":
laborDuration =
- operation.laborTime * (operation.operationQuantity ?? 0) * 1000;
+ (operation.laborTime ?? 0) * (operation.operationQuantity ?? 0) * 1000;
break;
}
@@ -126,47 +134,55 @@ export function makeDurations<
switch (operation.machineUnit) {
case "Hours/Piece":
machineDuration =
- operation.machineTime * (operation.operationQuantity ?? 0) * 3600000;
+ (operation.machineTime ?? 0) *
+ (operation.operationQuantity ?? 0) *
+ 3600000;
break;
case "Hours/100 Pieces":
machineDuration =
- (operation.machineTime / 100) *
+ ((operation.machineTime ?? 0) / 100) *
(operation.operationQuantity ?? 0) *
3600000;
break;
case "Hours/1000 Pieces":
machineDuration =
- (operation.machineTime / 1000) *
+ ((operation.machineTime ?? 0) / 1000) *
(operation.operationQuantity ?? 0) *
3600000;
break;
case "Minutes/Piece":
machineDuration =
- operation.machineTime * (operation.operationQuantity ?? 0) * 60000;
+ (operation.machineTime ?? 0) *
+ (operation.operationQuantity ?? 0) *
+ 60000;
break;
case "Minutes/100 Pieces":
machineDuration =
- (operation.machineTime / 100) *
+ ((operation.machineTime ?? 0) / 100) *
(operation.operationQuantity ?? 0) *
60000;
break;
case "Minutes/1000 Pieces":
machineDuration =
- (operation.machineTime / 1000) *
+ ((operation.machineTime ?? 0) / 1000) *
(operation.operationQuantity ?? 0) *
60000;
break;
case "Pieces/Hour":
machineDuration =
- ((operation.operationQuantity ?? 0) / operation.machineTime) * 3600000;
+ ((operation.operationQuantity ?? 0) / (operation.machineTime ?? 0)) *
+ 3600000;
break;
case "Pieces/Minute":
machineDuration =
- ((operation.operationQuantity ?? 0) / operation.machineTime) * 60000;
+ ((operation.operationQuantity ?? 0) / (operation.machineTime ?? 0)) *
+ 60000;
break;
case "Seconds/Piece":
machineDuration =
- operation.machineTime * (operation.operationQuantity ?? 0) * 1000;
+ (operation.machineTime ?? 0) *
+ (operation.operationQuantity ?? 0) *
+ 1000;
break;
}
diff --git a/apps/mes/app/utils/durations.ts b/apps/mes/app/utils/durations.ts
index 18fe214e73..7f0308ec0c 100644
--- a/apps/mes/app/utils/durations.ts
+++ b/apps/mes/app/utils/durations.ts
@@ -1,13 +1,21 @@
-import type {
- BaseOperation,
- BaseOperationWithDetails,
- Operation,
- OperationWithDetails,
-} from "~/services/jobs.service";
-
export function makeDurations<
- T extends BaseOperation | BaseOperationWithDetails
->(operation: T): T extends BaseOperation ? Operation : OperationWithDetails {
+ T extends {
+ setupTime?: number;
+ setupUnit: string;
+ laborTime?: number;
+ laborUnit: string;
+ machineTime?: number;
+ machineUnit: string;
+ operationQuantity: number | null;
+ }
+>(
+ operation: T
+): T & {
+ duration: number;
+ setupDuration: number;
+ laborDuration: number;
+ machineDuration: number;
+} {
let setupDuration = 0;
let laborDuration = 0;
let machineDuration = 0;
@@ -15,44 +23,58 @@ export function makeDurations<
// Calculate setup duration
switch (operation.setupUnit) {
case "Total Hours":
- setupDuration = operation.setupTime * 3600000; // Convert hours to milliseconds
+ setupDuration = (operation.setupTime ?? 0) * 3600000; // Convert hours to milliseconds
break;
case "Total Minutes":
- setupDuration = operation.setupTime * 60000; // Convert minutes to milliseconds
+ setupDuration = (operation.setupTime ?? 0) * 60000; // Convert minutes to milliseconds
break;
case "Hours/Piece":
setupDuration =
- operation.setupTime * operation.operationQuantity * 3600000;
+ (operation.setupTime ?? 0) *
+ (operation.operationQuantity ?? 0) *
+ 3600000;
break;
case "Hours/100 Pieces":
setupDuration =
- (operation.setupTime / 100) * operation.operationQuantity * 3600000;
+ ((operation.setupTime ?? 0) / 100) *
+ (operation.operationQuantity ?? 0) *
+ 3600000;
break;
case "Hours/1000 Pieces":
setupDuration =
- (operation.setupTime / 1000) * operation.operationQuantity * 3600000;
+ ((operation.setupTime ?? 0) / 1000) *
+ (operation.operationQuantity ?? 0) *
+ 3600000;
break;
case "Minutes/Piece":
- setupDuration = operation.setupTime * operation.operationQuantity * 60000;
+ setupDuration =
+ (operation.setupTime ?? 0) * (operation.operationQuantity ?? 0) * 60000;
break;
case "Minutes/100 Pieces":
setupDuration =
- (operation.setupTime / 100) * operation.operationQuantity * 60000;
+ ((operation.setupTime ?? 0) / 100) *
+ (operation.operationQuantity ?? 0) *
+ 60000;
break;
case "Minutes/1000 Pieces":
setupDuration =
- (operation.setupTime / 1000) * operation.operationQuantity * 60000;
+ ((operation.setupTime ?? 0) / 1000) *
+ (operation.operationQuantity ?? 0) *
+ 60000;
break;
case "Pieces/Hour":
setupDuration =
- (operation.operationQuantity / operation.setupTime) * 3600000;
+ ((operation.operationQuantity ?? 0) / (operation.setupTime ?? 0)) *
+ 3600000;
break;
case "Pieces/Minute":
setupDuration =
- (operation.operationQuantity / operation.setupTime) * 60000;
+ ((operation.operationQuantity ?? 0) / (operation.setupTime ?? 0)) *
+ 60000;
break;
case "Seconds/Piece":
- setupDuration = operation.setupTime * operation.operationQuantity * 1000;
+ setupDuration =
+ (operation.setupTime ?? 0) * (operation.operationQuantity ?? 0) * 1000;
break;
}
@@ -60,37 +82,51 @@ export function makeDurations<
switch (operation.laborUnit) {
case "Hours/Piece":
laborDuration =
- operation.laborTime * operation.operationQuantity * 3600000;
+ (operation.laborTime ?? 0) *
+ (operation.operationQuantity ?? 0) *
+ 3600000;
break;
case "Hours/100 Pieces":
laborDuration =
- (operation.laborTime / 100) * operation.operationQuantity * 3600000;
+ ((operation.laborTime ?? 0) / 100) *
+ (operation.operationQuantity ?? 0) *
+ 3600000;
break;
case "Hours/1000 Pieces":
laborDuration =
- (operation.laborTime / 1000) * operation.operationQuantity * 3600000;
+ ((operation.laborTime ?? 0) / 1000) *
+ (operation.operationQuantity ?? 0) *
+ 3600000;
break;
case "Minutes/Piece":
- laborDuration = operation.laborTime * operation.operationQuantity * 60000;
+ laborDuration =
+ (operation.laborTime ?? 0) * (operation.operationQuantity ?? 0) * 60000;
break;
case "Minutes/100 Pieces":
laborDuration =
- (operation.laborTime / 100) * operation.operationQuantity * 60000;
+ ((operation.laborTime ?? 0) / 100) *
+ (operation.operationQuantity ?? 0) *
+ 60000;
break;
case "Minutes/1000 Pieces":
laborDuration =
- (operation.laborTime / 1000) * operation.operationQuantity * 60000;
+ ((operation.laborTime ?? 0) / 1000) *
+ (operation.operationQuantity ?? 0) *
+ 60000;
break;
case "Pieces/Hour":
laborDuration =
- (operation.operationQuantity / operation.laborTime) * 3600000;
+ ((operation.operationQuantity ?? 0) / (operation.laborTime ?? 0)) *
+ 3600000;
break;
case "Pieces/Minute":
laborDuration =
- (operation.operationQuantity / operation.laborTime) * 60000;
+ ((operation.operationQuantity ?? 0) / (operation.laborTime ?? 0)) *
+ 60000;
break;
case "Seconds/Piece":
- laborDuration = operation.laborTime * operation.operationQuantity * 1000;
+ laborDuration =
+ (operation.laborTime ?? 0) * (operation.operationQuantity ?? 0) * 1000;
break;
}
@@ -98,39 +134,55 @@ export function makeDurations<
switch (operation.machineUnit) {
case "Hours/Piece":
machineDuration =
- operation.machineTime * operation.operationQuantity * 3600000;
+ (operation.machineTime ?? 0) *
+ (operation.operationQuantity ?? 0) *
+ 3600000;
break;
case "Hours/100 Pieces":
machineDuration =
- (operation.machineTime / 100) * operation.operationQuantity * 3600000;
+ ((operation.machineTime ?? 0) / 100) *
+ (operation.operationQuantity ?? 0) *
+ 3600000;
break;
case "Hours/1000 Pieces":
machineDuration =
- (operation.machineTime / 1000) * operation.operationQuantity * 3600000;
+ ((operation.machineTime ?? 0) / 1000) *
+ (operation.operationQuantity ?? 0) *
+ 3600000;
break;
case "Minutes/Piece":
machineDuration =
- operation.machineTime * operation.operationQuantity * 60000;
+ (operation.machineTime ?? 0) *
+ (operation.operationQuantity ?? 0) *
+ 60000;
break;
case "Minutes/100 Pieces":
machineDuration =
- (operation.machineTime / 100) * operation.operationQuantity * 60000;
+ ((operation.machineTime ?? 0) / 100) *
+ (operation.operationQuantity ?? 0) *
+ 60000;
break;
case "Minutes/1000 Pieces":
machineDuration =
- (operation.machineTime / 1000) * operation.operationQuantity * 60000;
+ ((operation.machineTime ?? 0) / 1000) *
+ (operation.operationQuantity ?? 0) *
+ 60000;
break;
case "Pieces/Hour":
machineDuration =
- (operation.operationQuantity / operation.machineTime) * 3600000;
+ ((operation.operationQuantity ?? 0) / (operation.machineTime ?? 0)) *
+ 3600000;
break;
case "Pieces/Minute":
machineDuration =
- (operation.operationQuantity / operation.machineTime) * 60000;
+ ((operation.operationQuantity ?? 0) / (operation.machineTime ?? 0)) *
+ 60000;
break;
case "Seconds/Piece":
machineDuration =
- operation.machineTime * operation.operationQuantity * 1000;
+ (operation.machineTime ?? 0) *
+ (operation.operationQuantity ?? 0) *
+ 1000;
break;
}
@@ -143,5 +195,5 @@ export function makeDurations<
setupDuration,
laborDuration,
machineDuration,
- } as T extends BaseOperation ? Operation : OperationWithDetails;
+ };
}