# Butchery stock deduction fix — 28 November 2026

## Change
- Reworked the butchery sale allocation to lock eligible branch stock rows and allocate FIFO.
- Stock is now deducted using an explicit SQL UPDATE against `butchery_batches.available_kg`, scoped by batch ID and branch.
- Batch matching prefers `butchery_product_id`; legacy batches without that link fall back to the exact `meat_type` name.
- Sale rows are inserted in the same database transaction as stock deductions. If any allocation or deduction fails, the transaction rolls back.
- Added a price validation and a stock availability check while holding row locks.
- Preserves the existing single combined receipt per sale reference.

## Deployment
1. Back up the application and database.
2. Upload the updated application files from this ZIP.
3. Preserve `.env`, `storage`, and user-uploaded files.
4. Run `php artisan optimize:clear`.

No SQL migration is required; the fix uses the existing `butchery_batches` and `butchery_sales` fields.

## Validation
- `php -l app/Http/Controllers/ButcheryController.php` passed.
- This environment cannot run the application's live MySQL transaction; test a small sale on a staging copy and verify `butchery_batches.available_kg` decreases by the quantity sold.
