# Penalty System - Implementation Checklist

## Pre-Deployment Checklist

### 1. Database Setup
- [ ] Run migrations: `python manage.py migrate`
- [ ] Verify new fields in `loan_products` table
- [ ] Verify new fields in `penalty_charges` table
- [ ] Backup database before deployment

### 2. Dependencies
- [ ] Install Celery: `pip install celery`
- [ ] Install Redis: `pip install redis`
- [ ] Install Redis server (or use Docker)
- [ ] Test Redis connection: `redis-cli ping`

### 3. Configuration
- [ ] Run setup script: `python setup_penalty_system.py`
- [ ] Verify loan products have penalty settings
- [ ] Review penalty rates for each product
- [ ] Adjust penalty modes if needed (auto/manual)
- [ ] Adjust penalty frequencies if needed

### 4. Celery Setup
- [ ] Verify `branch_system/celery.py` exists
- [ ] Verify `branch_system/__init__.py` imports celery
- [ ] Add Celery settings to `settings.py`:
  ```python
  CELERY_BROKER_URL = 'redis://localhost:6379/0'
  CELERY_RESULT_BACKEND = 'redis://localhost:6379/0'
  ```
- [ ] Test Celery worker: `celery -A branch_system worker -l info`
- [ ] Test Celery beat: `celery -A branch_system beat -l info`

### 5. Testing
- [ ] Test automatic penalty calculation
- [ ] Test manual penalty application
- [ ] Test penalty deletion (manual only)
- [ ] Test duplicate prevention
- [ ] Verify audit logs are created
- [ ] Test with different loan products
- [ ] Test with different penalty frequencies

### 6. UI Verification
- [ ] Penalty section shows on loan detail page
- [ ] "Apply Penalty" button appears for overdue loans
- [ ] Modal opens and closes correctly
- [ ] Suggested penalty calculates correctly
- [ ] Penalty list displays correctly
- [ ] Auto/Manual badges show correctly
- [ ] Delete button works for manual penalties

### 7. Production Setup
- [ ] Configure Supervisor/systemd for Celery
- [ ] Set up Celery log rotation
- [ ] Configure Redis persistence
- [ ] Set up monitoring for Celery tasks
- [ ] Configure alerts for failed tasks
- [ ] Document production URLs and credentials

## Post-Deployment Checklist

### Day 1
- [ ] Verify Celery worker is running
- [ ] Verify Celery beat is running
- [ ] Check Redis is running
- [ ] Monitor first automatic penalty run (1:00 AM)
- [ ] Review penalties applied
- [ ] Check for any errors in logs
- [ ] Verify audit logs are created

### Week 1
- [ ] Monitor daily penalty application
- [ ] Review penalty amounts for accuracy
- [ ] Check for duplicate penalties
- [ ] Verify no missed penalties
- [ ] Review staff feedback on manual penalties
- [ ] Check system performance
- [ ] Review Celery task execution times

### Month 1
- [ ] Analyze penalty patterns
- [ ] Review penalty rates for appropriateness
- [ ] Check borrower feedback
- [ ] Verify compliance with regulations
- [ ] Review audit trail completeness
- [ ] Optimize if needed

## Training Checklist

### For Administrators
- [ ] How to view penalties on loan detail page
- [ ] How to apply manual penalties
- [ ] How to delete manual penalties
- [ ] How to view penalty history
- [ ] Understanding penalty calculations
- [ ] When to use manual vs automatic
- [ ] How to check if system is running

### For Support Staff
- [ ] How to explain penalties to borrowers
- [ ] How to check penalty status
- [ ] How to escalate penalty issues
- [ ] Understanding penalty modes
- [ ] How to view audit logs

## Monitoring Checklist

### Daily
- [ ] Check Celery worker status
- [ ] Check Celery beat status
- [ ] Review penalties applied overnight
- [ ] Check for any error logs
- [ ] Verify Redis is running

### Weekly
- [ ] Review total penalties applied
- [ ] Check for any anomalies
- [ ] Review manual penalties applied
- [ ] Check audit log completeness
- [ ] Review system performance

### Monthly
- [ ] Analyze penalty trends
- [ ] Review penalty rates
- [ ] Check compliance
- [ ] Review borrower complaints
- [ ] Optimize settings if needed

## Troubleshooting Checklist

### Penalties Not Being Applied
- [ ] Check Celery worker is running
- [ ] Check Celery beat is running
- [ ] Check Redis is running
- [ ] Verify loan products have `penalty_mode='auto'`
- [ ] Check loans are actually overdue
- [ ] Review Celery logs for errors
- [ ] Check database connectivity

### Manual Penalty Issues
- [ ] Verify user is staff
- [ ] Check loan is overdue
- [ ] Verify form validation
- [ ] Check database permissions
- [ ] Review application logs

### Duplicate Penalties
- [ ] Check duplicate prevention logic
- [ ] Review `applied_date` field
- [ ] Check Celery task execution
- [ ] Verify timezone settings

## Rollback Plan

If issues occur:

1. **Stop Celery Services**
   ```bash
   # Stop worker and beat
   pkill -f "celery worker"
   pkill -f "celery beat"
   ```

2. **Disable Automatic Penalties**
   ```sql
   UPDATE loan_products SET penalty_mode = 'manual';
   ```

3. **Remove Incorrect Penalties** (if needed)
   ```sql
   -- Backup first!
   DELETE FROM penalty_charges 
   WHERE applied_date >= 'YYYY-MM-DD' 
   AND is_automatic = TRUE;
   ```

4. **Restore from Backup** (if necessary)
   ```bash
   # Restore database backup
   mysql -u user -p database < backup.sql
   ```

5. **Investigate and Fix**
   - Review logs
   - Identify root cause
   - Apply fix
   - Test thoroughly

6. **Redeploy**
   - Run migrations if needed
   - Restart Celery services
   - Monitor closely

## Sign-Off

### Development Team
- [ ] Code reviewed
- [ ] Tests passed
- [ ] Documentation complete
- [ ] Signed off by: _________________ Date: _______

### QA Team
- [ ] Functional testing complete
- [ ] Performance testing complete
- [ ] Security review complete
- [ ] Signed off by: _________________ Date: _______

### Operations Team
- [ ] Infrastructure ready
- [ ] Monitoring configured
- [ ] Backup procedures in place
- [ ] Signed off by: _________________ Date: _______

### Business Team
- [ ] Requirements met
- [ ] Compliance verified
- [ ] Training complete
- [ ] Signed off by: _________________ Date: _______

---

**Deployment Date**: _________________  
**Deployed By**: _________________  
**Version**: 1.0
