# Saudi Invoice Layout - Auto Installation

## Automatic Setup Using Migration

I've created a Laravel migration that will automatically add the Saudi invoice layout to your database.

## How to Run

### Method 1: Command Line (Recommended)

Open your terminal/command prompt in the project root and run:

```bash
php artisan migrate
```

This will:
- ✅ Automatically create Saudi Invoice layout in database
- ✅ Set all required fields (logo, QR code, bilingual labels)
- ✅ Configure QR code fields
- ✅ Set proper design template
- ✅ Show success message with layout ID

### Method 2: Run Specific Migration

If you want to run only this migration:

```bash
php artisan migrate --path=database/migrations/2026_02_27_035546_add_saudi_invoice_layout.php
```

## After Migration

### Step 1: Verify Layout Created

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

Or check in UI:
- Go to: Settings → Invoice Layouts
- You should see "Saudi Invoice - فاتورة سعودية" in the list

### Step 2: Assign to Location

**Option A: Using UI**
1. Go to: Settings → Business Locations
2. Click Edit on your location
3. Select "Saudi Invoice - فاتورة سعودية" from Invoice Layout dropdown
4. Click Save

**Option B: Using SQL**
```sql
-- Get the layout ID first
SELECT id FROM invoice_layouts WHERE design = 'saudi-invoice';

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

### Step 3: Upload Logo

1. Go to: Settings → Invoice Layouts
2. Find "Saudi Invoice - فاتورة سعودية"
3. Click Edit
4. Upload your logo (PNG, 200x80px recommended)
5. Click Save

### Step 4: Test

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

## Rollback (If Needed)

To remove the Saudi invoice layout:

```bash
php artisan migrate:rollback --step=1
```

This will delete the Saudi invoice layout from database.

## Troubleshooting

### Migration Already Ran?

If you see "Nothing to migrate", the migration already ran successfully.

Check if layout exists:
```sql
SELECT * FROM invoice_layouts WHERE design = 'saudi-invoice';
```

### Migration Failed?

Check Laravel logs:
```
storage/logs/laravel.log
```

Common issues:
1. Database connection error - check .env file
2. Table doesn't exist - run all migrations first: `php artisan migrate`
3. Duplicate entry - layout already exists

### Want to Re-run Migration?

1. First rollback:
```bash
php artisan migrate:rollback --step=1
```

2. Then run again:
```bash
php artisan migrate
```

## What the Migration Does

The migration file (`2026_02_27_035546_add_saudi_invoice_layout.php`) automatically:

1. ✅ Gets your business ID
2. ✅ Creates new invoice layout with name "Saudi Invoice - فاتورة سعودية"
3. ✅ Sets design to "saudi-invoice"
4. ✅ Enables logo display (show_logo = 1)
5. ✅ Enables QR code display (show_qr_code = 1)
6. ✅ Configures QR code fields:
   - business_name
   - tax_1 (VAT number)
   - invoice_no
   - invoice_datetime
   - total_amount
   - customer_name
7. ✅ Sets bilingual table labels (Arabic/English)
8. ✅ Sets invoice heading: "فاتورة ضريبية / TAX INVOICE"
9. ✅ Sets footer text with thank you message
10. ✅ Logs success message

## Files Involved

- **Migration:** `database/migrations/2026_02_27_035546_add_saudi_invoice_layout.php`
- **Template:** `resources/views/sale_pos/receipts/saudi-invoice.blade.php`
- **Guides:** 
  - SAUDI_INVOICE_SETUP.md
  - SAUDI_INVOICE_URDU_GUIDE.md
  - DATABASE_SETUP_URDU.md

## Quick Commands Reference

```bash
# Run migration
php artisan migrate

# Check migration status
php artisan migrate:status

# Rollback last migration
php artisan migrate:rollback --step=1

# Refresh all migrations (WARNING: deletes all data!)
php artisan migrate:refresh

# Clear cache after migration
php artisan cache:clear
php artisan config:clear
```

## Success Indicators

After running migration, you should see:

```
Migrating: 2026_02_27_035546_add_saudi_invoice_layout
✅ Saudi Invoice Layout created successfully!
Layout ID: 5
To use this layout, go to: Settings → Business Locations → Edit → Select 'Saudi Invoice'
Migrated:  2026_02_27_035546_add_saudi_invoice_layout (XX.XXms)
```

## Next Steps

1. ✅ Run migration: `php artisan migrate`
2. ✅ Upload logo: Settings → Invoice Layouts → Edit
3. ✅ Assign to location: Settings → Business Locations → Edit
4. ✅ Test sale: POS Pro → Complete Sale → Check Invoice

That's it! Your Saudi invoice layout is ready to use! 🇸🇦
