# Task 11 — Inventory Reports

## Goal
Build the full inventory reporting suite: stock ledger, movement history, purchase history.

---

## Reports to Build

### 1. Stock Ledger (per item, per warehouse)
Shows running balance and all movements for an item.

**Query:**
```php
StockMovement::where('item_id', $itemId)
    ->where('warehouse_id', $warehouseId)
    ->whereDate('movement_date', '>=', $dateFrom)
    ->whereDate('movement_date', '<=', $dateTo)
    ->orderBy('movement_date')->orderBy('id')
    ->select([
        'movement_date', 'movement_number', 'movement_type',
        'quantity', 'unit_cost', 'total_cost',
        'average_cost_before', 'average_cost_after',
        'reference_type', 'reference_id',
    ])
```

**View columns:** Date | Reference | Type | In Qty | Out Qty | Balance | Avg Cost | Total Value

### 2. Stock Movement Summary
Aggregated in/out/adjustment by item for a date range.

**Query:**
```php
StockMovement::query()
    ->join('items', 'items.id', '=', 'stock_movements.item_id')
    ->selectRaw('item_id, SUM(CASE WHEN movement_type IN ("in","transfer_in") THEN quantity ELSE 0 END) as total_in')
    ->selectRaw('SUM(CASE WHEN movement_type IN ("out","transfer_out") THEN quantity ELSE 0 END) as total_out')
    ->selectRaw('SUM(CASE WHEN movement_type LIKE "adjustment%" THEN quantity ELSE 0 END) as adjustments')
    ->groupBy('item_id')
```

### 3. Purchase History by Supplier
```php
PurchaseReceipt::where('supplier_id', $supplierId)
    ->with(['lines.item', 'purchaseOrder'])
    ->orderByDesc('receipt_date')
```

### 4. Items Not Moving (Dead Stock)
```php
StockBalance::query()
    ->where('quantity', '>', 0)
    ->whereDoesntHave('item.stockMovements', function($q) use ($cutoffDate) {
        $q->where('movement_date', '>=', $cutoffDate);
    })
```

---

## Controller: `app/Http/Controllers/Admin/Inventory/InventoryReportController.php`

Methods: stockLedger(), movementSummary(), purchaseHistory(), deadStock()

---

## Permissions
```
inventory.reports.stock_ledger.view
inventory.reports.movement_summary.view
inventory.reports.purchase_history.view
```

---

## Routes
```php
Route::prefix('inventory/reports')->as('inventory.reports.')->group(function () {
    Route::get('/stock-ledger', [InventoryReportController::class, 'stockLedger'])->name('stock-ledger');
    Route::get('/movement-summary', [InventoryReportController::class, 'movementSummary'])->name('movement-summary');
    Route::get('/purchase-history', [InventoryReportController::class, 'purchaseHistory'])->name('purchase-history');
    Route::get('/dead-stock', [InventoryReportController::class, 'deadStock'])->name('dead-stock');
    Route::get('/valuation', [StockValuationController::class, 'valuation'])->name('valuation');
    Route::get('/low-stock', [StockValuationController::class, 'lowStock'])->name('low-stock');
});
```
