# CLAUDE.md

This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.

## Project Overview

SSOMA is a PHP-based occupational health and safety management system with two main modules:

1. **atencionesmedicas** - Medical attention tracking for workplace incidents and health services
2. **recursoshumanos** - Human resources worker management system

Both modules share a MySQL database (`medsursa_atencionmedica`) and follow similar architectural patterns.

## Technology Stack

- **Backend**: PHP with PDO (MySQL)
- **Frontend**: Bootstrap 5.3, jQuery, DataTables
- **Dependencies**: PHPOffice/PHPSpreadsheet (Excel exports)
- **Server**: XAMPP (Apache + PHP + MySQL)

## Development Setup

### Initial Setup
```bash
# Navigate to each module and install Composer dependencies
cd atencionesmedicas
composer install

cd ../recursoshumanos
composer install
```

### Database Connection
Each module has a `conexion.php` file that establishes a PDO connection:
- Database: `medsursa_atencionmedica`
- Connection uses PDO with prepared statements
- Configuration includes error mode, fetch mode, and emulated prepares disabled

**Security Note**: The current connection files have hardcoded credentials. In production, these should be moved to environment variables or a config file outside the web root.

## Database Architecture

### Core Tables
- **Trabajador**: Worker/employee information (shared across modules)
  - Primary key: `id_trabajador`
  - Contains: personal info, job details, contract information

- **Atencion_Medica**: Medical attention records
  - Primary key: `id_atencion`
  - Foreign keys: `id_trabajador`, `id_evento`
  - Contains: medical diagnoses (CIE-10), treatments, medical rest periods

- **Evento**: Event types for medical attentions
  - Primary key: `id_evento`
  - Referenced by: `Atencion_Medica.id_evento`

### Key Relationships
- `Atencion_Medica.id_trabajador` → `Trabajador.id_trabajador`
- `Atencion_Medica.id_evento` → `Evento.id_evento`
- Worker lookup in forms uses `dni_ce` field

## Code Architecture

### CRUD Pattern
All CRUD operations follow this structure:
1. **index.php**: Main listing page with filters and pagination/DataTables
2. **agregar.php**: POST handler for creating records
3. **editar.php**: POST handler for updating records
4. **eliminar.php**: POST handler for deleting records
5. **modal_*.php**: Bootstrap modals containing forms

### Module Differences
- **atencionesmedicas**: Uses custom pagination (`mi_paginacion.php`)
- **recursoshumanos**: Uses jQuery DataTables for client-side pagination and filtering

### Common Patterns

#### PDO Prepared Statements
Always use prepared statements with bound parameters:
```php
$stmt = $pdo->prepare("SELECT * FROM Trabajador WHERE dni_ce = :dni");
$stmt->bindParam(':dni', $dni);
$stmt->execute();
```

#### Dynamic Filtering
Both modules implement filter systems:
- Build WHERE clauses dynamically from GET parameters
- Use parameterized queries to prevent SQL injection
- See `filtros.php` for filter UI components

#### Excel Export
Uses PHPOffice/PhpSpreadsheet:
- Query all records (or filtered subset)
- Apply styling (headers, borders, colors)
- Auto-size columns
- Stream to browser as download

#### User Feedback
- **Toastify.js**: Success/error notifications
- **SweetAlert2**: Confirmation dialogs (especially for deletions)
- Messages passed via URL parameters or localStorage

### Foreign Key Lookups
When creating medical attentions:
1. Lookup `id_trabajador` using `dni_ce` from Trabajador table
2. Lookup `id_evento` using `tipo_evento` from Evento table
3. Insert into Atencion_Medica with foreign key IDs

Example in [agregar.php](atencionesmedicas/agregar.php:26-43).

## File Structure

### atencionesmedicas/
- `index.php`: Main listing with custom pagination
- `agregar.php`, `editar_atencion.php`, `eliminar.php`: CRUD handlers
- `modal_add_atencion.php`, `modal_edit_atencion.php`: Form modals
- `exportar_excel.php`: Excel export with PhpSpreadsheet
- `filtros.php`: Filter form component
- `mi_paginacion.php`: Pagination component
- `conexion.php`: Database connection

### recursoshumanos/
- `index.php`: Main listing with DataTables
- `agregar.php`, `editar.php`, `eliminar.php`: CRUD handlers
- `modal_agregar_trabajador.php`, `modal_editar_trabajador.php`: Form modals
- `exportar.php`: Excel export
- `filtros.php`: Filter form component
- `conexion.php`: Database connection
- `styles.php`: Inline CSS styles

## Important Development Notes

### When Adding Features
1. Follow the existing CRUD pattern structure
2. Use PDO prepared statements for all database operations
3. Include input validation and sanitization
4. Redirect with URL parameters for success/error messages
5. Use Bootstrap modals for forms to maintain consistency

### When Modifying Queries
- Both modules use JOINs between Atencion_Medica and Trabajador
- Pay attention to field aliases (e.g., `t.dni_ce AS dni`)
- Test pagination/filtering after query changes
- Note: `tipo_evento` is stored directly in Atencion_Medica table, not always joined from Evento

### Common Gotchas
- Both modules have vendor directories with PhpSpreadsheet - they're independent
- Some duplicate structures exist (e.g., `atencionesmedicas/atencionesmedicas/vendor/`)
- Character encoding issues may appear in comments (ensure UTF-8)
- Toast messages use both URL parameters and localStorage (different patterns in each module)
