# Rollover Enhancements Deployment Guide

This guide provides multiple options for deploying the rollover enhancements to production, including fixes for migration issues.

## 🚨 Migration Issue Fix

The error you're encountering is due to an inconsistent migration history. The script will automatically fix this by:

1. Removing the problematic migration record
2. Adding the `rollover_date` column directly to the database
3. Creating the proper migration record
4. Running Django migrations

## 📋 Deployment Options

### Option 1: Simple One-Liner (Recommended)

```bash
cd /home/acbptxvs/public_html/branchbusinessadvance.co.ke
source /home/acbptxvs/virtualenv/public_html/branchbusinessadvance.co.ke/3.13/bin/activate
python deploy_rollover_simple.py
```

### Option 2: Full Python Script

```bash
cd /home/acbptxvs/public_html/branchbusinessadvance.co.ke
source /home/acbptxvs/virtualenv/public_html/branchbusinessadvance.co.ke/3.13/bin/activate
python deploy_rollover_enhancements.py
```

### Option 3: Shell Script (Linux/Mac)

```bash
cd /home/acbptxvs/public_html/branchbusinessadvance.co.ke
chmod +x deploy_rollover_enhancements.sh
./deploy_rollover_enhancements.sh
```

### Option 4: PowerShell Script (Windows)

```powershell
cd /home/acbptxvs/public_html/branchbusinessadvance.co.ke
./deploy_rollover_enhancements.ps1
```

## 🔧 Manual Steps (If Scripts Fail)

If the automated scripts fail, you can run these commands manually:

### Step 1: Fix Migration History

```sql
DELETE FROM django_migrations WHERE app = 'users' AND name = '0011_manual_add_is_default';
```

### Step 2: Add rollover_date Column

```sql
ALTER TABLE rollover_requests 
ADD COLUMN rollover_date DATE NULL 
COMMENT 'Preferred rollover date';
```

### Step 3: Create Migration Record

```sql
INSERT INTO django_migrations (app, name, applied) 
VALUES ('loans', '0018_add_rollover_date_field', NOW());
```

### Step 4: Run Django Migrations

```bash
python manage.py migrate --noinput
```

## ✅ Verification

After deployment, verify the installation by running:

```python
python -c "
import os
import django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'branch_system.settings')
django.setup()

from loans.models import RolloverRequest
fields = [field.name for field in RolloverRequest._meta.fields]
print('SUCCESS: rollover_date field found!' if 'rollover_date' in fields else 'ERROR: rollover_date field missing!')
"
```

## 🎯 Features Deployed

After successful deployment, you'll have:

1. **Enhanced Rollover Form** with:
   - New Loan Amount field
   - New Duration field
   - Custom Interest Rate field (optional)
   - Custom Processing Fee field (optional)
   - **Rollover Date field (NEW!)**
   - Reason for Rollover field

2. **Complete History Button** in:
   - Client popup modal (in `/clients/` page)
   - Individual loan detail pages (e.g., `/loans/490535aa-1c0f-423d-a172-204451619a70/`)

3. **Database Updates**:
   - `rollover_date` column added to `rollover_requests` table
   - Migration history fixed

## 🚨 Troubleshooting

### If you get "Unknown column" error:
- The script will automatically add the missing column
- Run the deployment script again

### If you get migration history errors:
- The script will automatically fix the migration history
- This is a one-time fix

### If you get permission errors:
- Make sure you're running as the correct user
- Check file permissions on the project directory

## 📞 Support

If you encounter any issues:

1. Check the error messages carefully
2. Try the manual steps above
3. Verify your Django settings and database connection
4. Ensure you're in the correct virtual environment

The deployment scripts are designed to be safe and will not damage existing data. They only add new functionality and fix migration issues.
