# Invoice Print Templates - Store Settings Integration

## Summary of Changes

Both **Sales** and **Purchase** invoice print templates have been updated to dynamically use store settings from the database instead of hardcoded values.

---

## Files Updated

1. **`Sales/print-invoice.php`**
2. **`Purchase/print-invoice.php`**

---

## Database Integration

### Store Settings Query Added
```php
// Get store settings
$storeSettingsQuery = "SELECT * FROM tblstore_settings WHERE StoreCode = 'ST0002' LIMIT 1";
$storeSettingsResult = mysqli_query($con, $storeSettingsQuery);
$storeSettings = null;

if ($storeSettingsResult && mysqli_num_rows($storeSettingsResult) > 0) {
    $storeSettings = mysqli_fetch_assoc($storeSettingsResult);
}
```

This query runs immediately after invoice validation and before fetching sale data.

---

## Dynamic Fields Implemented

### 1. **Header Section** (Company Details)

#### Before (Hardcoded):
```html
<h1>SUNRISE ENTERPRISE</h1>
<p><strong>General Store - Delhi-181005</strong></p>
<p># S-50, 3rd PTC Building, I.T. Estate, New Delhi-1358XX</p>
<p><strong>Contact No. :</strong> +91-985689XXX9, +91- 98458XXX38</p>
<p><strong>GSTIN :</strong> 07BCUP03647XXXX</p>
```

#### After (Dynamic from DB):
```php
<h1><?php echo htmlspecialchars($storeSettings['StoreName'] ?? 'STORE NAME'); ?></h1>
<p><strong><?php echo htmlspecialchars($storeSettings['City']); ?> - <?php echo htmlspecialchars($storeSettings['Postcode']); ?></strong></p>
<p><?php echo htmlspecialchars($storeSettings['Address']); ?></p>
<p><strong>Contact No. :</strong> <?php echo htmlspecialchars($storeSettings['Mobile']); ?>, <?php echo htmlspecialchars($storeSettings['Phone']); ?></p>
<p><strong>Email :</strong> <?php echo htmlspecialchars($storeSettings['Email']); ?></p>
<p><strong>GSTIN :</strong> <?php echo htmlspecialchars($storeSettings['GSTNumber']); ?></p>
<p><strong>Website :</strong> <?php echo htmlspecialchars($storeSettings['StoreWebsite']); ?></p>
```

**Uses:** `StoreName`, `City`, `Postcode`, `Address`, `Mobile`, `Phone`, `Email`, `GSTNumber`, `StoreWebsite`

---

### 2. **Company Logo** (Header)

#### Before:
```html
<img src="../img/logo-left.jpg" alt="Logo">
```

#### After:
```php
<?php if ($storeSettings && !empty($storeSettings['StoreLogo1'])): ?>
    <img src="../<?php echo htmlspecialchars($storeSettings['StoreLogo1']); ?>" alt="Logo">
<?php else: ?>
    <div style='font-size:24px;color:#1976d2;'>🏪</div>
<?php endif; ?>
```

**Uses:** `StoreLogo1` - Displays uploaded logo or default icon

---

### 3. **Bank Details Section**

#### Before (Hardcoded):
```html
<span class="detail-label">Bank Name :</span> STATE BANK OF INDIA
<span class="detail-label">Branch :</span> Delhi
<span class="detail-label">Account No :</span> 20412XXXX05
<span class="detail-label">IFSC Code :</span> SBIN003XXXX
<span class="detail-label">UPI ID :</span> yourid@upi
```

#### After (Dynamic - Multi-line):
```php
<?php if ($storeSettings && !empty($storeSettings['BankDetails'])): 
    $bankLines = explode("\n", $storeSettings['BankDetails']);
    foreach ($bankLines as $line): 
        $line = trim($line);
        if (!empty($line)):
?>
    <div class="detail-row">
        <span class="detail-value"><?php echo htmlspecialchars($line); ?></span>
    </div>
<?php 
        endif;
    endforeach; 
endif; 
?>
```

**Uses:** `BankDetails` - Each line from textarea displayed separately

**Example Format:**
```
Ac. Name : UZRS SOFTWARE SOLUTIONS
Ac.No: 9250 2000 2281 803
IFSC: UTIB0001234
```

---

### 4. **Signature Section**

#### Before:
```html
<div style="font-weight: bold;">For, SUNRISE ENTERPRISE</div>
<div class="qr-code">
    <img src="https://api.qrserver.com/v1/create-qr-code/?..." alt="QR Code">
</div>
```

#### After (Conditional - Signature or QR):
```php
<div style="font-weight: bold;">For, <?php echo strtoupper(htmlspecialchars($storeSettings['StoreName'])); ?></div>

<?php if ($storeSettings && $storeSettings['ShowSignatureOnInvoice'] && !empty($storeSettings['SignatureImage'])): ?>
    <img src="../<?php echo htmlspecialchars($storeSettings['SignatureImage']); ?>" alt="Signature" 
         style="max-width: 120px; max-height: 60px;">
<?php else: ?>
    <div class="qr-code">
        <img src="https://api.qrserver.com/v1/create-qr-code/?..." alt="QR Code">
    </div>
<?php endif; ?>
```

**Uses:** `StoreName`, `ShowSignatureOnInvoice` (checkbox), `SignatureImage`

**Logic:**
- If "Show Signature on Invoice" is checked AND signature image exists → Display signature
- Otherwise → Display QR code

---

### 5. **Declaration Section**

#### Before:
```html
<p>1. Subject to Mehsana jurisdiction</p>
```

#### After:
```php
<p>1. Subject to <?php echo htmlspecialchars($storeSettings['City'] ?? 'Local'); ?> jurisdiction</p>
```

**Uses:** `City` - Dynamic city name in jurisdiction statement

---

### 6. **Footer Banner/Logo**

#### Before:
```html
<div class="footer-image">
    <img src="path/to/your/footer-image.jpg" alt="Footer Banner" 
         onerror="this.style.display='none'">
</div>
```

#### After:
```php
<?php if ($storeSettings && !empty($storeSettings['StoreLogo2'])): ?>
<div class="footer-image">
    <img src="../<?php echo htmlspecialchars($storeSettings['StoreLogo2']); ?>" alt="Footer Banner">
</div>
<?php endif; ?>
```

**Uses:** `StoreLogo2` - Only displays if second logo is uploaded

---

## Store Settings Fields Used

| Field Name | Invoice Section | Usage |
|------------|-----------------|-------|
| `StoreName` | Header, Signature | Company name display |
| `StoreLogo1` | Header | Main company logo |
| `StoreLogo2` | Footer | Footer banner/logo |
| `Mobile` | Header | Primary contact number |
| `Phone` | Header | Secondary contact number |
| `Email` | Header | Email address |
| `GSTNumber` | Header | GST registration number |
| `StoreWebsite` | Header | Website URL (optional) |
| `Address` | Header | Full address |
| `City` | Header, Declaration | City name |
| `Postcode` | Header | PIN/ZIP code |
| `State` | - | Available for future use |
| `BankDetails` | Bank Details Section | Multi-line bank info |
| `ShowSignatureOnInvoice` | Signature Section | Boolean toggle |
| `SignatureImage` | Signature Section | Uploaded signature file |

---

## Benefits

✅ **Single Point of Update** - Change store details once in settings, reflects on all invoices  
✅ **No Code Editing** - Update via Store Settings page, no PHP file modifications  
✅ **Consistent Branding** - Same store info across Sales & Purchase invoices  
✅ **Dynamic Logos** - Upload and change logos anytime  
✅ **Signature Control** - Toggle signature display on/off  
✅ **Bank Info Management** - Easy update of payment details  

---

## Testing Checklist

- [ ] Print Sales Invoice - Check all dynamic fields
- [ ] Print Purchase Invoice - Check all dynamic fields  
- [ ] Update store settings and verify changes reflect on invoices
- [ ] Upload Logo 1 and verify in invoice header
- [ ] Upload Logo 2 and verify in invoice footer
- [ ] Toggle "Show Signature" checkbox and verify behavior
- [ ] Update bank details and verify multi-line display

---

## Fallback Behavior

If store settings are not found or fields are empty:

- **Store Name** → Displays "STORE NAME"
- **Logo 1** → Shows 🏪 icon
- **Logo 2** → Footer section hidden
- **Bank Details** → Shows "N/A"
- **Signature** → Shows QR code instead
- **Other Fields** → Shows "N/A" or empty

---

**Updated:** November 5, 2025  
**Status:** ✅ Fully Implemented  
**Applies To:** Sales & Purchase Invoice Print Templates
