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.

fix: Order Line item bugs and status inference (#410)

Sid480adae40ac4
diff --git a/apps/erp/app/modules/inventory/ui/Shipments/ShipmentLines.tsx b/apps/erp/app/modules/inventory/ui/Shipments/ShipmentLines.tsx
index dc2221ec04..eed63dcc11 100644
--- a/apps/erp/app/modules/inventory/ui/Shipments/ShipmentLines.tsx
+++ b/apps/erp/app/modules/inventory/ui/Shipments/ShipmentLines.tsx
@@ -333,13 +333,7 @@ function ShipmentLineItem({
(line.shippedQuantity || 0) > (line.fulfillment?.job?.quantity || 0);
return (
- <div
- className={cn(
- "flex flex-col border-b p-6 gap-6 relative",
-
- className
- )}
- >
+ <div className={cn("flex flex-col border-b p-6 gap-6 relative", className)}>
<div className="absolute top-6 right-6">
{line.fulfillment?.type === "Job" ? (
<div className="flex flex-col items-end gap-0">
@@ -390,10 +384,10 @@ function ShipmentLineItem({
<VStack spacing={0} className="max-w-[380px] w-full">
<div className="w-full overflow-hidden">
<span className="text-sm font-medium truncate block w-full">
- {item?.name}
+ {item?.readableIdWithRevision}
</span>
<span className="text-xs text-muted-foreground truncate block w-full">
- {item?.readableIdWithRevision}
+ {item?.name}
</span>
</div>
<div className="mt-2">
diff --git a/apps/erp/app/modules/sales/ui/SalesOrder/SalesOrderSummary.tsx b/apps/erp/app/modules/sales/ui/SalesOrder/SalesOrderSummary.tsx
index 27b6453332..094be7bbe4 100644
--- a/apps/erp/app/modules/sales/ui/SalesOrder/SalesOrderSummary.tsx
+++ b/apps/erp/app/modules/sales/ui/SalesOrder/SalesOrderSummary.tsx
@@ -20,7 +20,7 @@ import {
useDisclosure,
VStack
} from "@carbon/react";
-import { formatDate } from "@carbon/utils";
+import { formatDate, getSalesOrderJobStatus } from "@carbon/utils";
import {
getLocalTimeZone,
isSameDay,
@@ -295,47 +295,12 @@ function LineItems({
{lines.map((line) => {
if (!line.id) return null;
- const jobs = (salesOrder?.jobs?.filter(
- (j) => (j as unknown as SalesOrderJob).salesOrderLineId === line.id
- ) ?? []) as SalesOrderJob[];
-
const isMade = line.methodType === "Make";
- const hasEnoughJobsToCoverQuantity =
- jobs.reduce((acc, job) => acc + job.productionQuantity, 0) >=
- (line.saleQuantity ?? 0);
-
- const hasEnoughCompletedToCoverQuantity =
- jobs.reduce((acc, job) => acc + job.quantityComplete, 0) >=
- (line.saleQuantity ?? 0);
-
- const hasAnyQuantityReleased =
- jobs.reduce((acc, job) => {
- if (job.status !== "Planned" && job.status !== "Draft") {
- return acc + job.productionQuantity;
- }
- return acc;
- }, 0) > 0;
-
- const jobVariant: "red" | "orange" | "green" =
- hasEnoughJobsToCoverQuantity && hasEnoughCompletedToCoverQuantity
- ? "green"
- : hasEnoughJobsToCoverQuantity
- ? "orange"
- : "red";
-
- const jobLabel:
- | "Requires Jobs"
- | "In Progress"
- | "Complete"
- | "Planned" =
- hasEnoughJobsToCoverQuantity && hasEnoughCompletedToCoverQuantity
- ? "Complete"
- : hasEnoughJobsToCoverQuantity
- ? hasAnyQuantityReleased
- ? "In Progress"
- : "Planned"
- : "Requires Jobs";
+ const { jobLabel, jobVariant, jobs } = getSalesOrderJobStatus(
+ salesOrder?.jobs as SalesOrderJob[],
+ line as any
+ );
return (
<motion.div
diff --git a/packages/utils/src/status.ts b/packages/utils/src/status.ts
index 576ba5ad95..8ecb79e869 100644
--- a/packages/utils/src/status.ts
+++ b/packages/utils/src/status.ts
@@ -2,7 +2,24 @@ import type { Database } from "@carbon/database";
type SalesOrderLine = Pick<
Database["public"]["Tables"]["salesOrderLine"]["Row"],
- "salesOrderLineType" | "invoicedComplete" | "sentComplete"
+ | "salesOrderLineType"
+ | "invoicedComplete"
+ | "sentComplete"
+ | "id"
+ | "methodType"
+ | "saleQuantity"
+ | "quantitySent"
+>;
+
+type SalesOrderJob = Pick<
+ Database["public"]["Tables"]["job"]["Row"],
+ | "salesOrderLineId"
+ | "productionQuantity"
+ | "quantityComplete"
+ | "status"
+ | "id"
+ | "jobId"
+ | "dueDate"
>;
export const getSalesOrderStatus = (
@@ -66,3 +83,66 @@ export const getPurchaseOrderStatus = (
return { status, allInvoices, allLinesReceived };
};
+
+export const getSalesOrderJobStatus = (
+ jobs: SalesOrderJob[],
+ line: SalesOrderLine
+) => {
+ const filteredJobs = jobs.filter((j) => j.salesOrderLineId === line.id);
+ const isMade = line.methodType === "Make";
+ const saleQuantity = line.saleQuantity ?? 0;
+
+ const totalProduction = filteredJobs.reduce(
+ (acc, job) => acc + job.productionQuantity,
+ 0
+ );
+ const totalCompleted = filteredJobs.reduce(
+ (acc, job) => acc + job.quantityComplete,
+ 0
+ );
+ const totalReleased = filteredJobs.reduce((acc, job) => {
+ if (job.status !== "Planned" && job.status !== "Draft") {
+ return acc + job.productionQuantity;
+ }
+ return acc;
+ }, 0);
+
+ const hasEnoughJobsToCoverQuantity = totalProduction >= saleQuantity;
+ const hasEnoughCompletedToCoverQuantity = totalCompleted >= saleQuantity;
+ const hasAnyQuantityReleased = totalReleased > 0;
+ const isCompleted =
+ hasEnoughJobsToCoverQuantity && hasEnoughCompletedToCoverQuantity;
+ const isPartiallyShipped =
+ line.quantitySent > 0 && line.quantitySent < saleQuantity;
+
+ let jobVariant: "green" | "red" | "orange";
+ let jobLabel:
+ | "Completed"
+ | "Requires Jobs"
+ | "In Progress"
+ | "Planned"
+ | "Shipped"
+ | "Partially Shipped";
+
+ if (isCompleted && line.sentComplete) {
+ jobLabel = "Shipped";
+ jobVariant = "green";
+ } else if (isCompleted) {
+ jobLabel = "Completed";
+ jobVariant = "green";
+ } else if (isPartiallyShipped) {
+ jobLabel = "Partially Shipped";
+ jobVariant = "orange";
+ } else if (isMade && filteredJobs.length === 0) {
+ jobLabel = "Requires Jobs";
+ jobVariant = "red";
+ } else if (hasAnyQuantityReleased) {
+ jobLabel = "In Progress";
+ jobVariant = "orange";
+ } else {
+ jobLabel = "Planned";
+ jobVariant = "orange";
+ }
+
+ return { jobVariant, jobLabel, jobs: filteredJobs };
+};