# Phase 9: Filament Admin Updates

## Objective
Ensure every new model, field, and enum value introduced by the Shop Manager feature is properly reflected in the Filament back-office admin panel.

---

## Context

The project has 24 existing Filament Resources with 48 RelationManagers. The following resources are directly impacted:

| Resource | Impact |
|---|---|
| `EmployeeResource` | Add SHOP_MANAGER role option + permissions |
| `ProductResource` | Add barcode, SKU, is_active, low_stock_threshold fields + stock adjustment |
| `OrderResource` | Display POS/CASH payment types, payment details, receipt photos |
| **InvoiceResource** (NEW) | Full CRUD for invoices |
| `RolePermissionsManager` page | Add shop_manager permissions |

---

## Tasks

### 9.1 EmployeeResource — SHOP_MANAGER Role

**File:** `app/Filament/Resources/EmployeeResource.php`

Currently the role assignment select uses `AdminRole` enum options. After adding `SHOP_MANAGER` to the enum:

- Add `SHOP_MANAGER` to the role options available to Super Admins and Hospitality
- Ensure the role label displays properly in the table column
- Hospitality admins should be able to assign SHOP_MANAGER role

### 9.2 RolePermissionsManager — Shop Manager Permissions

**File:** `app/Filament/Pages/RolePermissionsManager.php`

Add permission group for shop_manager module:
```
shop_manager.view_dashboard
shop_manager.manage_carts
shop_manager.manage_products
shop_manager.manage_orders
shop_manager.manage_invoices
shop_manager.manage_customers
shop_manager.manage_inventory
```

Create migration to seed these permissions.

### 9.3 ProductResource — New Fields

**File:** `app/Filament/Resources/ProductResource.php`

Add to form:
- `is_active` toggle (after `can_buy`, `can_rent` toggles)
- `sku` text input (in the main form section)

**File:** `app/Filament/Resources/ProductResource/RelationManagers/VariationsRelationManager.php`

Add to variation form:
- `barcode` text input
- `low_stock_threshold` numeric input (default 5)

Add to variation table:
- `barcode` column (searchable)
- `current_stock` column (computed from stocks)
- `reorder_status` badge column (healthy/warning/low/out_of_stock)

### 9.4 ProductResource — Stock Adjustment Action

**File:** `app/Filament/Resources/ProductResource/RelationManagers/StocksRelationManager.php`

Add a header action "Quick Refill" that creates a new stock entry with:
- Variation select
- Quantity input
- Start date = now
- No end date (permanent stock)

### 9.5 OrderResource — POS/CASH Display

**File:** `app/Filament/Resources/OrderResource.php`

Update the ViewOrder page to:
- Display `payment_details` JSON for CASH orders (amount received, amount returned)
- Show receipt photo for POS orders (media collection)
- Show cash photo for CASH orders (media collection)
- Add POS and CASH to the payment_provider filter options

**File:** `app/Filament/Resources/OrderResource/Pages/ViewOrder.php`

Add infolist sections for:
- Payment details (conditional on provider type)
- Receipt/cash photo display

### 9.6 InvoiceResource (NEW)

**File:** `app/Filament/Resources/InvoiceResource.php`

READ-ONLY resource (invoices are auto-generated, not manually created):

**Table columns:**
- `invoice_number` (searchable)
- `order.public_id` (link to order)
- `user.fullname` (searchable, link to customer)
- `place.name` (searchable)
- `total` (formatted as money)
- `issued_at` (datetime, sortable)

**Filters:**
- Place (select)
- Date range (issued_at)
- Customer search

**View page:**
- Full invoice details (infolist)
- Line items table
- Customer snapshot
- Place snapshot
- PDF download action button
- "Resend to customer" action button
- "Send to email" action button (with email input modal)

**Relation Manager on OrderResource:**
- `InvoiceRelationManager` — shows invoice linked to the order

**Relation Manager on CustomerResource:**
- `InvoicesRelationManager` — shows all invoices for the customer

**Pages:**
```php
'index' => Pages\ListInvoices::route('/'),
'view' => Pages\ViewInvoice::route('/{record}'),
```

No create/edit pages (invoices are immutable).

**Permissions:**
- `invoice.view_any`
- `invoice.view`
- `invoice.resend`

### 9.7 CustomerResource — Orders & Invoices Relations

**File:** `app/Filament/Resources/CustomerResource.php`

Add new RelationManagers:
- `OrdersRelationManager` — list customer's orders (if not already present)
- `InvoicesRelationManager` — list customer's invoices with PDF download

### 9.8 PlaceResource — Shop Manager Stats Widget

Optional: Add a "Shop Sales" tab or RelationManager to PlaceResource showing:
- Total shop revenue for the place
- Active shop managers count
- Recent shop orders

---

## Files Created/Modified

| Action | File |
|---|---|
| Modify | `app/Filament/Resources/EmployeeResource.php` |
| Modify | `app/Filament/Pages/RolePermissionsManager.php` |
| Modify | `app/Filament/Resources/ProductResource.php` |
| Modify | `app/Filament/Resources/ProductResource/RelationManagers/VariationsRelationManager.php` |
| Modify | `app/Filament/Resources/ProductResource/RelationManagers/StocksRelationManager.php` |
| Modify | `app/Filament/Resources/OrderResource.php` |
| Modify | `app/Filament/Resources/OrderResource/Pages/ViewOrder.php` |
| Create | `app/Filament/Resources/InvoiceResource.php` |
| Create | `app/Filament/Resources/InvoiceResource/Pages/ListInvoices.php` |
| Create | `app/Filament/Resources/InvoiceResource/Pages/ViewInvoice.php` |
| Create | `app/Filament/Resources/OrderResource/RelationManagers/InvoiceRelationManager.php` |
| Create | `app/Filament/Resources/CustomerResource/RelationManagers/InvoicesRelationManager.php` |
| Modify | `app/Filament/Resources/CustomerResource.php` (register InvoicesRelationManager) |
| Create | `database/migrations/..._seed_shop_manager_permissions.php` |
| Create | `tests/Feature/Filament/InvoiceResourceTest.php` |

---

## Acceptance Criteria

- [ ] SHOP_MANAGER role is assignable in EmployeeResource
- [ ] Product form includes is_active, SKU; variation form includes barcode, low_stock_threshold
- [ ] Order view shows POS/CASH payment details and photos
- [ ] InvoiceResource is browsable with search, filter, PDF download
- [ ] Invoice can be resent from Filament
- [ ] Permissions are seeded and configurable in RolePermissionsManager
- [ ] All Filament tests pass
- [ ] `./gitCheck.sh` passes
