# Loan Database Fix - Outdated Interest and Processing Fee Values

## Problem
LOAN-000193 (and other Mwamba loans) have OUTDATED values stored in the database:
- **Database stored**: KES 29,344.00 (WRONG - old rates)
- **Should be**: KES 35,370.00 (CORRECT - current rates)

The database contains old interest and processing fee amounts that don't match the current system rates.

## Root Cause
When system settings for Mwamba product were updated (interest rate changed from 10% to 30%, processing fee from 2% to 5%), existing loans in the database were NOT updated. They still have the old calculated values.

## Example: LOAN-000193
### Currently Stored in Database (Wrong - Old Rates)
- Principal: KES 26,200.00
- Interest: KES 2,620.00 (10% - OLD rate)
- Processing Fee: KES 524.00 (2% - OLD rate)
- **Total: KES 29,344.00** ✗

### Should Be (Correct - Current Rates)
- Principal: KES 26,200.00
- Interest: KES 7,860.00 (30% - CURRENT rate)
- Processing Fee: KES 1,310.00 (5% - CURRENT rate)
- **Total: KES 35,370.00** ✓

## Solution
Update the database to recalculate interest_amount, processing_fee, and total_amount for all loans using current system rates.

## How to Fix

### Option 1: Python Script (Recommended)
```bash
python fix_loan_database_values.py
```

This script will:
1. Check all loans for incorrect amounts
2. Recalculate using current system rates
3. Update the database
4. Show a summary of changes

### Option 2: SQL Script
```bash
mysql -u acbptxvs_phin -p acbptxvs_branch_system < fix_loan_amounts_sql.sql
```

Or run in phpMyAdmin.

## What Gets Updated
For each loan, the script updates:
- `interest_amount` = principal × current interest rate
- `processing_fee` = principal × current processing fee rate
- `total_amount` = principal + interest_amount + processing_fee

## Verification
After running the fix, LOAN-000193 should show:
- Interest Amount: KES 7,860.00
- Processing Fee: KES 1,310.00
- Total Amount: KES 35,370.00

## Important Notes
1. **This updates the database** - Loan terms will be recalculated
2. **Uses current system rates** - All loans updated to current product rates
3. **Affects all loans** - Not just Mwamba, but all products
4. **Backup recommended** - Consider backing up the loans table first

## Display Logic
The application correctly uses `get_display_interest_amount()` and `get_display_processing_fee_amount()` methods which calculate based on current rates. After this fix, the database values will match what's displayed.

