# Point 5 Update - Stock Reservation, Commit, Release Rules

This update hardens WMS stock behaviour for fulfilment and dispatch.

## Implemented rules

1. Order creation does not deduct stock.
2. Fulfilment stock validation checks warehouse-level SKU availability.
3. Pick task creation reserves stock:
   - inventory_stocks.available_qty decreases
   - inventory_stocks.reserved_qty increases
   - order_items.reserved_qty increases
   - stock_movements gets RESERVE row
4. Pick scan moves reserved stock to picked:
   - reserved_qty decreases
   - picked_qty increases
   - stock_movements gets PICK row
5. Pack confirmation moves picked stock to packed:
   - picked_qty decreases
   - packed_qty increases
   - stock_movements gets PACK row
6. Ready for Dispatch performs final validation and ensures stock is reserved.
7. Handover to Driver commits stock to TMS:
   - packed/picked/reserved qty decreases
   - out_for_delivery_qty increases
   - order_items.dispatched_qty increases
   - order_items.stock_reservation_status becomes COMMITTED
   - orders.stock_committed_at is set
   - stock_movements gets COMMIT row
8. Before dispatch, stock can be released/cancelled using:
   POST /api/v1/fulfilment/orders/:id/release-stock

## API added

POST /api/v1/fulfilment/orders/:id/release-stock
Body: { "reason": "Customer cancelled before dispatch" }

This releases reserved/picked/packed stock back to available stock and marks the fulfilment order cancelled.

## Migration file

backend/src/database/migrations/20260626_stock_reservation_commit_rules.sql

For local development, `npm run db:sync` remains supported.
