# Salary Components Migration Guide

## Overview
This migration creates the necessary tables and default components for managing employee salary structures (HRA, DA, Special Allowances, TA, PF, ESIC, Professional Tax, TDS).

## Files Created

1. **create_salary_components_migration.sql** - Complete migration with all sites support
2. **create_salary_components_direct.sql** - Simple direct SQL for phpMyAdmin
3. **run_salary_components_migration.php** - PHP script to run migration via browser

## How to Run Migration

### Option 1: Using phpMyAdmin (Recommended)
1. Open phpMyAdmin
2. Select your database
3. Go to SQL tab
4. Open and copy contents from `create_salary_components_direct.sql`
5. Paste and execute
6. Verify tables are created

### Option 2: Using PHP Script
1. Open browser
2. Navigate to: `http://your-domain/database/run_salary_components_migration.php`
3. Migration will run automatically
4. Check results

### Option 3: Using MySQL Command Line
```bash
mysql -u username -p database_name < database/migrations/create_salary_components_migration.sql
```

## Tables Created

### 1. salary_components
Stores all salary component definitions (earnings and deductions).

**Columns:**
- component_id (Primary Key)
- site_id (Foreign Key to sites)
- component_code (Unique code: BASIC, HRA, DA, etc.)
- component_name (Display name)
- component_type (earning/deduction)
- calculation_type (fixed/percentage/formula)
- component_status (active/inactive)

### 2. employee_salary_structure
Stores actual salary component amounts for each employee.

**Columns:**
- structure_id (Primary Key)
- site_id (Foreign Key)
- employee_id (Foreign Key to employees)
- component_id (Foreign Key to salary_components)
- amount (Decimal value)
- effective_from (Date)
- effective_to (Date, nullable)
- structure_status (active/inactive)

## Default Components Created

### Earnings:
- BASIC - Basic Salary
- HRA - House Rent Allowance
- DA - Dearness Allowance
- SPECIAL_ALLOWANCE - Special Allowance
- TA - Travel Allowance

### Deductions:
- PF - Provident Fund
- ESIC - Employee State Insurance Corporation
- PROFESSIONAL_TAX - Professional Tax
- TDS - Tax Deducted at Source

## Verification

After running migration, verify:

```sql
-- Check if tables exist
SHOW TABLES LIKE 'salary_components';
SHOW TABLES LIKE 'employee_salary_structure';

-- Check components created
SELECT * FROM salary_components WHERE site_id = 1;

-- Check structures (should be empty initially)
SELECT COUNT(*) FROM employee_salary_structure;
```

## Troubleshooting

### Error: Table already exists
- This is normal if tables already exist
- Migration uses `CREATE TABLE IF NOT EXISTS`, so it's safe to run multiple times

### Error: Foreign key constraint fails
- Make sure `sites` table exists
- Make sure `employees` table exists
- Check that site_id values match existing sites

### Components not saving
1. Verify tables are created
2. Check that components exist for your site_id
3. Check database connection
4. Check error logs in PHP

## Next Steps

After migration:
1. Go to Edit Employee page
2. Fill in salary components (HRA, DA, etc.)
3. Save employee
4. Check New Joinee Statutory page - data should sync automatically

## Support

If you encounter issues:
1. Check PHP error logs
2. Check MySQL error logs
3. Verify database user has CREATE and INSERT permissions
4. Ensure foreign key constraints are satisfied

