# M1 Sidebar - Data Scope Location Fix

**Issue:** Locations weren't showing in the Data Scope tab  
**Root Cause:** Query was looking for `user_data_permissions` table which doesn't exist  
**Fix Applied:** January 17, 2026

---

## What Was Fixed

### Problem
The Data Scope tab query was failing silently because it tried to join with the `user_data_permissions` table, which doesn't exist in your database. This caused the location list to be empty.

### Solution
Modified both the `scope()` method and `switchLocation()` method to:
1. **Check if the table exists** before querying
2. **Show all locations** when the table doesn't exist (no permission restrictions)
3. **Still support permission checks** when the table does exist (future-proof)

---

## Files Modified

### 1. `controllers/RightSidebarController.php`
**Method:** `scope()` (lines 35-75)

**Changes:**
- Added table existence check: `SHOW TABLES LIKE 'user_data_permissions'`
- If table exists: Use permission-based query (original logic)
- If table doesn't exist: Show all locations with `has_access = 1`
- Added `WHERE l.deleted_at IS NULL` to filter out deleted locations

### 2. `controllers/LocationController.php`
**Method:** `switchLocation()` (lines 294-352)

**Changes:**
- Added table existence check
- If table exists: Verify user permissions (original logic)
- If table doesn't exist: Grant access to all locations (`hasAccess = true`)
- Added location existence validation before switching
- Better error messages

---

## Your Locations

You currently have **2 active locations**:

| ID | Code | Name   | City   | Status |
|----|------|--------|--------|--------|
| 2  | GAR  | Garage | Garage | active |
| 3  | FARM | Farm   | Place  | active |

---

## How It Works Now

### Data Scope Tab
1. Opens sidebar → Click "Scope" tab
2. Shows:
   - **Current Location:** (if set) or "All Locations"
   - **Switch Location:** List of all your locations
     - ✅ All Locations (clears filter)
     - ✅ Farm
     - ✅ Garage
   - **Access Summary:** Shows location counts

### Location Switching
1. Click on a location name in the list
2. Page reloads with flash message: "Switched to {Location Name}"
3. Session stores: `$_SESSION['current_location_id'] = {id}`
4. Future queries can filter by this location

---

## Testing

### Test the Fix Now:

1. **Open Customer Page:**
   ```
   http://localhost/companies/1
   ```

2. **Click M1 Button** (right side of screen)

3. **Click Scope Tab** (filter icon)

4. **You Should See:**
   ```
   Current Location
   [Badge showing current location or "All Locations"]
   
   Switch Location
   🌐 All Locations
   📍 Farm
   📍 Garage
   
   Access Summary
   2 Accessible Locations
   0 Restricted
   ```

5. **Click "Farm"** → Should reload with message "Switched to Farm"

6. **Click "Garage"** → Should reload with message "Switched to Garage"

7. **Click "All Locations"** → Should reload with message "Switched to all locations"

---

## Future: Permission-Based Access

When you implement the `user_data_permissions` table in the future, the code will automatically switch to permission-based filtering:

### Table Schema (for future reference):
```sql
CREATE TABLE user_data_permissions (
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT NOT NULL,
    location_id INT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    UNIQUE KEY (user_id, location_id),
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
    FOREIGN KEY (location_id) REFERENCES locations(id) ON DELETE CASCADE
);
```

Once this table exists and has data, the code will:
- Only show locations the user has permission to access
- Restrict location switching based on permissions
- Admins (`system.admin` permission) bypass restrictions

---

## Summary

✅ **Fixed:** Data Scope tab now shows your 2 locations  
✅ **Fixed:** Location switching works without permission table  
✅ **Backward Compatible:** Still supports permissions when table exists  
✅ **Future-Proof:** Ready for data access control implementation

**Go test it now at:** `http://localhost/companies/1`

The scope tab should now display "Farm" and "Garage" as clickable options! 🎉
