# QR Code Broken Image Fix - فاتورة سعودية

## Problem: QR Code Broken Icon Show Ho Raha Hai 🖼️❌

Jab QR code ki jagah broken image icon (📷 with X) dikhta hai, iska matlab hai:
- QR code text generate nahi ho raha
- Ya QR code library missing hai
- Ya settings properly configure nahi hain

## 🚀 Quick Fix (3 Steps)

### Step 1: SQL Fix Run Karein

**phpMyAdmin mein yeh commands run karein:**

```sql
-- Enable QR code aur fields set 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 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;
```

### Step 2: Cache Clear Karein

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

### Step 3: Naya Sale Karein

**Important:** Purane transactions update nahi hote!
- POS Pro se **NAYA sale** karein
- Invoice check karein
- QR code ab properly show hona chahiye ✅

## 🔍 Root Causes & Solutions

### Cause 1: QR Code Fields Empty ❌

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

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

**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 = '[]';
```

### Cause 2: show_qr_code Disabled ❌

**Problem:** QR code display disabled hai

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

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

### Cause 3: Transaction Status Not Final ❌

**Problem:** Transaction ka status "final" nahi hai

**Check:**
```sql
SELECT id, invoice_no, status FROM transactions ORDER BY id DESC LIMIT 5;
```

**Important:** QR code sirf "final" status wale transactions ke liye generate hota hai!

**Types:**
- ✅ Completed sales → status = 'final' → QR code shows
- ❌ Quotations → status = 'quotation' → NO QR code
- ❌ Draft → status = 'draft' → NO QR code

### Cause 4: DNS2D Package Missing ❌

**Problem:** QR code generation library install nahi hai

**Check:**
```bash
composer show | grep barcode
```

**Fix:**
```bash
composer require milon/barcode
```

### Cause 5: Tax Number Missing ❌

**Problem:** Business tax number set nahi hai (QR mein chahiye)

**Check:**
```sql
SELECT id, name, tax_number_1 FROM business;
```

**Fix:**
1. Settings → Business Settings → Tax
2. Tax Number 1: Enter your VAT number
3. Save

## 📋 Complete Diagnostic

### Run This SQL to Check Everything:

```sql
-- 1. Check invoice layout settings
SELECT 
    id,
    name,
    design,
    show_qr_code AS 'QR Enabled?',
    qr_code_fields AS 'QR Fields',
    CASE 
        WHEN show_qr_code = 1 AND qr_code_fields IS NOT NULL THEN '✓ OK'
        ELSE '✗ ISSUE'
    END AS status
FROM invoice_layouts;

-- 2. Check recent transactions
SELECT 
    id,
    invoice_no,
    type,
    status,
    CASE 
        WHEN status = 'final' AND type IN ('sell', 'sales_order') THEN '✓ Will show QR'
        ELSE '✗ No QR'
    END AS qr_status
FROM transactions 
ORDER BY id DESC 
LIMIT 10;

-- 3. Check business tax info
SELECT 
    id,
    name,
    tax_number_1 AS 'VAT Number',
    CASE 
        WHEN tax_number_1 IS NOT NULL THEN '✓ Set'
        ELSE '✗ Missing'
    END AS status
FROM business;
```

## 🔧 Step-by-Step Fix

### Fix 1: Enable Everything

```sql
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
    );
```

### Fix 2: Set Tax Number

```sql
UPDATE business 
SET tax_number_1 = '300000000000003',  -- Your VAT number
    tax_label_1 = 'VAT Number'
WHERE id = 1;
```

### Fix 3: Clear Cache

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

### Fix 4: Test with New Sale

1. POS Pro open karein
2. Items add karein
3. Payment complete karein
4. Invoice print hoga
5. QR code check karein

## ✅ Verification Checklist

QR code properly show ho raha hai check karein:

- [ ] QR code image dikh raha hai (not broken icon)?
- [ ] QR code scan ho raha hai?
- [ ] QR code mein data hai?
- [ ] Invoice details QR mein hain?

**Test QR Code:**
- Mobile se QR code scan karein
- Invoice details show honi chahiye:
  - Business name
  - VAT number
  - Invoice number
  - Date/time
  - Total amount
  - Customer name

## 🎯 Common Mistakes

### ❌ Mistake 1: Purana Transaction Check Kar Rahe Hain

**Problem:** Old transactions update nahi hote

**Solution:** Naya sale karke test karein

### ❌ Mistake 2: Cache Clear Nahi Kiya

**Problem:** Old cached data use ho raha hai

**Solution:** 
```bash
php artisan cache:clear
php artisan view:clear
```

### ❌ Mistake 3: Wrong Transaction Type

**Problem:** Quotation ya draft dekh rahe hain

**Solution:** Completed sale (status = 'final') check karein

### ❌ Mistake 4: QR Fields Empty

**Problem:** qr_code_fields = '[]' ya NULL

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

## 🆘 Still Not Working?

### Test 1: Run Diagnostic Script

```bash
php test_qr_generation.php
```

Yeh script check karega:
- DNS2D class installed hai?
- Transaction status kya hai?
- QR settings enabled hain?
- QR code generate ho raha hai?

### Test 2: Check Laravel Logs

```
storage/logs/laravel.log
```

Agar koi error hai to yahan dikhega.

### Test 3: Manual QR Test

Browser console mein:
```javascript
console.log(receipt_details.show_qr_code);
console.log(receipt_details.qr_code_text);
```

Agar `qr_code_text` empty hai to issue hai.

### Test 4: Check DNS2D Package

```bash
composer show milon/barcode
```

Agar "Package milon/barcode not found" aaye to:
```bash
composer require milon/barcode
```

## 📱 QR Code Content

Properly generated QR code mein yeh hona chahiye:

```
Business Name: Your Company
VAT: 300000000000003
Invoice: INV-001
Date: 2026-02-27 10:30:00
Total: 1,375.00 SAR
Customer: Walk-In Customer
```

## 🎉 Success Indicators

QR code sahi hai agar:
- ✅ Image properly load ho rahi hai
- ✅ Scan karne par data milta hai
- ✅ Invoice details readable hain
- ✅ No broken icon
- ✅ Black and white squares visible hain

## 📝 Summary

**Most Common Fix:**

```sql
-- 1. Enable QR
UPDATE invoice_layouts 
SET show_qr_code = 1,
    qr_code_fields = '["business_name","tax_1","invoice_no","invoice_datetime","total_amount","customer_name"]';

-- 2. Set tax number
UPDATE business SET tax_number_1 = 'YOUR_VAT_NUMBER' WHERE id = 1;
```

**Then:**
```bash
# 3. Clear cache
php artisan cache:clear

# 4. Test with new sale
```

**Result:** QR code properly show hoga! ✅

## 🔗 Related Files

- `FIX_QR_IMAGE_ISSUE.sql` - SQL fix script
- `test_qr_generation.php` - Diagnostic script
- `QR_CODE_FIX_URDU.md` - General QR fix guide
- `fix_qr_code_issue.sql` - Alternative fix

**Bas SQL run karein, cache clear karein, aur naya sale karein!** 🎊
