# Quick Reference: PL11089/PL689 Fix

## 🔴 The Problem

```
User requests: PL11089
System returns: PL689 (WRONG!)
```

## 🟢 The Solution

Changed SQL queries from:
```sql
WHERE RTRIM(LTRIM(document_number)) = ?
```

To:
```sql
WHERE RTRIM(LTRIM(document_number)) COLLATE Latin1_General_BIN = ? COLLATE Latin1_General_BIN
```

## 📊 Before vs After

| Scenario | Before Fix | After Fix |
|----------|------------|-----------|
| Query "PL11089" | Returns PL689 ❌ | Returns PL11089 ✅ |
| Query "PL689" | Returns PL689 ✅ | Returns PL689 ✅ |
| Query "BP703" | Returns BP703 ✅ | Returns BP703 ✅ |

## 🧪 Quick Test Commands

```bash
# Test 1: PL11089 should return PL11089 only
curl "http://localhost:8001/documents/by-document-number?document_number=PL11089" | jq '.items[].document_number'

# Expected output:
# "PL11089"

# Test 2: PL689 should return PL689 only  
curl "http://localhost:8001/documents/by-document-number?document_number=PL689" | jq '.items[].document_number'

# Expected output:
# "PL689"

# Test 3: Run full test suite
./test_fix_pl_mismatch.sh
```

## 📁 Modified Files (7 queries total)

### aumentum_browser_service.py
- ✅ Line 827: `lr_source_document` query
- ✅ Line 857: `alf_node_properties` query

### aumentum_api.py
- ✅ Line 301: `/documents/by-document-number`
- ✅ Line 900: `/documents/metadata-by-number`
- ✅ Line 1279: `/documents/by-document-number-enhanced`
- ✅ Line 1370: `/documents/pdf-by-document-number-fixed`
- ✅ Line 1502: `/documents/list-by-document-number`

## 🚀 To Apply Fix

```bash
# 1. Changes are already in the code files
# 2. Restart API server
pkill -f "python.*aumentum_api.py"
cd /home/plagis/workspace/plagis_aumentum
python3 aumentum_api.py &

# 3. Test
./test_fix_pl_mismatch.sh
```

## 💡 What Changed Technically

**Binary Collation** forces exact character-by-character matching:

```sql
-- Without binary collation (default SQL Server):
-- Might use fuzzy matching rules
'PL689' ≈ 'PL11089' in some edge cases ❌

-- With binary collation:
-- Exact byte comparison
'PL689' ≠ 'PL11089' always ✅
```

## ⚡ One-Line Summary

Added `COLLATE Latin1_General_BIN` to all document_number queries for exact matching.

---

**Status**: ✅ Fix Applied  
**Testing**: Run `./test_fix_pl_mismatch.sh`

