# Battery Components Product Management

## Overview
This document describes the setup and features for managing battery components in the inventory system, similar to the BOM management interface.

## Products Added
15 battery component products have been added to the system:

1. **BATT-LIFEPO4-314AH** - LiFePO4 battery cell, 3.2V 314ah, REPT ($60.00 cost / $75.00 selling)
2. **BATT-BMS-001** - Battery Management System ($127.00 / $160.00)
3. **BATT-SHELL-001** - Shell box ($200.00 / $250.00)
4. **BATT-SPACER-001** - Spacers/brackets ($45.00 / $60.00)
5. **BATT-INSUL-001** - Insulating materials ($75.00 / $95.00)
6. **BATT-BUSBAR-001** - Busbars/connecting pieces ($85.00 / $110.00)
7. **BATT-WIRE-001** - Wiring harnesses ($20.00 / $30.00)
8. **BATT-FUSE-001** - Fuse & Fuse holder ($12.00 / $18.00)
9. **BATT-CONN-001** - Connector(Ports) ($12.00 / $18.00)
10. **BATT-DISP-001** - Display/touch screen ($27.00 / $40.00)
11. **BATT-LED-001** - LED light ($5.00 / $10.00)
12. **BATT-BREAK-001** - Breaker ($15.00 / $25.00)
13. **BATT-COOL-001** - Cooling system ($57.00 / $80.00)
14. **BATT-BOLT-001** - Fastening Bolts and Nuts ($0.40 / $0.60)
15. **BATT-SWITCH-001** - Switch ($5.00 / $10.00)

All products are categorized under:
- **Category 10**: Battery Modules (for battery cells)
- **Category 8**: Components (for other parts)

## Features Implemented

### 1. View Products
- **URL**: `/products`
- **Permission**: `inventory.view`
- Displays all active products in a datatable
- Search and filter functionality

### 2. View Product Details
- **URL**: `/products/{id}`
- **Permission**: `inventory.view`
- Shows complete product information including:
  - Basic details (SKU, name, description, category)
  - Pricing (cost, selling price, margin calculation)
  - Stock levels by location
  - Reorder information
  - Documents & files

### 3. Edit Product
- **URL**: `/products/{id}/edit`
- **Permission**: `inventory.edit`
- Update product information:
  - Name, SKU, barcode
  - Product type, category
  - Pricing (cost, selling, tax rate)
  - Reorder levels
  - Status (active/inactive)

### 4. Delete Product (Soft Delete)
- **URL**: POST to `/products/{id}/delete`
- **Permission**: `inventory.delete`
- Moves product to trash (soft delete)
- Product data is preserved and can be restored
- Accessible via Action Menu on product detail page

### 5. View Trash
- **URL**: `/products/trash`
- **Permission**: `inventory.view`
- Lists all deleted products with:
  - SKU, name, category
  - Price and stock information
  - Deletion timestamp
- Actions available:
  - Restore product
  - Permanently delete product

### 6. Restore Product
- **URL**: POST to `/products/{id}/restore`
- **Permission**: `inventory.delete`
- Restores a soft-deleted product back to active status
- Product retains all original data

### 7. Permanent Delete
- **URL**: POST to `/products/{id}/force-delete`
- **Permission**: `inventory.delete`
- Permanently removes product from database
- **Warning**: This action cannot be undone
- Requires additional confirmation

## Action Menu Configuration

The product detail page uses the Action Menu component with the following actions:

| Action | Type | Permission | Order | Group |
|--------|------|------------|-------|-------|
| Edit Product | Link | inventory.edit | 10 | standard |
| Delete Product | Form Submit | inventory.delete | 15 | actions |
| *(divider)* | Divider | - | 20 | - |
| Back | Link | - | 30 | navigation |

Action menu data is stored in the `page_actions` table with `page_identifier = 'product_detail'`.

## File Changes

### Modified Files:
1. **views/products/index.php**
   - Added "View Trash" button in header

2. **views/products/trash.php**
   - Fixed column names to match product table schema
   - Uses `name` instead of `product_name`
   - Uses `selling_price` for pricing
   - Proper date formatting

3. **models/Product.php**
   - Added `onlyTrashed()` method override to include category join
   - Returns products with `category_name` from joined table

### Database Changes:
1. **products table**
   - 15 new battery component products inserted via migration
   - Migration file: `database/migrations/030_add_battery_components.sql`

2. **page_actions table**
   - Added "Delete Product" action for product_detail page
   - `page_identifier = 'product_detail'`
   - `action_type = 'form_submit'`
   - `action_target = 'products/{id}/delete'`

## Routes (Already Existed)

All necessary routes were already configured in `public/index.php`:

```php
$router->get('/products/trash', 'ProductController@trash');
$router->get('/products/create', 'ProductController@create');
$router->post('/products/store', 'ProductController@store');
$router->get('/products/([0-9]+)/edit', 'ProductController@edit');
$router->post('/products/([0-9]+)/update', 'ProductController@update');
$router->post('/products/([0-9]+)/delete', 'ProductController@delete');
$router->post('/products/([0-9]+)/restore', 'ProductController@restore');
$router->post('/products/([0-9]+)/force-delete', 'ProductController@forceDelete');
$router->get('/products/([0-9]+)', 'ProductController@show');
$router->get('/products', 'ProductController@index');
```

## Controller Methods (Already Existed)

The `ProductController` already had all necessary methods:
- `index()` - List products
- `show($id)` - View product details
- `create()` - Show create form
- `store()` - Save new product
- `edit($id)` - Show edit form
- `update($id)` - Update product
- `delete($id)` - Soft delete (move to trash)
- `trash()` - List deleted products
- `restore($id)` - Restore from trash
- `forceDelete($id)` - Permanently delete

## Usage Examples

### View All Products
Navigate to: `/products`

### View a Battery Component
Navigate to: `/products/{id}` (e.g., `/products/72` for BMS)

### Delete a Product
1. Navigate to product detail page
2. Click "Actions" dropdown
3. Click "Delete Product"
4. Confirm deletion
5. Product moves to trash

### Restore a Product
1. Navigate to: `/products/trash`
2. Find the deleted product
3. Click "Restore" button
4. Product is restored to active status

### Permanently Delete a Product
1. Navigate to: `/products/trash`
2. Find the deleted product
3. Click "Delete Forever" button
4. Confirm permanent deletion
5. Product is permanently removed (cannot be undone)

## Testing Checklist

- [x] All 15 battery components added to database
- [x] Products viewable in `/products` list
- [x] Product detail page shows all information
- [x] Edit product functionality works
- [x] Delete button in Action Menu
- [x] Soft delete moves to trash
- [x] Trash page displays deleted products
- [x] Restore functionality works
- [x] Permanent delete functionality works
- [x] Permissions checked for all actions

## Notes

- All products use soft delete by default via the `SoftDelete` trait
- The `deleted_at` column stores the deletion timestamp
- Products in trash are excluded from normal queries
- The Product model extends `BaseModel` and uses `SoftDelete` trait
- Action menu is database-driven via `page_actions` table
- All CRUD operations require appropriate permissions
