# Saudi Invoice Layout - Database Setup Guide

## Quick Setup (3 Steps)

### Step 1: Add Saudi Layout to Database

**Option A: Using phpMyAdmin (Recommended)**

1. Open phpMyAdmin
2. Select your database
3. Click "SQL" tab
4. Copy and paste this command:

```sql
INSERT INTO invoice_layouts (
    business_id, name, invoice_heading, footer_text, design,
    show_logo, show_business_name, show_location_name,
    show_landmark, show_city, show_state, show_zip_code, show_country,
    show_mobile_number, show_alternate_number, show_email,
    show_tax_1, show_tax_2, show_barcode, show_qr_code,
    qr_code_fields, table_product_label, table_qty_label,
    table_unit_price_label, table_subtotal_label, invoice_no_prefix,
    common_settings, created_at, updated_at
) VALUES (
    1, 'Saudi Invoice', 'فاتورة ضريبية / TAX INVOICE',
    'شكراً لتعاملكم معنا | Thank you for your business',
    'saudi-invoice', 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1,
    '["business_name","tax_1","invoice_no","invoice_datetime","total_amount","customer_name"]',
    'الصنف / Product', 'الكمية / Qty', 'السعر / Price', 'الإجمالي / Total',
    'INV', '{"show_qr_code_label":true}', NOW(), NOW()
);
```

5. Click "Go" button
6. You should see: "1 row inserted"

**Option B: Using SQL File**

1. Download `quick_add_saudi_layout.sql` file
2. Open phpMyAdmin
3. Select your database
4. Click "Import" tab
5. Choose the SQL file
6. Click "Go"

### Step 2: Get the Layout ID

Run this to see your new layout:

```sql
SELECT id, name, design FROM invoice_layouts WHERE design = 'saudi-invoice';
```

Note down the `id` (let's say it's 5)

### Step 3: Assign to Location

Replace `5` with your layout ID and `1` with your location ID:

```sql
UPDATE business_locations 
SET invoice_layout_id = 5
WHERE id = 1;
```

## Verify Setup

Check if everything is set correctly:

```sql
-- Check invoice layout
SELECT 
    id,
    name,
    design,
    show_logo,
    show_qr_code,
    qr_code_fields
FROM invoice_layouts 
WHERE design = 'saudi-invoice';

-- Check location assignment
SELECT 
    id,
    name,
    invoice_layout_id
FROM business_locations;
```

## Upload Logo

After database setup:

1. Go to: **Settings → Invoice Layouts**
2. Find "Saudi Invoice" in the list
3. Click **Edit**
4. Upload your logo (PNG, 200x80px recommended)
5. Click **Save**

## Test Invoice

1. Go to **POS Pro**
2. Make a test sale
3. Invoice will open with Saudi design
4. Check:
   - ✓ Green colors
   - ✓ Arabic/English text
   - ✓ Logo (if uploaded)
   - ✓ QR code
   - ✓ Professional layout

## Troubleshooting

### Layout Not Showing in Dropdown?

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

Or refresh the page (Ctrl+F5)

### Design Not Applied?

Check the design field:
```sql
SELECT id, name, design FROM invoice_layouts;
```

Should show: `saudi-invoice`

If not, update it:
```sql
UPDATE invoice_layouts 
SET design = 'saudi-invoice' 
WHERE name = 'Saudi Invoice';
```

### QR Code Not Showing?

Check QR settings:
```sql
SELECT show_qr_code, qr_code_fields 
FROM invoice_layouts 
WHERE design = 'saudi-invoice';
```

Should show:
- show_qr_code: 1
- qr_code_fields: ["business_name","tax_1",...]

If not, update:
```sql
UPDATE invoice_layouts 
SET show_qr_code = 1,
    qr_code_fields = '["business_name","tax_1","invoice_no","invoice_datetime","total_amount","customer_name"]'
WHERE design = 'saudi-invoice';
```

## Multiple Layouts

If you want to keep your old layout and add Saudi as new:

1. The INSERT command creates a NEW layout
2. You can switch between layouts in Settings
3. Each location can use different layout

To switch layout for a location:
```sql
-- See all layouts
SELECT id, name FROM invoice_layouts;

-- Assign layout to location
UPDATE business_locations 
SET invoice_layout_id = YOUR_LAYOUT_ID
WHERE id = YOUR_LOCATION_ID;
```

## Update Existing Layout to Saudi Design

If you want to convert your existing layout to Saudi design:

```sql
-- Update your existing layout (change id = 1 to your layout id)
UPDATE invoice_layouts 
SET design = 'saudi-invoice',
    name = 'Saudi Invoice',
    invoice_heading = 'فاتورة ضريبية / TAX INVOICE',
    footer_text = 'شكراً لتعاملكم معنا | Thank you for your business',
    show_logo = 1,
    show_qr_code = 1,
    show_barcode = 0,
    qr_code_fields = '["business_name","tax_1","invoice_no","invoice_datetime","total_amount","customer_name"]',
    table_product_label = 'الصنف / Product',
    table_qty_label = 'الكمية / Qty',
    table_unit_price_label = 'السعر / Price',
    table_subtotal_label = 'الإجمالي / Total'
WHERE id = 1;
```

## Complete Setup Checklist

- [ ] Run INSERT SQL command
- [ ] Verify layout created (SELECT query)
- [ ] Assign layout to location (UPDATE business_locations)
- [ ] Upload logo from UI (Settings → Invoice Layouts)
- [ ] Set VAT number (Settings → Business Settings)
- [ ] Test with sample sale
- [ ] Check logo appears
- [ ] Check QR code appears
- [ ] Verify colors and layout

## SQL Files Provided

1. **quick_add_saudi_layout.sql** - Simple INSERT command
2. **add_saudi_invoice_layout.sql** - Detailed version with all fields
3. **setup_saudi_invoice.sql** - Update existing layout

Choose any one based on your needs!

## Support

If you face any issues:

1. Check Laravel logs: `storage/logs/laravel.log`
2. Run diagnostic: `/sells/{transaction_id}/diagnostic`
3. Verify database values with SELECT queries
4. Clear cache and try again

## Summary

**Quick Commands:**

```sql
-- 1. Add layout
INSERT INTO invoice_layouts (business_id, name, design, show_logo, show_qr_code, qr_code_fields, created_at, updated_at)
VALUES (1, 'Saudi Invoice', 'saudi-invoice', 1, 1, '["business_name","tax_1","invoice_no","invoice_datetime","total_amount"]', NOW(), NOW());

-- 2. Get layout ID
SELECT id FROM invoice_layouts WHERE design = 'saudi-invoice';

-- 3. Assign to location (replace 5 with your layout id)
UPDATE business_locations SET invoice_layout_id = 5 WHERE id = 1;

-- 4. Verify
SELECT * FROM invoice_layouts WHERE design = 'saudi-invoice';
```

Done! Your Saudi invoice layout is ready! 🇸🇦
