# Sales Dashboard App

A web application for managing and visualizing sales data stored in Google Sheets.

## Features

✅ **Google Sheets Integration** - Connect directly to your Google Sheets database  
✅ **Real-time Data** - View live data from your spreadsheet  
✅ **Advanced Filtering** - Filter by Cliente, Zona, Comercial, Familia, and Mês  
✅ **Summary Statistics** - View key metrics at a glance  
✅ **Professional UI** - Clean and modern dashboard interface  
✅ **Secure Authentication** - OAuth 2.0 with Google

## Columns Supported

- **Cliente** - Client name
- **Zona** - Zone/Region
- **Comercial** - Salesperson
- **Desconto** - Discount percentage
- **Prazo Pagamento Dias** - Payment terms (days)
- **Código** - Product code
- **Referencia** - Product reference
- **Familia** - Product family
- **Mês** - Month
- **Quant** - Quantity
- **Faturaçao** - Invoice/Billing amount

## Setup Instructions

### 1. Prerequisites

- Python 3.7+
- Google Account
- Google Sheets with your sales data

### 2. Install Dependencies

```bash
pip install -r requirements.txt
```

### 3. Google Cloud Setup

1. Go to [Google Cloud Console](https://console.cloud.google.com/)
2. Create a new project
3. Enable the following APIs:
   - Google Sheets API
   - Google Drive API
4. Create an OAuth 2.0 Client ID (Desktop application)
5. Download the credentials as JSON and save as `credentials.json` in the project root

### 4. Configuration

1. Update `REDIRECT_URI` in `app.py` if using a different address
2. Ensure `credentials.json` is in the same directory as `app.py`

### 5. Run the Application

```bash
python app.py
```

Then open your browser to `http://localhost:5000`

### 6. First Login

1. Click "Login with Google"
2. Authorize the application
3. On the dashboard, enter your Google Sheets ID:
   - Copy from URL: `https://docs.google.com/spreadsheets/d/SHEET_ID/edit`
   - Paste the SHEET_ID in the configuration box
4. Click "Connect to Sheet"

## Project Structure

```
Sales Dashboard App_VScode/
├── app.py                  # Main Flask application
├── credentials.json        # Google OAuth credentials (not in repo)
├── requirements.txt        # Python dependencies
├── templates/
│   ├── base.html          # Base template
│   ├── index.html         # Login page
│   └── dashboard.html     # Main dashboard
└── static/                # CSS, JS assets
```

## API Endpoints

### Authentication
- `GET /` - Home page
- `GET /login` - Initiate Google OAuth login
- `GET /oauth2callback` - OAuth callback
- `GET /logout` - Logout user
- `GET /clearsession` - Clear session (debugging)

### Dashboard & Data
- `GET /dashboard` - Main dashboard page
- `POST /set-spreadsheet-id` - Configure Google Sheets ID
- `GET /get-sheets-data` - Fetch filtered data

## Filtering

The dashboard supports filtering by:
- **Cliente** - Specific client
- **Zona** - Specific zone
- **Comercial** - Specific salesperson
- **Familia** - Product family
- **Mês** - Specific month

## Security Notes

- OAuth 2.0 provides secure authentication
- Credentials are stored in secure session cookies
- Never commit `credentials.json` to version control
- Add `credentials.json` to `.gitignore`

## Troubleshooting

### State Mismatch Error
- Clear session: Visit `/clearsession`
- Ensure `SESSION_COOKIE_SECURE` is `False` for localhost
- Check that redirect URI matches Google Cloud Console

### Cannot Find Spreadsheet
- Verify the Google Sheets ID is correct
- Ensure the sheet is shared or accessible to the authenticated user
- Check that Google Sheets API is enabled in Google Cloud Console

### Data Not Loading
- Verify column headers match exactly (including accents and case)
- Check that numeric columns don't have text values
- Ensure the sheet has data below the header row

## Development

To modify filters or add new columns:

1. Edit `dashboard.html` to add/remove filter sections
2. Update column headers in `fetch_sheets_data()` if needed
3. Modify the DataFrame processing logic for custom calculations

## License

Proprietary - Globale RC

## Support

For issues or questions about the dashboard, please contact the development team.
