# Debug Guide - Global Invoice Scheme Prefixes

## Issues Fixed
I've added comprehensive debugging to identify why the apply button isn't working and prefixes aren't showing.

## How to Test and Debug

### Step 1: Open Browser Developer Tools
1. **Press F12** or right-click and select "Inspect"
2. **Go to Console tab**
3. **Clear the console** (click the clear button)

### Step 2: Test Apply Button
1. **Navigate**: Business Settings > Business Locations > Edit any location
2. **Scroll down** to "Global Invoice Scheme Prefixes" section
3. **Enter a prefix** (e.g., "TEST-") in any field
4. **Click the Apply button**
5. **Check the console** for debug messages

### Expected Console Messages
You should see these messages in order:
```
Global prefix script loaded
Apply button clicked
Type: expense
Prefix field: found
Prefix value: TEST-
Sending AJAX request
AJAX request starting
AJAX success: {success: true, msg: "Prefix applied successfully..."}
```

### Step 3: Check Server Logs
If the AJAX fails, check Laravel logs:
1. **Open**: `storage/logs/laravel.log`
2. **Look for these messages**:
   ```
   applyGlobalPrefix method called
   Request data: {"_token":"...","type":"expense","prefix":"TEST-"}
   Processing - Business ID: 1, Type: expense, Prefix: TEST-
   ```

### Step 4: Test Prefix Display
1. **Refresh the edit page**
2. **Check if prefixes appear** in the fields
3. **Check logs for**:
   ```
   Loading prefix for type: expense, scheme_id: 123
   Prefix found: TEST-
   Current prefixes array: {"expense":"TEST-","purchase":"","purchase_return":""}
   ```

## Common Issues and Solutions

### Issue 1: "Apply button not working"
**Symptoms**: No console messages when clicking apply
**Causes**:
- JavaScript error on page
- jQuery not loaded
- Event listener not attached

**Solutions**:
1. Check for JavaScript errors in console
2. Verify jQuery is loaded (type `$` in console)
3. Check if button has correct class `apply-global-prefix`

### Issue 2: "Global fixes not showing"
**Symptoms**: Prefix fields are empty after reload
**Causes**:
- Controller not passing prefixes to view
- Database has no invoice schemes
- View not using prefix variables

**Solutions**:
1. Check Laravel logs for prefix loading messages
2. Verify invoice schemes exist in database
3. Check if `$current_prefixes` is passed to view

### Issue 3: AJAX Error
**Symptoms**: Console shows "AJAX error"
**Causes**:
- Route not found
- Permission denied
- Server error

**Solutions**:
1. Check if route exists: `business-location/apply-global-prefix`
2. Verify user has `business_settings.access` permission
3. Check Laravel error logs

## Manual Testing Steps

### Test 1: Verify Route
```javascript
// In browser console, test if route exists
fetch('/business-location/apply-global-prefix', {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify({_token: 'your-token', type: 'expense', prefix: 'TEST'})
})
```

### Test 2: Check Prefix Loading
```php
// In controller, temporarily add this to see data:
dd($current_prefixes);
```

### Test 3: Verify Database
```sql
-- Check if invoice schemes exist
SELECT * FROM invoice_schemes WHERE business_id = [YOUR_BUSINESS_ID];

-- Check business location scheme references
SELECT id, name, expense_invoice_scheme_id, purchase_invoice_scheme_id, purchase_return_invoice_scheme_id 
FROM business_locations WHERE business_id = [YOUR_BUSINESS_ID];
```

## Quick Fix Checklist

- [ ] Browser console shows no JavaScript errors
- [ ] "Global prefix script loaded" appears in console
- [ ] Apply button click shows debug messages
- [ ] Laravel logs show controller method being called
- [ ] Prefix fields show current values after reload
- [ ] AJAX request returns success response

## If Still Not Working

1. **Clear browser cache** and reload
2. **Restart Laravel server**: `php artisan serve`
3. **Clear Laravel cache**: `php artisan cache:clear`
4. **Check file permissions** on view files
5. **Verify all changes were saved** correctly

## Contact Support
If issues persist after following this guide, provide:
- Browser console screenshots
- Laravel log excerpts
- Database query results
- Exact error messages
