# Amortization Schedule and Days Overdue Fixes

## Issues Fixed

### 1. Amortization Schedule - Processing Fee in Beginning Balance

**Problem:**
- The amortization schedule was including the processing fee in the beginning balance
- This made the beginning balance show KES 16,712.78 instead of KES 16,438.80
- Processing fee should be a one-time charge on the first payment, not part of the amortizing balance

**Solution:**
- Modified `loans/models.py` - `get_amortization_schedule()` method
- Changed the amortizing amount to only include principal + interest
- Processing fee is now only added to the first payment's scheduled payment
- Beginning balance now correctly shows only the principal + interest that needs to be repaid over time

**Changes:**
```python
# Before: Processing fee was part of amortizing balance
total_amount = principal + interest_amount + processing_fee_amount
remaining_balance = total_amount

# After: Processing fee is one-time, not amortized
amortizing_amount = principal + interest_amount
remaining_balance = amortizing_amount
processing_fee_first_payment = processing_fee_amount  # Only on first payment
```

### 2. Days Overdue Calculation

**Problem:**
- Loan LOAN-000036 showed "5 days overdue" when the due date was Dec 16, 2025
- Today is Nov 22, 2025 - that's 24 days in the FUTURE, not overdue!
- The calculation was doing `(today - due_date).days` without checking if due_date is in the past
- This resulted in negative days being treated as overdue

**Solution:**
- Modified `loans/repayment_scheduler.py` - `get_missed_payment_periods()` method
- Added check to ensure due date has actually passed before counting as overdue
- Used `max(0, (date.today() - due_date).days)` to prevent negative values
- Only add to missed_periods if `due_date <= date.today()`

**Changes:**
```python
# Before: Counted future dates as overdue
days_overdue = (date.today() - due_date).days
missed_periods.append({...})

# After: Only count if due date has passed
days_overdue = max(0, (date.today() - due_date).days)
if due_date <= date.today():
    missed_periods.append({...})
```

## Expected Results

### For LOAN-000036:

**Amortization Schedule:**
- Principal Amount: KES 13,699.00
- Interest Amount: KES 2,739.80
- Processing Fee: KES 273.98 (one-time, first payment only)
- Total Amount: KES 16,712.78
- Beginning Balance (Payment 1): KES 16,438.80 (principal + interest only)
- First Payment: KES 821.94 (includes KES 273.98 processing fee)
- Subsequent Payments: KES 547.96 (no processing fee)

**Days Overdue:**
- Due Date: Dec 16, 2025
- Today: Nov 22, 2025
- Days Overdue: 0 (not overdue, due in 24 days)
- Status: Active (not overdue)

## Files Modified

1. `loans/models.py` - `get_amortization_schedule()` method
2. `loans/repayment_scheduler.py` - `get_missed_payment_periods()` method

## Testing

To verify the fixes:
1. View the amortization schedule for LOAN-000036
2. Check that beginning balance excludes processing fee
3. Check that processing fee only appears in first payment
4. View loan detail page
5. Verify "days overdue" is 0 or not shown
6. Verify loan is not marked as overdue

## Deployment

These changes should be deployed to production to fix the incorrect calculations.
