# Task 06 — Supplier Master (CRUD)

## Goal
Full CRUD for the `suppliers` table created in Task 02.

---

## Service: `app/Services/Inventory/Supplier/SupplierService.php`

```php
public function getAll(int $perPage = 15): LengthAwarePaginator
{
    return Supplier::with(['branch', 'apAccount'])
        ->orderBy('code')
        ->paginate($perPage);
}

public function getActive(): Collection
{
    return Supplier::active()->orderBy('code')->get();
}

public function getFormOptions(): array
{
    return [
        'branches'  => Branch::active()->get(),
        'accounts'  => Account::where('is_active', true)->get(),
    ];
}

public function create(array $data): Supplier
{
    $data['created_by'] = auth('admin')->id();
    return Supplier::create($data);
}

public function update(Supplier $supplier, array $data): Supplier
{
    $data['updated_by'] = auth('admin')->id();
    $supplier->update($data);
    return $supplier->fresh();
}

public function delete(Supplier $supplier): void
{
    // Check: block delete if supplier has purchase orders
    if ($supplier->purchaseOrders()->exists()) {
        throw new \Exception('Cannot delete supplier with existing purchase orders.');
    }
    $supplier->delete();
}

public function getBalance(Supplier $supplier): float
{
    // AP balance = opening_balance + purchase receipts posted - payments made
    $received = $supplier->purchaseReceipts()
        ->where('status', 'posted')
        ->sum('total_cost');
    return $supplier->opening_balance + $received;
    // Subtract payments when implemented in Task 20
}
```

---

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

Standard CRUD: index, create, store, show, edit, update, destroy.

`show()` method should display:
- Supplier details
- List of purchase orders (last 10)
- Current AP balance

---

## Views

Path: `resources/views/dashboard/admin/inventory/suppliers/`

Files needed:
- `index.blade.php` — DataTable with code, name, phone, email, branch, is_active
- `create.blade.php` — Form with all fields
- `edit.blade.php` — Same as create, pre-filled
- `show.blade.php` — Supplier card + recent POs + balance

---

## Permissions

```php
'inventory.suppliers.view'
'inventory.suppliers.create'
'inventory.suppliers.update'
'inventory.suppliers.delete'
```

---

## Routes

```php
Route::group(['prefix' => 'inventory/suppliers', 'as' => 'inventory.suppliers.'], function () {
    Route::get('/', [SupplierController::class, 'index'])->name('index');
    Route::get('/create', [SupplierController::class, 'create'])->name('create');
    Route::post('/', [SupplierController::class, 'store'])->name('store');
    Route::get('/{supplier}', [SupplierController::class, 'show'])->name('show');
    Route::get('/{supplier}/edit', [SupplierController::class, 'edit'])->name('edit');
    Route::put('/{supplier}', [SupplierController::class, 'update'])->name('update');
    Route::delete('/{supplier}', [SupplierController::class, 'destroy'])->name('destroy');
});
```
