Skip to content

Fix production completion consuming reserved raw materials - #38

Draft
cursor[bot] wants to merge 1 commit into
mainfrom
cursor/critical-bug-investigation-a08b
Draft

Fix production completion consuming reserved raw materials#38
cursor[bot] wants to merge 1 commit into
mainfrom
cursor/critical-bug-investigation-a08b

Conversation

@cursor

@cursor cursor Bot commented Aug 30, 2026

Copy link
Copy Markdown

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_inventory only checked quantity_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 < consumed and set available_quantity = new_qoh - reserved, which can go negative. The Production UI loaded only quantity_on_hand into available_qty (and kept the last hub row when several existed).

Fix

  • Migration 20260830110000_production_completion_respect_reserved_rm.sql: lock RM first; reject when on-hand - reserved < consumed before 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.
  • New-run form uses unreserved ATP (sumAvailableByRawMaterialId) and the same cover check as the RPC.

Validation

  • npm test — 30 passed (3 new hub ATP tests)
  • npm run typecheck — clean
  • npm run lint — existing DateFilter / FinishedGoodsLotLabel warnings only
  • npm run build — clean
  • No local Postgres in this environment; SQL reviewed against the current completion + reservation RPCs

Out of scope

Did not change open drafts #19, #20, #29, #30, #35, #36, #37 or older May supply-order drafts.

Open in Web View Automation 

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>
@vercel

vercel Bot commented Aug 30, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
project Ready Ready Preview Aug 30, 2026 11:21am

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant