# Customer Data Isolation - Implementation Fix

## Problem Identified

The initial implementation had a **conflict** between two filtering mechanisms:

1. **Creator-based filter** (`fk_user_creat`) - NEW: Filters by who created the customer
2. **Sales representative filter** (`societe_commerciaux` table) - OLD: Filters by assigned sales rep

When both filters were applied together, they created conflicts causing incorrect results.

## Solution Applied

**Changed the logic to use CREATOR-BASED filtering as the PRIMARY method** for non-admin users without the `societe.client.voir` permission.

### How It Works Now:

```php
// If user is NOT admin AND doesn't have "voir" permission
if (!$user->admin && !$user->hasRight('societe', 'client', 'voir')) {
    // Filter by creator ONLY
    $sql .= " AND s.fk_user_creat = " . ((int) $user->id);
} else {
    // Use original Dolibarr sales rep filtering
    // (for users with "voir" permission or admins)
}
```

## Files Modified

1. ✅ `htdocs/societe/list.php` - Customer list
2. ✅ `htdocs/societe/card.php` - Individual customer view
3. ✅ `htdocs/societe/pdf-customer.php` - PDF export
4. ✅ `htdocs/societe/index.php` - Dashboard (3 queries updated)
5. ✅ `htdocs/societe/class/client.class.php` - Customer statistics
6. ✅ `htdocs/societe/ajax/ajaxcompanies.php` - Autocomplete search

## Setup Instructions

### For Admin User (e.g., James if admin):
- ✅ Keep `societe.client.voir` permission **ENABLED**
- ✅ OR mark user as **Admin** in user settings
- Result: Can see ALL customers

### For Regular Users (e.g., m.muthu, Karthi, Sriram):
- ❌ **REMOVE** the `societe.client.voir` permission
- ✅ Keep `societe.lire` (read) permission
- ✅ Keep `societe.creer` (create) permission
- Result: Can see ONLY customers they created

## How to Remove the Permission

1. Login as Admin
2. Go to: **Home → Setup → Users**
3. Click on user (e.g., m.muthu)
4. Go to **Permissions** tab
5. Find **Thirdparties** section
6. Look for: **"See all thirdparties"** (societe.client.voir)
7. Click the **red X** to remove it
8. Repeat for other regular users

## Expected Behavior After Fix

### Admin Login:
- Customer List → Shows **ALL** customers (James's + m.muthu's + everyone's)
- Statistics → Shows **TOTAL** count

### James Login (without voir permission):
- Customer List → Shows **ONLY** customers created by James
- Try to access m.muthu's customer → **"Access Forbidden"**
- Statistics → Shows **ONLY** James's customer count

### m.muthu Login (without voir permission):
- Customer List → Shows **ONLY** customers created by m.muthu
- Try to access James's customer → **"Access Forbidden"**
- Statistics → Shows **ONLY** m.muthu's customer count

## Testing Steps

1. **Remove `societe.client.voir` permission** from James and m.muthu
2. **Login as James** → Create 2 new customers
3. **Login as m.muthu** → Create 3 new customers
4. **Check James's list** → Should see ONLY 2 customers (his own)
5. **Check m.muthu's list** → Should see ONLY 3 customers (his own)
6. **Login as Admin** → Should see ALL 5 customers

## Debug Scripts

Two debug scripts are available to troubleshoot:

1. **debug_filter.php** - Shows user permissions and filtering logic
2. **debug_customers_detail.php** - Shows detailed customer creation info

Access via:
```
http://localhost/admin_crm/htdocs/societe/debug_filter.php
http://localhost/admin_crm/htdocs/societe/debug_customers_detail.php
```

## Important Notes

⚠️ **Existing Customers**: Customers created BEFORE this fix may have:
- NULL `fk_user_creat` value
- Wrong creator assigned

To fix existing data, you may need to:
1. Check the debug script to see which customers have NULL creators
2. Manually update the `fk_user_creat` field in the database
3. OR reassign customers to the correct creator

## SQL Query to Check Existing Data

```sql
SELECT s.rowid, s.nom, s.fk_user_creat, u.login as creator
FROM llx_societe s
LEFT JOIN llx_user u ON s.fk_user_creat = u.rowid
WHERE s.client IN (1, 3)
ORDER BY s.rowid;
```

This will show you all customers and who created them.

---

**Date Fixed**: 2026-04-23
**Status**: ✅ Complete
