# Mwamba Loan Interest Calculation Fix

## Problem Identified

Several Mwamba loans had **incorrect interest calculations**. The loans were being charged interest based on an incorrect duration calculation instead of the actual loan duration.

### The Bug

Some loans were created with interest calculated using **1.2 months** instead of the actual loan duration:
- 60-day loans (2 months) were charged for 1.2 months
- 180-day loans (6 months) were charged for 1.2 months
- This resulted in significantly **undercharged interest**

### Root Cause

The issue appears to have been in an earlier version of the loan creation logic where the product's `duration_months` field (set to 2 for Mwamba) was being incorrectly used or there was a calculation error.

## Correct Calculation

For Mwamba loans with **10% monthly interest rate**:

```
Interest = Principal × 10% × (Duration in days ÷ 30)
```

### Examples:
- **60 days (2 months)**: Principal × 10% × 2 = **20% total interest**
- **90 days (3 months)**: Principal × 10% × 3 = **30% total interest**
- **180 days (6 months)**: Principal × 10% × 6 = **60% total interest**

This is **simple interest**, NOT compound interest:
- Month 1: 10% of principal
- Month 2: 10% of principal
- Month 3: 10% of principal
- Total: 30% of principal (for 3 months)

## Loans Fixed

### Fixed 6 Loans:

1. **TST41320** (60 days)
   - Principal: KES 110,000
   - Interest: KES 13,200 → **KES 22,000** (+KES 8,800)
   - Total: KES 126,500 → **KES 135,300**

2. **TST92965** (60 days)
   - Principal: KES 120,000
   - Interest: KES 14,400 → **KES 24,000** (+KES 9,600)
   - Total: KES 138,000 → **KES 147,600**

3. **TST62936** (60 days)
   - Principal: KES 130,000
   - Interest: KES 15,600 → **KES 26,000** (+KES 10,400)
   - Total: KES 149,500 → **KES 159,900**

4. **TST48982** (180 days)
   - Principal: KES 180,000
   - Interest: KES 21,600 → **KES 108,000** (+KES 86,400)
   - Total: KES 207,000 → **KES 293,400**

5. **TST29734** (180 days)
   - Principal: KES 190,000
   - Interest: KES 22,800 → **KES 114,000** (+KES 91,200)
   - Total: KES 218,500 → **KES 309,700**

6. **LOAN-000054** (90 days)
   - Principal: KES 100,000
   - Interest: KES 36,000 → **KES 30,000** (-KES 6,000)
   - Total: KES 139,000 → **KES 133,000**
   - Note: This loan was overcharged (12% rate was used instead of 10%)

## Current Status

✅ **All Mwamba loans now have correct interest calculations**

The current loan creation code in `loans/views.py` (line 822) correctly calculates:
```python
months = Decimal(str(max(1, math.ceil(requested_duration / 30))))
interest_amount = loan_product.calculate_interest(requested_amount, months)
```

## Verification

To verify any Mwamba loan's interest:
1. Get the principal amount
2. Get the duration in days
3. Calculate: `Principal × 0.10 × (Days ÷ 30)`
4. Compare with the loan's interest_amount

Example for 90-day loan with KES 100,000 principal:
```
100,000 × 0.10 × (90 ÷ 30) = 100,000 × 0.10 × 3 = 30,000
```

## Scripts Created

1. **check_all_mwamba_loans.py** - Analyzes all Mwamba loans and identifies incorrect calculations
2. **auto_fix_all_mwamba_loans.py** - Automatically fixes all Mwamba loans with incorrect interest
3. **update_mwamba_and_create_loan.py** - Creates sample Mwamba loans with correct 10% monthly interest

## Date Fixed
December 17, 2025
