<?php
// layout_a4_tally.php
// Variables available: $company, $inv, $client, $items
?>

<div class="print-root">

<div class="tally-invoice">

    <!-- HEADER -->
    <table class="ti-header">
        <tr>
            <td class="ti-left">
                <?php if (!empty($company['logo'])): ?>
                    <img src="../<?= $company['logo'] ?>" class="ti-logo">
                <?php endif; ?>
                <div class="ti-company"><?= strtoupper($company['company_name']) ?></div>
                <div class="ti-small">
                    <?= nl2br(htmlspecialchars($company['address'])) ?><br>
                    GSTIN: <?= $company['gst_no'] ?><br>
                    Ph: <?= $company['phone'] ?>
                </div>
            </td>

            <td class="ti-right">
                <div class="ti-title">
                    <?= $inv['status'] === 'final' ? 'TAX INVOICE' : 'QUOTATION' ?>
                </div>
                <div class="ti-small">Invoice No: <?= $inv['invoice_no'] ?></div>
                <div class="ti-small">Date: <?= date('d-m-Y', strtotime($inv['invoice_date'])) ?></div>
                <svg id="barcode" data-invoice="<?= $inv['invoice_no'] ?>"></svg>
            </td>
        </tr>
    </table>

    <!-- PARTY -->
    <table class="ti-party">
        <tr>
            <td>
                <b>Bill To</b><br>
                <?= htmlspecialchars($client['name']) ?><br>
                <?= htmlspecialchars($client['address'] ?? '') ?>
            </td>
            <td>
                <b>Ship To</b><br>
                <?= htmlspecialchars($client['name']) ?><br>
                <?= htmlspecialchars($client['address'] ?? '') ?>
            </td>
        </tr>
    </table>

    <!-- ITEMS -->
    <table class="ti-items">
        <thead>
            <tr>
                <th>Sr</th>
                <th>Item Description</th>
                <th>HSN</th>
                <th>Qty</th>
                <th>Rate</th>
                <th>GST%</th>
                <th>Amount</th>
            </tr>
        </thead>
        <tbody>
        <?php $i=1; while($row = $items->fetch_assoc()): ?>
            <tr>
                <td class="c"><?= $i++ ?></td>
                <td><?= htmlspecialchars($row['product_name']) ?></td>
                <td class="c"><?= $row['hsn_code'] ?></td>
                <td class="r"><?= $row['sold_qty'] ?></td>
                <td class="r"><?= number_format($row['selling_price_incl'],2) ?></td>
                <td class="c"><?= $row['gst_percent'] ?>%</td>
                <td class="r"><?= number_format($row['total_amount'],2) ?></td>
            </tr>
        <?php endwhile; ?>
        </tbody>
    </table>

    <!-- TOTALS -->
    <table class="ti-totals">
        <tr><td>Taxable</td><td class="r"><?= number_format($inv['total_taxable'],2) ?></td></tr>
        <tr><td>CGST</td><td class="r"><?= number_format($inv['total_gst']/2,2) ?></td></tr>
        <tr><td>SGST</td><td class="r"><?= number_format($inv['total_gst']/2,2) ?></td></tr>
        <tr class="grand"><td>Grand Total</td><td class="r"><?= number_format($inv['net_total'],2) ?></td></tr>
    </table>

    <!-- QR -->
    <div class="ti-qr">
        <div id="upiQR"
             data-upi="<?= $company['upi_id'] ?>"
             data-name="<?= $company['upi_name'] ?>"
             data-amount="<?= number_format($inv['net_total'],2,'.','') ?>">
        </div>
        <div class="ti-small">Scan & Pay ₹<?= number_format($inv['net_total'],2) ?></div>
    </div>

    <!-- FOOTER -->
    <div class="ti-footer">
        This is a computer generated invoice.
    </div>

</div>
</div>
