# Boost Plus Loan Calculation Explained

## Overview

Boost Plus is a flexible loan product with **monthly-based interest and processing fees** calculated based on the loan duration.

## Correct Calculation Formula

```
Total Amount = Principal + Interest + Processing Fee
```

Where:
- **Principal**: The base loan amount
- **Interest**: Calculated monthly based on duration
- **Processing Fee**: Calculated monthly based on duration (for Boost Plus only)

## Example: LOAN-000096

### Loan Details
- **Product**: Boost Plus
- **Principal**: KES 15,000.00
- **Duration**: 60 days (2 months)
- **Interest Rate**: 20% per month
- **Processing Fee**: 2% per month

### Step-by-Step Calculation

#### 1. Calculate Months
```
Duration in months = 60 days ÷ 30 days = 2 months
```

#### 2. Calculate Interest
```
Interest = Principal × Interest Rate × Months
Interest = 15,000 × 20% × 2
Interest = 15,000 × 0.20 × 2
Interest = KES 6,000.00
```

#### 3. Calculate Processing Fee
**For Boost Plus, processing fee is charged monthly:**
```
Processing Fee = Principal × Processing Fee Rate × Months
Processing Fee = 15,000 × 2% × 2
Processing Fee = 15,000 × 0.02 × 2
Processing Fee = KES 600.00
```

#### 4. Calculate Total
```
Total Amount = Principal + Interest + Processing Fee
Total Amount = 15,000 + 6,000 + 600
Total Amount = KES 21,600.00 ✅
```

## Why the Bug Showed KES 18,300

The bug in the edit_loan function was calculating:
```python
# WRONG CALCULATION
total_amount = principal_amount + interest_amount
total_amount = 15,000 + 6,000
total_amount = KES 21,000  # But somehow showed 18,300
```

The system was **missing the processing fee** entirely, and there may have been additional calculation errors.

## Boost Plus vs Other Products

### Boost Plus (Monthly Fees)
- **Interest**: Charged monthly (20% × months)
- **Processing Fee**: Charged monthly (2% × months)
- **Example (60 days)**:
  - Interest: 15,000 × 20% × 2 = 6,000
  - Processing Fee: 15,000 × 2% × 2 = 600
  - Total: 21,600

### Other Products (One-Time Fee)
- **Interest**: Charged monthly (20% × months)
- **Processing Fee**: One-time charge (2% × 1)
- **Example (60 days)**:
  - Interest: 15,000 × 20% × 2 = 6,000
  - Processing Fee: 15,000 × 2% × 1 = 300
  - Total: 21,300

## Code Implementation

The calculation is handled in `loans/models.py`:

```python
def calculate_processing_fee(self, amount, months=1):
    """Calculate processing fee for given amount and duration"""
    fee_rate = Decimal(str(self.get_processing_fee())) / Decimal('100')
    base_fee = Decimal(str(amount)) * fee_rate
    
    # For Boost Plus, processing fee is charged monthly like interest
    if self.product_type == 'boost_plus':
        return base_fee * Decimal(str(months))
    
    # For other products, processing fee is one-time
    return base_fee
```

## Verification Checklist

To verify Boost Plus calculations are correct:

1. **Check the loan product settings:**
   - Interest Rate: 20% per month
   - Processing Fee: 2% per month
   - Duration: Flexible (7, 14, 30, 60, 90 days)

2. **Verify a 60-day loan:**
   - Principal: 15,000
   - Interest: 6,000 (20% × 2 months)
   - Processing Fee: 600 (2% × 2 months)
   - Total: 21,600 ✅

3. **Test different durations:**
   - 30 days (1 month): 15,000 + 3,000 + 300 = 18,300
   - 60 days (2 months): 15,000 + 6,000 + 600 = 21,600
   - 90 days (3 months): 15,000 + 9,000 + 900 = 24,900

## Common Mistakes

### ❌ Mistake 1: Forgetting Processing Fee
```
Total = Principal + Interest
Total = 15,000 + 6,000 = 21,000  # WRONG!
```

### ❌ Mistake 2: One-Time Processing Fee for Boost Plus
```
Processing Fee = 15,000 × 2% × 1 = 300  # WRONG for Boost Plus!
Should be: 15,000 × 2% × 2 = 600
```

### ❌ Mistake 3: Wrong Month Calculation
```
Months = 60 days ÷ 31 days = 1.93 months  # WRONG!
Should be: 60 days ÷ 30 days = 2 months
```

## Summary

✅ **Correct Formula:**
```
Total = Principal + (Principal × Interest Rate × Months) + (Principal × Processing Fee Rate × Months)
```

✅ **For 60-day Boost Plus loan of KES 15,000:**
```
Total = 15,000 + (15,000 × 20% × 2) + (15,000 × 2% × 2)
Total = 15,000 + 6,000 + 600
Total = KES 21,600.00
```

This is now correctly implemented in the system after the bug fix!
