# Customer Requests Table Fix

## Problem
The production server is showing this error when accessing `/reports/customer-requests/`:

```
ProgrammingError at /reports/customer-requests/
(1146, "Table 'acbptxvs_branch_system.customer_requests' doesn't exist")
```

## Root Cause
The production database has two related issues:

1. **Migration Dependency Conflict**: `admin.0001_initial` was applied before its dependency `users.0001_initial`, causing Django to refuse to run any migrations.

2. **Missing Tables**: The `customer_requests` table and other enhanced models tables don't exist because the migration `0002_enhanced_reports_models.py` couldn't be applied due to the dependency issue.

## Missing Tables
The following tables are missing from production:
- `customer_requests`
- `registration_fees`
- `registration_fee_payments`
- `report_schedules`
- `report_executions`

## Solution Options

### Option 1: Fix Migration Dependencies (Recommended)
Upload `fix_migration_dependency_issue.py` to your production server and run:
```bash
python fix_migration_dependency_issue.py
```

This script will:
1. Fix the migration dependency conflict
2. Mark `users.0001_initial` as applied
3. Create missing tables manually if needed
4. Verify the fix works
5. Test the customer requests endpoint

### Option 2: SQL-Only Fix (Fastest)
If you have direct database access, run the SQL script:
```bash
mysql -u your_username -p your_database < fix_migration_sql_only.sql
```

This will:
- Fix the migration dependency issue
- Create all missing tables
- Insert sample data
- Mark migrations as applied

### Option 3: Manual Steps
1. Fix the dependency issue:
   ```bash
   python manage.py migrate users 0001_initial --fake
   ```
2. Then run the reports migration:
   ```bash
   python manage.py migrate reports
   ```

### Option 4: Original Production Fix
If the dependency issue is already resolved, use:
```bash
python fix_production_customer_requests.py
```

## Files Created for This Fix

1. **`fix_migration_dependency_issue.py`** - Fixes migration dependency conflict (RECOMMENDED)
2. **`fix_migration_sql_only.sql`** - SQL-only fix for direct database execution (FASTEST)
3. **`fix_production_customer_requests.py`** - Original production fix script
4. **`create_customer_requests_table.sql`** - Manual SQL table creation script
5. **`fix_customer_requests_table.py`** - Local development fix script
6. **`CUSTOMER_REQUESTS_FIX_README.md`** - This documentation

## What Was Fixed

### 1. Model Import Issue
Added proper import of enhanced models in `reports/models.py`:
```python
# Import enhanced models to ensure they're registered with Django
from .enhanced_models import *
```

### 2. Migration Status
The migration `0002_enhanced_reports_models.py` exists and includes all the required models, but wasn't applied to production.

### 3. Database Tables
The following tables need to be created in production:

#### customer_requests
- Stores customer service requests and inquiries
- Used by the `/reports/customer-requests/` endpoint

#### registration_fees
- Stores registration fees for different products
- Used by registration fee reports

#### registration_fee_payments
- Tracks registration fee payments
- Used by payment tracking reports

#### report_schedules
- Manages scheduled report generation
- Used by automated reporting system

#### report_executions
- Tracks report execution history
- Used by report monitoring

## Testing the Fix

After applying the fix, test these URLs:
- `https://branchbusinessadvance.co.ke/reports/customer-requests/`
- `https://branchbusinessadvance.co.ke/reports/registration-fees/`
- `https://branchbusinessadvance.co.ke/reports/`

The customer requests page should no longer show the table error and should either:
- Redirect to login (if not authenticated)
- Show the customer requests list (if authenticated)

## Prevention

To prevent this issue in the future:
1. Always run `python manage.py migrate` after deploying new code
2. Check migration status with `python manage.py showmigrations`
3. Test critical endpoints after deployment
4. Keep development and production databases in sync

## Sample Data

The SQL script includes sample registration fees:
- Boost Registration Fee: KES 500
- Boost Plus Registration Fee: KES 750
- Mwamba Registration Fee: KES 1,000
- Imara Registration Fee: KES 1,500
- Account Opening Fee: KES 200
- Document Processing Fee: KES 100
- Statement Request Fee: KES 50

## Support

If you continue to experience issues after applying this fix:
1. Check database connection settings
2. Verify user permissions for database operations
3. Review Django settings configuration
4. Check server logs for additional error details

The fix should resolve the "Table doesn't exist" error and make the customer requests functionality fully operational.