# Sales Invoice Templates System

## Overview
The sales invoice system now uses a template-based architecture, allowing for multiple invoice designs and easy customization.

---

## Folder Structure

```
Sales/
├── print-invoice.php              # Router - redirects to selected template
└── templates/
    └── sale-invoice-template1.php # Default GST invoice template
```

---

## How It Works

### 1. **Template Router** (`print-invoice.php`)
- Acts as a central router for all invoice print requests
- Checks database for template preference
- Redirects to the appropriate template file
- Default: `sale-invoice-template1.php`

### 2. **Template Files** (`templates/`)
- Contains actual invoice designs
- Each template is a standalone PHP file
- Can have different layouts, styles, and data presentations
- Currently available: **Template 1** (GST Tax Invoice)

---

## Database Integration

### New Column Added to `tblstore_settings`:
```sql
InvoiceTemplate VARCHAR(100) DEFAULT 'sale-invoice-template1.php'
```

This stores which template to use for invoices. Can be changed from Store Settings page in the future.

---

## Usage

### For Developers:
**To create a new template:**
1. Copy `sale-invoice-template1.php` to `sale-invoice-template2.php`
2. Modify the design/layout as needed
3. Update store settings to use new template:
   ```sql
   UPDATE tblstore_settings 
   SET InvoiceTemplate = 'sale-invoice-template2.php' 
   WHERE StoreCode = 'ST0002';
   ```

### For Users:
**Printing an invoice:**
1. Navigate to Sales → View/Print
2. Click "Print Invoice"
3. System automatically loads the configured template
4. Click Print button

**URL Structure:**
```
/Sales/print-invoice.php?invoice=INV001
↓ Redirects to ↓
/Sales/templates/sale-invoice-template1.php?invoice=INV001
```

---

## Template 1 Features (sale-invoice-template1.php)

✅ **Dynamic Store Information** (from Store Settings)
- Store Logo (Logo 1)
- Company Name
- Address, City, Postcode
- Contact Numbers (Mobile, Phone)
- Email, Website
- GST Number

✅ **Customer Details**
- Bill To & Ship To sections
- Customer name, address, state
- GST Number

✅ **Invoice Information**
- Invoice Number & Date
- Payment Mode
- Reverse Charge status
- Purchase Order details

✅ **Line Items Table**
- Serial Number
- Product Description
- HSN Code
- Quantity, Price, Discount
- Basic Rate (after discount)
- Taxable Amount
- GST Percentage & Amount
- Total per item

✅ **GST Summary**
- CGST Amount
- SGST Amount
- IGST Amount
- Freight/Packing Charges
- Round Off
- Grand Total

✅ **Bank Details** (Multi-line from Store Settings)

✅ **Amount in Words**

✅ **Footer Section**
- Declaration with dynamic city jurisdiction
- Signature (if enabled) OR QR Code
- Footer Banner (Logo 2)

---

## Path Structure

### Template File Paths:
Since templates are in a subfolder, all includes use `../../` instead of `../`:

```php
// Config
include_once('../../includes/config.php');

// Redirects
header('location:../../login.php');

// Images
<img src="../../<?php echo $storeSettings['StoreLogo1']; ?>">
```

---

## Future Enhancements

### Possible Template Variations:
1. **Template 2** - Thermal Printer (80mm) Invoice
2. **Template 3** - Minimalist Design
3. **Template 4** - Letterhead Style
4. **Template 5** - Multiple Languages

### Template Selector (Future):
Add dropdown in Store Settings page:
```php
<select name="invoiceTemplate">
    <option value="sale-invoice-template1.php">Template 1 - GST Standard</option>
    <option value="sale-invoice-template2.php">Template 2 - Thermal</option>
    <option value="sale-invoice-template3.php">Template 3 - Minimalist</option>
</select>
```

---

## Benefits

✅ **Easy Customization** - Create new templates without touching core files  
✅ **Multiple Designs** - Switch between templates per business need  
✅ **Store-Specific** - Different stores can use different templates  
✅ **Maintainable** - Each template is independent  
✅ **Scalable** - Add unlimited templates  

---

## Technical Details

### Template Requirements:
1. Must accept `?invoice=` parameter
2. Must connect to database via `../../includes/config.php`
3. Must check user session
4. Must fetch store settings from `tblstore_settings`
5. Must fetch sale data from `tblsales` and `tblsales_gst_details`

### Print Button:
```html
<button onclick="window.print()">🖨️ Print</button>
<button onclick="window.close()">❌ Close</button>
```

### Print CSS:
```css
@media print {
    .no-print { display: none !important; }
    .invoice-container { border: none; padding: 0; }
}
```

---

## Testing

**Test the router:**
```
http://localhost/customized/UZRS-inventory/UZRS-inventory/Sales/print-invoice.php?invoice=INV001
```

**Direct template access:**
```
http://localhost/customized/UZRS-inventory/UZRS-inventory/Sales/templates/sale-invoice-template1.php?invoice=INV001
```

---

**Created:** November 5, 2025  
**Status:** ✅ Fully Implemented  
**Default Template:** sale-invoice-template1.php  
**Location:** `/Sales/templates/`
