Skip to content

Fix stock balance corruption across document lifecycle#65

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

Fix stock balance corruption across document lifecycle#65
cursor[bot] wants to merge 1 commit into
masterfrom
cursor/critical-bug-investigation-7960

Conversation

@cursor

@cursor cursor Bot commented Jun 15, 2026

Copy link
Copy Markdown

Bug and impact

Issued stock-linked documents could corrupt inventory balances when users bulk-cancelled, deleted, or edited issued invoices because those lifecycle paths bypassed stock reversal/rebuild logic. Warehouse deletion after issuing all stock could also orphan movement references, and transfers could overdraw a source warehouse into negative stock.

Root cause

Stock movement application was wired into single document issue/cancel, but bulk cancel/delete, single delete, and issued-document update paths mutated document status/lines without balancing stock movements. Warehouse deletion only checked non-zero balances, not movement history, and transfers did not validate source availability.

Fix

  • Rebuild stock issue movements when issued document lines are updated.
  • Reverse stock before single/bulk delete and bulk cancel.
  • Make document issue stock application idempotent and lock the document while issuing.
  • Block warehouse deletion once movement history exists.
  • Reject transfers that exceed source warehouse stock.
  • Add feature tests for the affected API paths.

Validation

  • APP_KEY=base64:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA= BTCPAY_BASE_URL=https://example.test BTCPAY_API_KEY=test php artisan test --filter='CompanyStockDocumentMovementTest|CompanyWarehouseTest'
    • Passed: 13 tests, 74 assertions
    • Warnings only: missing local .env in this runner and unrelated PHPUnit doc-comment metadata deprecation.
Open in Web View Automation 

Co-authored-by: SiriusBig <webiumsk@users.noreply.github.com>
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