#!/usr/bin/env python
"""
Deploy Processing Fees Report Fix to Production

This script:
1. Updates the simple_reports_service.py to use actual processing_fee field
2. Updates the enhanced_processing_fees_report view to show all loans with fees
3. Provides instructions for deployment
"""

print("=" * 80)
print("PROCESSING FEES REPORT FIX - DEPLOYMENT GUIDE")
print("=" * 80)

print("""
CHANGES MADE:
=============

1. reports/simple_reports_service.py
   - Fixed get_processing_fees_report() to use actual loan.processing_fee field
   - Previously calculated as 2% of total_amount (incorrect)
   - Now uses the actual processing_fee stored in the database

2. reports/views.py
   - Updated enhanced_processing_fees_report() view
   - Changed default period from 'current_month' to 'all_time'
   - Now shows ALL loans with processing fees, not just active ones
   - Added proper date filtering for different periods
   - Only includes loans where processing_fee > 0
   - Better error handling and logging

DEPLOYMENT STEPS FOR PRODUCTION:
=================================

1. Upload the modified files to production:
   - reports/simple_reports_service.py
   - reports/views.py

2. Restart the application:
   - If using cPanel: Touch the passenger_wsgi.py file
   - If using systemd: sudo systemctl restart gunicorn
   - Or: python restart_app.py

3. Test the report:
   - Visit: https://branchbusinessadvance.co.ke/reports/processing-fees/
   - Should now show actual processing fees data

4. If still showing zeros, check production database:
   - Run: python check_processing_fees_data.py
   - Verify loans have processing_fee values set
   - If not, loans may need to be updated

TROUBLESHOOTING:
================

If the report still shows zeros after deployment:

A. Check if loans exist in production:
   python -c "import django; django.setup(); from loans.models import Loan; print(f'Total loans: {Loan.objects.count()}')"

B. Check if loans have processing fees:
   python -c "import django; django.setup(); from loans.models import Loan; from django.db.models import Sum; print(f'Total fees: {Loan.objects.aggregate(Sum(\"processing_fee\"))[\"processing_fee__sum\"] or 0}')"

C. Check branch filtering:
   - Admin users should see all loans
   - Other users only see loans from their branch
   - Try logging in as admin to see all data

D. Check date filtering:
   - Default is now 'all_time' which shows all loans
   - Try different period filters in the UI

QUICK FIX COMMANDS:
===================

# Upload files to production (via cPanel File Manager or FTP)
# Then restart:

cd /home/branchbu/public_html
touch tmp/restart.txt
# OR
python restart_app.py

# Check logs for errors:
tail -f logs/error.log
""")

print("\n" + "=" * 80)
print("FILES TO UPLOAD:")
print("=" * 80)
print("  1. reports/simple_reports_service.py")
print("  2. reports/views.py")
print("\n" + "=" * 80)
print("DEPLOYMENT READY!")
print("=" * 80)
