# SQL Migration Files - Complete Feature Setup

## Overview
Yeh file sabhi migration files ka summary hai jo start se ab tak add kiye gaye features ke liye zaroori hain.

## Migration Files

### 1. `complete_feature_migrations.sql` ⭐ (RECOMMENDED)
**Location:** `database/migrations/complete_feature_migrations.sql`

**Contains:**
- ✅ Sites/Branches table
- ✅ User-Sites relationship table
- ✅ Branch-wise permissions table
- ✅ Users table site_id column (if missing)

**Use This:** Agar aap sabhi features ek saath setup karna chahte hain.

**How to Run:**
```sql
-- phpMyAdmin mein import karein ya MySQL command line se:
mysql -u root -p your_database < database/migrations/complete_feature_migrations.sql
```

---

### 2. `add_branch_permissions.sql`
**Location:** `database/migrations/add_branch_permissions.sql`

**Contains:**
- ✅ Site Role Permissions table only

**Use This:** Agar sirf branch-wise permissions feature chahiye.

---

## Features Added (Start to End)

### Feature 1: Multi-Branch System
**Tables Required:**
- `sites` - Branch/Site information

**Files Created:**
- `admin/sites.php` - Branch list
- `admin/sites_create.php` - Create branch
- `admin/sites_edit.php` - Edit branch

**Migration:** Included in `complete_feature_migrations.sql`

---

### Feature 2: Branch-wise User Assignment
**Tables Required:**
- `user_sites` - User-Branch relationship

**Files Created:**
- Enhanced `admin/users_create.php` - Branch selection
- Enhanced `admin/users_edit.php` - Multiple branch assignment
- Enhanced `login.php` - Branch selection for multi-branch users

**Migration:** Included in `complete_feature_migrations.sql`

---

### Feature 3: Branch-wise Permissions
**Tables Required:**
- `site_role_permissions` - Branch-specific role permissions

**Files Created:**
- `admin/branch_permissions.php` - Branch permission management
- `admin/create_branch_permissions_table.php` - Table creation helper
- Updated `includes/functions.php` - Branch-aware permission checking

**Migration:** 
- `database/migrations/add_branch_permissions.sql`
- Included in `complete_feature_migrations.sql`

---

### Feature 4: Role Management System
**Tables Required:**
- `roles` - Already exists
- `permissions` - Already exists
- `role_permissions` - Already exists

**Files Created/Enhanced:**
- `admin/roles.php` - Role Manager
- `admin/roles_create.php` - Add Role
- `admin/roles_edit.php` - Role Editor
- `admin/roles_edit_detail.php` - Role Editor Detail
- `admin/roles_view.php` - View Role Permissions
- `admin/roles_delete.php` - Delete Role
- `admin/permissions.php` - Permission Setup
- `admin/permissions_edit.php` - Edit Permission
- `admin/user_access_role.php` - User Access Role

**Migration:** No new tables needed (already exists)

---

### Feature 5: Stock Group Menu
**Tables Required:**
- None (UI only feature)

**Files Modified:**
- `includes/header.php` - Added Stock Group dropdown menu

**Migration:** No database changes needed

---

## Quick Setup Guide

### Option 1: Complete Setup (Recommended)
```sql
-- Import complete migration file
SOURCE database/migrations/complete_feature_migrations.sql;
```

### Option 2: Step by Step
```sql
-- Step 1: Branch Permissions only
SOURCE database/migrations/add_branch_permissions.sql;
```

---

## Verification

After running migrations, verify tables exist:

```sql
-- Check all tables
SHOW TABLES LIKE 'sites';
SHOW TABLES LIKE 'user_sites';
SHOW TABLES LIKE 'site_role_permissions';

-- Check users table has site_id
DESCRIBE users;
```

---

## Notes

1. **Safe to Run Multiple Times:** All migrations use `CREATE TABLE IF NOT EXISTS` - safe to run multiple times
2. **No Data Loss:** Existing data will not be affected
3. **Backward Compatible:** All changes are backward compatible
4. **Foreign Keys:** All foreign keys are properly set up with CASCADE/SET NULL

---

## Support

Agar koi issue aaye to check karein:
- Error logs: `error_log` file
- Database connection: `config/config.php`
- Table structure: `DESCRIBE table_name;`

