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



?>

<div class="tally-invoice">



<?php if($inv['status'] == 'draft'): ?>
    <div class="watermark">QUOTATION</div>
<?php endif; ?>

<!-- ================= TALLY HEADER ================= -->
<table class="ti-header" width="100%" cellpadding="0" cellspacing="0">




    <tr>
        <!-- TOP LEFT: LOGO + COMPANY NAME -->
        <td class="ti-head-left">
            <div class="ti-head-brand">
                <?php if(!empty($company['logo'])): ?>
					<img src="../<?= $company['logo'] ?>" class="ti-head-logo" style="max-width:90px; max-height:70px; width:auto; height:auto;">
                <?php endif; ?>
                <span class="ti-head-company"><?= strtoupper($company['company_name']) ?></span>
            </div>
        </td>

        <!-- TOP RIGHT: TAX INVOICE -->
        <td class="ti-head-right ti-tax-title">
            <?= ($inv['status'] == 'final') ? 'TAX INVOICE' : 'QUOTATION' ?>
        </td>
    </tr>

    <tr>
        <!-- BOTTOM LEFT: ADDRESS -->
        <td class="ti-head-left ti-head-address">
            <?= nl2br(htmlspecialchars($company['address'])) ?><br>
            GSTIN: <?= htmlspecialchars($company['gst_no']) ?><br>
            Ph: <?= htmlspecialchars($company['phone']) ?> |
            Email: <?= htmlspecialchars($company['email']) ?>
        </td>

        <!-- BOTTOM RIGHT: META + BARCODE -->
        <td class="ti-head-right ti-head-meta">
            <div class="ti-original">ORIGINAL FOR RECIPIENT</div>
            <div><b>Invoice No:</b> <?= htmlspecialchars($inv['invoice_no']) ?></div>
            <div><b>Date:</b> <?= date('d-m-Y', strtotime($inv['invoice_date'])) ?></div>
            <svg id="barcode" data-invoice="<?= htmlspecialchars($inv['invoice_no']) ?>"></svg>
        </td>
    </tr>
</table>

<!-- ================= PARTY DETAILS ================= -->
<table class="ti-party" width="100%" cellpadding="0" cellspacing="0">
    <tr>
        <td width="50%" class="ti-party-box">
            <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']) ?><br>
            <?php endif; ?>
            Mobile: <?= htmlspecialchars($client['phone'] ?? '') ?>
        </td>

        <td width="50%" class="ti-party-box">
            <b>Ship To</b><br>
            <?= htmlspecialchars($client['name']) ?><br>
            <?= nl2br(htmlspecialchars($client['address'] ?? '')) ?>
        </td>
    </tr>
</table>

<!-- ================= ITEM GRID ================= -->
<table class="ti-items">
    <thead>
        <tr>
            <th class="w-sr">Sr</th>
            <th class="w-item">Item Description</th>
            <th class="w-hsn">HSN</th>
            <th class="w-mrp">MRP</th>
            <th class="w-qty">Qty</th>
            <th class="w-rate">Rate</th>
            <th class="w-gst">GST%</th>
            <th class="w-disc">Disc</th>
            <th class="w-amt">Amount</th>
        </tr>
    </thead>
    <tbody>
        <?php $i = 1; ?>
        <?php while($row = $items->fetch_assoc()): ?>
            <tr>
                <td class="c"><?= $i++ ?></td>

                <td>
                    <?= htmlspecialchars($row['product_name']) ?>
                    <?php if(!empty($row['description'])): ?>
                        <div class="item-sub-desc">
                            <?= nl2br(htmlspecialchars($row['description'])) ?>
                        </div>
                    <?php endif; ?>
                </td>

                <td class="c" style="text-align:right"><?= $row['hsn_code'] ?></td>
                <td class="r" style="text-align:right"><?= number_format($row['mrp'],2) ?></td>
                <td class="r" style="text-align:right"><?= $row['sold_qty'] ?></td>
                <td class="r" style="text-align:right"><?= number_format($row['selling_price_incl'],2) ?></td>
                <td class="c" style="text-align:right"><?= $row['gst_percent'] ?>%</td>
                <td class="c" style="text-align:right"><?= $row['discount_value'] ?></td>
                <td class="r" style="text-align:right"><?= number_format($row['total_amount'],2) ?></td>
            </tr>
        <?php endwhile; ?>
    </tbody>
</table>


<?php
$exactTotal   = $inv['total_taxable'] + $inv['total_gst'];
$roundedTotal = round($exactTotal);
$roundOff     = $roundedTotal - $exactTotal;

// Build HSN summary
$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;
}
?>

<table class="ti-bottom" width="100%" cellpadding="0" cellspacing="0">
    <tr>
        <!-- HSN SUMMARY (LEFT) -->
        <td width="60%" valign="top">
            <table class="ti-hsn" width="100%">
                <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 foreach($hsnSummary as $hsn=>$d): ?>
                    <tr>
                        <td class="c" style="text-align:right"><?= $hsn ?></td>
                        <td class="r" style="text-align:right"><?= number_format($d['taxable'],2) ?></td>
                        <td class="c" style="text-align:right"><?= $d['percent'] ?>%</td>
                        <td class="r" style="text-align:right"><?= number_format($d['cgst'],2) ?></td>
                        <td class="c" style="text-align:right"><?= $d['percent'] ?>%</td>
                        <td class="r" style="text-align:right"><?= number_format($d['sgst'],2) ?></td>
                        <td class="r" style="text-align:right"><?= number_format($d['cgst']+$d['sgst'],2) ?></td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </td>

        <!-- TOTALS BOX (RIGHT) -->
        <td width="40%" valign="top">
            <table class="ti-totals" width="100%">
                <tr>
                    <td>Taxable Amount</td>
                    <td class="r" style="text-align:right"><?= number_format($inv['total_taxable'],2) ?></td>
                </tr>
                <tr>
                    <td>CGST</td>
                    <td class="r" style="text-align:right"><?= number_format($inv['total_gst']/2,2) ?></td>
                </tr>
                <tr>
                    <td>SGST</td>
                    <td class="r" style="text-align:right"><?= number_format($inv['total_gst']/2,2) ?></td>
                </tr>
                <tr>
                    <td>Round Off</td>
                    <td class="r" style="text-align:right"><?= number_format($roundOff,2) ?></td>
                </tr>
                <tr class="ti-grand-row">
                    <td>Grand Total</td>
                    <td class="r" style="text-align:right"><?= number_format($roundedTotal,2) ?></td>
                </tr>
            </table>
        </td>
    </tr>
</table>


<table class="ti-bottom">
    <tr>
        <td width="60%" valign="top">
            <!-- HSN SUMMARY -->
        </td>

        <td width="40%" valign="top">
            <!-- TOTALS BOX -->
        </td>
    </tr>
</table>

<!-- ================= AMOUNT IN WORDS ================= -->
<div class="ti-words">
    <b>Amount (in words):</b> <?= numberToWords($roundedTotal) ?>
</div>

<!-- ================= BANK & UPI ================= -->
<table class="ti-bank-wrap" width="100%" cellpadding="0" cellspacing="0">
    <tr>
        <td width="70%" class="ti-bank-box">
            <b>Bank Details</b><br>
            Bank : <?= 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="ti-qr-box">
			<div class="ti-qr-center">
				<div id="upiQR"
					 data-upi="<?= htmlspecialchars($company['upi_id']) ?>"
					 data-name="<?= htmlspecialchars($company['upi_name']) ?>"
					 data-amount="<?= number_format($roundedTotal,2,'.','') ?>">
				</div>
				<div class="ti-qr-text">Scan & Pay</div>
				<div class="ti-qr-amount">₹ <?= number_format($roundedTotal,2) ?></div>
			</div>
		</td>
    </tr>
</table>

<!-- ================= TERMS ================= -->
<table class="ti-terms-wrap" width="100%" cellpadding="0" cellspacing="0">
    <tr>
        <td class="ti-terms-box">
            <b>Terms & Conditions</b><br>
            1. Goods once sold will not be taken back.<br>
            2. Interest @18% p.a. will be charged on delayed payments.<br>
            3. All disputes subject to local jurisdiction only.
        </td>
    </tr>
</table>

<!-- ================= DECLARATION & SIGN ================= -->
<table class="ti-decl-wrap" width="100%" cellpadding="0" cellspacing="0">
    <tr>
        <td width="65%" 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>
            <i>This is a computer generated invoice.</i>
        </td>
        <td width="35%" class="ti-sign-box">
            For <b><?= htmlspecialchars($company['company_name']) ?></b><br><br><br>
            Authorised Signatory
        </td>
    </tr>
</table>

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