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.
TypeScript ai-agentsbill-of-materialserpinventory-managementleanmanufacturingmanufacturing-execution-systemmcp-servermesmrpopen-source-erppostgresqlproduction-planningqmsquality-management-systemreact-routerself-hostedsupabasetraceabilitytypescript
fix(database): sync job quantityComplete on production updates (#717)
Siddharth Gaikwadcfcedf1c49e3
diff --git a/packages/database/supabase/migrations/20260428000000_sync-job-quantity-complete-on-production.sql b/packages/database/supabase/migrations/20260428000000_sync-job-quantity-complete-on-production.sqlnew file mode 100644index 0000000000..3ab3891609--- /dev/null+++ b/packages/database/supabase/migrations/20260428000000_sync-job-quantity-complete-on-production.sql@@ -0,0 +1,106 @@+-- Fix: job.quantityComplete not updated until job is fully complete.+--+-- Previously, job.quantityComplete was only written when the very last operation+-- finished (inside sync_finish_job_operation). So a job with 24/25 operations done+-- would still show quantityComplete = 0 on the job row when queried via API.+--+-- Fix: after every productionQuantity INSERT/UPDATE/DELETE, sync job.quantityComplete+-- from the last top-level operation's quantityComplete. This keeps in-progress API+-- values current while preventing intermediate top-level operations from driving+-- job-level completion.+-- Only applies to jobs that are not yet Completed or Cancelled.+CREATE OR REPLACE FUNCTION sync_update_job_operation_quantities(+ p_table TEXT,+ p_operation TEXT,+ p_new JSONB,+ p_old JSONB+)+RETURNS VOID+LANGUAGE plpgsql+SECURITY DEFINER+SET search_path = public+AS $$+DECLARE+ v_job_operation_id TEXT;+ v_job_id TEXT;+ v_is_last_top_level_operation BOOLEAN := FALSE;+BEGIN+ IF p_operation = 'INSERT' THEN+ v_job_operation_id := p_new->>'jobOperationId';++ UPDATE "jobOperation"+ SET+ "quantityComplete" = "quantityComplete" ++ CASE WHEN (p_new->>'type') = 'Production' THEN (p_new->>'quantity')::numeric ELSE 0 END,+ "quantityReworked" = "quantityReworked" ++ CASE WHEN (p_new->>'type') = 'Rework' THEN (p_new->>'quantity')::numeric ELSE 0 END,+ "quantityScrapped" = "quantityScrapped" ++ CASE WHEN (p_new->>'type') = 'Scrap' THEN (p_new->>'quantity')::numeric ELSE 0 END+ WHERE id = v_job_operation_id;++ ELSIF p_operation = 'UPDATE' THEN+ v_job_operation_id := p_new->>'jobOperationId';++ UPDATE "jobOperation"+ SET+ "quantityComplete" = "quantityComplete"+ - CASE WHEN (p_old->>'type') = 'Production' THEN (p_old->>'quantity')::numeric ELSE 0 END+ + CASE WHEN (p_new->>'type') = 'Production' THEN (p_new->>'quantity')::numeric ELSE 0 END,+ "quantityReworked" = "quantityReworked"+ - CASE WHEN (p_old->>'type') = 'Rework' THEN (p_old->>'quantity')::numeric ELSE 0 END+ + CASE WHEN (p_new->>'type') = 'Rework' THEN (p_new->>'quantity')::numeric ELSE 0 END,+ "quantityScrapped" = "quantityScrapped"+ - CASE WHEN (p_old->>'type') = 'Scrap' THEN (p_old->>'quantity')::numeric ELSE 0 END+ + CASE WHEN (p_new->>'type') = 'Scrap' THEN (p_new->>'quantity')::numeric ELSE 0 END+ WHERE id = v_job_operation_id;++ ELSIF p_operation = 'DELETE' THEN+ v_job_operation_id := p_old->>'jobOperationId';++ UPDATE "jobOperation"+ SET+ "quantityComplete" = "quantityComplete" -+ CASE WHEN (p_old->>'type') = 'Production' THEN (p_old->>'quantity')::numeric ELSE 0 END,+ "quantityReworked" = "quantityReworked" -+ CASE WHEN (p_old->>'type') = 'Rework' THEN (p_old->>'quantity')::numeric ELSE 0 END,+ "quantityScrapped" = "quantityScrapped" -+ CASE WHEN (p_old->>'type') = 'Scrap' THEN (p_old->>'quantity')::numeric ELSE 0 END+ WHERE id = v_job_operation_id;+ END IF;++ -- Sync job.quantityComplete only when this operation is the last top-level operation.+ -- "Top-level" means operation belongs to the root make method (parentMaterialId IS NULL).+ -- "Last" means no other top-level operation depends on it.+ -- Skip if job is already Completed or Cancelled (sync_finish_job_operation owns that).+ SELECT jo."jobId" INTO v_job_id+ FROM "jobOperation" jo+ WHERE jo.id = v_job_operation_id;++ SELECT EXISTS (+ SELECT 1+ FROM "jobOperation" jo+ INNER JOIN "jobMakeMethod" jmm ON jmm.id = jo."jobMakeMethodId"+ WHERE jo.id = v_job_operation_id+ AND jmm."parentMaterialId" IS NULL+ AND NOT EXISTS (+ SELECT 1+ FROM "jobOperationDependency" dep+ INNER JOIN "jobOperation" child_jo ON child_jo.id = dep."operationId"+ INNER JOIN "jobMakeMethod" child_jmm ON child_jmm.id = child_jo."jobMakeMethodId"+ WHERE dep."dependsOnId" = jo.id+ AND child_jmm."parentMaterialId" IS NULL+ )+ ) INTO v_is_last_top_level_operation;++ IF v_job_id IS NOT NULL AND v_is_last_top_level_operation THEN+ UPDATE "job"+ SET "quantityComplete" = (+ SELECT COALESCE(jo."quantityComplete", 0)+ FROM "jobOperation" jo+ WHERE jo.id = v_job_operation_id+ )+ WHERE id = v_job_id+ AND status NOT IN ('Completed', 'Cancelled');+ END IF;+END;+$$;