# QR Code Nahi Dikh Raha - Fix Guide

## Problem: Invoice mein QR code show nahi ho raha

QR code nahi dikhnay ki **3 main wajah** hoti hain:

### 1. Invoice Layout Settings ❌
- `show_qr_code` disabled hai
- `qr_code_fields` empty hai ya set nahi hai

### 2. Transaction Status ❌
- Transaction ka status "final" nahi hai
- QR code sirf "final" status wale transactions ke liye generate hota hai

### 3. Template Issue ❌
- Invoice template file missing hai
- Template mein QR code section nahi hai

## Quick Fix (3 Methods)

### Method 1: SQL Script (Fastest) ⚡

**phpMyAdmin open karein aur yeh run karein:**

```sql
-- Enable QR code for all layouts
UPDATE invoice_layouts 
SET show_qr_code = 1,
    qr_code_fields = '["business_name","tax_1","invoice_no","invoice_datetime","total_amount","customer_name"]'
WHERE show_qr_code = 0 OR qr_code_fields IS NULL OR qr_code_fields = '[]';

-- Verify
SELECT id, name, show_qr_code, qr_code_fields FROM invoice_layouts;
```

### Method 2: PHP Diagnostic Script 🔍

**Terminal mein run karein:**

```bash
php diagnose_and_fix_qr.php
```

Yeh script:
- ✓ Sab settings check karega
- ✓ Issues batayega
- ✓ Automatic fix kar sakta hai
- ✓ Detailed report dega

### Method 3: Manual UI Fix 🖱️

1. **Settings → Invoice Layouts** mein jaaein
2. **Apni layout Edit** karein
3. **"Show QR Code" checkbox** check karein
4. **QR Code Fields** select karein:
   - ✓ business_name
   - ✓ tax_1 (VAT Number)
   - ✓ invoice_no
   - ✓ invoice_datetime
   - ✓ total_amount
   - ✓ customer_name
5. **Save** karein

## Step-by-Step Fix

### Step 1: Check Current Settings

```sql
-- Apni invoice layout ki settings dekhein
SELECT 
    id,
    name,
    design,
    show_qr_code,
    qr_code_fields
FROM invoice_layouts;
```

**Expected Result:**
- `show_qr_code` = 1 (enabled)
- `qr_code_fields` = ["business_name","tax_1",...] (not empty)

### Step 2: Enable QR Code

Agar disabled hai to enable karein:

```sql
-- Specific layout ke liye (ID 1 ko apni layout ID se replace karein)
UPDATE invoice_layouts 
SET show_qr_code = 1,
    qr_code_fields = '["business_name","tax_1","invoice_no","invoice_datetime","total_amount","customer_name"]'
WHERE id = 1;
```

Ya sab layouts ke liye:

```sql
-- Sab layouts ke liye
UPDATE invoice_layouts 
SET show_qr_code = 1,
    qr_code_fields = '["business_name","tax_1","invoice_no","invoice_datetime","total_amount","customer_name"]';
```

### Step 3: Verify Transaction Status

```sql
-- Recent transactions check karein
SELECT 
    id,
    invoice_no,
    type,
    status
FROM transactions 
ORDER BY id DESC 
LIMIT 10;
```

**Important:** Status "final" hona chahiye QR code ke liye!

### Step 4: Test with New Sale

1. **POS Pro** open karein
2. **Naya sale** complete karein
3. **Invoice** check karein
4. **QR code** dikh raha hai? ✓

## Common Issues & Solutions

### Issue 1: QR Code Field Empty

**Problem:** `qr_code_fields` NULL ya empty hai

**Fix:**
```sql
UPDATE invoice_layouts 
SET qr_code_fields = '["business_name","tax_1","invoice_no","invoice_datetime","total_amount","customer_name"]'
WHERE qr_code_fields IS NULL OR qr_code_fields = '[]' OR qr_code_fields = 'null';
```

### Issue 2: show_qr_code Disabled

**Problem:** `show_qr_code` = 0

**Fix:**
```sql
UPDATE invoice_layouts 
SET show_qr_code = 1
WHERE show_qr_code = 0;
```

### Issue 3: Transaction Status Not Final

**Problem:** Transaction ka status "draft" ya "quotation" hai

**Fix:** Yeh normal hai. QR code sirf final invoices mein dikhta hai.
- POS se sale complete karne par status automatically "final" hota hai
- Quotations mein QR code nahi hota

### Issue 4: Old Transactions

**Problem:** Purane transactions mein QR code nahi aa raha

**Fix:** Purane transactions update nahi hote. Naya sale karein test ke liye.

### Issue 5: Cache Issue

**Problem:** Settings update karne ke baad bhi nahi dikh raha

**Fix:**
```bash
php artisan cache:clear
php artisan config:clear
php artisan view:clear
```

## Verification Checklist

Settings fix karne ke baad yeh sab check karein:

- [ ] `show_qr_code` = 1 hai?
- [ ] `qr_code_fields` mein values hain?
- [ ] Invoice layout location ko assigned hai?
- [ ] Transaction status "final" hai?
- [ ] Naya sale kiya test ke liye?
- [ ] Cache clear kiya?
- [ ] Browser refresh kiya (Ctrl+F5)?

## Complete Fix Command (All-in-One)

Ek hi command mein sab fix karne ke liye:

```sql
-- Step 1: Enable QR code
UPDATE invoice_layouts 
SET show_qr_code = 1,
    qr_code_fields = '["business_name","tax_1","invoice_no","invoice_datetime","total_amount","customer_name"]',
    common_settings = JSON_SET(
        COALESCE(common_settings, '{}'),
        '$.show_qr_code_label', true
    );

-- Step 2: Verify
SELECT 
    id,
    name,
    show_qr_code AS 'QR Enabled',
    qr_code_fields AS 'QR Fields'
FROM invoice_layouts;

-- Step 3: Check location assignment
SELECT 
    bl.id,
    bl.name,
    bl.invoice_layout_id,
    il.name AS layout_name,
    il.show_qr_code
FROM business_locations bl
LEFT JOIN invoice_layouts il ON bl.invoice_layout_id = il.id;
```

## Testing

Fix karne ke baad:

1. **Cache clear karein:**
   ```bash
   php artisan cache:clear
   ```

2. **Browser refresh karein** (Ctrl+F5)

3. **POS Pro se naya sale karein**

4. **Invoice check karein:**
   - QR code bottom right mein hona chahiye
   - 200x200px size ka
   - Scan karne par invoice details milni chahiye

## Diagnostic Tool Use Karein

Agar abhi bhi issue hai to diagnostic tool use karein:

**Browser mein open karein:**
```
http://127.0.0.1:8001/sells/{transaction_id}/diagnostic
```

Replace `{transaction_id}` with actual transaction ID.

Yeh page aapko exactly batayega kya issue hai.

## Important Notes

1. **QR Code sirf "final" transactions ke liye:**
   - Completed sales ✓
   - Quotations ✗
   - Draft invoices ✗

2. **QR Code Fields required:**
   - Kam se kam 1 field select hona chahiye
   - Recommended: business_name, tax_1, invoice_no, total_amount

3. **Template file:**
   - File exist karni chahiye: `resources/views/sale_pos/receipts/saudi-invoice.blade.php`
   - Template mein QR code section hona chahiye

4. **Purane transactions:**
   - Purane transactions update nahi hote
   - Naya sale karke test karein

## Quick Reference

**Enable QR Code:**
```sql
UPDATE invoice_layouts SET show_qr_code = 1;
```

**Set QR Fields:**
```sql
UPDATE invoice_layouts SET qr_code_fields = '["business_name","tax_1","invoice_no","invoice_datetime","total_amount","customer_name"]';
```

**Check Settings:**
```sql
SELECT id, name, show_qr_code, qr_code_fields FROM invoice_layouts;
```

**Clear Cache:**
```bash
php artisan cache:clear
```

## Still Not Working?

Agar abhi bhi QR code nahi dikh raha:

1. **Laravel logs check karein:**
   ```
   storage/logs/laravel.log
   ```

2. **Browser console check karein** (F12)

3. **Diagnostic tool run karein:**
   ```bash
   php diagnose_and_fix_qr.php
   ```

4. **Template file check karein:**
   ```
   resources/views/sale_pos/receipts/saudi-invoice.blade.php
   ```
   Line 662-665 mein QR code section hona chahiye

5. **Database directly check karein:**
   ```sql
   SELECT * FROM invoice_layouts WHERE id = YOUR_LAYOUT_ID;
   ```

## Summary

**Sabse Common Fix:**
```sql
UPDATE invoice_layouts 
SET show_qr_code = 1,
    qr_code_fields = '["business_name","tax_1","invoice_no","invoice_datetime","total_amount","customer_name"]';
```

**Phir:**
1. Cache clear karein
2. Naya sale karein
3. Invoice check karein

**QR code ab dikhna chahiye!** ✅

Agar abhi bhi issue hai to `diagnose_and_fix_qr.php` script run karein ya diagnostic page use karein.
