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.
Scrap & Unscrap Flow — Tracked-Entity Scrap in MES + ERP Stock Scrap/Unscrap with Full Posting
Status: draft (autonomous-mode spec — all Open Questions resolved autonomously; review the Autonomous: markers before implementation) Author: Claude (for Brad Barbin) Date: 2026-08-06 Research:
.ai/research/scrap-unscrap-flow.md(SAP / Oracle Fusion / NetSuite / Epicor / Infor / JobBOSS / Plex / Fulcrum survey) Related:.ai/specs/2026-07-14-inventory-adjustment-gl-posting.md(shared posting core),.ai/specs/2026-08-04-batch-split-identity-flip.md(split conventions),.claude/rules/traceability-model.md,.claude/rules/mes-job-operation-ui.md
TLDR
Scrap in Carbon today is a quantity bucket: MES scrap inserts a
productionQuantity type='Scrap' row and backflushes materials, but the
scrapped output never touches trackedEntity status, itemLedger, cost layers,
or the GL — and the operation's auto-Done predicate counts scrap toward
targetQuantity, so a scrapped serial silently shorts the job. This spec makes
scrap a first-class inventory/traceability/GL event across three surfaces,
shared by the MES job operation view and assembly view (which already share
QuantityModal, the scrap route, and the entity-scrap route):
- Scrap the serial unit being made → terminal
Scrappedstatus on the selected entity, aScrapgenealogy activity, materials-backflush kept, DrscrapAccount/ Cr WIP for the unit's consumed-material cost, and — improving on every surveyed competitor — auto-spawn of the next serial (the existingjobOperationSerialCompletespawn). The auto-Done predicate stops countingquantityScrapped, so the operation stays open until good quantity reachestargetQuantity— no target mutation. - Scrap an already-made BOM part → Pull from Inventory: scrap from stock
(Dr
scrapAccount/ Cr inventory), requirement stays open so the operator pulls a replacement. Make to Order:Scrappedstatus + DrscrapAccount/ Cr WIP, plus an opt-out replacement flow that reopens the subassembly's Done operations, tops upoperationQuantitycapacity when scrap exceeds the planned allowance, and spawns the replacement serial ("made 3, scrapped 1 → make 1 more"). - Batch parts → partial-quantity scrap via the batch-split convention
(child departs
Scrapped, parent keeps its id).
In the ERP, available stock gains first-class Scrap / Unscrap actions
(SAP 551/552 + Oracle Return-from-Scrap pattern) built on the existing
post-inventory-adjustment → bookAdjustment core with
offsetAccount = accountDefault.scrapAccount (already seeded), mandatory scrap
reason, Scrapped entity status, and unscrap reversing at the original
scrapped cost via correctionOfItemLedgerId. Every path updates inventory,
itemLedger, cost layers, and journalLines atomically.
Problem Statement
Verified current state (file:line refs from repo @ new-york):
- Operation-output scrap posts nothing.
apps/mes/app/routes/x+/scrap.tsx→insertScrapQuantity(operations.service.ts:1516) insertsproductionQuantitytype='Scrap', then backflushes BOM materials via theissueedge function. NotrackedEntitychange, noitemLedger, nocostLedger, no journal. The consumed materials sit in WIP forever (untilclose-jobwashes them tomaterialVarianceAccount). - The scrapped serial still counts as done. The auto-Done predicate
(
sync_update_job_operation_quantities, newest20260706181125:91) is(quantityComplete + quantityReworked + quantityScrapped) >= targetQuantity— buttargetQuantityis defined as the good quantity before scrap (20260119120000column comment;get-methodsetsoperationQuantity= target + planned scrap allowance). So scrap consumes good-target capacity: "make 3, scrap 1" flips the op Done with 2 good units and no replacement serial is ever created — and even planned scrap shorts the job (target 10 good + 2 planned scrap auto-Dones at 8 good + 2 scrap). - The only posting scrap path conflates scrap with consumption.
issuecasescrapTrackedEntity(functions/issue/index.ts:1682-1871; reached only fromx+/entity+/$materialId.$trackedEntityId.scrap.tsx) sets the entitystatus='Consumed', postsConsumption/Job Consumptioninto WIP (job absorbs the loss into finished-goods cost), bumpsjobMaterial.quantityIssued(so the requirement looks satisfied and no replacement is prompted), takes no scrap reason, and posts nothing for Make-to-Order materials. - No terminal scrap state, no scrap ledger identity.
trackedEntityStatus=Available|Reserved|On Hold|Consumed|Rejected— noScrapped. NoScrapvalue initemLedgerDocumentType/journalLineDocumentType. Scrap is invisible to traceability queries and indistinguishable in the GL. - No ERP stock scrap/unscrap. The quantities page offers only
Positive/Negative Adjmt./Set Quantity with the generic
inventoryAdjustmentVarianceAccountoffset; scrapping a damaged shelf part is indistinguishable from count shrinkage, and there is no way to restore a wrongly-scrapped entity. - The building blocks already exist:
accountDefault.scrapAccount(20260726012013, seeded with fallback toinventoryAdjustmentVarianceAccount);bookAdjustmentinfunctions/shared/post-adjustment.tsalready accepts anoffsetAccount(the header comment names scrapAccount;post-nonconformanceuses it); the serial-spawn logic (jobOperationSerialComplete,issue/index.ts:1301-1367); the batch-split builder (shared/batch-split.ts); rework machinery (reworktable,trigger-reworkop-quantity pattern);calculateCOGS;correctionOfItemLedgerId; scrap reasons (scrapReasontable +productionQuantity.scrapReasonId).
Proposed Solution
0. Shared vocabulary (migration)
trackedEntityStatus+='Scrapped'— terminal. Excluded everywhereRejectedis excluded (get_inventory_quantities,get_available_tracked_entities,get_picking_list_tracked_available, picker RPCs). UnlikeConsumed, aScrappedentity is recoverable via Unscrap (ERP) and is queryable for scrap analysis.itemLedgerDocumentType+='Scrap'— stamped on scrap and unscrap movements (unscrap = positive quantity, same documentType, linked viacorrectionOfItemLedgerId).journalLineDocumentType+='Scrap'(separate enum — the journal/ledger enums are different; lessonjournalLineDocumentType…).itemLedger.scrapReasonIdTEXT NULL, FK →scrapReason— reason lands on the movement for stock scrap (MES production scrap keeps its reason onproductionQuantity.scrapReasonId).trackedActivity.typevalues'Scrap'/'Unscrap'(TEXT column — no enum change).
1. MES: scrap the tracked unit being made (use case 1)
New issue edge-function case jobOperationSerialScrap (mirror of
jobOperationSerialComplete, one Kysely transaction), invoked from the
existing scrap route when the parent make method is serial-tracked. The
QuantityModal scrap branch and x+/scrap.tsx are shared by
JobOperation.tsx and AssemblyView.tsx, so both views get this by
construction — the modal gains the selected serial's readableId
confirmation, keeps ScrapReason (required) + notes.
Transaction steps:
productionQuantitytype='Scrap',scrapReasonId, notes (unchanged shape; the quantity-sync interceptor keeps aggregatingquantityScrapped).- Backflush the unit's BOM for the current operation
(
issueJobOperationMaterials) — kept: material cost enters WIP exactly as today (NetSuite "Issue for Scrap" pattern). trackedActivitytype='Scrap',sourceDocument='Job Operation', withtrackedActivityInput(entity, qty 1); entitystatus='Scrapped', attributes stamped (Operation ${jobOperationId},Scrap Reason). The entity's consumed-material genealogy is preserved — the scrapped unit keeps its full input tree.- GL (when
accountingEnabled): DrscrapAccount/ CrworkInProgressAccountfor the unit's accumulated consumed-material cost: the current-op backflush cost (computed in-transaction) plus the cost of prior consumption ledger rows reachable from the entity's genealogy inputs. Labor/overhead already absorbed stay in WIP and settle viaclose-jobvariance (v1 — see Design Decision 6).sourceType'Job Consumption'-family journal withdocumentType='Scrap',documentId=jobId. Dimension tags (Brad, 2026-08-07): every scrap journal line carriesjournalLineDimensionrows for whichever of these dimensions are active on the company group — Item, ItemPostingGroup, Location (the adjustment precedent) plus ScrapReason (newdimensionEntityTypevalue, valueId =scrapReason.id), WorkCenter (the operation'sworkCenterId), and Employee (the scrapping user) — so one flatscrapAccountslices by reason/work center/operator/item in reporting instead of ever needing multiple scrap accounts. - Spawn the next serial (the user-visible headline): same
getNextSerialNumbers+Reserved-entity insert asjobOperationSerialComplete:1301-1367, spawned whenever good (non-Scrapped) entities for the make method < the good quantity still required. ReturnnewTrackedEntityId; the client navigates to it exactly as the complete flow does. - Auto-Done predicate fix + reopen (Brad, 2026-08-07): the migration
redefines
sync_update_job_operation_quantities(fork the newest body,20260706181125) so the Done predicate becomes(quantityComplete + quantityReworked) >= targetQuantity— scrap no longer counts toward the good target.targetQuantityis already the good quantity (20260119120000), so an operation now stays open until the good units are actually made; notargetQuantitymutation on scrap, ever. In the scrap transaction itself:- Reopen
status='Done'operations of the affectedjobMakeMethodto'Ready'— the replacement unit must pass through all operations, not just the current one. (A reopened op re-flips Done via the interceptor as soon as the replacement's quantities are recorded.) - Capacity top-up only beyond plan: when cumulative actual scrap
exceeds the planned allowance (
operationQuantity − targetQuantity), bumpoperationQuantityby the excess, bumpjob.scrapQuantitylikewise when the make method is the root (keeps the generatedjob.productionQuantityand serial planning coherent), and refresh the make method'sjobMaterialestimated quantities (quantityPerParent × new operationQuantity— mirroringrecalculateJobRequirementsmath, which is ERP-side and unavailable in Deno). Within the allowance, capacity already exists — no writes. - Reschedule via the same invoke
trigger-reworkuses.
- Reopen
Batch parent being made: scrap quantity q is recorded as
productionQuantity type='Scrap' (as today) + the same WIP→scrap GL leg for
q units' material cost; the in-progress lot's trackedEntity.quantity only
ever receives good quantity at jobOperationBatchComplete, so no entity write
is needed pre-completion; the §1.6 reopen/top-up applies identically (no
serial spawn — the lot absorbs the replacement quantity).
2. MES: scrap an already-made BOM part (use case 2)
Rework of issue case scrapTrackedEntity (route
x+/entity+/$materialId.$trackedEntityId.scrap.tsx, reachable from the
Materials section of both views). Payload gains required scrapReasonId and
makeReplacement: boolean. Branches by jobMaterial.methodType:
- Pull from Inventory / Purchase to Order (the part physically came from
stock): the part is scrapped from stock, not consumed into the job:
itemLedgerentryType='Negative Adjmt.',documentType='Scrap',scrapReasonId, negative quantity at the entity's actual on-hand bin (resolveTrackedEntityBin— lesson: never.find(...)?.storageUnitId).- Cost layers via
calculateCOGS; GL DrscrapAccount/ Cr inventory (resolveInventoryAccount) through the sharedbookAdjustmentcore. - Entity
status='Scrapped';trackedActivitytype='Scrap'with the entity as input (andparentTrackedEntityIdoutput when passed, as today). jobMaterial.quantityIssuedis NOT bumped (behavior change — today it is): the requirement stays open, so the operator issues a replacement from stock ("just scrap the part" + pull another). Batch partial scrap usesbuildBatchSplitRecords— the departing child becomes theScrappedentity; the parent keeps its id (spec2026-08-04).
- Make to Order (a subassembly made inside this job — never in inventory):
- No stock ledger row (correct today, kept — its cost is already in WIP).
- Entity
status='Scrapped'+Scrapactivity (today:Consumed+ attribute). - GL: Dr
scrapAccount/ Cr WIP at the subassembly's accumulated consumed-material cost (same valuation rule as §1.4 — it is a unit being made, one level down). - Replacement flow when
makeReplacement(modal checkbox, default ON — "made 3, scrapped 1 → make 1 more"): apply §1.6's reopen/top-up to the subassembly'sjobMakeMethod(reopen its Done ops; bumpoperationQuantity+ material estimates only for scrap beyond the planned allowance —targetQuantityuntouched) and spawn the replacementReservedserial entity (serial) — batch lots need only the reopen. The subassembly's ops won't re-flip Done until the replacement's good quantity reaches their target (§1.6 predicate). Record areworkrow (triggeredAtJobOperationId= the operation the scrap came from,targetJobOperationId= the subassembly's first operation,reason= scrap reason name) so the existing rework surfaces show it.
3. ERP: scrap / unscrap available stock
Extends post-inventory-adjustment + shared/post-adjustment.ts (no new
edge function):
- Payload
adjustmentTypegains'Scrap'and'Unscrap';scrapReasonIdrequired for Scrap only — Unscrap inherits the reason from the original scrap movement it reverses (falls back to any supplied reason, then null), so the operator never re-enters it;unscrapOfItemLedgerIdoptional (set by the Unscrap UI). - Scrap = the existing negative-adjustment machinery with:
offsetAccount = accountDefault.scrapAccount(runtime fallback toinventoryAdjustmentVarianceAccount, per the seed comment),documentType='Scrap',scrapReasonIdon the ledger row; tracked entity →status='Scrapped'keeping itsquantity(the record of what was scrapped),trackedActivitytype='Scrap'. Serial = whole entity; batch = partial via the split convention (departing child isScrapped). Journal lines carry the §1.4 dimension set minus WorkCenter (no operation context); Employee = the adjusting user. - Unscrap (Oracle Return-from-Scrap pattern) = mirrored positive movement:
- Tracked: select a
Scrappedentity →status='Available', positiveitemLedger(documentType='Scrap',correctionOfItemLedgerId= the original scrap row, same bin), cost layer- journal (Dr inventory / Cr
scrapAccount) at the original scrapped cost read from the linked scrap movement'scostLedger— not current cost (Fusion precedent; avoids P&L leakage on round-trips).
- journal (Dr inventory / Cr
- Untracked: positive adjustment with
scrapAccountoffset at current cost (v1 — no original-movement linkage to reverse against).
- Tracked: select a
- UI (copy the adjustment-drawer precedent,
x+/inventory+/quantities+/$itemId.adjustment.tsx):- The adjustment form's type select gains Scrap (shows required
ScrapReason+ notes). - The item's tracking view gains a Scrapped filter listing
Scrappedentities with an Unscrap row action (optional comment + confirm; the reason is inherited from the original scrap movement, not re-entered). - Detail overlays follow the Drawer convention. All new strings through
Lingui. Permission:
inventory_update(same as adjustments today); MES scrap routes stayproduction_update-gated.
- The adjustment form's type select gains Scrap (shows required
4. Ripple updates
- Status-aware readers: add
'Scrapped'to every exclusion that lists'Rejected'(get_inventory_quantities, availability/picking RPCs,TrackedEntityPickerfilters). Grep for'Rejected'in migrations + app code; redefine touched SQL functions by forking the newest body (grep-all-migrations lesson). complete_job_to_inventoryis untouched: it keys onquantityComplete(good units only) and its serial loop readsReservedentities — aScrappedentity never reaches Assembly Output. Verify the serial-selection predicate excludesScrappedexplicitly.- MES
getNextIncompleteSerialEntity/isSerialEntityIncompleteForOperationtreatScrappedlikeConsumed(skip). - App-side mirrors of the Done predicate:
InspectionView.tsx:385computesopRemaining = target − complete − scrapped − reworked— drop thequantityScrappedterm to match the new predicate; sweep everyquantityScrappedusage in remaining/progress/Done math across MES + ERP (display-only usages like the JobDag badge and step-record navigationoperationQuantity + quantityScrappedstay). - Docs:
docs/production + inventory pages;.claude/rules/traceability-model + inventory-system + mes-job-operation-ui updates; ERPProductionQuantityForm(the ERP mirror of the MES modal) keeps working — it writesproductionQuantityonly and is out of scope for entity effects.
Design Decisions
| # | Decision | Choice | Rationale |
|---|---|---|---|
| 1 | Multi-tenancy (heuristic 1) | No new tables. Every write carries companyId + audit fields; edge-function payloads require userId (zod) |
House convention; null-audit-column lesson |
| 2 | Service shape (heuristic 2) | Posting logic in issue cases + shared/post-adjustment.ts; ERP/MES services stay thin {data,error} wrappers |
Matches post-* family + adjustment-spec precedent |
| 3 | RLS (heuristic 3) | N/A — no new tables; new column/enum values inherit existing policies | — |
| 4 | Permissions (heuristic 4) | MES scrap: production_update (existing routes); ERP scrap/unscrap: inventory_update |
Scrapping stock is an inventory action; shop-floor scrap is production |
| 5 | Forms (heuristic 5) | Extend QuantityModal scrap branch, entity-scrap modal, ERP adjustment form — ValidatedForm + zod validators, scrapReasonId required on every scrap |
Fulcrum one-tap UX; reason mandatory is universal consensus |
| 6 | WIP scrap valuation | Materials-only at report time (Dr scrap / Cr WIP for consumed-material cost from genealogy); labor/overhead stay in WIP → close-job variance. Full cost-through-operation (SyteLine) deferred |
Deterministic and implementable now; Carbon already has close-variance machinery; avoids per-unit labor attribution |
| 7 | Terminal status | New Scrapped status, not Consumed/Rejected reuse |
Scrapped ≠ consumed-into-parent; unscrap must find them; Epicor SCRAPPED / SAP ME precedent |
| 8 | Replacement is explicit, effects are in-txn | Serial-being-made: spawn always (the user's ask; beats all competitors). MTO subassembly: makeReplacement checkbox default ON. Capacity writes only when scrap exceeds the planned allowance (operationQuantity + job.scrapQuantity top-up) |
Universal "no auto-inflation" consensus, adapted — the operator action IS the explicit trigger |
| 9 | Reopen ops vs clone ops | Reopen Done ops on the same routing; targetQuantity never mutated; operationQuantity topped up only beyond the planned allowance |
trigger-rework's cloned path is for re-work of existing units; a replacement is new work on the same method |
| 10 | Stock-component scrap accounting | Dr scrapAccount / Cr inventory (not through WIP); quantityIssued not bumped |
"Just scrap the part" + pull another; scrap loss is cost-of-quality, not product cost; SAP reverse-and-scrap pattern collapsed to one movement |
| 11 | Unscrap cost | Original scrapped cost via correctionOfItemLedgerId linkage (tracked); current cost (untracked v1) |
Fusion Return-from-Scrap; NetSuite's current-cost reversal is a documented audit weakness |
| 12 | Scrap offset account | ONE flat accountDefault.scrapAccount (by id), both directions; analysis via journalLineDimension tags (ScrapReason / WorkCenter / Employee / Item / ItemPostingGroup / Location) — per-reason account mapping rejected, not deferred (Brad, 2026-08-07) |
Account exists (20260726012013); no-matrix-config rule; dimensions give Fusion-grade reporting without account proliferation |
| 13 | Ledger identity | documentType='Scrap' + itemLedger.scrapReasonId; entry types stay Positive/Negative Adjmt. (no new itemLedgerType) |
Additive; keeps every on-hand/valuation reader working unchanged |
| 14 | Backward compat (heuristic 7) | Additive enums/column; TWO deliberate behavior changes: (a) scrapTrackedEntity stops bumping quantityIssued and posts to scrap instead of WIP; (b) the auto-Done predicate drops quantityScrapped (Brad, 2026-08-07) — in-flight ops that would have auto-Done via scrap now stay open until good qty reaches target (manual Finish still available) |
targetQuantity is already defined as the good quantity — counting scrap against it was shorting jobs even within planned scrap |
| 15 | Module layout (heuristic 6) | Additions live in existing inventory.service.ts / production.service.ts / MES services; no new modules |
One service/models file per module |
Data Model Changes
No new tables. One migration (pnpm db:migrate:new scrap-unscrap-flow,
HHMMSS randomized, idempotent, enum ADD VALUE outside dependent statements):
ALTER TYPE "trackedEntityStatus" ADD VALUE IF NOT EXISTS 'Scrapped';
ALTER TYPE "itemLedgerDocumentType" ADD VALUE IF NOT EXISTS 'Scrap';
ALTER TYPE "journalLineDocumentType" ADD VALUE IF NOT EXISTS 'Scrap';
ALTER TYPE "dimensionEntityType" ADD VALUE IF NOT EXISTS 'ScrapReason';
ALTER TABLE "itemLedger" ADD COLUMN IF NOT EXISTS "scrapReasonId" TEXT;
ALTER TABLE "itemLedger" DROP CONSTRAINT IF EXISTS "itemLedger_scrapReasonId_fkey";
ALTER TABLE "itemLedger" ADD CONSTRAINT "itemLedger_scrapReasonId_fkey"
FOREIGN KEY ("scrapReasonId") REFERENCES "scrapReason"("id") ON DELETE SET NULL;
Follow-up statements (same migration, forked from newest bodies): (a)
redefine sync_update_job_operation_quantities (newest: 20260706181125)
with the Done predicate
("quantityComplete" + "quantityReworked") >= "targetQuantity" — scrap
removed (Brad, 2026-08-07); (b) add 'Scrapped' to the 'Rejected'
exclusions in get_inventory_quantities, get_available_tracked_entities,
get_picking_list_tracked_available (list every migration touching each
function first — sibling-branch lesson). Then
pnpm run generate:types before typechecking. config.toml unchanged (no new
functions).
API / Service Changes
issueedge function: new casejobOperationSerialScrap; reworked casescrapTrackedEntity(reason required,makeReplacement, methodType branches per §2); shared capacity-bump helper used by both.post-inventory-adjustment:adjustmentType'Scrap'/'Unscrap',scrapReasonId,unscrapOfItemLedgerId;bookAdjustmentgainsscrapReasonIdpassthrough + fixed-cost override for unscrap reversal. The Unscrap branch readsscrapReasonIdoff the resolved original scrap movement and books the ledger row,trackedActivity, and ScrapReason journal dimension with that inherited value.- MES
x+/scrap.tsx: routes serial-tracked parents tojobOperationSerialScrap; batch/untracked path adds the WIP→scrap GL leg. - Entity-scrap route action: validator gains
scrapReasonId(required) +makeReplacement. - ERP
inventory.service.ts: wrapper params for scrap/unscrap;getTrackedEntitiesfilter forScrapped. - No Inngest changes. Reschedule via the existing schedule invoke.
UI Changes
- MES
QuantityModal(shared by both views): scrap branch shows the selected serialreadableIdwhen serial-tracked; reason already required. - MES entity-scrap modal (Materials section, both views): adds
ScrapReason(required), notes, and themakeReplacementcheckbox (MTO only, default ON). - ERP adjustment drawer:
Scraptype option with reason; tracking view gainsScrappedfilter +Unscraprow action (Drawer overlay convention,size="md"; MES additionssize="lg"). All strings through Lingui.
Acceptance Criteria
- Serial WIP scrap + spawn. Serial job qty 3 (no planned scrap): scrap
the in-progress serial at op 2 with reason → entity
Scrapped,productionQuantityScrap row, aScrapactivity with the entity as input, op 1 (Done) reopens to Ready,targetQuantitystays 3 on every op,operationQuantity3→4 andjob.scrapQuantity0→1 (scrap exceeded the zero planned allowance), a newReservedserial exists and the view navigates to it; no op flips Done until its good quantity reaches 3; job completion receives exactly 3 to inventory. - Planned-scrap allowance. Job qty 10 with 20% planned scrap
(
targetQuantity10,operationQuantity12): completing 8 good + scrapping 2 does NOT auto-Done the op (today it does); no capacity writes occur (within allowance); the op flips Done when good quantity reaches 10. - Serial WIP scrap GL + dimensions. With accounting enabled and $40 of
materials backflushed into the scrapped unit across ops: one balanced
journal Dr scrapAccount $40 / Cr WIP $40,
documentType='Scrap'; with Item, ScrapReason, WorkCenter, and Employee dimensions active on the company group, every line carriesjournalLineDimensiontags for the item, the chosen scrap reason, the operation's work center, and the operator; with accounting disabled: identical ledger writes, zero journals. - Assembly view parity. The same scrap (serial + entity scrap) driven
from
/x/assembly/:idproduces byte-identical DB effects to the operation view. - Stock component scrap. Scrap a picked (Available) batch entity qty 2
of 5 from lineside: split per the identity-flip (parent keeps id, qty 5→3;
child qty 2
Scrapped),itemLedger−2Negative Adjmt./ScrapwithscrapReasonIdat the lineside bin, layers consumed viacalculateCOGS, Dr scrapAccount / Cr inventory at that cost,quantityIssuedunchanged, and the material's issue requirement still shows the outstanding quantity. - MTO subassembly scrap + replacement. Subassembly make method built 3
serials, scrap 1 with Make Replacement ON: entity
Scrapped, Dr scrap / Cr WIP at its consumed-material cost, no stock ledger row, subassembly Done ops reopened (targetQuantityunchanged;operationQuantitytopped up only past the allowance), replacementReservedserial spawned, areworkrow recorded; with Make Replacement OFF only the scrap effects occur. - ERP scrap. Scrap an Available serial entity from the quantities page
with reason: status
Scrapped,itemLedger−1documentType='Scrap'+scrapReasonId, Dr scrapAccount / Cr inventory at COGS cost; entity disappears from on-hand, availability, and picker RPCs but appears under the Scrapped filter. - ERP unscrap at original cost. Item cost moves $10→$12 after
scrapping at $10: Unscrap restores the entity to
Availableat its original bin, positive ledger linked viacorrectionOfItemLedgerId, Dr inventory / Cr scrapAccount at $10, and a subsequent shipment consumes the restored layer at $10. - Untracked scrap/unscrap. Untracked item: Scrap −5 with reason posts the scrapAccount pair; Unscrap +5 posts the mirror at current cost.
- Batch WIP scrap. Batch parent op
targetQuantity10 (no planned scrap): scrap 2 pre-completion → Scrap productionQuantity row, WIP→scrap GL for 2 units' material cost,operationQuantity10→12, target stays 10, the op stays open until 10 good complete, lot completes with good quantity only. - No regressions: a plain Negative Adjmt. still offsets to
inventoryAdjustmentVarianceAccountwith NULL documentType (byte-identical);complete_job_to_inventoryoutput rows never referenceScrappedentities; picking/return sweeps ignoreScrappedentities. -
pnpm run generate:typesthenpnpm exec turbo run typecheck --filter=erp --filter=mesgreen; migration re-runs safely; edge-function own-filedeno checkerror delta = 0.
Risks
| Risk | Severity | Mitigation |
|---|---|---|
| Predicate change strands in-flight ops (good < target after scrap already counted) or fights reopened ops (premature re-flip when good already ≥ target) | High | Operator Finish still force-Dones any op; predicate only fires on productionQuantity changes (no retroactive flips); acceptance criteria pin the reopen→re-flip flow; test the last-op case (job already Completed → serial scrap blocked, entity is then stock — use ERP scrap) |
Pre-20260120 ops were backfilled targetQuantity = operationQuantity (includes planned scrap allowance) — with scrap excluded they auto-Done only at good = target-with-allowance |
Med | Acceptable strictness (manual Finish covers); optional backfill for open ops (targetQuantity = operationQuantity − plannedScrapShare) evaluated at plan time |
| Genealogy-derived material cost is incomplete (untracked components consumed without entity linkage) | Med | Fall back to current-op backflush cost + jobMaterial unit estimates for prior ops; state valuation basis in the journal description; labor already excluded by decision 6 |
Behavior change on scrapTrackedEntity (no quantityIssued bump, scrap-not-WIP posting) surprises existing users |
Med | Flagged as decision 10/14 for Brad's veto; release note; the old absorb-into-job semantics remain available by issuing normally |
Scrapped exclusion missed in one status-aware reader → phantom on-hand |
Med | Single grep sweep for 'Rejected' across migrations + app; fork newest function bodies; tie-out panel verifies subledger↔GL after scrap |
| Unscrap original-cost reversal when the layer was partially consumed since | Low | Reversal creates a NEW layer at original unit cost (no resurrection of the old layer); correctionOfItemLedgerId keeps the audit chain |
| Enum ADD VALUE + same-migration usage (Postgres restriction) | Low | ADD VALUE statements first, function redefinitions second; migration validated in a rolled-back psql txn |
Open Questions
All resolved autonomously (no human available mid-loop). Each is a veto point — review before implementation. Ask-First flag: items 1–2 touch production-critical table schemas (additive-only); per root AGENTS.md this spec does NOT authorize implementation until Brad approves it.
- New terminal status vs reuse
Consumed/Rejected? — Autonomous: add'Scrapped'(decision 7). Reuse would make unscrap and scrap analysis impossible to query and conflates distinct physical states. - Ledger identity: new
itemLedgerTypevs documentType? — Autonomous: keepPositive/Negative Adjmt.entry types; add'Scrap'documentType +itemLedger.scrapReasonId(decision 13). New entry types would ripple through every valuation/on-hand reader for zero information gain. - WIP scrap valuation basis? — Autonomous: consumed-material cost only, labor to close variance (decision 6). SyteLine-style full cost-through-operation deferred until per-unit labor attribution exists.
- Does stock-component scrap absorb into the job (today's behavior) or
post to scrap? — Autonomous: post to scrap, don't bump
quantityIssued(decision 10) — matches the user's "just scrap the part" framing; flagged as the one deliberate behavior change. - Replacement mechanics: reopen + bump vs clone ops vs new job? —
Autonomous: bump quantities + reopen on the same routing (decision 9);
trigger-reworkcloning is for reprocessing existing units, a replacement is new units of planned work. - Unscrap cost basis? — Autonomous: original scrapped cost for tracked, current cost for untracked v1 (decision 11, Fusion precedent).
- Auto-Done predicate: exclude scrap instead of bumping targets? —
Answer (Brad, 2026-08-07): exclude
quantityScrappedfrom the predicate. Overrides the initial autonomous resolution. Cross-checked against the schema:targetQuantityis documented as the good quantity before scrap (20260119120000) andoperationQuantitycarries the planned allowance, so the old predicate was shorting jobs even within planned scrap.targetQuantityis never mutated by scrap; in-flight-op impact covered in Risks;quantityReworkedstays in the predicate (out of scope). - Per-reason GL account mapping (Fusion/Epicor)? — Answer (Brad,
2026-08-07): rejected permanently — ONE
scrapAccount, with scrap reason as a journal dimension instead. AdddimensionEntityTypevalue'ScrapReason'; scrap journal lines tag ScrapReason, WorkCenter, and Employee alongside the existing Item / ItemPostingGroup / Location, for whichever dimensions the company group has active.valueIdis polymorphic by design (no FK), soscrapReason.idslots in cleanly.
Changelog
2026-08-07: Subcomponent scrap reworked to be state-based (Brad) — the original
scrapTrackedEntitybranched onmethodTypeand rejectedConsumedentities, but the only UI entry (the Unconsume tab) lists consumed subcomponents, so it never worked. Now: entity state drives the posting (Available→ scrap from stock;Consumed→ relieve WIP + decrementjobMaterial.quantityIssuedto reopen the requirement);methodTypeonly gates the MTO make-replacement flow. UI: a dedicated Scrap tab inIssueMaterialModal(ScrapTab) lists Available + Consumed entities for the material, replacing the (broken) per-row Scrap button in the Unconsume tab. Two judgment calls: state (not methodType) drives accounting; consumed-scrap decrementsquantityIssued.2026-08-07: Implemented on
mes-scrap-serial-rework-flow— see.ai/plans/2026-08-07-scrap-unscrap-flow.md. Notable deviations from the plan: (1) the status-aware RPCs needed noScrappedexclusion — scrap posts negative ledger movements and availability RPCs already filterstatus='Available'; (2) the serial/batch/untracked scrap consolidated into ONEissuecasejobOperationScrap; (3) the entity-scrap route had no UI caller before this — addedScrapEntityModal+ a Scrap action in the IssueMaterialModal unconsume tab; (4)rework.trackedEntityIdno longer exists so it was omitted from the rework insert; (5) ERP Unscrap resolves location/bin/cost server-side from the scrap movement (a Scrapped tracked-entity row carries no location), solocationIdis optional for Unscrap. Browser verification pending (Task 14).2026-08-07: Auto-Done predicate now excludes
quantityScrapped(Brad) —targetQuantityis already the good-quantity target, so ops stay open until good units are made; replaced the target-bump design with reopen +operationQuantitytop-up beyond the planned allowance. Ripple: app-side predicate mirrors (InspectionView.opRemaining) swept; new planned-scrap acceptance criterion; in-flight-op risk rows added.2026-08-07: Single scrap account + dimensions (Brad) — per-reason account mapping rejected permanently; scrap journal lines carry ScrapReason (new
dimensionEntityType), WorkCenter, and Employee dimensions alongside Item / ItemPostingGroup / Location.2026-08-06: Created (autonomous mode). Grounded in: competitor research (
.ai/research/scrap-unscrap-flow.md, 4-track survey), code exploration of the scrap/completion/adjustment/rework machinery (issuecasesjobOperationSerialComplete:1173/scrapTrackedEntity:1682,sync_update_job_operation_quantitiesauto-Done predicate20260706181125:91,bookAdjustmentoffset-account support,accountDefault.scrapAccountseed20260726012013,trigger-reworkquantity pattern, batch-split identity flip). All Open Questions carry Autonomous: resolutions pending Brad's review; implementation gated on spec approval.