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

?>

<div class="a4-invoice">

    <!-- HEADER -->
    <table width="100%" class="header-table">
        <tr>
            <td width="15%" class="logo-cell">
    <?php if(!empty($company['logo'])): ?>
        <img src="../<?= $company['logo'] ?>" class="company-logo">
    <?php endif; ?>
</td>
            <td width="55%" class="company-details">
                <h2><?= htmlspecialchars($company['company_name']) ?></h2>
                <div><?= nl2br(htmlspecialchars($company['address'])) ?></div>
                <div>GSTIN: <?= htmlspecialchars($company['gst_no']) ?></div>
                <div>Mobile: <?= htmlspecialchars($company['phone']) ?> | Email: <?= htmlspecialchars($company['email']) ?></div>
            </td>
            <td width="30%" class="invoice-box">
                <div class="invoice-title"><?= (($inv['total_gst'] ?? 0) == 0) ? 'INVOICE' : 'TAX INVOICE' ?></div>
                <div class="original-text">ORIGINAL FOR RECIPIENT</div>

                <div class="invoice-meta">
                    <svg id="barcode" data-invoice="<?= htmlspecialchars($inv['invoice_no']) ?>"></svg>
                    <div><b>Invoice No:</b> <?= htmlspecialchars($inv['invoice_no']) ?></div>
                    <div><b>Date:</b> <?= date('d-m-Y', strtotime($inv['invoice_date'])) ?></div>
                </div>
            </td>
        </tr>
    </table>

    <!-- CUSTOMER BLOCK -->
    <table width="100%" class="customer-table">
        <tr>
            <td width="50%">
                <b>Bill To:</b><br>
                <?= htmlspecialchars($client['name']) ?><br>
                <?= nl2br(htmlspecialchars($client['address'] ?? '')) ?><br>
                <?php if(!empty($client['gst_no'])): ?>
                    GSTIN: <?= htmlspecialchars($client['gst_no']) ?>
                <?php endif; ?>
            </td>
            <td width="50%">
                <b>Ship To:</b><br>
                <?= htmlspecialchars($client['name']) ?><br>
                <?= nl2br(htmlspecialchars($client['address'] ?? '')) ?>
            </td>
        </tr>
    </table>

    <!-- ITEM TABLE -->
    <table width="100%" class="item-table">
        <thead>
            <tr>
                <th>Sr</th>
                <th>Item Description</th>
                <th>MRP</th>
                <th>Brand</th>
                <th>HSN</th>
                <th>Tax %</th>
                <th>Qty</th>
                <th>Rate (Incl)</th>
                <th>Disc</th>
                <th>Per</th>
                <th>Amount</th>
            </tr>
        </thead>
        <tbody>
        <?php 
        $i = 1;
        while($row = $items->fetch_assoc()):
            $gst_percent = $row['gst_percent'];
            $rate_incl   = $row['selling_price_incl'];   // after discount, inclusive
            $discDisplay = ($row['discount_type'] == 'percent')
                ? $row['discount_value'].' %'
                : '₹ '.$row['discount_value'];
        ?>
            <tr>
                <td class="c"><?= $i++ ?></td>
                <td><?= htmlspecialchars($row['product_name']) ?></td>
                <td class="r"><?= number_format($row['mrp'],2) ?></td>
                <td><?= htmlspecialchars($row['brand']) ?></td>
                <td class="c"><?= htmlspecialchars($row['hsn_code']) ?></td>
                <td class="c"><?= $gst_percent ?>%</td>
                <td class="r"><?= $row['sold_qty'] ?></td>
                <td class="r"><?= number_format($rate_incl,2) ?></td>
                <td class="c"><?= $discDisplay ?></td>
                <td class="c"><?= htmlspecialchars($row['unit']) ?></td>
                <td class="r"><?= number_format($row['total_amount'],2) ?></td>
            </tr>
        <?php endwhile; ?>
        </tbody>
    </table>
    <!-- TOTALS & TAX SUMMARY -->
    <table width="100%" class="totals-table">
        <tr>
            <td width="60%" valign="top">

                <!-- HSN WISE TAX SUMMARY -->
                <table width="100%" class="hsn-table">
                    <thead>
                        <tr>
                            <th rowspan="2">HSN</th>
                            <th rowspan="2">Taxable</th>
                            <th colspan="2">CGST</th>
                            <th colspan="2">SGST</th>
                            <th rowspan="2">Total Tax</th>
                        </tr>
                        <tr>
                            <th>%</th>
                            <th>Amt</th>
                            <th>%</th>
                            <th>Amt</th>
                        </tr>
                    </thead>
                    <tbody>
                    <?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,
                                'percent' => $r['gst_percent']/2
                            ];
                        }
                        $hsnSummary[$hsn]['taxable'] += $r['taxable_amount'];
                        $hsnSummary[$hsn]['cgst'] += $r['gst_amount']/2;
                        $hsnSummary[$hsn]['sgst'] += $r['gst_amount']/2;
                    }

                    foreach($hsnSummary as $hsn=>$d):
                    ?>
                        <tr>
                            <td class="c"><?= $hsn ?></td>
                            <td class="r"><?= number_format($d['taxable'],2) ?></td>
                            <td class="c"><?= $d['percent'] ?>%</td>
                            <td class="r"><?= number_format($d['cgst'],2) ?></td>
                            <td class="c"><?= $d['percent'] ?>%</td>
                            <td class="r"><?= number_format($d['sgst'],2) ?></td>
                            <td class="r"><?= number_format($d['cgst']+$d['sgst'],2) ?></td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>

            </td>

            <td width="40%" valign="top">



                <!-- TOTALS BOX -->
                <table width="100%" class="grand-table">
                    <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>
					<?php 
$exactTotal = $inv['total_taxable'] + $inv['total_gst'];
$roundedTotal = round($exactTotal);
$roundOff = $roundedTotal - $exactTotal;
?> 
						<td>Round Off</td>
						<td class="r"><?= number_format($roundOff,2) ?></td>
					</tr>
					<tr class="grand-total">
						<td>Grand Total</td>
						<td class="r"><?= number_format($roundedTotal,2) ?></td>
					</tr>
                </table>

            </td>
        </tr>
    </table>

    <!-- AMOUNT IN WORDS -->
    <div class="amount-words">
        <b>Amount (in words):</b> <?= numberToWords($inv['net_total']) ?>
    </div>

    <!-- BANK & UPI -->
    <table width="100%" class="bank-table">
        <tr>
            <td width="70%">
                <b>Bank Details</b><br>
                Bank Name : <?= htmlspecialchars($company['bank_name']) ?><br>
                A/C No : <?= htmlspecialchars($company['account_no']) ?><br>
                IFSC : <?= htmlspecialchars($company['ifsc']) ?><br>
                Branch : <?= htmlspecialchars($company['branch'] ?? '') ?>
            </td>
            <td width="30%" class="qr-box">
                <div id="upiQR"
					 data-upi="<?= htmlspecialchars($company['upi_id']) ?>"
					 data-name="<?= htmlspecialchars($company['upi_name']) ?>"
					 data-amount="<?= number_format($inv['net_total'],2,'.','') ?>">
				</div>
                <div class="qr-text">Scan & Pay</div>
            </td>
        </tr>
    </table>

    <!-- TERMS & CONDITIONS -->
    <div class="terms">
        <b>Terms & Conditions</b>
        <ol>
            <li>Goods once sold will not be taken back.</li>
            <li>All disputes subject to local jurisdiction only.</li>
            <li>Interest @18% p.a. will be charged if payment is delayed.</li>
        </ol>
    </div>

    <!-- DECLARATION & SIGN -->
    <table width="100%" class="footer-table">
        <tr>
            <td width="60%">
                <b>Declaration:</b><br>
                We declare that this invoice shows the actual price of the goods described and that all particulars are true and correct.<br><br>
                <i>This is a computer generated invoice.</i>
            </td>
            <td width="40%" class="sign-box">
                For <b><?= htmlspecialchars($company['company_name']) ?></b><br><br><br>
                Authorised Signatory
            </td>
        </tr>
    </table>

</div> <!-- end a4-invoice -->
