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: partial shipments from jobs

Brad Barbincaf5e8774792
diff --git a/apps/erp/app/modules/production/ui/Jobs/JobStatus.tsx b/apps/erp/app/modules/production/ui/Jobs/JobStatus.tsx
index cc4944b673..fdad53d01a 100644
--- a/apps/erp/app/modules/production/ui/Jobs/JobStatus.tsx
+++ b/apps/erp/app/modules/production/ui/Jobs/JobStatus.tsx
@@ -12,6 +12,7 @@ function JobStatus({ status }: JobStatusProps) {
case "Planned":
return <Status color="yellow">{status}</Status>;
case "Ready":
+ return <Status color="blue">Released</Status>; // TODO: update this properly
case "In Progress":
return <Status color="blue">{status}</Status>;
case "Paused":
diff --git a/apps/erp/app/modules/production/ui/Jobs/JobsTable.tsx b/apps/erp/app/modules/production/ui/Jobs/JobsTable.tsx
index 2023d36e90..8f2b1176ec 100644
--- a/apps/erp/app/modules/production/ui/Jobs/JobsTable.tsx
+++ b/apps/erp/app/modules/production/ui/Jobs/JobsTable.tsx
@@ -9,6 +9,7 @@ import {
HStack,
MenuIcon,
MenuItem,
+ Progress,
toast,
useDisclosure,
VStack,
@@ -233,7 +234,22 @@ const JobsTable = memo(({ data, count, tags }: JobsTableProps) => {
{
accessorKey: "quantity",
header: "Quantity",
- cell: (item) => item.getValue<number>(),
+ cell: ({ row }) => {
+ if (row.original.status === "Ready") {
+ return (
+ <Progress
+ value={
+ ((row.original.quantityComplete ?? 0) /
+ (row.original.quantity ?? 0)) *
+ 100
+ }
+ numerator={(row.original.quantityComplete ?? 0).toString()}
+ denominator={(row.original.quantity ?? 0).toString()}
+ />
+ );
+ }
+ return row.original.quantity;
+ },
meta: {
icon: <LuHash />,
},
diff --git a/apps/erp/app/modules/sales/ui/SalesOrder/SalesOrderLineJobs.tsx b/apps/erp/app/modules/sales/ui/SalesOrder/SalesOrderLineJobs.tsx
index a1087cf095..1abfc8a64b 100644
--- a/apps/erp/app/modules/sales/ui/SalesOrder/SalesOrderLineJobs.tsx
+++ b/apps/erp/app/modules/sales/ui/SalesOrder/SalesOrderLineJobs.tsx
@@ -318,7 +318,7 @@ export function SalesOrderJobItem({ job }: { job: SalesOrderJob }) {
<div>
<label className="text-xs text-muted-foreground">Shipped</label>
<p className="text-sm">
- {job.quantityShipped ?? 0}/{job.quantityComplete ?? 0}
+ {job.quantityShipped ?? 0}/{job.productionQuantity ?? 0}
</p>
</div>
</HStack>
diff --git a/packages/database/supabase/functions/create/index.ts b/packages/database/supabase/functions/create/index.ts
index f9d71e5758..ef3fd07d0c 100644
--- a/packages/database/supabase/functions/create/index.ts
+++ b/packages/database/supabase/functions/create/index.ts
@@ -2090,18 +2090,17 @@ serve(async (req: Request) => {
[]) {
if (!salesOrderLine.itemId) return;
- const quantityAvailable = isSerial
- ? Math.min(job.quantityComplete - job.quantityShipped, 0)
- : job.quantity - job.quantityShipped;
+ const quantityToShip =
+ (job.quantityComplete ?? 0) - (job.quantityShipped ?? 0);
- if (!isSerial || (isSerial && quantityAvailable > 0)) {
+ if (!isSerial || (isSerial && quantityToShip > 0)) {
const fulfillment = await trx
.insertInto("fulfillment")
.values({
salesOrderLineId: salesOrderLine.id,
type: "Job",
jobId: job.id,
- quantity: quantityAvailable,
+ quantity: quantityToShip,
companyId: companyId,
createdBy: userId,
})
@@ -2111,7 +2110,7 @@ serve(async (req: Request) => {
const fulfillmentId = fulfillment?.[0]?.id;
const shippingAndTaxUnitCost =
- (salesOrderLine.shippingCost / quantityAvailable +
+ (salesOrderLine.shippingCost / quantityToShip +
(salesOrderLine.unitPrice ?? 0)) *
(1 + salesOrderLine.taxPercent);
@@ -2127,7 +2126,7 @@ serve(async (req: Request) => {
outstandingQuantity:
salesOrderLine.quantityToSend ??
salesOrderLine.saleQuantity,
- shippedQuantity: quantityAvailable,
+ shippedQuantity: quantityToShip,
requiresSerialTracking: isSerial,
requiresBatchTracking: isBatch,
unitPrice: shippingAndTaxUnitCost,
@@ -2376,18 +2375,17 @@ serve(async (req: Request) => {
if (salesOrderLine.data.methodType === "Make") {
for await (const job of jobs.data ?? []) {
if (!salesOrderLine.data.itemId) return;
- const quantityAvailable = isSerial
- ? Math.min(job.quantityComplete - job.quantityShipped, 0)
- : job.quantity - job.quantityReceivedToInventory;
+ const quantityToShip =
+ (job.quantityComplete ?? 0) - (job.quantityShipped ?? 0);
- if (!isSerial || (isSerial && quantityAvailable > 0)) {
+ if (!isSerial || (isSerial && quantityToShip > 0)) {
const fulfillment = await trx
.insertInto("fulfillment")
.values({
salesOrderLineId: salesOrderLineId,
type: "Job",
jobId: job.id,
- quantity: quantityAvailable,
+ quantity: quantityToShip,
companyId: companyId,
createdBy: userId,
})
@@ -2397,7 +2395,7 @@ serve(async (req: Request) => {
const fulfillmentId = fulfillment?.[0]?.id;
const shippingAndTaxUnitCost =
- (salesOrderLine.data.shippingCost / quantityAvailable +
+ (salesOrderLine.data.shippingCost / quantityToShip +
(salesOrderLine.data.unitPrice ?? 0)) *
(1 + salesOrderLine.data.taxPercent);
@@ -2409,9 +2407,9 @@ serve(async (req: Request) => {
companyId: companyId,
fulfillmentId,
itemId: salesOrderLine.data.itemId,
- orderQuantity: quantityAvailable,
- outstandingQuantity: quantityAvailable,
- shippedQuantity: quantityAvailable,
+ orderQuantity: job.productionQuantity ?? 0,
+ outstandingQuantity: job.productionQuantity ?? 0,
+ shippedQuantity: quantityToShip,
requiresSerialTracking: isSerial,
requiresBatchTracking: isBatch,
unitPrice: shippingAndTaxUnitCost,
diff --git a/packages/database/supabase/functions/post-shipment/index.ts b/packages/database/supabase/functions/post-shipment/index.ts
index 14e8178b45..e6df75dfaf 100644
--- a/packages/database/supabase/functions/post-shipment/index.ts
+++ b/packages/database/supabase/functions/post-shipment/index.ts
@@ -202,7 +202,10 @@ serve(async (req: Request) => {
const newQuantityComplete =
currentJob?.status === "Completed"
? currentJob?.quantityComplete
- : (currentJob?.quantityComplete ?? 0) + shippedQuantity;
+ : Math.max(
+ currentJob?.quantityComplete ?? 0,
+ shippedQuantity
+ );
const newStatus =
currentQuantityShipped + shippedQuantity >=
(currentJob?.quantity ?? 0)
@@ -230,7 +233,10 @@ serve(async (req: Request) => {
const newQuantityComplete =
currentJob?.status === "Completed"
? currentJob?.quantityComplete
- : (currentJob?.quantityComplete ?? 0) + shippedQuantity;
+ : Math.max(
+ currentJob?.quantityComplete ?? 0,
+ shippedQuantity
+ );
const newStatus =
currentQuantityShipped + shippedQuantity >=
(currentJob?.quantity ?? 0)
@@ -1431,8 +1437,8 @@ serve(async (req: Request) => {
currentQuantityShipped - shippedQuantity
);
const newQuantityComplete = Math.max(
- 0,
- (currentJob?.quantityComplete ?? 0) - shippedQuantity
+ currentJob?.quantityComplete ?? 0,
+ shippedQuantity
);
// Update status based on new quantities
diff --git a/packages/database/supabase/migrations/20251010124503_auto-update-quantity.sql b/packages/database/supabase/migrations/20251010124503_auto-update-quantity.sql
new file mode 100644
index 0000000000..43e93db7a9
--- /dev/null
+++ b/packages/database/supabase/migrations/20251010124503_auto-update-quantity.sql
@@ -0,0 +1,65 @@
+CREATE INDEX "idx_jobOperationDependency_dependsOnId" ON "jobOperationDependency" ("dependsOnId");
+
+CREATE OR REPLACE FUNCTION update_job_operation_quantities()
+RETURNS TRIGGER
+SECURITY DEFINER
+SET search_path = public
+LANGUAGE plpgsql
+AS $$
+DECLARE
+ job_id TEXT;
+BEGIN
+ IF TG_OP = 'INSERT' THEN
+ -- Increment quantities on insert
+ UPDATE "jobOperation"
+ SET
+ "quantityComplete" = "quantityComplete" +
+ CASE WHEN NEW."type" = 'Production' THEN NEW.quantity ELSE 0 END,
+ "quantityReworked" = "quantityReworked" +
+ CASE WHEN NEW."type" = 'Rework' THEN NEW.quantity ELSE 0 END,
+ "quantityScrapped" = "quantityScrapped" +
+ CASE WHEN NEW."type" = 'Scrap' THEN NEW.quantity ELSE 0 END
+ WHERE id = NEW."jobOperationId";
+
+
+ ELSIF TG_OP = 'UPDATE' THEN
+ -- Adjust quantities on update by removing old values and adding new values
+ UPDATE "jobOperation"
+ SET
+ "quantityComplete" = "quantityComplete"
+ - CASE WHEN OLD."type" = 'Production' THEN OLD.quantity ELSE 0 END
+ + CASE WHEN NEW."type" = 'Production' THEN NEW.quantity ELSE 0 END,
+ "quantityReworked" = "quantityReworked"
+ - CASE WHEN OLD."type" = 'Rework' THEN OLD.quantity ELSE 0 END
+ + CASE WHEN NEW."type" = 'Rework' THEN NEW.quantity ELSE 0 END,
+ "quantityScrapped" = "quantityScrapped"
+ - CASE WHEN OLD."type" = 'Scrap' THEN OLD.quantity ELSE 0 END
+ + CASE WHEN NEW."type" = 'Scrap' THEN NEW.quantity ELSE 0 END
+ WHERE id = NEW."jobOperationId";
+ ELSIF TG_OP = 'DELETE' THEN
+ -- Decrement quantities on delete
+ UPDATE "jobOperation"
+ SET
+ "quantityComplete" = "quantityComplete" -
+ CASE WHEN OLD."type" = 'Production' THEN OLD.quantity ELSE 0 END,
+ "quantityReworked" = "quantityReworked" -
+ CASE WHEN OLD."type" = 'Rework' THEN OLD.quantity ELSE 0 END,
+ "quantityScrapped" = "quantityScrapped" -
+ CASE WHEN OLD."type" = 'Scrap' THEN OLD.quantity ELSE 0 END
+ WHERE id = OLD."jobOperationId";
+ END IF;
+
+ IF is_last_job_operation(NEW.id) AND NEW."type" = 'Production' THEN
+ SELECT "jobId" INTO job_id FROM "jobOperation" WHERE id = NEW."jobOperationId";
+ UPDATE "job"
+ SET "quantityComplete" = (
+ SELECT COALESCE(SUM(pq.quantity), 0)
+ FROM "productionQuantity" pq
+ JOIN "jobOperation" jo ON jo.id = pq."jobOperationId"
+ WHERE jo."jobId" = job_id AND pq."type" = 'Production'
+ )
+ WHERE id = job_id;
+ END IF;
+ RETURN NULL;
+END;
+$$;