# Deployment Guide: Fix Rollover Loan Total Amount Bug

## Overview
This deployment fixes the rollover loan total amount calculation bug where rollover_fee was being counted twice.

## Files to Upload to cPanel

Upload these files to your cPanel production directory:

1. **loans/models.py** - Contains the bug fix at line 1443
2. **fix_existing_rollover_totals.py** - Script to fix existing loan data

## Deployment Steps

### Step 1: Upload Fixed Code

Upload the modified `loans/models.py` file to:
```
/home/your-cpanel-user/branch-system/loans/models.py
```

### Step 2: Upload Data Migration Script

Upload `fix_existing_rollover_totals.py` to:
```
/home/your-cpanel-user/branch-system/fix_existing_rollover_totals.py
```

### Step 3: Restart Application

In cPanel, restart the Python application:
- Go to **Setup Python App**
- Click **Restart** button for your application

### Step 4: Fix Existing Loan Data

Connect to cPanel via SSH or use cPanel Terminal and run:

```bash
cd /home/your-cpanel-user/branch-system
python fix_existing_rollover_totals.py
```

Or to fix a specific loan (like LOAN-000069):
```bash
python fix_existing_rollover_totals.py LOAN-000069
```

### Step 5: Verify the Fix

1. **Check LOAN-000069** in the admin panel:
   - Total Amount should now be: KES 14,884.00
   - Outstanding Balance: KES 14,884.00 (if no payments)

2. **Verify the calculation**:
   ```
   Principal:      12,200.00
   Interest:        2,440.00
   Processing Fee:    244.00
   ─────────────────────────
   Total:          14,884.00 ✓
   ```

## Alternative: Quick SQL Fix for LOAN-000069

If you prefer, you can fix LOAN-000069 directly in the database:

```sql
-- Fix the loan
UPDATE loans 
SET total_amount = principal_amount + interest_amount + processing_fee 
WHERE loan_number = 'LOAN-000069';

-- Also fix the application
UPDATE loan_applications 
SET total_amount = (
    SELECT principal_amount + interest_amount + processing_fee 
    FROM loans 
    WHERE loans.application_id = loan_applications.id
)
WHERE id = (
    SELECT application_id 
    FROM loans 
    WHERE loan_number = 'LOAN-000069'
);
```

Or for ALL affected loans:

```sql
-- Fix all loans where total doesn't match components
UPDATE loans 
SET total_amount = principal_amount + interest_amount + processing_fee 
WHERE total_amount != (principal_amount + interest_amount + processing_fee);

-- Fix corresponding applications
UPDATE loan_applications la
JOIN loans l ON l.application_id = la.id
SET la.total_amount = l.total_amount
WHERE l.total_amount = l.principal_amount + l.interest_amount + l.processing_fee;
```

## Verification Queries

Check if any loans still have incorrect totals:

```sql
SELECT 
    loan_number,
    principal_amount,
    interest_amount,
    processing_fee,
    total_amount,
    (principal_amount + interest_amount + processing_fee) AS correct_total,
    (total_amount - (principal_amount + interest_amount + processing_fee)) AS difference
FROM loans 
WHERE total_amount != (principal_amount + interest_amount + processing_fee)
AND status = 'active'
ORDER BY created_at DESC;
```

## Rollback Plan (if needed)

If you need to rollback:

1. **Revert code**:
   - Restore the old `loans/models.py` from backup
   - Or change line 1443 back to:
     ```python
     total_amount = principal_amount + interest_amount + processing_fee + rollover_fee
     ```

2. **Revert data** (only if you ran the migration script):
   - You'll need to restore from a database backup
   - Or manually recalculate affected loans

## Support

If you encounter issues:
1. Check Django logs in cPanel
2. Verify the file upload was successful
3. Ensure the Python application restarted
4. Run the verification queries above
