# Product Destruction Feature

## Overview
This feature allows users to mark expired inventory products for destruction and manage the destruction process.

## Features Implemented

### 1. Database Schema
Added new columns to `inventory_details` table:
- `marked_for_destruction` (boolean) - Indicates if product is marked for destruction
- `destroyed` (boolean) - Indicates if product has been destroyed
- `destroyed_at` (timestamp) - Date and time when product was destroyed
- `destruction_file` (string) - Path to the destruction record file

### 2. User Interface

#### Inventario Vencido Tab
- Added checkbox "Habilitar para destrucción" to enable destruction mode
- When enabled, batch modals show a "Para destrucción" checkbox for each product
- Products marked for destruction are saved when the modal is closed

#### Productos a Destruir Tab
- Shows all products marked for destruction
- Displays: Country, Distributor, SKU, Description, Batch, Quantity, Expiration Date, Marked Date
- Checkboxes to select products for destruction
- "Destruir seleccionados" button to initiate destruction process
- Badge showing total count of marked products

#### Productos Destruidos Section
- Accordion-style display of destroyed products grouped by destruction date
- Each accordion item shows:
  - Destruction date
  - Number of products destroyed
  - Link to view destruction record file
  - List of all products destroyed on that date

### 3. Destruction Process

1. **Enable Destruction Mode**: Check "Habilitar para destrucción" in the expired inventory tab
2. **Mark Products**: Open batch details modal and check "Para destrucción" for desired products
3. **Review Marked Products**: Go to "Productos a destruir" tab to review marked products
4. **Confirm Destruction**:
   - Select products to destroy using checkboxes
   - Click "Destruir seleccionados"
   - Upload required destruction record file (PDF, JPG, PNG, DOC, DOCX)
   - Confirm the action (irreversible)
5. **View Destroyed Products**: Destroyed products appear in the "Productos destruidos" section

### 4. Backend Endpoints

- `POST /bitacora-dispositivos/mark-for-destruction` - Mark products for destruction
- `POST /bitacora-dispositivos/unmark-for-destruction` - Unmark products
- `GET /bitacora-dispositivos/get-marked-for-destruction` - Get marked products list
- `POST /bitacora-dispositivos/confirm-destruction` - Confirm destruction with file upload
- `GET /bitacora-dispositivos/get-destroyed-products` - Get destroyed products grouped by date

### 5. Data Filtering

- Destroyed products are automatically filtered out from expired inventory listings
- Marked products (not yet destroyed) are also filtered from batch modals in expired inventory
- This prevents re-processing of products already in the destruction pipeline

## Installation

### 1. Run Database Migration

You need to add the new columns to the `inventory_details` table. Run the migration script:

```bash
# Navigate to the project root
cd d:\laragon\www\merck

# Run the migration using PHP CLI (if available)
php add_destruction_columns.php

# OR use Laragon's PHP
d:\laragon\bin\php\php-8.x.x-Win32-vs16-x64\php.exe add_destruction_columns.php
```

Alternatively, you can run the SQL directly in your database:

```sql
ALTER TABLE `inventory_details`
ADD COLUMN `marked_for_destruction` TINYINT(1) DEFAULT 0 AFTER `quantity`,
ADD COLUMN `destroyed` TINYINT(1) DEFAULT 0 AFTER `marked_for_destruction`,
ADD COLUMN `destroyed_at` TIMESTAMP NULL DEFAULT NULL AFTER `destroyed`,
ADD COLUMN `destruction_file` VARCHAR(255) NULL DEFAULT NULL AFTER `destroyed_at`;
```

### 2. Create Upload Directory

The system needs a directory to store destruction record files:

```bash
mkdir -p public/uploads/destruction_records
chmod 755 public/uploads/destruction_records
```

On Windows (PowerShell):
```powershell
New-Item -ItemType Directory -Force -Path "public\uploads\destruction_records"
```

### 3. Clear Cache (if applicable)

If your application uses caching, clear it to ensure new routes are recognized.

## File Changes

### Modified Files:
1. `app/Controllers/LogDispController.php` - Added destruction endpoints and filters
2. `app/views/log-disp-global.php` - Added new tab and UI elements
3. `public/assets/js/bitacora-dispositivos-global.js` - Added destruction functionality
4. `public/index.php` - Added new routes

### New Files:
1. `add_destruction_columns.php` - Database migration script

## Usage Notes

- Only expired products can be marked for destruction
- Destruction requires uploading a supporting document
- Once products are destroyed, the action cannot be undone
- Destroyed products are permanently marked and removed from active inventory listings
- Users can view historical destruction records organized by date
- The system supports roles with different permissions (configurable based on your role system)

## Security Considerations

- File uploads are validated for allowed types (PDF, JPG, PNG, DOC, DOCX)
- User country restrictions apply to destruction operations (non-admin users)
- Destruction files are stored with unique timestamps to prevent conflicts

## Troubleshooting

### Migration Issues
If the migration script fails:
- Check database connection settings
- Verify user has ALTER TABLE permissions
- Run SQL directly in phpMyAdmin or MySQL client

### Upload Directory Issues
If file uploads fail:
- Verify directory exists: `public/uploads/destruction_records/`
- Check directory permissions
- Ensure web server has write access

### Products Not Showing/Hiding
- Clear browser cache
- Check JavaScript console for errors
- Verify routes are properly registered in index.php

## Future Enhancements

Possible improvements for future versions:
- Export destruction records to Excel
- Email notifications when products are destroyed
- Bulk destruction operations with batch file uploads
- Destruction approval workflow for multi-level authorization
- Archive destroyed products to separate historical table
