<?php
ob_start();

include 'db.php';
require 'vendor/autoload.php';

use Dompdf\Dompdf;
use Dompdf\Options;

$id = intval($_GET['id'] ?? 0);

/* ================= FETCH INVOICE ================= */
$inv = mysqli_query($con, "SELECT * FROM invoices WHERE id='$id'");
$invoice = mysqli_fetch_assoc($inv);
if (!$invoice) die('Invoice not found');

/* ================= FETCH ITEMS ================= */
$items_q = mysqli_query($con, "SELECT * FROM invoice_items WHERE invoice_id='$id' LIMIT 7");

$items = [];
$items_subtotal = 0;

while ($row = mysqli_fetch_assoc($items_q)) {
    $row['total'] = (float)$row['total'];
    $items_subtotal += $row['total'];
    $items[] = $row;
}

/* ================= GST ================= */
$cgst = 0;
$sgst = 0;
$igst = 0;

$state = strtolower(trim($invoice['state']));

if ($state == 'west bengal') {
    $cgst = round($items_subtotal * 0.015, 2);
    $sgst = round($items_subtotal * 0.015, 2);
} else {
    $igst = round($items_subtotal * 0.03, 2);
}

/* ================= HM GST LOGIC ================= */

$hm_base_amount = 0;

foreach ($items as $it) {
    $hm_base_amount += (float)$it['hm_value'];
} 
$hm_cgst = 0;
$hm_sgst = 0;
$hm_igst = 0;

if ($state == 'west bengal') {

    // Inside State → 9% + 9%
    $hm_cgst = round($hm_base_amount * 0.09, 2);
    $hm_sgst = round($hm_base_amount * 0.09, 2);

} else {

    // Outside State → 18%
    $hm_igst = round($hm_base_amount * 0.18, 2);

}

/* ================= OTHER CHARGES ================= */
$other_charges = 0;
$other_charges_list = [];

$other_q = mysqli_query($con, "SELECT * FROM invoice_other_charges WHERE invoice_id='$id'");
while ($charge = mysqli_fetch_assoc($other_q)) {
    $other_charges += (float)$charge['amount'];
    $other_charges_list[] = $charge;
}

/* ================= PAYMENTS ================= */
/* ================= PAYMENTS ================= */
$payments = [
    'UPI' => 0,
    'Cheque' => 0,
    'Metal' => 0,
    'Cash' => 0,
    'Card' => 0
];

$payments_q = mysqli_query($con,
    "SELECT payment_type, SUM(amount) total_amount
     FROM invoice_payments
     WHERE invoice_id='$id'
     GROUP BY payment_type"
);

while ($payment = mysqli_fetch_assoc($payments_q)) {

    $ptype = trim($payment['payment_type']); // no strtolower/ucfirst

    if (array_key_exists($ptype, $payments)) {
        $payments[$ptype] = (float)$payment['total_amount'];
    }
}
/* ================= METAL & DUES ================= */
$metal = 106365.00;
$dues  = 0.00;

/* ================= FINAL AMOUNT ================= */
$final_amount = round((float)$invoice['final_amount'], 2);
$round_final_amount = round($final_amount);
$round_off = $round_final_amount - $final_amount;

/* ================= DISCOUNT ================= */
$discount_type  = $invoice['discount_type'] ?? 'value';
$discount_value = (float)($invoice['discount_value'] ?? 0);

$discount_amount = 0;
if ($discount_value > 0) {
    if ($discount_type == 'percent') {
        $discount_amount = ($items_subtotal + $other_charges) * $discount_value / 100;
    } else {
        $discount_amount = $discount_value;
    }
}

/* ===================================================
    SINGLE DOWNLOAD BLOCK (FINAL)
=================================================== */
if (isset($_GET['download'])) {

    ob_end_clean();

    $options = new Options();
    $options->set('defaultFont', 'DejaVu Sans');
    $options->set('isRemoteEnabled', true);

    $dompdf = new Dompdf($options);

    ob_start();

    if ($_GET['download'] == '1') {
        include 'invoice_view_print.php';
        $filename = "invoice_main_{$invoice['invoice_no']}.pdf";
    } 
    elseif ($_GET['download'] == '2') {
        include 'invoice_view_print1.php';
        $filename = "invoice_copy_{$invoice['invoice_no']}.pdf";
    } 
    else {
        exit;
    }

    $html = ob_get_clean();

    $dompdf->loadHtml($html);
    $dompdf->setPaper('A4', 'portrait');
    $dompdf->render();

    header("Content-Type: application/pdf");
    header("Content-Disposition: attachment; filename=\"$filename\"");
    header("Cache-Control: private, max-age=0, must-revalidate");
    header("Pragma: public");

    echo $dompdf->output();
    exit;
}

/* ================= NUMBER TO WORDS ================= */
if (!function_exists('numberToWords')) {
function numberToWords($num) {

    $num = round($num);

    $ones = [
        0 => '', 1 => 'One', 2 => 'Two', 3 => 'Three', 4 => 'Four',
        5 => 'Five', 6 => 'Six', 7 => 'Seven', 8 => 'Eight',
        9 => 'Nine', 10 => 'Ten', 11 => 'Eleven', 12 => 'Twelve',
        13 => 'Thirteen', 14 => 'Fourteen', 15 => 'Fifteen',
        16 => 'Sixteen', 17 => 'Seventeen', 18 => 'Eighteen',
        19 => 'Nineteen'
    ];

    $tens = [
        2 => 'Twenty', 3 => 'Thirty', 4 => 'Forty',
        5 => 'Fifty', 6 => 'Sixty', 7 => 'Seventy',
        8 => 'Eighty', 9 => 'Ninety'
    ];

    if ($num == 0) return 'Zero';

    $result = '';

    if ($num >= 10000000) {
        $result .= numberToWords(floor($num / 10000000)) . ' Crore ';
        $num %= 10000000;
    }

    if ($num >= 100000) {
        $result .= numberToWords(floor($num / 100000)) . ' Lakh ';
        $num %= 100000;
    }

    if ($num >= 1000) {
        $result .= numberToWords(floor($num / 1000)) . ' Thousand ';
        $num %= 1000;
    }

    if ($num >= 100) {
        $result .= $ones[floor($num / 100)] . ' Hundred ';
        $num %= 100;
    }

    if ($num > 0) {
        if ($num < 20) {
            $result .= $ones[$num] . ' ';
        } else {
            $result .= $tens[floor($num / 10)];
            if ($num % 10 > 0) {
                $result .= ' ' . $ones[$num % 10];
            }
        }
    }

    return trim($result);
}
}
/* ================= STATE CODE ================= */
$state_codes = [

    // ===== STATES =====
    'andhra pradesh' => '37',
    'arunachal pradesh' => '12',
    'assam' => '18',
    'bihar' => '10',
    'chhattisgarh' => '22',
    'goa' => '30',
    'gujarat' => '24',
    'haryana' => '06',
    'himachal pradesh' => '02',
    'jharkhand' => '20',
    'karnataka' => '29',
    'kerala' => '32',
    'madhya pradesh' => '23',
    'maharashtra' => '27',
    'manipur' => '14',
    'meghalaya' => '17',
    'mizoram' => '15',
    'nagaland' => '13',
    'odisha' => '21',
    'punjab' => '03',
    'rajasthan' => '08',
    'sikkim' => '11',
    'tamil nadu' => '33',
    'telangana' => '36',
    'tripura' => '16',
    'uttar pradesh' => '09',
    'uttarakhand' => '05',
    'west bengal' => '19',

    // ===== UNION TERRITORIES =====
    'andaman and nicobar islands' => '35',
    'chandigarh' => '04',
    'dadra and nagar haveli and daman and diu' => '26',
    'delhi' => '07',
    'jammu and kashmir' => '01',
    'ladakh' => '38',
    'lakshadweep' => '31',
    'puducherry' => '34'
];
$state_code = $state_codes[$state] ?? '';
$formatted_date = date('d-m-Y', strtotime($invoice['invoice_date']));
$formatted_time = date('h:i:s A', strtotime($invoice['invoice_date']));
$customer_name = ucwords(strtolower($invoice['client_name']));
$customer_address = ucwords(strtolower($invoice['address']));
$customer_city = ucwords(strtolower($invoice['city']));
$customer_state = ucwords(strtolower($invoice['state']));
/* ================= NORMAL PAGE LOAD ================= */
$title = "Invoice View";
include 'include/header.php';
?>
<div id="page-wrapper">
    <div id="page-inner">
        <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" rel="stylesheet">

        <style>
        body {
            font-family: 'DejaVu Sans';
            background: #f2f2f2;
            margin: 0;
            font-size: 9px;
            font-weight: 400;
            color:#000;
        }

        h1,
        h2,
        h3,
        h4,
        h5,
        h6 {
            font-weight: 900;
        }

        strong,
        b {
            font-weight: 900;
        }

        .header h2 {
            font-weight: 900;
        }

        .info-box h5 {
            font-weight: 800;
        }

        .table-invoice th {
            font-weight: 800;
        }

        .table-invoice {
            font-size: 9px;
        }

        .table-invoice th,
        .table-invoice td {
            padding: 3px 4px;
            /* 6px → 3px */
        }

        .table-invoice td {
            font-weight: 600;
        }

        .footer-note {
            font-weight: 700;
            font-size: 12px;
        }

        .invoice-box {
            max-width: 900px;
            margin: auto;
            padding: 6px 12px;
            background: #fffdf8;
            border: 3px solid #d4af37;
            color: #333;
            font-size: 10px;
        }

        .header {
            display: flex;
            align-items: center;
            gap: 20px;
            padding: 18px 0 16px;
            margin-bottom: 15px;

            border-bottom: 5px solid #d4af37;
            position: relative;
        }

        .logo img {
            width: 100px;
            height: auto;
        }

        .header-text {
            flex: 1;
            text-align: center;
        }

        .header-text h2 {
            font-size: 18px;
            /* 24px → 18px */
            letter-spacing: 2px;
        }

        .header {
            padding: 10px 0;
            margin-bottom: 8px;
        }


        .header-text p {
            margin: 2px 0;
            font-size: 12px;
            color: #7b1e1e;
        }

        .info-grid {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 15px;
            margin-top: 10px;
            font-size: 12px;
        }

        .info-box {
            border: 1px solid #e5c87a;
            padding: 6px;
            background: #fffaf0;
        }

        .info-box h5 {
            margin: 0;
            color: #8b0000;
            font-size: 12px;
            border-bottom: 1px solid #e5c87a;
        }

        .items-wrapper {
            border: 1px solid #d4af37;
            background: #fffdf8;
            display: flex;
            flex-direction: column;
            margin-top: 15px;
        }

        .items-scroll {
            flex: 1;
            overflow-y: auto;
        }

        .table-invoice {
            width: 100%;
            border-collapse: collapse;
            font-size: 9px;
        }

        .table-invoice th {
            background: #f3e2a9;
            border: 1px solid #d4af37;
            padding: 6px;
            color: #5a2a27;
        }

        .table-invoice td {
            border: 1px solid #d4af37;
            padding: 5px 6px;
            text-align: center;
        }

        .total-table {
            margin-top: auto;
        }

        .total-table td {
            background: #fff1c1;
            font-weight: bold;
            border-top: 2px solid #d4af37;
        }

        .bottom-summary {
            margin-top: 8px;
            display: flex;
            justify-content: space-between;
            font-size: 11px;
        }

        .left-box,
        .right-box {
            border: 1px dashed #888;
            background: #fffdf8;
            padding: 6px;
            margin: 5px;
        }

        .left-box .row {
            display: flex;
            justify-content: space-between;
            margin-bottom: 6px;
        }

        .left-box .row strong {
            width: 60px;
            color: #7b1e1e;
        }

        .total-words {
            border: 1px dashed #888;
            padding: 6px 10px;
            margin-top: 10px;
            font-style: italic;
            background: #fffaf0;
        }

        .right-box table {
            width: 100%;
            border-collapse: collapse;
        }

        .right-box table tr {
            border-bottom: 1px dashed #888;
        }

        .right-box table td {
            padding: 5px 6px;
        }

        .right-box table td:first-child {
            font-weight: 600;
            color: #7b1e1e;
        }

        .right-box table td:last-child {
            text-align: right;
        }

        .footer-note {
            margin-top: 25px;
            font-style: italic;
            font-weight: bold;
            color: #a94442;
            text-align: center;
            background: #fff1c1;
            padding: 10px;
            font-size: 12px;
        }

        .footer-note a {
            color: #a94442;
            text-decoration: none;
            margin: 0 10px;
        }

        .footer-note .social-icons {
            margin-top: 10px;
        }

        .footer-note .social-icons a {
            margin-right: 15px;
            font-size: 16px;
        }

        .footer-note p {
            margin: 5px 0;
        }

        .sign-area {
            display: flex;
            justify-content: space-between;
            margin-top: 25px;
        }

        .sign-box {
            text-align: center;
            color: #7b1e1e;
        }

        @media print {
            body {
                background: #fff;
            }

            .invoice-box {
                border: none;
                padding: 0;
            }
        }

        .right-box table {
            font-size: 10px;
            /* Adjust this value as needed */
        }
        </style>
        <?php
$logoPath = __DIR__ . '/logo.png';
$logoData = base64_encode(file_get_contents($logoPath));
$logoSrc = 'data:image/png;base64,' . $logoData;
?>

        <div class="invoice-box">

            <table width="100%" cellpadding="0" cellspacing="0" style="
    background:#fffdf5;
    border-bottom:4px solid #d4af37;
    margin-bottom:10px;
">
                <tr>
                    <!-- LOGO -->
                    <td width="15%" align="center" style="padding:12px;">
                        <img src="<?= $logoSrc ?>" style="width:90px;">
                    </td>

                    <!-- CENTER TEXT -->
                    <td width="85%" align="center" style="padding:12px 10px;">
                        <h2 style="
                margin:0;
                color:#7b1e1e;
                font-size:20px;
                font-weight:900;
                letter-spacing:4px;
            ">
                           SHREE R.P. JEWELLERS 
                        </h2>

                        <!--            <p style="-->
                        <!--    margin:6px 0 2px;-->
                        <!--    font-size:10px;-->
                        <!--    font-weight:700;-->
                        <!--    color:#7b1e1e;-->
                        <!--">-->
                        <!--                Shree Rameshwar Prasad Sonar-->
                        <!--            </p>-->

                        <p style="
                margin:2px 0;
                font-size:12px;
                font-weight:600;
                color:#7b1e1e;
            "><b>Shree Rameshwar Prasad Sonar</b> <br>
                            <b>GSTIN:</b> 19AFSF4775H1Z3 <b>PAN No.</b> AFSFS4775H 
                        </p>
                    </td>
                </tr>
            </table>



            <!-- <div class="info-box">
                <table width="100%" cellpadding="0" cellspacing="0" style="border-collapse:collapse; font-size:9px;">
                    <tr>
                        LEFT : CUSTOMER DETAILS
                        <td width="50%" valign="top" style="padding-right:6px;">
                            <h5 style="font-size:11px;margin-bottom:4px;">Customer Details</h5>

                            <p style="margin:2px 0; font-weight: 600; color: black;">Invoice No:
                                <?= htmlspecialchars($invoice['invoice_no']) ?>
                            </p>
                            <p style="margin:2px 0; font-weight: 600; color: black;">Name:
                                <?= htmlspecialchars($customer_name) ?>
                            </p>
                            <p style="margin:2px 0; font-weight: 600; color: black;">Phone:
                                <?= htmlspecialchars($invoice['phone']) ?>
                            </p>
                            <p style="margin:2px 0; font-weight: 600; color: black;">Address:
                                <?= htmlspecialchars($customer_address) ?>,
                                <?= htmlspecialchars($customer_city) ?>,
                                <?= htmlspecialchars($customer_state) ?> -
                                <?= htmlspecialchars($invoice['pincode']) ?>
                            </p>

                            <p style="margin:2px 0; font-weight: 600; color: black;">PAN:
                                <?= htmlspecialchars($invoice['pan_no']) ?>
                            </p>
                            <p style="margin:2px 0; font-weight: 600; color: black;">GST:
                                <?= htmlspecialchars($invoice['gst_no']) ?>
                            </p>
                            <p style="margin:2px 0; font-weight: 600; color: black;">Date:
                                <?= $formatted_date ?>
                            </p>
                            <p style="margin:2px 0; font-weight: 600; color: black;">State Code:
                                <?= $state_code ?>
                            </p>

                        </td>

                        RIGHT : COMPANY + BANK DETAILS
                        <td width="50%" valign="top" style="padding-left:6px;border-left:1px dashed #c9a84e;">

                            <h5 style="font-size:11px;margin-bottom:4px;">Company Details</h5>




                            <div style="font-size:11px; line-height:1.3; color: black;">
                                 <strong>Bank Details:<br>
                                        BANK NAME : ICICI BANK<br>
                                        A/C : SRI R P JEWELLERS <br>
                                        A/C NO : 192905002553 <br>
                                        BRANCH : SILIGURI-S F ROAD <br>
                                        IFSC CODE : ICICI0001929 </strong><br><br>
                            </div>

                        </td>
                    </tr>
                </table>
            </div> -->

<div class="info-box">
                    <table width="100%" cellpadding="4" cellspacing="0"
                        style="border-collapse:collapse; font-size:10px;">
                        <!-- ROW 1 -->
                        <tr style="margin:2px 0; color: black;     font-size: 12px;">
                            <td style="margin-bottom:7px; font-weight: 400;"><strong>Invoice No:</strong>
                                <?= htmlspecialchars($invoice['invoice_no']) ?>
                            </td>


                            <td style="margin-bottom:7px;"><strong>Name:</strong>
                                <?= htmlspecialchars($customer_name) ?>
                            </td>
                            <td style="margin-bottom:7px;"><strong>Phone:</strong>
                                <?= htmlspecialchars($invoice['phone']) ?>
                            </td>
                            <td style="margin-bottom:7px;"><strong>Date:</strong>
                                <?= $formatted_date ?>
                            </td>
                        </tr>

                        <!-- ROW 2 -->
                        <tr style="margin:2px 0; font-weight: 600; color: black;     font-size: 12px;">
                            <td colspan="2" style="margin-bottom:7px;">
                                <strong>Address:</strong>
                                <?= htmlspecialchars($customer_address) ?>,
                                <?= htmlspecialchars($customer_city) ?>,
                                <?= htmlspecialchars($customer_state) ?> -
                                <?= htmlspecialchars($invoice['pincode']) ?>
                            </td>
                        </tr>

                        <!-- ROW 3 -->
                        <tr style="margin:2px 0; font-weight: 600; color: black;     font-size: 12px;">
                            <td style="margin-bottom:7px;"><strong>PAN:</strong>
                                <?= htmlspecialchars($invoice['pan_no']) ?>
                            </td>
                            <td style="margin-bottom:7px;"><strong>GST:</strong>
                                <?= htmlspecialchars($invoice['gst_no']) ?>
                            </td>
                            <td colspan="2" style="margin-bottom:7px;"><strong>State Code:</strong>
                                <?= $state_code ?>
                            </td>
                        </tr>

                    </table>
                </div>
            <div class="items-wrapper">

                <!-- ITEMS (SCROLLABLE) -->
                <div class="items-scroll">
                    <table class="table-invoice" style="color: black;">
                        <thead>
                            <tr>
                                <th>Description</th>
                                <th>H.S.N</th>
                                <th>Gross Weight (gm)</th>
                                <th>Net Weight (gm)</th>
                                <th>Rate in gms</th>
                                <th>Making</th>
                                <th>Stone Value</th>
                                <th>H.M</th>
                                <th>Total</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($items as $k => $item) : ?>
                            <tr>
                                <!-- <td>
                                    <?= $k + 1 ?>
                                </td> -->
                                <td><?= htmlspecialchars($item['description']) ?></td>

                                <!-- HSN -->
                                <td><?= htmlspecialchars($item['hsn']) ?></td>

                                <!-- Gross Weight -->
                                <td><?= number_format($item['gross_weight'], 3) ?></td>

                                <!-- Net Weight -->
                                <td><?= number_format($item['net_weight'], 3) ?></td>

                                <!-- Rate -->
                                <td><?= number_format($item['rate'], 2) ?></td>

                                <!-- Making -->
                                <td><?= number_format($item['making'], 2) ?></td>

                                <!-- Stone Value -->
                                <td><?= number_format($item['stone_value'], 2) ?></td>

                                <!-- H.M Value -->
                                <td><?= number_format($item['hm_value'], 2) ?></td>

                                <!-- Total -->
                                <td><?= number_format($item['total'], 2) ?></td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>

                <!-- TOTAL (FIXED AT BOTTOM) -->
                <table class="table-invoice total-table"  style="color: black;">
                    <tr>
                        <td colspan="8"><strong>TOTAL</strong></td>
                        <td><strong>
                                <?= number_format($items_subtotal, 2) ?>
                            </strong></td>
                    </tr>
                </table>

            </div>

            <table width="100%" cellpadding="6" cellspacing="0" style="border-collapse:collapse; font-size:13px;">
                <tr>
                    <!-- LEFT : CUSTOMER DETAILS -->
                    <td width="50%" valign="top">

                        <div class="left-box" >
                            <!-- <div class="row"><strong>UPI:</strong> ₹
                                <?= number_format($payments['UPI'], 2) ?>
                            </div>
                            <div class="row"><strong>Bank:</strong> ₹
                                <?= number_format($payments['Bank'], 2) ?>
                            </div>
                            <div class="row"><strong>Metal:</strong> ₹
                                <?= number_format($metal, 2) ?>
                            </div>
                            <div class="row"><strong>Cash:</strong> ₹
                                <?= number_format($payments['Cash'], 2) ?>
                            </div>
                            <div class="row"><strong>Dues:</strong> ₹
                                <?= number_format($dues, 2) ?>
                            </div> -->


                            <table class="table-invoice"  style="color: black;">
                                <thead>
                                    <tr>
                                        <th>UPI:</th>
                                        <th>Cheque:</th>
                                        <th>Metal:</th>
                                        <th>Cash:</th>
                                        <th>Card:</th>
                                    </tr>
                                </thead>

                                <tbody>

                                    <tr>
                                        <td>₹ <?= number_format($payments['UPI'], 2) ?></td>
                                        <td>₹ <?= number_format($payments['Cheque'], 2) ?></td>
                                        <td>₹ <?= number_format($payments['Metal'], 2) ?></td>
                                        <td>₹ <?= number_format($payments['Cash'], 2) ?></td>
                                        <td>₹ <?= number_format($payments['Card'], 2) ?></td>
                                    </tr>
                                </tbody>

                            </table>
                            <div class="total-words" style="font-weight:bold; font-size:13px; color: black;" >
                                Total :
                                <?= numberToWords($round_final_amount) ?> Only
                            </div>
                        </div>
                    </td>

                    <!-- RIGHT : INVOICE DETAILS -->
                    <td width="50%" valign="top">
                        <div class="right-box" style="color: black;">
                            <table style="color: black;">
                                <tr>
                                    <td>Total :</td>
                                    <td>₹
                                        <?= number_format($items_subtotal, 2) ?>
                                    </td>
                                </tr>
                                <!-- <tr>
                                    <td>C-Gst 1.5% :</td>
                                    <td>₹
                                        <?= number_format($cgst, 2) ?>
                                    </td>
                                </tr>
                                <tr>
                                    <td>S-Gst 1.5% :</td>
                                    <td>₹
                                        <?= number_format($sgst, 2) ?>
                                    </td>
                                </tr> -->
                                <?php if ($state == 'west bengal'): ?>

                                <!-- NORMAL GST -->
                                <tr>
                                    <td>C-GST 1.5% :</td>
                                    <td>₹ <?= number_format($cgst, 2) ?></td>
                                </tr>
                                <tr>
                                    <td>S-GST 1.5% :</td>
                                    <td>₹ <?= number_format($sgst, 2) ?></td>
                                </tr>

                                <!-- HM GST -->
                                <tr>
                                    <td>HM CGST 9% :</td>
                                    <td>₹ <?= number_format($hm_cgst, 2) ?></td>
                                </tr>
                                <tr>
                                    <td>HM SGST 9% :</td>
                                    <td>₹ <?= number_format($hm_sgst, 2) ?></td>
                                </tr>

                                <?php else: ?>

                                <!-- NORMAL GST -->
                                <tr>
                                    <td>I-GST 3% :</td>
                                    <td>₹ <?= number_format($igst, 2) ?></td>
                                </tr>

                                <!-- HM GST -->
                                <tr>
                                    <td>HM IGST 18% :</td>
                                    <td>₹ <?= number_format($hm_igst, 2) ?></td>
                                </tr>

                                <?php endif; ?>

                                <?php if($discount_amount > 0): ?>
                                <tr>
                                    <td>Discount
                                        <?php if($discount_type == 'percent'): ?>
                                        (<?= $discount_value ?>%)
                                        <?php endif; ?>
                                        :
                                    </td>
                                    <td style="color:#c00000;">
                                        - ₹ <?= number_format($discount_amount, 2) ?>
                                    </td>
                                </tr>
                                <?php endif; ?>
                                <tr>
                                    <td>Final Amount :</td>
                                    <td>₹ <?= number_format($final_amount, 2) ?></td>
                                </tr>

                                <tr>
                                    <td>Round Off :</td>
                                    <td>
                                        ₹ <?= number_format($round_off, 2) ?>
                                    </td>
                                </tr>

                                <tr style="font-size:14px; font-weight:bold;">
                                    <td>Grand Total :</td>
                                    <td>
                                        ₹ <?= number_format($round_final_amount, 2) ?>
                                    </td>
                                </tr>
                            </table>
                        </div>
                    </td>
                </tr>
            </table>

            <?php if (count($other_charges_list) > 0) : ?>

            <table class="table-invoice" style="margin-top:10px; font-size:11px; color: black;">
                <thead>
                    <tr>
                        <th>SL</th>
                        <th>Charge Name</th>
                        <th>Amount</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($other_charges_list as $idx => $charge) : ?>
                    <tr>
                        <td><?= $idx + 1 ?></td>
                        <td><?= htmlspecialchars($charge['charge_name']) ?></td>
                        <td style="text-align:right;">
                            <?= number_format($charge['amount'], 2) ?>
                        </td>
                    </tr>
                    <?php endforeach; ?>

                    <tr>
                        <td colspan="2" style="text-align:right;">
                            <strong>Total Other Charges</strong>
                        </td>
                        <td style="text-align:right;">
                            <strong><?= number_format($other_charges, 2) ?></strong>
                        </td>
                    </tr>

                    <?php
                        $grand_total_with_other = $round_final_amount + $other_charges;
                    ?>

                    <tr style="background:#fff1c1; font-weight:bold; font-size:13px;">
                        <td colspan="2" style="text-align:right;">Grand Total</td>
                        <td style="text-align:right;">
                            ₹ <?= number_format($grand_total_with_other, 2) ?>
                        </td>
                    </tr>

                </tbody>
            </table>

            <?php endif; ?>

            <div class="sign-area">
                <table width="100%">
                    <tr>
                        <!-- LEFT : CUSTOMER DETAILS -->
                        <td width="50%" valign="top">
                            <div class="sign-box">
                                ___________________________<br>Customer Signature
                            </div>
                        </td>

                        <!-- RIGHT : INVOICE DETAILS -->
                        <td width="50%" valign="top" style="padding-left: 40px;">
                            <div class="sign-box">
                                ___________________________<br>For R P JEWELLERS & SONS
                            </div>
                        </td>
                    </tr>
                </table>
            </div>
            <div class="footer-note">
                <p><strong>Visit Us:</strong> <strong>SASHTRI MARG, SANTOSHI NAGAR, SILIGURI - 734005</strong> <br>
                    <strong>Phone:</strong> <strong><a href="tel:+91 96415 77702">+91 96415 77702</a></strong> |
                    <strong><a href="tel:+91 97495 76542">+91 97495 76542</a></strong>
                </p>
                <div style="text-align:right; font-size:10px; margin-top:5px;">
                    <strong>Invoice Time:</strong> <?= $formatted_time ?>
                </div>
            </div>






        </div>
        <div style="text-align:center; margin:15px 0;">

            <a href="invoice_view.php?id=<?= $id ?>&download=1"
                style="padding:8px 20px; background:#7b1e1e; color:#fff; text-decoration:none; margin-right:10px; display:inline-block;">
                Download 1
            </a>

            <a href="invoice_view.php?id=<?= $id ?>&download=2"
                style="padding:8px 20px; background:#d4af37; color:#000; text-decoration:none; display:inline-block;">
                Download 2
            </a>

        </div>
    </div>
</div>

<?php include 'include/footer.php'; ?>