# Expenses System - cPanel Production Deployment Guide

## Issue Identified
The deployment script is failing because it's trying to connect with 'root' user credentials that don't match your production database setup.

## Solution: Manual Deployment Steps

### Step 1: Add Expenses App to Settings

1. **SSH into your cPanel server** or use **File Manager**

2. **Edit** `branch_system/settings.py`

3. **Find** the `INSTALLED_APPS` section

4. **Add** `'expenses',` to the list:

```python
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.humanize',
    'users',
    'loans',
    'reports',
    'utils',
    'payments',
    'expenses',  # <-- ADD THIS LINE
]
```

5. **Save** the file

### Step 2: Create Database Table

#### Option A: Using phpMyAdmin (Easiest)

1. **Login to cPanel**
2. **Open phpMyAdmin**
3. **Select your database** (acbptxvs_branch_system or similar)
4. **Click the "SQL" tab**
5. **Copy and paste** the contents of `create_expenses_table.sql`
6. **Click "Go"** to execute

#### Option B: Using SSH/Terminal

```bash
# Navigate to your project directory
cd /home/acbptxvs/public_html/branchbusinessadvance.co.ke

# Run the SQL file
mysql -u YOUR_DB_USER -p YOUR_DB_NAME < create_expenses_table.sql
```

Replace:
- `YOUR_DB_USER` with your actual database username
- `YOUR_DB_NAME` with your actual database name

### Step 3: Run Django Migrations

```bash
# Activate virtual environment
source /home/acbptxvs/virtualenv/public_html/branchbusinessadvance.co.ke/3.13/bin/activate

# Navigate to project
cd /home/acbptxvs/public_html/branchbusinessadvance.co.ke

# Run migrations
python manage.py makemigrations expenses
python manage.py migrate expenses
```

### Step 4: Collect Static Files

```bash
python manage.py collectstatic --noinput
```

### Step 5: Restart Application

In cPanel:
1. Go to **Setup Python App**
2. Find your application
3. Click **Restart**

Or via command line:
```bash
touch /home/acbptxvs/public_html/branchbusinessadvance.co.ke/tmp/restart.txt
```

### Step 6: Verify Installation

Visit your site and check:
- `/expenses/` - Should show expenses list
- `/expenses/add/` - Should show add expense form
- `/expenses/analytics/` - Should show analytics dashboard

## Troubleshooting

### Issue: "Access denied for user 'root'"

**Solution:** Your production database doesn't use 'root'. Check your actual credentials:

1. In cPanel, go to **MySQL Databases**
2. Note your database name and username
3. Update your `.env` file or settings with correct credentials

### Issue: "Table already exists"

**Solution:** The table was created successfully. Skip Step 2 and proceed to Step 3.

### Issue: "No module named 'expenses'"

**Solution:** Make sure:
1. The `expenses` folder exists in your project root
2. It contains `__init__.py`
3. You added it to `INSTALLED_APPS`
4. You restarted the application

### Issue: "Static files not loading"

**Solution:**
```bash
python manage.py collectstatic --noinput
```

Then check your `.htaccess` file has:
```apache
# Serve static files
Alias /static /home/acbptxvs/public_html/branchbusinessadvance.co.ke/staticfiles
<Directory /home/acbptxvs/public_html/branchbusinessadvance.co.ke/staticfiles>
    Require all granted
</Directory>
```

## Quick Verification Script

Create `verify_expenses_production.py`:

```python
import os
import django

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'branch_system.settings')
django.setup()

from django.db import connection

try:
    with connection.cursor() as cursor:
        cursor.execute("SHOW TABLES LIKE 'expenses_expense'")
        result = cursor.fetchone()
        if result:
            print("✓ Expenses table exists")
            cursor.execute("SELECT COUNT(*) FROM expenses_expense")
            count = cursor.fetchone()[0]
            print(f"✓ Table has {count} records")
        else:
            print("✗ Expenses table does not exist")
except Exception as e:
    print(f"✗ Error: {e}")
```

Run it:
```bash
python verify_expenses_production.py
```

## Files to Upload

Make sure these files are on your production server:

### Required Files:
- `expenses/` (entire folder)
  - `__init__.py`
  - `models.py`
  - `views.py`
  - `forms.py`
  - `urls.py`
  - `admin.py`
  - `migrations/`
    - `__init__.py`
    - `0001_initial.py`

- `templates/expenses/` (entire folder)
  - `expenses_list.html`
  - `expense_detail.html`
  - `expense_form.html`
  - `expense_approve.html`
  - `expense_reject.html`
  - `expense_confirm_delete.html`
  - `pending_approvals.html`
  - `expense_analytics.html`

### SQL File:
- `create_expenses_table.sql`

## Post-Deployment Checklist

- [ ] Expenses app added to INSTALLED_APPS
- [ ] Database table created
- [ ] Migrations run successfully
- [ ] Static files collected
- [ ] Application restarted
- [ ] Can access /expenses/ URL
- [ ] Can create new expense
- [ ] Can view expense details
- [ ] Can approve/reject expenses (admin)
- [ ] Analytics page loads
- [ ] Export to Excel works
- [ ] All emoji icons display correctly

## Support

If you encounter issues:

1. **Check Django logs:**
   ```bash
   tail -f /home/acbptxvs/logs/error.log
   ```

2. **Check Python app logs in cPanel:**
   - Setup Python App → Your App → View Logs

3. **Test database connection:**
   ```bash
   python manage.py dbshell
   ```

4. **Run Django check:**
   ```bash
   python manage.py check
   ```

## Success Indicators

You'll know it's working when:
- ✅ No errors in logs
- ✅ `/expenses/` page loads
- ✅ Can create and view expenses
- ✅ Icons display as emojis (not boxes)
- ✅ Analytics dashboard shows data
- ✅ Export to Excel downloads file

---

**Last Updated:** November 29, 2024  
**Environment:** cPanel with Python 3.13  
**Database:** MySQL/MariaDB  
**Status:** Ready for deployment
