Skip to content

Fix waste posts that overdraw a lot split across reasons - #36

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

Fix waste posts that overdraw a lot split across reasons#36
cursor[bot] wants to merge 1 commit into
mainfrom
cursor/critical-bug-investigation-ea8e

Conversation

@cursor

@cursor cursor Bot commented Aug 28, 2026

Copy link
Copy Markdown

Bug and impact

post_waste_event checked each line against the lot’s full on-hand quantity, not the combined quantity of every line targeting that row.

Trigger: Record waste → add two lines on the same lot (for example 8 spoilage + 8 damage) when only 10 are available. Each line looks valid. Submit.

Impact: The RPC posted the event and deducted both lines, driving quantity_on_hand negative. Staff had no available-qty hint on the picker, so this is an everyday split-reason workflow, not a contrived edge case.

Root cause

Validation locked the inventory row and compared available >= this_line.qty independently. The deduct pass then subtracted each line with no remaining-available guard. Stock-take already rejects duplicate inventory ids; waste never summed them.

Fix

  • Migration 20260828110000_waste_event_sum_duplicate_lines.sql: sum requested qty per hub/outlet inventory id before writes; reject with insufficient_*_available if the combined qty does not fit. Deduct UPDATEs require remaining available ≥ line qty and RAISE on miss so a race rolls back (does not RETURN success:false after writes).
  • Supervisor auth from Unify ERP chrome and daily sales/waste workflows #33 is preserved on the replaced function (staff for hub; staff or assigned-outlet supervisor for outlet).
  • Waste UI sums lines on the same lot against live available qty and blocks submit with a clear message. Split reasons that still fit (4+6 of 10) still post.

Validation

  • npm test: 31 passed (4 new tests for summing / over-allocation).
  • npm run typecheck and npm run build clean.
  • npm run lint: pre-existing DateFilter / FinishedGoodsLotLabel warnings only.
  • No local Postgres in this environment, so the SQL was reviewed against the live post_waste_event body (identifier-match rewrite + supervisor patch). Could not exercise the RPC against a live database.
Open in Web View Automation 

post_waste_event checked each line against full on-hand, so two
lines on the same lot (spoilage + damage) could both pass and then
drive quantity_on_hand negative. Sum requested qty per inventory
row before posting, reject over-allocation, and guard the deduct
UPDATE so a race still rolls back.

Co-authored-by: ezzademir <ezzademir@users.noreply.github.com>
@vercel

vercel Bot commented Aug 28, 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 28, 2026 11:13am

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