# Reports Dashboard Deployment Guide

This guide provides comprehensive instructions for deploying the enhanced reports dashboard with all the Django FieldError fixes applied.

## 🚀 Quick Start

### Development Deployment

1. **Run the deployment script:**
   ```bash
   python deploy_reports_fixes.py
   ```

2. **Follow the interactive prompts** to:
   - Create admin user
   - Setup sample data
   - Test endpoints

### Production Deployment

1. **Make the script executable:**
   ```bash
   chmod +x deploy_production.sh
   ```

2. **Run the production deployment:**
   ```bash
   sudo ./deploy_production.sh
   ```

## 📋 Prerequisites

### System Requirements
- Python 3.8+
- Django 5.2+
- PostgreSQL 12+ (recommended) or MySQL 8.0+
- Redis 6.0+ (for caching)
- Nginx (for production)

### Python Dependencies
Install from the requirements file:
```bash
pip install -r requirements_reports.txt
```

## 🔧 Configuration

### Environment Variables
1. Copy the environment template:
   ```bash
   cp .env.template .env
   ```

2. Update the values in `.env` for your environment:
   - Database credentials
   - Secret key
   - Email settings
   - Domain configuration

### Database Setup
```bash
# PostgreSQL
sudo -u postgres createuser branch_system
sudo -u postgres createdb branch_system_db -O branch_system

# Run migrations
python manage.py migrate
```

## 🛠️ What's Fixed

### Django FieldError Issues
- ✅ **"Cannot use None as a query value"** - Fixed by adding proper Coalesce functions
- ✅ **"Expression contains mixed types"** - Fixed by adding explicit output_field specifications
- ✅ **Date calculation errors** - Fixed by specifying proper DateField output types
- ✅ **Aggregation type mismatches** - Fixed by using consistent DecimalField types

### Specific Changes Made

#### 1. comprehensive_reports.py
- Added `output_field` to all `Value()` expressions
- Fixed `ExpressionWrapper` with proper field types
- Simplified complex aggregation queries
- Added proper date field specifications

#### 2. views.py
- Updated imports to include `Coalesce` from correct module
- Fixed aggregation queries with proper field types
- Added error handling for None values

#### 3. Database Queries
- All `Coalesce(Sum(...), Value(0))` now have proper output fields
- Date calculations use explicit `DateField()` specifications
- Decimal calculations maintain precision consistency

## 📊 Reports Available

### Core Reports
1. **Loans Due Report** - `/reports/loans-due/`
2. **Delinquent Loans Report** - `/reports/delinquent-loans/`
3. **Loans in Arrears Report** - `/reports/loans-in-arrears/`
4. **Processing Fees Report** - `/reports/processing-fees/`
5. **Interest Income Report** - `/reports/interest-income/`
6. **Registration Fees Report** - `/reports/registration-fees/`
7. **Customer Requests Report** - `/reports/customer-requests/`

### Dashboard
- **Main Dashboard** - `/reports/`
- **Analytics Dashboard** - `/reports/analytics/`

## 🧪 Testing

### Manual Testing
1. Start the development server:
   ```bash
   python manage.py runserver
   ```

2. Test each report endpoint:
   ```bash
   curl http://localhost:8000/reports/loans-due/
   curl http://localhost:8000/reports/delinquent-loans/
   curl http://localhost:8000/reports/processing-fees/
   ```

### Automated Testing
```bash
python manage.py test reports
```

## 🔍 Troubleshooting

### Common Issues

#### 1. FieldError: Expression contains mixed types
**Solution:** Ensure all `Value()` expressions have explicit `output_field`:
```python
# Wrong
Value(0)

# Correct
Value(0, output_field=models.DecimalField(max_digits=12, decimal_places=2))
```

#### 2. Cannot use None as a query value
**Solution:** Use `Coalesce` with proper field types:
```python
# Wrong
Sum('amount')

# Correct
Coalesce(Sum('amount'), Value(0, output_field=models.DecimalField(max_digits=12, decimal_places=2)))
```

#### 3. Date calculation errors
**Solution:** Specify date field types:
```python
# Wrong
Value(today) - F('due_date')

# Correct
Value(today, output_field=models.DateField()) - F('due_date')
```

### Debug Mode
Enable debug mode for detailed error information:
```python
# settings.py
DEBUG = True
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
        },
    },
    'loggers': {
        'django.db.backends': {
            'handlers': ['console'],
            'level': 'DEBUG',
        },
    },
}
```

## 📈 Performance Optimization

### Database Indexes
Add indexes for frequently queried fields:
```sql
CREATE INDEX idx_loans_status ON loans(status);
CREATE INDEX idx_loans_due_date ON loans(due_date);
CREATE INDEX idx_loans_borrower ON loans(borrower_id);
```

### Caching
Enable Redis caching:
```python
# settings.py
CACHES = {
    'default': {
        'BACKEND': 'django_redis.cache.RedisCache',
        'LOCATION': 'redis://127.0.0.1:6379/1',
        'OPTIONS': {
            'CLIENT_CLASS': 'django_redis.client.DefaultClient',
        }
    }
}
```

### Query Optimization
- Use `select_related()` for foreign key relationships
- Use `prefetch_related()` for many-to-many relationships
- Implement pagination for large datasets

## 🔒 Security Considerations

### Production Settings
```python
# settings.py
DEBUG = False
ALLOWED_HOSTS = ['your-domain.com']
SECURE_SSL_REDIRECT = True
SECURE_HSTS_SECONDS = 31536000
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
```

### Database Security
- Use strong passwords
- Enable SSL connections
- Restrict database access
- Regular security updates

## 📝 Maintenance

### Regular Tasks
1. **Database backups** - Daily automated backups
2. **Log rotation** - Weekly log cleanup
3. **Security updates** - Monthly dependency updates
4. **Performance monitoring** - Continuous monitoring

### Backup Strategy
```bash
# Database backup
pg_dump branch_system_db > backup_$(date +%Y%m%d).sql

# Media files backup
tar -czf media_backup_$(date +%Y%m%d).tar.gz media/
```

## 📞 Support

### Getting Help
1. Check the troubleshooting section above
2. Review Django logs: `/var/log/branch-system/django.log`
3. Check system logs: `journalctl -u branch-system`
4. Verify database connectivity
5. Test individual report endpoints

### Rollback Procedure
If deployment fails:
1. Stop the application: `systemctl stop branch-system`
2. Restore from backup: `cp -r /var/backups/branch-system/backup_YYYYMMDD/* /opt/branch-system/`
3. Restart services: `systemctl start branch-system`

## 🎯 Next Steps

After successful deployment:
1. Configure monitoring and alerting
2. Set up automated backups
3. Implement SSL certificates
4. Configure email notifications
5. Set up log aggregation
6. Plan for scaling and load balancing

---

## 📄 File Structure

```
branch-system/
├── deploy_reports_fixes.py      # Development deployment script
├── deploy_production.sh         # Production deployment script
├── requirements_reports.txt     # Python dependencies
├── .env.template               # Environment configuration template
├── DEPLOYMENT_README.md        # This file
├── reports/
│   ├── comprehensive_reports.py # Fixed report service
│   ├── views.py                # Fixed views
│   └── enhanced_models.py      # Report models
└── templates/
    └── reports/                # Report templates
```

## ✅ Deployment Checklist

- [ ] Python 3.8+ installed
- [ ] Database server running
- [ ] Redis server running (for caching)
- [ ] Environment variables configured
- [ ] Dependencies installed
- [ ] Database migrations applied
- [ ] Static files collected
- [ ] Admin user created
- [ ] Sample data loaded (optional)
- [ ] All report endpoints tested
- [ ] Production settings configured
- [ ] SSL certificate installed (production)
- [ ] Monitoring configured
- [ ] Backups scheduled

---

**🎉 Congratulations!** Your reports dashboard should now be running without any Django FieldError issues!