<?php

/* ================= STATE CODE ================= */
$state_codes = [
'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','delhi'=>'07'
];

$state_code = $state_codes[$state] ?? '';

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

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

/* ================= HM GST ================= */
$hm_cgst=$hm_sgst=$hm_igst=0;

if ($state == 'west bengal') {
    $hm_cgst = round($subtotal * 0.09,2);
    $hm_sgst = round($subtotal * 0.09,2);
} else {
    $hm_igst = round($subtotal * 0.18,2);
}

/* ================= FINAL ================= */
$final = $subtotal + $cgst + $sgst + $igst + $hm_cgst + $hm_sgst + $hm_igst;
$grand_total = round($final);
$round_off = $grand_total - $final;

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>=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);
    }

}
/* ================= SUPPLIER ================= */

$supplier         = trim($invoice['purchased_from'] ?? '');
$supplier_phone   = trim($invoice['supplier_phone'] ?? '');
$supplier_pan     = trim($invoice['supplier_pan'] ?? '');
$supplier_gst     = trim($invoice['supplier_gst'] ?? '');
$supplier_address = trim($invoice['supplier_address'] ?? '');
$supplier_city    = trim($invoice['supplier_city'] ?? '');
$supplier_state   = trim($invoice['supplier_state'] ?? '');
$supplier_pin     = trim($invoice['supplier_pincode'] ?? '');

$added_date    = date('d-m-Y', strtotime($invoice['added_on']));
$formatted_time = date('h:i:s A', strtotime($invoice['added_on']));

/* ================= FORMATTING ================= */

// Proper Case
$supplier         = ucwords(strtolower($supplier));
$supplier_address = ucwords(strtolower($supplier_address));
$supplier_city    = ucwords(strtolower($supplier_city));
$supplier_state   = ucwords(strtolower($supplier_state));

// Always Uppercase
$supplier_pan = strtoupper($supplier_pan);
$supplier_gst = strtoupper($supplier_gst);

/* ================= PAYMENTS ================= */

$payments = [
    'UPI'    => (float)$invoice['pay_upi'],
    'Cheque' => (float)$invoice['pay_cheque'],
    'Metal'  => (float)$invoice['pay_metal'],
    'Cash'   => (float)$invoice['pay_cash'],
    'Card'   => (float)$invoice['pay_card']
];
?>
<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" rel="stylesheet">

    <style>
    @page {
        size: A4;
        margin: 0;
    }

    body {
        margin: 0;
        padding: 0;
        font-family: 'DejaVu Sans';

    }

    .page {
        position: relative;
        width: 210mm;
        height: 297mm;
    }

    .bg {
        position: absolute;
        top: 0;
        left: 0;
        width: 210mm;
        height: 297mm;
    }

    .content {
        position: absolute;
        top: 0;
        left: 0;
        width: 210mm;
        height: 297mm;
        font-size: 12px;
    }



    /* Invoice specific styles */
    .invoice-wrap {
        position: absolute;
        top: 55mm;
        /* header ke neeche start */
        left: 15mm;
        /* left golden border ke andar */
        width: 180mm;
        font-family: Arial, Helvetica, sans-serif;
        color: #3b2f2f;
    }

    .invoice-header {
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        margin-bottom: 8px;
    }

    .company {
        font-size: 18px;
        font-weight: 700;
        color: #a66b00
    }

    .addr {
        font-size: 11px;
        color: #6b6b6b
    }

    .invoice-table {
        width: 100%;
        border-collapse: collapse;
        margin-top: 6px
    }

    .invoice-table th,
    .invoice-table td {
        border: 1px solid #e0cfa8;
        padding: 6px;
        font-size: 11px
    }

    .invoice-table th {
        background: #f7e9d0
    }

    .right-panel {
        width: 36%;
        float: right;
        box-sizing: border-box;
        padding-left: 8px
    }

    .left-panel {
        width: 62%;
        float: left;
        box-sizing: border-box
    }

    .totals {
        border: 1px dashed #d9c5a3;
        padding: 8px;
        margin-top: 6px;
        background: #fffaf1
    }

    .totals .row {
        display: flex;
        justify-content: space-between;
        margin: 4px 0;
        font-size: 12px
    }

    .charges-table {
        width: 100%;
        border-collapse: collapse;
        margin-top: 10px
    }

    .charges-table td {
        border: 1px solid #e9d9c0;
        padding: 6px;
        font-size: 11px
    }

    .signatures {
        display: flex;
        justify-content: space-between;
        margin-top: 18px
    }

    .footer {
        background: #fff2d9;
        padding: 8px;
        margin-top: 10px;
        text-align: center;
        font-size: 11px;
        color: #a15a00
    }

    .clear {
        clear: both
    }


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

    strong,
    b {
        font-weight: 600;
    }

    .header h2 {
        font-weight: 900;
    }

    .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;
    }


    .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;
    }

    .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: 2px 5px;
    }

    .right-box table td:first-child {
        font-weight: 400;
        color: #7b1e1e;
        font-size: 11px;
    }

    .right-box table td:last-child {
        text-align: right;
    }
    </style>
</head>
<?php
$logoPath = __DIR__ . '/2.jpg';
$logoData = base64_encode(file_get_contents($logoPath));
$logoSrc = 'data:image/png;base64,' . $logoData;
?>
<?php
$rupeePath = __DIR__ . '/rupees.png';
$rupeeData = base64_encode(file_get_contents($rupeePath));
$rupeeSrc = 'data:image/png;base64,' . $rupeeData;
?>

<body>

    <div class="page">

        <!-- Background Full A4 -->
        <img class="bg" src="<?php echo $logoSrc; ?>" alt="Invoice Background">

        <!-- Overlay Content -->
        <div class="content">
            <!-- outer flex container to center invoice on the page -->
            <div class="invoice-wrap">

                <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;">Supplier Details</h5>

                                <p style="margin:2px 0; font-weight: 600">Invoice No:
                                    <?= $invoice['invoice_no'] ?>
                                </p>
                                <p style="margin:2px 0; font-weight: 600">Name:
                                    <?= $supplier ?>
                                </p>
                                <p style="margin:2px 0; font-weight: 600">Phone:
                                    <?= $supplier_phone ?>
                                </p>
                                <p style="margin:2px 0; font-weight: 600">Address:
                                    <?= $supplier_address ?>, <?= $supplier_city ?>, <?= $supplier_state ?> -
                                    <?= $supplier_pin ?>
                                </p>

                                <p style="margin:2px 0; font-weight: 600">PAN:
                                    <?= $supplier_pan ?>
                                </p>
                                <p style="margin:2px 0; font-weight: 600">GST:
                                    <?= $supplier_gst ?>
                                </p>
                                <p style="margin:2px 0; font-weight: 600">Date:
                                    <?= $added_date ?>
                                </p>
                                <p style="margin:2px 0; font-weight: 600">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;">
                                    <strong>Bank Details:<br>
                                        A/C : R P JEWELLERS & SONS<br>
                                        A/C NO : 50200065493711 <br>
                                        BRANCH : BURDWAN ROAD,SILIGURI <br>
                                        IFSC CODE : HDFC0009521 </strong><br><br>
                                </div>

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


                <div class="items-wrapper">

                    <!-- ITEMS (SCROLLABLE) -->
                    <div class="items-scroll">
                        <table class="table-invoice">
                            <thead>
                                <tr>
                                    <th>Item</th>
                                    <th>Category</th>
                                    <th>Gross</th>
                                    <th>Net</th>
                                    <th>Rate</th>
                                    <th>Making</th>
                                    <th>Stone</th>
                                    <th>HM</th>
                                    <th>Total</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td><?= $invoice['item_name'] ?></td>
                                    <td><?= $invoice['category'] ?></td>
                                    <td><?= number_format($invoice['gross_weight'],3) ?></td>
                                    <td><?= number_format($invoice['net_weight'],3) ?></td>
                                    <td><?= number_format($invoice['rate'],2) ?></td>
                                    <td><?= number_format($invoice['making_value'],2) ?></td>
                                    <td><?= number_format($invoice['stone_value'],2) ?></td>
                                    <td><?= number_format($invoice['hm_value'],2) ?></td>
                                    <td><?= number_format($subtotal,2) ?></td>
                                </tr>
                            </tbody>
                        </table>
                    </div>

                    <!-- TOTAL (FIXED AT BOTTOM) -->
                    <table class="table-invoice total-table">
                        <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>  <img src="<?= $rupeeSrc ?>"
                                                style="height:9px; width:auto; vertical-align:middle;margin-bottom: 0px;">
                                <?= number_format($payments['UPI'], 2) ?>
                            </div>
                            <div class="row"><strong>Bank:</strong>  <img src="<?= $rupeeSrc ?>"
                                                style="height:9px; width:auto; vertical-align:middle;margin-bottom: 0px;">
                                <?= number_format($payments['Bank'], 2) ?>
                            </div>
                            <div class="row"><strong>Metal:</strong>  <img src="<?= $rupeeSrc ?>"
                                                style="height:9px; width:auto; vertical-align:middle;margin-bottom: 0px;">
                                <?= number_format($metal, 2) ?>
                            </div>
                            <div class="row"><strong>Cash:</strong>  <img src="<?= $rupeeSrc ?>"
                                                style="height:9px; width:auto; vertical-align:middle;margin-bottom: 0px;">
                                <?= number_format($payments['Cash'], 2) ?>
                            </div>
                            <div class="row"><strong>Dues:</strong>  <img src="<?= $rupeeSrc ?>"
                                                style="height:9px; width:auto; vertical-align:middle;margin-bottom: 0px;">
                                <?= number_format($dues, 2) ?>
                            </div> -->


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

                                    <tbody>

                                        <tr>
                                            <td> <img src="<?= $rupeeSrc ?>"
                                                    style="height:9px; width:auto; vertical-align:middle;margin-bottom: 0px;">
                                                <?= number_format($payments['UPI'], 2) ?></td>
                                            <td> <img src="<?= $rupeeSrc ?>"
                                                    style="height:9px; width:auto; vertical-align:middle;margin-bottom: 0px;">
                                                <?= number_format($payments['Cheque'], 2) ?></td>
                                            <td> <img src="<?= $rupeeSrc ?>"
                                                    style="height:9px; width:auto; vertical-align:middle;margin-bottom: 0px;">
                                                <?= number_format($payments['Metal'], 2) ?></td>
                                            <td> <img src="<?= $rupeeSrc ?>"
                                                    style="height:9px; width:auto; vertical-align:middle;margin-bottom: 0px;">
                                                <?= number_format($payments['Cash'], 2) ?></td>
                                            <td> <img src="<?= $rupeeSrc ?>"
                                                    style="height:9px; width:auto; vertical-align:middle;margin-bottom: 0px;">
                                                <?= number_format($payments['Card'], 2) ?></td>
                                        </tr>
                                    </tbody>

                                </table>
                                <div class="total-words" style="font-weight:bold; font-size:13px;">
                                    Total :
                                    <?= numberToWords($grand_total) ?> Only
                                </div>
                            </div>
                        </td>
                        <td width="50%" valign="top">
                            <div class="right-box">
                                <table>

                                    <!-- SUBTOTAL -->
                                    <tr>
                                        <td>Total :</td>
                                        <td> <img src="<?= $rupeeSrc ?>"
                                                style="height:9px; width:auto; vertical-align:middle;margin-bottom: 0px;">
                                            <?= number_format($subtotal, 2) ?></td>
                                    </tr>

                                    <?php if ($state == 'west bengal'): ?>

                                    <!-- NORMAL GST -->
                                    <tr>
                                        <td>C-GST 1.5% :</td>
                                        <td> <img src="<?= $rupeeSrc ?>"
                                                style="height:9px; width:auto; vertical-align:middle;margin-bottom: 0px;">
                                            <?= number_format($cgst, 2) ?></td>
                                    </tr>
                                    <tr>
                                        <td>S-GST 1.5% :</td>
                                        <td> <img src="<?= $rupeeSrc ?>"
                                                style="height:9px; width:auto; vertical-align:middle;margin-bottom: 0px;">
                                            <?= number_format($sgst, 2) ?></td>
                                    </tr>

                                    <!-- HM GST -->
                                    <tr>
                                        <td>HM CGST 9% :</td>
                                        <td> <img src="<?= $rupeeSrc ?>"
                                                style="height:9px; width:auto; vertical-align:middle;margin-bottom: 0px;">
                                            <?= number_format($hm_cgst, 2) ?></td>
                                    </tr>
                                    <tr>
                                        <td>HM SGST 9% :</td>
                                        <td> <img src="<?= $rupeeSrc ?>"
                                                style="height:9px; width:auto; vertical-align:middle;margin-bottom: 0px;">
                                            <?= number_format($hm_sgst, 2) ?></td>
                                    </tr>

                                    <?php else: ?>

                                    <!-- NORMAL GST -->
                                    <tr>
                                        <td>I-GST 3% :</td>
                                        <td> <img src="<?= $rupeeSrc ?>"
                                                style="height:9px; width:auto; vertical-align:middle;margin-bottom: 0px;">
                                            <?= number_format($igst, 2) ?></td>
                                    </tr>

                                    <!-- HM GST -->
                                    <tr>
                                        <td>HM IGST 18% :</td>
                                        <td> <img src="<?= $rupeeSrc ?>"
                                                style="height:9px; width:auto; vertical-align:middle;margin-bottom: 0px;">
                                            <?= number_format($hm_igst, 2) ?></td>
                                    </tr>

                                    <?php endif; ?>

                                    <!-- FINAL -->
                                    <tr>
                                        <td>Final Amount :</td>
                                        <td> <img src="<?= $rupeeSrc ?>"
                                                style="height:9px; width:auto; vertical-align:middle;margin-bottom: 0px;">
                                            <?= number_format($final, 2) ?></td>
                                    </tr>

                                    <tr>
                                        <td>Round Off :</td>
                                        <td> <img src="<?= $rupeeSrc ?>"
                                                style="height:9px; width:auto; vertical-align:middle;margin-bottom: 0px;">
                                            <?= number_format($round_off, 2) ?></td>
                                    </tr>

                                    <tr style="font-size:14px; font-weight:bold;">
                                        <td>Grand Total :</td>
                                        <td> <img src="<?= $rupeeSrc ?>"
                                                style="height:9px; width:auto; vertical-align:middle;margin-bottom: 0px;">
                                            <?= number_format($grand_total, 2) ?></td>
                                    </tr>

                                </table>
                            </div>
                        </td>

                    </tr>
                </table>



            </div> <!-- .invoice-wrap -->
        </div> <!-- flex center -->

        <div style="
    position:absolute;
    bottom:20px;
    right:30px;
    font-size:10px;
">
            <strong>Invoice Time:</strong> <?= $formatted_time ?>
        </div>
</body>

</html>