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: sales orders view not including jobs

Brad Barbin4fa993d20117
diff --git a/apps/erp/app/modules/sales/ui/SalesOrder/SalesOrderSummary.tsx b/apps/erp/app/modules/sales/ui/SalesOrder/SalesOrderSummary.tsx
index 93fd04137d..c21a21a94c 100644
--- a/apps/erp/app/modules/sales/ui/SalesOrder/SalesOrderSummary.tsx
+++ b/apps/erp/app/modules/sales/ui/SalesOrder/SalesOrderSummary.tsx
@@ -252,6 +252,8 @@ function LineItems({
(j) => (j as unknown as SalesOrderJob).salesOrderLineId === line.id
) ?? []) as SalesOrderJob[];
+ console.log({ jobs });
+
const isMade = line.methodType === "Make";
const hasEnoughJobsToCoverQuantity =
diff --git a/packages/database/supabase/migrations/20250326011605_opportunity-fix.sql b/packages/database/supabase/migrations/20250326011605_opportunity-fix.sql
new file mode 100644
index 0000000000..f2d9f53f2f
--- /dev/null
+++ b/packages/database/supabase/migrations/20250326011605_opportunity-fix.sql
@@ -0,0 +1,58 @@
+
+DROP VIEW IF EXISTS "salesOrders";
+CREATE OR REPLACE VIEW "salesOrders" WITH(SECURITY_INVOKER=true) AS
+ SELECT
+ s.*,
+ sl."thumbnailPath",
+ sl."itemType",
+ sl."orderTotal" + COALESCE(ss."shippingCost", 0) AS "orderTotal",
+ sl."jobs",
+ sl."lines",
+ st."name" AS "shippingTermName",
+ sp."paymentTermId",
+ ss."shippingMethodId",
+ ss."receiptRequestedDate",
+ ss."receiptPromisedDate",
+ ss."dropShipment",
+ ss."shippingCost"
+ FROM "salesOrder" s
+ LEFT JOIN (
+ SELECT
+ sol."salesOrderId",
+ MIN(CASE
+ WHEN i."thumbnailPath" IS NULL AND mu."thumbnailPath" IS NOT NULL THEN mu."thumbnailPath"
+ ELSE i."thumbnailPath"
+ END) AS "thumbnailPath",
+ SUM((1+COALESCE(sol."taxPercent", 0))*(COALESCE(sol."saleQuantity", 0)*(COALESCE(sol."unitPrice", 0)) + COALESCE(sol."shippingCost", 0) + COALESCE(sol."addOnCost", 0))) AS "orderTotal",
+ MIN(i."type") AS "itemType",
+ ARRAY_AGG(
+ CASE
+ WHEN j.id IS NOT NULL THEN json_build_object(
+ 'id', j.id,
+ 'jobId', j."jobId",
+ 'status', j."status",
+ 'productionQuantity', j."productionQuantity",
+ 'quantityComplete', j."quantityComplete",
+ 'salesOrderLineId', sol.id,
+ 'assignee', j."assignee"
+ )
+ ELSE NULL
+ END
+ ) FILTER (WHERE j.id IS NOT NULL) AS "jobs",
+ ARRAY_AGG(
+ json_build_object(
+ 'id', sol.id,
+ 'methodType', sol."methodType",
+ 'saleQuantity', sol."saleQuantity"
+ )
+ ) AS "lines"
+ FROM "salesOrderLine" sol
+ LEFT JOIN "item" i
+ ON i."id" = sol."itemId"
+ LEFT JOIN "modelUpload" mu ON mu.id = i."modelUploadId"
+ LEFT JOIN "job" j ON j."salesOrderId" = sol."salesOrderId" AND j."salesOrderLineId" = sol."id"
+ GROUP BY sol."salesOrderId"
+ ) sl ON sl."salesOrderId" = s."id"
+ LEFT JOIN "salesOrderShipment" ss ON ss."id" = s."id"
+ LEFT JOIN "shippingTerm" st ON st."id" = ss."shippingTermId"
+ LEFT JOIN "salesOrderPayment" sp ON sp."id" = s."id";