# Purchase Reference Debug Guide

## Issue: Purchase Reference Generating Random Words (JBULL)

The purchase reference is generating random words like "JBULL" instead of global prefix format. I've added comprehensive debugging to identify the root cause.

## Debug Steps

### Step 1: Test Purchase Creation
1. Create a new purchase transaction
2. Check the generated reference number
3. Look in Laravel logs: `storage/logs/laravel.log`

### Step 2: Check Debug Logs

**Expected Debug Output:**
```
getInvoiceNumber called - Business ID: 1, Status: final, Location ID: 1, Invoice Scheme ID: 123, Sale Type: purchase
Using provided invoice scheme ID: 123
Scheme found: YES
Scheme details - Name: Purchase Scheme - PUR-, Prefix: PUR-, Type: blank
Invoice number generated - Prefix: "PUR-", Count: "0001", Final: "PUR-0001"
```

**Problem Output (if issue exists):**
```
getInvoiceNumber called - Business ID: 1, Status: final, Location ID: 1, Invoice Scheme ID: [NULL], Sale Type: purchase
No invoice scheme ID provided, getting default scheme
Scheme found: YES
Scheme details - Name: Default Scheme, Prefix: [EMPTY], Type: blank
Invoice number generated - Prefix: "", Count: "JBULL", Final: "JBULL"
```

### Step 3: Identify Root Cause

**Possible Issues:**

1. **No Purchase Invoice Scheme Set:**
   - Purchase location has no `purchase_invoice_scheme_id`
   - Falls back to default scheme with empty prefix
   - Uses random word generation

2. **Purchase Invoice Scheme Has No Prefix:**
   - Scheme exists but prefix is empty
   - Generates random words instead of prefix format

3. **Scheme Not Found:**
   - Invalid scheme ID
   - Falls back to default behavior

### Step 4: Fix the Issue

**Solution 1: Set Purchase Invoice Scheme**
1. Go to Business Settings > Business Locations > Edit location
2. Set purchase prefix (e.g., "PUR-")
3. Click Apply to save
4. Verify scheme is created in database

**Solution 2: Check Database**
```sql
-- Check if purchase invoice scheme exists
SELECT * FROM invoice_schemes WHERE business_id = [YOUR_BUSINESS_ID] AND prefix LIKE 'PUR%';

-- Check location has purchase scheme assigned
SELECT id, name, purchase_invoice_scheme_id FROM business_locations WHERE business_id = [YOUR_BUSINESS_ID];
```

**Solution 3: Manual Scheme Creation**
If no scheme exists, create one manually:
```sql
INSERT INTO invoice_schemes (business_id, name, scheme_type, prefix, start_number, total_digits, is_default) 
VALUES ([YOUR_BUSINESS_ID], 'Purchase Scheme', 'blank', 'PUR-', 1, 4, 0);
```

### Step 5: Test Again

After fixing:
1. Create new purchase
2. Reference should be: `PUR-0001`
3. Check logs to confirm proper generation

## Quick Fix Checklist

- [ ] Purchase prefix set in business location settings?
- [ ] Applied prefix successfully?
- [ ] Purchase invoice scheme exists in database?
- [ ] Scheme has correct prefix?
- [ ] Location assigned to purchase scheme?

## Expected Final Result

Purchase references should generate as:
- `PUR-0001`, `PUR-0002`, `PUR-0003`
- NOT: `JBULL`, `RANDOM`, `TEST-123`

The debugging will show exactly where the issue is occurring.
