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
Supplier return: tracked-entity pre-select, flow-through, and partial splits
Branch: returns-module
Problem
On a purchase return order (supplier return), tracked entities (batches/serials) don't move through the flow:
- No pre-selection. Adding a return line from a specific receipt never auto-selects the batch, even when the receipt line has exactly one entity.
- Batch doesn't reach the shipment.
create → shipmentFromPurchaseReturnOrdernever copiespurchaseReturnOrderLineTrackedEntityonto the shipment's tracked entities, so the shipment's batch box starts empty. - No splits.
post-shipment'sPurchase Return Ordercase requires the linked entities to sum exactly toshippedQuantityand consumes them whole — a partial return (ship 2 of a batch of 3) throws.
Decisions (from user)
- Pre-selection is persisted at line creation (not UI-only).
- Full support: partial-batch splits, mirroring the Sales Order path.
Reference facts (grounded)
- Entities from a receipt carry
attributes["Receipt"](receiptId) ANDattributes["Receipt Line"](receiptLineId) — set by receipt tracking / create. - Shipment tracking is
trackedEntity.attributes["Shipment"]/["Shipment Line"](no join table).post-shipmentreads entities viaattributes ->> Shipment. buildBatchSplitRecords(functions/shared/batch-split.ts): parent keeps its id and is decremented; a newnanoidchild departs with the drawn qty,childStatus"Consumed", carrying"Split From Entity ID". Emits aSplitactivity + two net-zeroBatch SplititemLedger rows. Throws unless0 < draw < parent.quantity.- SO post split loop = the reference (post-shipment ~880-1006): builds a parent
"Shipment"activity, splits, decrements + strips shipment attrs off the parent, pushes the Batch-Split ledger pair, and retargets the outbound relief ledger row from parent → child. PRO books its own per-entityPurchase Return Shipmentnegative row, so that retarget is required (unlike the PO path, which books none). - Void needs no change. SO/PRO voids flip every
attributes ->> Shipmententity back toAvailable(status only); after a split those are the Consumed children, and the retargeted reversing ledger row already targets the child. Parent stays decremented — same fragmented-but-whole outcome SO already accepts. No merge.
Changes
1. Pre-select single entity, persisted (Fix C)
purchasing.service.ts: addgetReturnableEntitiesForReceiptLine(client, companyId, itemId, receiptLineId)—trackedEntitywhereitemId,status = "Available",attributes ->> Receipt Line = receiptLineId,companyId.routes/x+/purchase-return-order+/$id.new.tsx: after the line is created, if notrackedEntityIdswere submitted and the line has areceiptLineId, call the new fn; if it returns exactly one entity, persist it viasetPurchaseReturnOrderLineTrackedEntities. Covers both the line form and the "Add lines from receipt" modal (both route through this action). Non-tracked items return[]→ no-op.
2. Flow-through to the shipment (Fix A)
create/index.tsshipmentFromPurchaseReturnOrder:- Before the txn, read
purchaseReturnOrderLineTrackedEntity(embedtrackedEntity(id, attributes)) for the return lines, grouped by return line id; and (re-source path) read entities currently tagged with this shipment id. - Change the
shipmentLineinsert to.returning(["id", "lineId"]). - In the txn: strip
Shipment/Shipment Line/Shipment Line Indexfrom any stale entity, then stampShipment = shipmentId,Shipment Line = <new line id>onto each return line's selected entities (merging existing attributes).
- Before the txn, read
3. Partial splits at post (Fix B)
post-shipment/index.tsPurchase Return Orderpost case:- Declare
trackedEntitySplitsandsplitChildEdgesalongside the existing maps. - Replace the batch/serial
elseblock: requireentitySum >= shippedQuantity(else throw "assign tracking"), then drawshippedQuantityacross the linked entities. Each fully-drawn entity → full consume (existing behavior); the entity drawn partially → record a split (negative ledger row at the drawn qty, remember its index to retarget).itemShipmentQuantitiesaccumulates the drawn amount. - In the txn, before the
itemLedgerinsert: for each split callbuildBatchSplitRecords(activitySourceDocument "Shipment",childStatus "Consumed",binfromshipmentLine.storageUnitId+shipment.data.locationId), insert the child +Splitactivity + input/output edges, decrement the parent and strip its shipment attrs, push the Batch-Split ledger pair, and retarget the captured negativePurchase Return Shipmentrow to the child id. - Extend the
Return Shipmentactivity block to run when there are splits too, and add atrackedActivityInputedge (child @ drawn qty) for each split child.
- Declare
- Void: no change (analysis above).
Verification
pnpm exec turbo run typecheck --filter=erpdeno checkis not wired locally; rely on typecheck of shared types + careful review.- Browser (user, after stack up): add a return line from a receipt with one batch → batch pre-selected; create shipment → batch present on the shipment line; post a partial (2 of 3) → posts, parent batch keeps 1 Available, child 2 Consumed; void → both Available.
Out of scope
purchaseReturnOrderLineTrackedEntity.quantitystays hardcoded1(not read by the shipment flow; splits are driven offshippedQuantity).- No merge-on-void (matches SO).