<?php
require_once '../includes/number_to_words.php';
// Variables available: $company, $inv, $client, $items
$roundedTotal = round($inv['net_total']);
$roundOff = $roundedTotal - $inv['net_total'];

// HSN summary
$hsnSummary = [];
$items->data_seek(0);

while($row = $items->fetch_assoc()){
    $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'];
}

$items->data_seek(0);
?>

<div class="modern-invoice">

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

    <!-- HEADER -->
    <div class="mi-header">
        <div class="mi-left">
            <?php if(!empty($company['logo'])): ?>
                <img src="../<?= $company['logo'] ?>" class="mi-logo">
            <?php endif; ?>

            <div>
                <div class="mi-company"><?= strtoupper($company['company_name']) ?></div>
                <div class="mi-small">
                    <?= 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; ?>
                </div>
            </div>
        </div>

        <div class="mi-right">
            <div class="mi-title">
                <?= ($inv['status'] === 'final') ? "TAX INVOICE" : "QUOTATION" ?>
            </div>

            <div class="mi-inv-meta">
                <div><b>Invoice No:</b> <?= htmlspecialchars($inv['invoice_no']) ?></div>
                <div><b>Date:</b> <?= date('d-m-Y', strtotime($inv['invoice_date'])) ?></div>
            </div>

            <svg id="barcode" data-invoice="<?= htmlspecialchars($inv['invoice_no']) ?>"></svg>
        </div>
    </div>

    <!-- BILL TO -->
    <div class="mi-party">
        <div class="mi-box">
            <div class="mi-box-title">Bill To</div>
            <b><?= htmlspecialchars($client['name']) ?></b><br>
            <?= nl2br(htmlspecialchars($client['address'] ?? '')) ?><br>
            Mobile: <?= htmlspecialchars($client['phone'] ?? '') ?><br>
            <?php if(!empty($client['gst_no'])): ?>
                GSTIN: <?= htmlspecialchars($client['gst_no']) ?>
            <?php endif; ?>
        </div>

        <div class="mi-box">
            <div class="mi-box-title">Ship To</div>
            <b><?= htmlspecialchars($client['name']) ?></b><br>
            <?= nl2br(htmlspecialchars($client['address'] ?? '')) ?>
        </div>
    </div>

    <!-- ITEMS -->
    <table class="mi-items">
        <thead>
            <tr>
                <th style="width:5%;">#</th>
                <th style="width:35%;">Item</th>
                <th style="width:10%;">HSN</th>
                <th style="width:8%;">MRP</th>
                <th style="width:7%;">Qty</th>
                <th style="width:10%;">Rate</th>
                <th style="width:8%;">Disc</th>
                <th style="width:7%;">GST%</th>
                <th style="width:10%;">Amount</th>
            </tr>
        </thead>

        <tbody>
        <?php
        $sr = 1;
        $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'];
                }
            }
        ?>
            <tr>
                <td class="c"><?= $sr++ ?></td>
                <td>
                    <b><?= htmlspecialchars($row['product_name']) ?></b>
                    <?php if(!empty($row['item_description'])): ?>
                        <div class="mi-desc"><?= nl2br(htmlspecialchars($row['item_description'])) ?></div>
                    <?php endif; ?>
                </td>
                <td class="c"><?= htmlspecialchars($row['hsn_code']) ?></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"><?= $discText ?></td>
                <td class="c"><?= number_format($row['gst_percent'],2) ?></td>
                <td class="r"><b><?= number_format($row['total_amount'], 2) ?></b></td>
            </tr>
        <?php endwhile; ?>
        </tbody>
    </table>

    <!-- TOTALS + HSN -->
    <div class="mi-bottom">

        <!-- LEFT -->
        <div class="mi-left-panel">

            <div class="mi-section-title">HSN Summary</div>
            <table class="mi-hsn">
                <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>

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

            <div class="mi-bank">
                <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'] ?? '') ?>
            </div>

        </div>

        <!-- RIGHT -->
        <div class="mi-right-panel">

            <table class="mi-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>
                    <td>Round Off</td>
                    <td class="r"><?= number_format($roundOff,2) ?></td>
                </tr>
                <tr class="mi-grand">
                    <td>Grand Total</td>
                    <td class="r">₹ <?= number_format($roundedTotal,2) ?></td>
                </tr>
            </table>

            <div class="mi-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="mi-qr-text">Scan & Pay</div>
                <div class="mi-qr-amt">₹ <?= number_format($roundedTotal,2) ?></div>
            </div>

        </div>
    </div>

    <!-- TERMS + SIGN -->
    <div class="mi-footer">
        <div class="mi-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>

        <div class="mi-sign">
            For <b><?= htmlspecialchars($company['company_name']) ?></b><br><br><br>
            Authorised Signatory
        </div>
    </div>

</div>
