<?php
require_once '../includes/number_to_words.php';
// Variables available: $company, $inv, $client, $items
?>

<div class="tally-invoice">

    <!-- HEADER -->
    <table class="ti-header" cellspacing="0" cellpadding="0">
        <tr>
            <td class="ti-head-left">
                <div class="ti-brand">
                    <?php if(!empty($company['logo'])): ?>
                        <img src="../<?= $company['logo'] ?>" class="ti-logo">
                    <?php endif; ?>
                    <div class="ti-company-name">
                        <?= strtoupper($company['company_name']) ?>
                    </div>
                </div>
            </td>

            <td class="ti-head-right">
                <div class="ti-title">TAX INVOICE</div>
            </td>
        </tr>

        <tr>
            <td class="ti-address">
                <?= nl2br(htmlspecialchars($company['address'])) ?><br>
                GSTIN: <?= htmlspecialchars($company['gst_no']) ?><br>
                Ph: <?= htmlspecialchars($company['phone']) ?>
                <?php if(!empty($company['email'])): ?>
                    | Email: <?= htmlspecialchars($company['email']) ?>
                <?php endif; ?>
            </td>

            <td class="ti-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 -->
    <table class="ti-party" cellspacing="0" cellpadding="0">
        <tr>
            <td 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 class="ti-party-box">
                <b>Ship To</b><br>
                <?= htmlspecialchars($client['name']) ?><br>
                <?= nl2br(htmlspecialchars($client['address'] ?? '')) ?>
            </td>
        </tr>
    </table>

    <!-- ITEMS -->
    <table class="ti-items" cellspacing="0" cellpadding="0">
        <thead>
            <tr>
                <th style="width:4%;">Sr</th>
                <th style="width:35%;">Item Description</th>
                <th style="width:10%;">HSN</th>
                <th style="width:6%;">Unit</th>
                <th style="width:8%;">MRP</th>
                <th style="width:6%;">Qty</th>
                <th style="width:10%;">Rate</th>
                <th style="width:6%;">GST%</th>
                <th style="width:7%;">Disc</th>
                <th style="width:12%;">Amount</th>
            </tr>
        </thead>
        <tbody>

        <?php
        $i = 1;
        $hsnSummary = [];

        $items->data_seek(0);
        while($row = $items->fetch_assoc()):

            $discText = "-";
            if(!empty($row['discount_type'])) {
                if($row['discount_type'] == "percent") {
                    $discText = $row['discount_value']."%";
                } else {
                    $discText = "₹".$row['discount_value'];
                }
            }

            $hsn = $row['hsn_code'] ?? '';
            if(!isset($hsnSummary[$hsn])) {
                $hsnSummary[$hsn] = [
                    "taxable" => 0,
                    "gst" => 0,
                    "percent" => $row['gst_percent']
                ];
            }

            $hsnSummary[$hsn]["taxable"] += $row['taxable_amount'];
            $hsnSummary[$hsn]["gst"] += $row['gst_amount'];
        ?>

            <tr>
                <td class="c"><?= $i++ ?></td>
                <td>
                    <?= htmlspecialchars($row['product_name']) ?>
                    <?php if(!empty($row['item_description'])): ?>
                        <div class="ti-desc"><?= nl2br(htmlspecialchars($row['item_description'])) ?></div>
                    <?php endif; ?>
                </td>
                <td class="c"><?= htmlspecialchars($row['hsn_code']) ?></td>
                <td class="c"><?= htmlspecialchars($row['unit']) ?></td>
                <td class="r"><?= number_format($row['mrp'] ?? 0, 2) ?></td>
                <td class="r"><?= number_format($row['sold_qty'], 2) ?></td>
                <td class="r"><?= number_format($row['selling_price_incl'], 2) ?></td>
                <td class="c"><?= number_format($row['gst_percent'],2) ?></td>
                <td class="c"><?= $discText ?></td>
                <td class="r"><?= 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;
    ?>

    <!-- HSN + TOTALS -->
    <table class="ti-bottom" cellspacing="0" cellpadding="0">
        <tr>
            <td width="60%" valign="top">
                <table class="ti-hsn" cellspacing="0" cellpadding="0">
                    <thead>
                        <tr>
                            <th>HSN</th>
                            <th>Taxable</th>
                            <th>GST%</th>
                            <th>GST Amt</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach($hsnSummary as $hsn => $d): ?>
                            <tr>
                                <td class="c"><?= htmlspecialchars($hsn) ?></td>
                                <td class="r"><?= number_format($d['taxable'],2) ?></td>
                                <td class="c"><?= number_format($d['percent'],2) ?></td>
                                <td class="r"><?= number_format($d['gst'],2) ?></td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </td>

            <td width="40%" valign="top">
                <table class="ti-totals" cellspacing="0" cellpadding="0">
                    <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">
                        <td>Grand Total</td>
                        <td class="r"><?= number_format($roundedTotal,2) ?></td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>

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

    <!-- BANK + QR -->
    <table class="ti-bank" cellspacing="0" cellpadding="0">
        <tr>
            <td width="70%" class="ti-bank-box">
                <b>Bank Details</b><br>
                Bank: <?= htmlspecialchars($company['bank_name']) ?><br>
                A/C: <?= htmlspecialchars($company['account_no']) ?><br>
                IFSC: <?= htmlspecialchars($company['ifsc']) ?><br>
                Branch: <?= htmlspecialchars($company['branch'] ?? '') ?>
            </td>

            <td width="30%" class="ti-qr-box">
                <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-amt">₹ <?= number_format($roundedTotal,2) ?></div>
            </td>
        </tr>
    </table>

    <!-- TERMS -->
    <div class="ti-terms">
        <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.
    </div>

    <!-- DECLARATION + SIGN -->
    <table class="ti-sign" cellspacing="0" cellpadding="0">
        <tr>
            <td width="65%" class="ti-decl">
                <b>Declaration</b><br>
                <p class="decdata">We declare that this invoice shows the actual price of the goods described and that all particulars are true and correct.</p><br><br>
                <i>This is a computer generated invoice.</i>
            </td>
            <td width="35%" class="ti-auth">
                For <b><?= htmlspecialchars($company['company_name']) ?></b><br><br><br>
                Authorised Signatory
            </td>
        </tr>
    </table>

</div>
