# Customer Workflow - Troubleshooting Guide

## Issue: Workflow Not Working

If the automatic customer workflow is not working, follow these steps to diagnose and fix the issue.

---

## Step 1: Verify Extrafields Exist

The workflow requires these extrafields to exist in the database:
- `advance_paid` (Type: price)
- `registration_number` (Type: varchar)

### How to Check:

1. **Via Database:**
   ```sql
   SELECT name, label, type 
   FROM llx_extrafields 
   WHERE elementtype = 'societe' 
   AND name IN ('advance_paid', 'registration_number');
   ```

2. **Via Dolibarr Interface:**
   - Go to: **Setup** → **Third Parties** → **Extrafields**
   - Check if `advance_paid` and `registration_number` exist

### If Missing:
Create them via: **Setup** → **Third Parties** → **Extrafields** → **New Attribute**

---

## Step 2: Verify Extrafields Table Has Columns

The `llx_societe_extrafields` table must have these columns.

### Check via SQL:
```sql
SHOW COLUMNS FROM llx_societe_extrafields LIKE 'advance_paid';
SHOW COLUMNS FROM llx_societe_extrafields LIKE 'registration_number';
```

### If Columns Missing:
The columns are automatically created when you create extrafields via the Dolibarr interface. If they don't exist, recreate the extrafields.

---

## Step 3: Test the Workflow Queries

Run this test script to verify the workflow logic:

```bash
cd c:\xampp\htdocs\admin_crm
php test_workflow_flow.php
```

This will show:
- ✓ If the extrafields table exists
- ✓ If required columns exist
- ✓ How many customers are in each workflow stage
- ✓ Sample customer data with their workflow status

---

## Step 4: Check Context Page URLs

Make sure you're accessing the correct URLs:

### Enquiry List (Step 2):
```
http://localhost/admin_crm/htdocs/societe/list.php?contextpage=enquiry
```
Shows: Customers with NO advance_paid AND NO registration_number

### Booked Customers (Step 3):
```
http://localhost/admin_crm/htdocs/societe/list.php?contextpage=booked_customers
```
Shows: Customers WITH advance_paid but NO registration_number

### Registered Customers (Step 4):
```
http://localhost/admin_crm/htdocs/societe/list.php?contextpage=registered_customers
```
Shows: Customers WITH registration_number

---

## Step 5: Verify Customer Has Correct Type

Customers must have `client` field set to 1 or 3 to appear in the workflow lists.

### Check via SQL:
```sql
SELECT rowid, nom, client 
FROM llx_societe 
WHERE rowid = [CUSTOMER_ID];
```

- `client = 1` → Customer
- `client = 2` → Prospect
- `client = 3` → Both Customer and Prospect

### If client = 0 or 2:
Edit the customer and check the "Customer" checkbox (though this is now commented out, existing customers should still have the correct value).

---

## Step 6: Test with Sample Data

### Test Case 1: New Customer (Should appear in Enquiry)
```sql
-- Create a test customer
INSERT INTO llx_societe (nom, client, status, fk_user_creat) 
VALUES ('Test Enquiry Customer', 1, 1, 1);

-- Get the ID
SET @new_id = LAST_INSERT_ID();

-- Insert extrafields (empty values)
INSERT INTO llx_societe_extrafields (fk_object, advance_paid, registration_number) 
VALUES (@new_id, NULL, NULL);

-- This customer should appear in: Enquiry list
```

### Test Case 2: Booked Customer (Should appear in Booked Customers)
```sql
-- Create a test customer
INSERT INTO llx_societe (nom, client, status, fk_user_creat) 
VALUES ('Test Booked Customer', 1, 1, 1);

SET @new_id = LAST_INSERT_ID();

-- Insert extrafields with advance_paid
INSERT INTO llx_societe_extrafields (fk_object, advance_paid, registration_number) 
VALUES (@new_id, 5000, NULL);

-- This customer should appear in: Booked Customers list
```

### Test Case 3: Registered Customer (Should appear in Registered Customers)
```sql
-- Create a test customer
INSERT INTO llx_societe (nom, client, status, fk_user_creat) 
VALUES ('Test Registered Customer', 1, 1, 1);

SET @new_id = LAST_INSERT_ID();

-- Insert extrafields with registration_number
INSERT INTO llx_societe_extrafields (fk_object, advance_paid, registration_number) 
VALUES (@new_id, 5000, 'REG-2024-001');

-- This customer should appear in: Registered Customers list
```

---

## Step 7: Check for Common Issues

### Issue 1: All customers showing in one list
**Cause:** Extrafields table not joined properly  
**Fix:** Already fixed in list.php - the extrafields table is now always joined for workflow context pages

### Issue 2: No customers showing in any list
**Possible causes:**
- Customers don't have `client = 1` or `client = 3`
- Extrafields don't exist
- Extrafields table columns don't exist

### Issue 3: Customers showing in wrong list
**Check the data:**
```sql
SELECT s.rowid, s.nom, ef.advance_paid, ef.registration_number,
  CASE 
    WHEN ef.registration_number IS NOT NULL AND ef.registration_number != '' THEN 'Registered'
    WHEN ef.advance_paid IS NOT NULL AND ef.advance_paid != 0 AND ef.advance_paid != '' THEN 'Booked'
    ELSE 'Enquiry'
  END as expected_list
FROM llx_societe s
LEFT JOIN llx_societe_extrafields ef ON s.rowid = ef.fk_object
WHERE s.client IN (1, 3)
ORDER BY s.rowid DESC
LIMIT 10;
```

---

## Step 8: Clear Cache

Sometimes Dolibarr caches data. Clear the cache:

1. Go to: **Setup** → **Setup** → **Other setup**
2. Click **Clear cache** button
3. Or manually delete files in: `documents/admin/temp/`

---

## Quick Diagnostic Commands

Run these SQL queries to quickly diagnose issues:

### 1. Count customers by workflow stage:
```sql
SELECT 
  CASE 
    WHEN ef.registration_number IS NOT NULL AND ef.registration_number != '' THEN 'Registered'
    WHEN ef.advance_paid IS NOT NULL AND ef.advance_paid != 0 AND ef.advance_paid != '' THEN 'Booked'
    ELSE 'Enquiry'
  END as workflow_stage,
  COUNT(*) as count
FROM llx_societe s
LEFT JOIN llx_societe_extrafields ef ON s.rowid = ef.fk_object
WHERE s.client IN (1, 3)
GROUP BY workflow_stage;
```

### 2. Check if extrafields exist:
```sql
SELECT name, label, type 
FROM llx_extrafields 
WHERE elementtype = 'societe' 
ORDER BY pos;
```

---

## Files Modified for Workflow

1. **htdocs/societe/card.php**
   - Lines ~1501-1516: Commented out checkboxes (create form)
   - Lines ~2357-2372: Commented out checkboxes (edit form)

2. **htdocs/societe/list.php**
   - Lines ~225-244: Added context page handling
   - Lines ~636-641: Fixed extrafields table JOIN
   - Lines ~830-844: Added automatic workflow filtering

---

## Need Help?

If the workflow still doesn't work after following these steps:

1. Run the test script: `php test_workflow_flow.php`
2. Check the output for any errors
3. Verify all SQL queries return expected results
4. Check Dolibarr error logs in: `documents/admin/`
