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
feat: post receipts for parts with serial and lot tracking
Brad Barbind9fb008b1866
diff --git a/apps/erp/app/modules/inventory/inventory.service.ts b/apps/erp/app/modules/inventory/inventory.service.tsindex 155974b4fd..8093eec3bf 100644--- a/apps/erp/app/modules/inventory/inventory.service.ts+++ b/apps/erp/app/modules/inventory/inventory.service.ts@@ -168,7 +168,7 @@ export async function getInventoryItemsCount(.from("item").select("id, readableId", { count: "exact" }).eq("companyId", companyId)- .eq("itemTrackingType", "Inventory");+ .in("itemTrackingType", ["Inventory", "Serial", "Lot"]);if (args?.search) {query = query.or(diff --git a/apps/erp/app/modules/inventory/ui/Inventory/InventoryActivity.tsx b/apps/erp/app/modules/inventory/ui/Inventory/InventoryActivity.tsxindex ef5c7a405f..e4dd93d1e5 100644--- a/apps/erp/app/modules/inventory/ui/Inventory/InventoryActivity.tsx+++ b/apps/erp/app/modules/inventory/ui/Inventory/InventoryActivity.tsx@@ -7,7 +7,13 @@ import type { ItemLedger } from "../../types";const getActivityText = (ledgerRecord: ItemLedger) => {switch (ledgerRecord.documentType) {case "Purchase Receipt":- return `received ${ledgerRecord.quantity} units`;+ return `received ${ledgerRecord.quantity} units${+ ledgerRecord.lotNumber ? ` from lot ${ledgerRecord.lotNumber}` : ""+ }${+ ledgerRecord.serialNumber+ ? ` with serial number ${ledgerRecord.serialNumber}`+ : ""+ }`;case "Purchase Invoice":return `invoiced ${ledgerRecord.quantity} units`;case "Sales Shipment":diff --git a/apps/erp/app/routes/x+/purchase-invoice+/$invoiceId.new.tsx b/apps/erp/app/routes/x+/purchase-invoice+/$invoiceId.new.tsxindex b6dcfec129..d24b4bbed0 100644--- a/apps/erp/app/routes/x+/purchase-invoice+/$invoiceId.new.tsx+++ b/apps/erp/app/routes/x+/purchase-invoice+/$invoiceId.new.tsx@@ -57,9 +57,7 @@ export async function action({ request, params }: ActionFunctionArgs) {);}- throw redirect(- path.to.purchaseInvoiceLine(invoiceId, createPurchaseInvoiceLine.data.id)- );+ throw redirect(path.to.purchaseInvoiceDetails(invoiceId));}export default function NewPurchaseInvoiceLineRoute() {diff --git a/apps/erp/app/routes/x+/purchase-order+/$orderId.new.tsx b/apps/erp/app/routes/x+/purchase-order+/$orderId.new.tsxindex 120c7f9786..20c41ef34f 100644--- a/apps/erp/app/routes/x+/purchase-order+/$orderId.new.tsx+++ b/apps/erp/app/routes/x+/purchase-order+/$orderId.new.tsx@@ -54,9 +54,7 @@ export async function action({ request, params }: ActionFunctionArgs) {);}- const purchaseOrderLineId = createPurchaseOrderLine.data.id;-- throw redirect(path.to.purchaseOrderLine(orderId, purchaseOrderLineId));+ throw redirect(path.to.purchaseOrderDetails(orderId));}export default function NewPurchaseOrderLineRoute() {diff --git a/apps/erp/app/routes/x+/sales-order+/$orderId.new.tsx b/apps/erp/app/routes/x+/sales-order+/$orderId.new.tsxindex 0ea7c593a3..16652a9b45 100644--- a/apps/erp/app/routes/x+/sales-order+/$orderId.new.tsx+++ b/apps/erp/app/routes/x+/sales-order+/$orderId.new.tsx@@ -49,9 +49,7 @@ export async function action({ request, params }: ActionFunctionArgs) {);}- const salesOrderLineId = createSalesOrderLine.data.id;-- throw redirect(path.to.salesOrderLine(orderId, salesOrderLineId));+ throw redirect(path.to.salesOrderDetails(orderId));}export default function NewSalesOrderLineRoute() {diff --git a/packages/database/supabase/functions/post-receipt/index.ts b/packages/database/supabase/functions/post-receipt/index.tsindex e992ae5fd9..b96baccdb8 100644--- a/packages/database/supabase/functions/post-receipt/index.ts+++ b/packages/database/supabase/functions/post-receipt/index.ts@@ -51,7 +51,12 @@ serve(async (req: Request) => {const [receipt, receiptLines] = await Promise.all([client.from("receipt").select("*").eq("id", receiptId).single(),- client.from("receiptLine").select("*").eq("receiptId", receiptId),+ client+ .from("receiptLine")+ .select(+ "*, receiptLineTracking(id, quantity, serialNumber(number), lotNumber(number))"+ )+ .eq("receiptId", receiptId),]);if (receipt.error) throw new Error("Failed to fetch receipt");@@ -640,6 +645,46 @@ serve(async (req: Request) => {companyId,});}++ if (receiptLine.requiresLotTracking) {+ itemLedgerInserts.push({+ postingDate: today,+ itemId: receiptLine.itemId,+ itemReadableId: receiptLine.itemReadableId ?? "",+ quantity: receiptLine.receivedQuantity,+ locationId: receiptLine.locationId,+ shelfId: receiptLine.shelfId,+ entryType: "Positive Adjmt.",+ documentType: "Purchase Receipt",+ documentId: receipt.data?.id ?? undefined,+ lotNumber:+ receiptLine.receiptLineTracking?.[0]?.lotNumber?.number,+ externalDocumentId: receipt.data?.externalDocumentId ?? undefined,+ createdBy: userId,+ companyId,+ });+ }++ if (receiptLine.requiresSerialTracking) {+ receiptLine.receiptLineTracking.forEach((tracking) => {+ itemLedgerInserts.push({+ postingDate: today,+ itemId: receiptLine.itemId,+ itemReadableId: receiptLine.itemReadableId ?? "",+ quantity: 1,+ locationId: receiptLine.locationId,+ shelfId: receiptLine.shelfId,+ entryType: "Positive Adjmt.",+ documentType: "Purchase Receipt",+ documentId: receipt.data?.id ?? undefined,+ serialNumber: tracking.serialNumber?.number,+ externalDocumentId:+ receipt.data?.externalDocumentId ?? undefined,+ createdBy: userId,+ companyId,+ });+ });+ }}const accountingPeriodId = await getCurrentAccountingPeriod(@@ -749,6 +794,14 @@ serve(async (req: Request) => {}).where("id", "=", receiptId).execute();++ await trx+ .updateTable("receiptLineTracking")+ .set({+ posted: true,+ })+ .where("receiptId", "=", receiptId)+ .execute();});break;}diff --git a/packages/database/supabase/migrations/20250128094053_traceability.sql b/packages/database/supabase/migrations/20250128094053_traceability.sqlindex 758a96e29d..f1376505f6 100644--- a/packages/database/supabase/migrations/20250128094053_traceability.sql+++ b/packages/database/supabase/migrations/20250128094053_traceability.sql@@ -390,3 +390,85 @@ BEGINWHERE rl.id = p_receipt_line_id;END;$$ LANGUAGE plpgsql;++CREATE OR REPLACE FUNCTION get_item_quantities(location_id TEXT)+RETURNS TABLE (+ "itemId" TEXT,+ "companyId" TEXT,+ "locationId" TEXT,+ "quantityOnHand" NUMERIC,+ "quantityOnPurchaseOrder" NUMERIC,+ "quantityOnSalesOrder" NUMERIC,+ "quantityOnProdOrder" NUMERIC,+ "quantityAvailable" NUMERIC,+ "materialSubstanceId" TEXT,+ "materialFormId" TEXT,+ "grade" TEXT,+ "dimensions" TEXT,+ "finish" TEXT,+ "readableId" TEXT,+ "type" "itemType",+ "name" TEXT,+ "active" BOOLEAN,+ "itemTrackingType" "itemTrackingType",+ "thumbnailPath" TEXT,+ "locationName" TEXT,+ "unitOfMeasureCode" TEXT+) AS $$+BEGIN+ RETURN QUERY+ SELECT+ i."id" AS "itemId",+ i."companyId",+ loc."id" AS "locationId",+ SUM(COALESCE(inv."quantityOnHand", 0)) AS "quantityOnHand",+ SUM(COALESCE(inv."quantityOnPurchase", 0)) AS "quantityOnPurchaseOrder",+ SUM(COALESCE(inv."quantityOnSalesOrder", 0)) AS "quantityOnSalesOrder",+ SUM(COALESCE(inv."quantityOnProductionOrder", 0)) AS "quantityOnProdOrder",+ SUM(COALESCE(inv."quantityOnHand", 0)) -+ SUM(COALESCE(inv."quantityOnSalesOrder", 0)) -+ SUM(COALESCE(inv."quantityOnProductionOrder", 0)) AS "quantityAvailable",+ mat."materialSubstanceId",+ mat."materialFormId",+ mat."grade",+ mat."dimensions",+ mat."finish",+ i."readableId",+ i."type",+ i."name",+ i."active",+ i."itemTrackingType",+ CASE+ WHEN i."thumbnailPath" IS NULL AND mu."thumbnailPath" IS NOT NULL THEN mu."thumbnailPath"+ ELSE i."thumbnailPath"+ END AS "thumbnailPath",+ loc."name" AS "locationName",+ i."unitOfMeasureCode"+ FROM "item" i+ INNER JOIN "location" loc ON loc.id = location_id+ LEFT JOIN "itemInventory" inv ON i."id" = inv."itemId" AND inv."locationId" = loc."id"+ LEFT JOIN "material" mat ON i."id" = mat."itemId"+ LEFT JOIN "modelUpload" mu ON mu.id = i."modelUploadId"+ WHERE i."itemTrackingType" IN ('Inventory', 'Serial', 'Lot')+ AND i."companyId" = loc."companyId"+ AND inv."locationId" = location_id+ GROUP BY+ i."id",+ i."companyId",+ loc."id",+ mat."materialSubstanceId",+ mat."materialFormId",+ mat."grade",+ mat."dimensions",+ mat."finish",+ i."readableId",+ i."type",+ i."name",+ i."active",+ i."itemTrackingType",+ i."thumbnailPath",+ mu."thumbnailPath",+ loc."name",+ i."unitOfMeasureCode";+END;+$$ LANGUAGE plpgsql SECURITY INVOKER;\ No newline at end of file