Fix production completion consuming reserved raw materials - #38
Draft
cursor[bot] wants to merge 1 commit into
Draft
Fix production completion consuming reserved raw materials#38cursor[bot] wants to merge 1 commit into
cursor[bot] wants to merge 1 commit into
Conversation
Completion compared consume qty to hub on-hand and ignored reservations from pending RM supply orders, so a run could steal reserved stock and leave available_quantity negative. Validate available qty before posting finished goods, and show the same ATP in the new-run form. Co-authored-by: ezzademir <ezzademir@users.noreply.github.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug and impact
Completing a production run can steal raw materials that are already reserved for a pending outlet supply order.
Trigger: Hub has 10kg flour. Staff create an RM supply order reserving 8kg (available = 2). The same day they complete a run that consumes 5kg flour. The new-run form showed on-hand (10) as “available,” and
post_production_completion_inventoryonly checkedquantity_on_hand.Result: Hub becomes QOH 5 / reserved 8 / available −3. Dispatch of the flour order then fails (
Cannot fulfill more than on-hand quantity). Production used stock that was promised to the outlet.This is separate from open draft PRs #29 (phantom FG on RM missing/short after insert), #36 (waste sibling overdraw), and #37 (sales sibling overdraw).
Root cause
The lot-era completion RPC deducted RM with
IF v_qoh < consumedand setavailable_quantity = new_qoh - reserved, which can go negative. The Production UI loaded onlyquantity_on_handintoavailable_qty(and kept the last hub row when several existed).Fix
20260830110000_production_completion_respect_reserved_rm.sql: lock RM first; reject whenon-hand - reserved < consumedbefore inserting FG; RAISE if a later deduct still sees a shortfall so PostgREST cannot commit a partial post._hub_adjust_raw_material(void/restore) uses the same reserved floor.sumAvailableByRawMaterialId) and the same cover check as the RPC.Validation
npm test— 30 passed (3 new hub ATP tests)npm run typecheck— cleannpm run lint— existing DateFilter / FinishedGoodsLotLabel warnings onlynpm run build— cleanOut of scope
Did not change open drafts #19, #20, #29, #30, #35, #36, #37 or older May supply-order drafts.