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(inventory): ship a sales order from the order's location (#2074)
Brad Barbin5d823d0d2d8a
diff --git a/apps/erp/app/modules/inventory/AGENTS.md b/apps/erp/app/modules/inventory/AGENTS.mdindex 02a68b7372..13135343b3 100644--- a/apps/erp/app/modules/inventory/AGENTS.md+++ b/apps/erp/app/modules/inventory/AGENTS.md@@ -66,7 +66,7 @@ Each operation is one `defineServiceFn` const in `inventory.server.ts`. A route| Operation | Route | What it holds ||---|---|---|-| `createReceipt` / `createShipment` | `receipt+/new.tsx`, `shipment+/new.tsx` | The `create` posting for the source document; a return order or rental agreement with a Draft document returns that one. A sales or purchase order shipment needs the caller's default location |+| `createReceipt` / `createShipment` | `receipt+/new.tsx`, `shipment+/new.tsx` | The `create` posting for the source document; a return order or rental agreement with a Draft document returns that one. A purchase order shipment needs the caller's default location; a sales order shipment ships from the order's location, or the caller's default location when one of its lines ships from there || `updateReceipt` / `updateShipment` | `$receiptId.details.tsx`, `$shipmentId.details.tsx` | A changed source or location rebuilds the lines; a rental document keeps its agreement || `postReceipt` / `postShipment` | `$receiptId.post.tsx`, `$shipmentId.post.tsx` | Storage rules (and over-receipt, and the order's sales rules), Pending, the posting, back to Draft on failure, labels, lead times, the workflow moment. A shipment also applies the expired-batch policy and saves the packing slip (`packing-slip.pdf.tsx`, shared with `file+/shipment+/$id[.]pdf.tsx`) || `voidReceipt` / `voidShipment` | `$receiptId.void.tsx`, `$shipmentId.void.tsx` | Posted only; not a receipt a purchase invoice created. A receipt void also refuses stock that has left its bin since posting and an untracked lot an inspection rejected; the refusal reaches the caller in its own words |diff --git a/apps/erp/app/modules/inventory/inventory.server.ts b/apps/erp/app/modules/inventory/inventory.server.tsindex 93351c3a04..67f96e6c46 100644--- a/apps/erp/app/modules/inventory/inventory.server.ts+++ b/apps/erp/app/modules/inventory/inventory.server.ts@@ -1260,7 +1260,9 @@ type ExpiredEntityPolicy = "Warn" | "Block" | "BlockWithOverride";* order, a purchase order, a purchase return order, an outbound warehouse* transfer or a rental agreement. With no source document it creates an* empty shipment. The location is the caller's default location, which a- * sales order or purchase order shipment requires.+ * purchase order shipment requires. A sales order shipment ships from the+ * order's location, or from the caller's default location when one of the+ * order's lines ships from there.** A return order or rental agreement with a Draft shipment already returns* that shipment instead of a second one.@@ -1275,7 +1277,22 @@ export const createShipment = defineServiceFn({permission: { create: "inventory" },async run(ctx, { sourceDocument, sourceDocumentId }) {- const locationId = await service.defaultLocationOf(ctx);+ let locationId = await service.defaultLocationOf(ctx);+ if (sourceDocument === "Sales Order") {+ const location = await service.salesOrderShipmentLocationOf(+ ctx,+ sourceDocumentId,+ locationId+ );+ if (location.error) {+ return ctx.error(+ "internal",+ "Failed to load the sales order's location",+ location.error+ );+ }+ locationId = location.data ?? undefined;+ }if (!locationId &&diff --git a/apps/erp/app/modules/inventory/inventory.service.ts b/apps/erp/app/modules/inventory/inventory.service.tsindex c40cc88488..8e4c982272 100644--- a/apps/erp/app/modules/inventory/inventory.service.ts+++ b/apps/erp/app/modules/inventory/inventory.service.ts@@ -5137,6 +5137,51 @@ export async function defaultLocationOf(ctx: ServiceContext) {return defaults.data?.locationId ?? undefined;}+/**+ * The location a new shipment from a sales order ships from: the order's own+ * location, unless one of its lines ships from the caller's default location+ * (as a receipt from a purchase order does). The shipment only takes the lines+ * at its location, so a caller defaulted elsewhere got an empty shipment.+ */+export async function salesOrderShipmentLocationOf(+ ctx: ServiceContext,+ salesOrderId: string,+ callerLocationId: string | undefined+) {+ const [salesOrder, callerLine] = await Promise.all([+ ctx.asSystem.supabase+ .from("salesOrder")+ .select("locationId")+ .eq("id", salesOrderId)+ .eq("companyId", ctx.company.id)+ .maybeSingle(),+ callerLocationId+ ? ctx.asSystem.supabase+ .from("salesOrderLine")+ .select("id")+ .eq("salesOrderId", salesOrderId)+ .eq("companyId", ctx.company.id)+ .eq("locationId", callerLocationId)+ .in("salesOrderLineType", [+ "Part",+ "Material",+ "Tool",+ "Fixture",+ "Consumable"+ ])+ .limit(1)+ : { data: [], error: null }+ ]);+ const error = salesOrder.error ?? callerLine.error;+ if (error) return { data: null, error };+ return {+ data: callerLine.data?.length+ ? callerLocationId+ : (salesOrder.data?.locationId ?? callerLocationId),+ error: null+ };+}+/** Print a document's labels when its location's printer auto-prints. */export async function autoPrint(ctx: ServiceContext,