# 🚨 URGENT: Deploy Rollover Fix to Production

## Error on Production
```
IntegrityError: (1062, "Duplicate entry '13112026-000009' for key 'loans.loan_number'")
URL: /loans/rollovers/eaded8df-35ba-4b19-b471-414adbe8d3c6/approve/
```

## Problem
**Production server is still running OLD code** that tries to create a new loan instead of updating the existing one.

## Solution
Upload the fixed `loans/models.py` and `loans/views.py` to production immediately.

## Quick Deployment Steps

### Option 1: Via cPanel File Manager (Fastest)

1. **Go to cPanel → File Manager**
2. **Navigate to**: `/home/xygbfpsg/phingrazuri/loans/`
3. **Upload** the fixed `loans/models.py` file (from your local `c:\Users\BEST\Desktop\grazuri\loans\models.py`)
4. **Navigate to**: `/home/xygbfpsg/phingrazuri/loans/`
5. **Upload** the fixed `loans/views.py` file
6. **Restart Django**:
   - Navigate to `/home/xygbfpsg/phingrazuri/tmp/`
   - Create or touch `restart.txt` file

### Option 2: Via FTP/SFTP

1. **Connect to**: grazuri.uzuriapps.xyz
2. **Upload files**:
   - Local: `c:\Users\BEST\Desktop\grazuri\loans\models.py`
   - Remote: `/home/xygbfpsg/phingrazuri/loans/models.py`
   
   - Local: `c:\Users\BEST\Desktop\grazuri\loans\views.py`
   - Remote: `/home/xygbfpsg/phingrazuri/loans/views.py`

3. **Restart Django**: Touch `/home/xygbfpsg/phingrazuri/tmp/restart.txt`

### Option 3: Via SSH (If available)

```bash
# SSH into server
ssh username@grazuri.uzuriapps.xyz

# Navigate to project
cd ~/phingrazuri

# Backup current files
cp loans/models.py loans/models.py.backup.$(date +%Y%m%d_%H%M%S)
cp loans/views.py loans/views.py.backup.$(date +%Y%m%d_%H%M%S)

# Upload new files (via scp from local machine):
# From local machine:
scp c:\Users\BEST\Desktop\grazuri\loans\models.py username@grazuri.uzuriapps.xyz:~/phingrazuri/loans/
scp c:\Users\BEST\Desktop\grazuri\loans\views.py username@grazuri.uzuriapps.xyz:~/phingrazuri/loans/

# Restart Django
touch tmp/restart.txt
```

## Files to Upload

### 1. loans/models.py
**Local path**: `c:\Users\BEST\Desktop\grazuri\loans\models.py`  
**Remote path**: `/home/xygbfpsg/phingrazuri/loans/models.py`

**Key changes**:
- Line ~1777: `RolloverRequest.approve()` method
- Updates existing loan instead of creating new one
- Uses custom interest/fee from rollover request

### 2. loans/views.py
**Local path**: `c:\Users\BEST\Desktop\grazuri\loans\views.py`  
**Remote path**: `/home/xygbfpsg/phingrazuri/loans/views.py`

**Key changes**:
- Line ~2726: Updated success message

## After Deployment

### 1. Verify Files Uploaded
Check file modification dates on server to confirm upload.

### 2. Restart Django
**CRITICAL**: Django must be restarted to load new code!

Via cPanel:
- Application → Python Apps → Restart

Or via SSH:
```bash
touch ~/phingrazuri/tmp/restart.txt
```

### 3. Test the Fix
1. Go to: https://grazuri.uzuriapps.xyz/loans/rollovers/
2. Try to approve the pending rollover
3. Should work without IntegrityError
4. Verify loan was updated (same number, new amounts)

## Verification Commands

### Check if new code is loaded:
```python
# SSH into server and run:
cd ~/phingrazuri
python manage.py shell

# In Python shell:
from loans.models import RolloverRequest
import inspect
source = inspect.getsource(RolloverRequest.approve)
print("UPDATE" in source)  # Should print True if new code loaded
```

## Current Situation

**Rollover Request ID**: `eaded8df-35ba-4b19-b471-414adbe8d3c6`  
**Loan Number**: `13112026-000009`  
**Status**: Pending (failed to approve)

**What's happening**:
- Old code tries to create NEW loan with number `13112026-000009`
- But loan `13112026-000009` already exists
- Result: Duplicate key error

**What will happen after fix**:
- New code will UPDATE existing loan `13112026-000009`
- No new loan created = no duplicate error
- Rollover approval succeeds

## Rollback Plan (If Needed)

If deployment causes issues:
```bash
# Restore backups
cp loans/models.py.backup.YYYYMMDD_HHMMSS loans/models.py
cp loans/views.py.backup.YYYYMMDD_HHMMSS loans/views.py

# Restart
touch tmp/restart.txt
```

## Urgency Level

🚨 **HIGH** - Users cannot approve rollovers until this is fixed

## Estimated Deployment Time

- **2-5 minutes** (file upload + restart)

## Success Criteria

✅ Rollover approvals work without IntegrityError  
✅ Existing loan gets updated (not new loan created)  
✅ Loan number stays the same  
✅ Custom interest amounts respected  

---

**Next Action**: Upload the two files and restart Django NOW!
