<?php
$hsnSummary = [];
$items->data_seek(0);

while($r = $items->fetch_assoc()){
    $hsn = $r['hsn_code'];
    if(!isset($hsnSummary[$hsn])){
        $hsnSummary[$hsn] = [
            'taxable' => 0,
            'cgst' => 0,
            'sgst' => 0,
            'rate' => $r['gst_percent']/2
        ];
    }
    $hsnSummary[$hsn]['taxable'] += $r['taxable_amount'];
    $hsnSummary[$hsn]['cgst'] += $r['gst_amount']/2;
    $hsnSummary[$hsn]['sgst'] += $r['gst_amount']/2;
}

$exact = $inv['total_taxable'] + $inv['total_gst'];
$rounded = round($exact);
$roundoff = $rounded - $exact;
?>

<table class="ti-bottom" width="100%">
<tr>
    <!-- HSN SUMMARY -->
    <td width="60%" valign="top">
        <table class="ti-hsn">
            <thead>
                <tr>
                    <th rowspan="2">HSN</th>
                    <th rowspan="2">Taxable</th>
                    <th colspan="2">CGST</th>
                    <th colspan="2">SGST</th>
                </tr>
                <tr>
                    <th>%</th><th>Amt</th>
                    <th>%</th><th>Amt</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach($hsnSummary as $hsn=>$d): ?>
                <tr>
                    <td class="c"><?= $hsn ?></td>
                    <td class="r"><?= number_format($d['taxable'],2) ?></td>
                    <td class="c"><?= $d['rate'] ?>%</td>
                    <td class="r"><?= number_format($d['cgst'],2) ?></td>
                    <td class="c"><?= $d['rate'] ?>%</td>
                    <td class="r"><?= number_format($d['sgst'],2) ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </td>

    <!-- TOTALS -->
    <td width="40%" valign="top">
        <table class="ti-totals">
            <tr><td>Taxable Amount</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><td>Round Off</td><td class="r"><?= number_format($roundoff,2) ?></td></tr>
            <tr class="ti-grand-row">
                <td>Grand Total</td>
                <td class="r"><?= number_format($rounded,2) ?></td>
            </tr>
        </table>
    </td>
</tr>
</table>

<div class="ti-words">
    <b>Amount in Words:</b> <?= numberToWords($rounded) ?>
</div>

<table class="ti-bank-wrap" width="100%">
<tr>
    <td class="ti-bank-box">
        <b>Bank Details</b><br>
        Bank : <?= $company['bank_name'] ?><br>
        A/C No : <?= $company['account_no'] ?><br>
        IFSC : <?= $company['ifsc'] ?>
    </td>
    <td class="ti-qr-box">
        <div id="upiQR"
             data-upi="<?= $company['upi_id'] ?>"
             data-name="<?= $company['upi_name'] ?>"
             data-amount="<?= number_format($rounded,2,'.','') ?>">
        </div>
        <div class="ti-qr-text">Scan & Pay</div>
		<div class="ti-qr-amount">₹ <?= number_format($rounded,2) ?></div>
    </td>
</tr>
</table>

<table class="ti-decl-wrap" width="100%">
<tr>
    <td class="ti-decl-box">
        <b>Declaration</b><br>
            <p style="white-space: wrap;">We declare that this invoice shows the actual price of the goods described and that all particulars are true and correct.</p><br>
    </td>
    <td class="ti-sign-box">
        For <b><?= $company['company_name'] ?></b><br><br>
        Authorised Signatory
    </td>
</tr>
</table>