# Production Deployment - Original Amounts Tracking

## Quick Start

### 1. Edit Database Credentials

Open `migrate_original_amounts_direct.py` and edit these lines (around line 11):

```python
DB_CONFIG = {
    'host': 'localhost',           # Your MySQL host
    'user': 'root',                # Your MySQL username
    'password': 'YOUR_PASSWORD',   # Your MySQL password
    'database': 'xygbfpsg_loans',  # Your database name
    'charset': 'utf8mb4',
    'cursorclass': pymysql.cursors.DictCursor
}
```

### 2. Run the Migration Script

```bash
python migrate_original_amounts_direct.py
```

This will:
- ✓ Add `original_interest_amount` column
- ✓ Add `original_processing_fee` column
- ✓ Populate them for all existing loans
- ✓ Show verification results

### 3. Upload Updated Code Files

Upload these files to production:
```
loans/models.py
reports/views.py
templates/loans/loan_detail.html
```

### 4. Run Django Migrations

```bash
python manage.py makemigrations
python manage.py migrate
```

### 5. Restart Application

```bash
# Your restart command here
systemctl restart gunicorn
# or
touch wsgi.py
```

### 6. Verify

Go to: https://grazuri.uzuriapps.xyz/reports/disbursed-loans/

Check that rolled-over loans show their ORIGINAL interest and fees!

---

## What This Does

**Before:**
- Disbursement report showed rolled-over interest and fees (wrong!)

**After:**
- Disbursement report shows original interest and fees (correct!)

**Example - Faith's Loan:**
- Before: Interest KES 21,000 (rollover amount)
- After: Interest KES 30,000 (original amount)

---

## Files Changed

1. **loans/models.py** - Added 2 new fields + preservation logic
2. **reports/views.py** - Use original values in disbursement report
3. **templates/loans/loan_detail.html** - Approval modal improvements
4. **migrate_original_amounts_direct.py** - Database migration script

---

## Troubleshooting

### "Access denied for user"
- Check DB_CONFIG username and password
- Ensure user has ALTER and UPDATE privileges

### "pymysql module not found"
```bash
pip install pymysql
```

### "Table doesn't exist"
- Check DB_CONFIG database name
- Ensure you're connecting to the right database

---

## Rollback (if needed)

If something goes wrong:

```sql
ALTER TABLE loans DROP COLUMN original_interest_amount;
ALTER TABLE loans DROP COLUMN original_processing_fee;
```

Then restore the old code files.

---

## Support

If you encounter any issues:
1. Check the script output for specific error messages
2. Verify database credentials are correct
3. Ensure MySQL user has necessary privileges
4. Check that pymysql is installed (`pip list | grep pymysql`)
