<?php
// REQUIRED HELPERS
require_once '../includes/number_to_words.php';

/*
Variables available:
$company
$inv
$client
$items   (mysqli result)
*/
?>

<div class="simple-invoice">

<!-- ================= HEADER ================= -->
<div class="si-header">
    <div class="si-company">
        <div class="si-company-name"><?= htmlspecialchars($company['company_name']) ?></div>
        <div class="si-company-info">
            <?= nl2br(htmlspecialchars($company['address'])) ?><br>
            GSTIN: <?= htmlspecialchars($company['gst_no']) ?><br>
            Phone: <?= htmlspecialchars($company['phone']) ?>
        </div>
    </div>

    <div class="si-invoice-meta">
        <div class="si-title">
            <?= ($inv['status'] === 'final') ? 'TAX INVOICE' : 'QUOTATION' ?>
        </div>
        <div>Invoice No: <?= htmlspecialchars($inv['invoice_no']) ?></div>
        <div>Date: <?= date('d-m-Y', strtotime($inv['invoice_date'])) ?></div>

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

<!-- ================= PARTY ================= -->
<div class="si-party">
    <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; ?>
</div>

<!-- ================= ITEMS ================= -->
<table class="si-items">
    <thead>
        <tr>
            <th>Sr</th>
            <th>Item Description</th>
            <th>HSN</th>
            <th>MRP</th>
            <th>Qty</th>
            <th>Rate</th>
            <th>Disc</th>
            <th>GST%</th>
            <th>Amount</th>
        </tr>
    </thead>
    <tbody>
    <?php
    $i = 1;
    $hsnSummary = [];

    while($row = $items->fetch_assoc()):
        $discText = '-';
        if ($row['discount_type'] === 'percent') {
            $discText = $row['discount_value'] . '%';
        } elseif ($row['discount_type'] === 'amount') {
            $discText = '₹' . number_format($row['discount_value'],2);
        }

        // HSN SUMMARY BUILD
        $hsn = $row['hsn_code'];
        if (!isset($hsnSummary[$hsn])) {
            $hsnSummary[$hsn] = [
                'taxable' => 0,
                'gst' => 0,
                'rate' => $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['description'])): ?>
                    <div class="si-desc"><?= nl2br(htmlspecialchars($row['description'])) ?></div>
                <?php endif; ?>
            </td>
            <td class="c"><?= htmlspecialchars($row['hsn_code']) ?></td>
            <td class="r"><?= isset($row['mrp']) && $row['mrp'] > 0 ? number_format($row['mrp'], 2): '-' ?></td>
            <td class="r"><?= $row['sold_qty'] ?></td>
            <td class="r"><?= number_format($row['selling_price_incl'],2) ?></td>
            <td class="c"><?= $discText ?></td>
            <td class="c"><?= $row['gst_percent'] ?>%</td>
            <td class="r"><?= number_format($row['total_amount'],2) ?></td>
        </tr>
    <?php endwhile; ?>
    </tbody>
</table>

<!-- ================= TOTALS ================= -->
<table class="si-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 class="si-grand">
        <td>Grand Total</td>
        <td class="r"><?= number_format($inv['net_total'],2) ?></td>
    </tr>
</table>

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

<!-- ================= HSN SUMMARY ================= -->
<table class="si-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"><?= $hsn ?></td>
            <td class="r"><?= number_format($d['taxable'],2) ?></td>
            <td class="c"><?= $d['rate'] ?>%</td>
            <td class="r"><?= number_format($d['gst'],2) ?></td>
        </tr>
    <?php endforeach; ?>
    </tbody>
</table>

<!-- ================= BANK + QR ================= -->
<div class="si-bank">
    <div>
        <b>Bank Details</b><br>
        Bank: <?= htmlspecialchars($company['bank_name']) ?><br>
        A/C: <?= htmlspecialchars($company['account_no']) ?><br>
        IFSC: <?= htmlspecialchars($company['ifsc']) ?>
    </div>

    <div class="si-qr">
        <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><?= number_format($inv['net_total'],2) ?></div>
        <div>Scan & Pay</div>
    </div>
</div>

<!-- ================= TERMS & SIGN ================= -->
<div class="si-footer">
    <div>
        <b>Terms & Conditions</b><br>
        Goods once sold will not be taken back.<br>
        Subject to local jurisdiction.
    </div>

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

</div>
