<?php
require("includes/check_auth_user.php");

require("includes/dbconnect.php");

include('includes/function.php');

$admin = $_SESSION["admin_" . $dbname];

$zdb = $_SESSION["zdbyear_" . $dbname];

$zroot = $_SESSION["zdir"];

$zcomp = $_SESSION["comp_" . $dbname];

$myyear = $_SESSION[$myyear];

mysqli_select_db($conn, $zdb);

$vouch = $_GET['vouchno'];

$getqry = mysqli_query($conn, "SELECT *,

        DATE_FORMAT(`vouchdt`, '%d/%m/%Y') as mydt,

        DATE_FORMAT(`chaldt`,  '%d/%m/%Y') as mychaldt,

        DATE_FORMAT(`orddt`,   '%d/%m/%Y') as myorddt

     FROM `salemain`

     WHERE vouchno='$vouch'

     LIMIT 1"
        ) or die(mysqli_error($conn));

$getdata = mysqli_fetch_array($getqry);

$cmpqry = mysqli_query($conn, "SELECT * FROM $zroot.`company` WHERE `code`='$zcomp' LIMIT 1") or die(mysqli_error($conn));

$getcmpname = mysqli_fetch_array($cmpqry);

$partyqry = mysqli_query($conn, "SELECT * FROM accmast WHERE `accode`='$getdata[party]' LIMIT 1") or die(mysqli_error($conn));

$showparty = mysqli_fetch_array($partyqry);

$isIGST = ($getdata['totigst'] > 0);
?>

<!DOCTYPE HTML>

<html lang="en">

    <head>

        <meta charset="utf-8">

        <style>

            body {

                width: 100%;

                margin: 0;

                padding: 0;

                font-family: Arial, Helvetica, sans-serif;

                font-size: 10pt;

                color: #000;

            }

            @media print {

                .noprint {
                    display: none !important;
                }

                html, body {
                    width: 210mm;
                    height: 297mm;
                }

                .page {
                    margin: 0 !important;
                    border: none !important;
                    page-break-after: always;
                }

            }

            * {
                box-sizing: border-box;
            }



            .page {

                width: 210mm;

                min-height: 297mm;

                padding: 3mm;

                margin: 8mm auto;

                /*border: 1px #ccc solid;*/

                background: #fff;

            }



            .inv-wrap {

                width: 100%;

                border-collapse: collapse;

                border: 1.5px solid #000;

            }

            .inv-wrap td, .inv-wrap th {
                padding: 0;
            }



            /* ?? Items table ?? */

            .items-tbl {
                width:100%;
                border-collapse:collapse;
            }

            .items-tbl th {

                border-bottom: 1px solid #000;

                padding: 5px 5px;

                font-weight: bold;

                font-size: 10pt;

                background: #f0f0f0;

                text-align: center;

            }

            .items-tbl td {

                padding: 4px 5px;

                font-size: 10pt;

                vertical-align: top;

            }

            /*.items-tbl tr.item-row td { border-bottom: 1px solid #ddd; }*/

            .items-tbl .col-sr   {
                width:4%;
                text-align:center;
                border-right:1px solid #000;
            }

            .items-tbl .col-desc {
                width:51%;
                text-align:left;
                border-right:1px solid #000;
            }

            .items-tbl .col-hsn  {
                width:13%;
                text-align:center;
                border-right:1px solid #000;
            }

            .items-tbl .col-qty  {
                width:8%;
                text-align:right;
                border-right:1px solid #000;
            }

            .items-tbl .col-rate {
                width:10%;
                text-align:right;
                border-right:1px solid #000;
            }

            .items-tbl .col-amt  {
                width:14%;
                text-align:right;
            }



            /* ?? Totals ?? */

            .totals-tbl {
                width:100%;
                border-collapse:collapse;
            }

            .totals-tbl td {
                padding: 4px 10px;
                font-size: 10pt;
            }

            .totals-tbl .tt-label {
                width:62%;
                text-align:left;
            }

            .totals-tbl .tt-val   {
                width:38%;
                text-align:right;
            }

            .totals-tbl tr.bill-amt td {

                border-top: 1.5px solid #000;

                font-size: 12pt;

                font-weight: bold;

            }



            /* ?? Bank table ?? */

            .bank-tbl {
                width:100%;
                border-collapse:collapse;
            }

            .bank-tbl td {
                padding: 3px 6px;
                font-size: 9.5pt;
            }

            .bank-tbl .lbl {
                width:38%;
                font-weight: bold;
            }

        </style>

    </head>

    <body>



        <div class="noprint" style="text-align:right; padding:6px 10px;">

            <button onclick="window.print()" style="padding:6px 20px; margin-right:8px; font-size:11pt;">Print</button>

            <button onclick="window.close()" style="padding:6px 20px; font-size:11pt;">Close</button>

        </div>



        <div class="page">

            <table class="inv-wrap">

                <tbody>



                    <!-- ??????????????????????????????????????????

                         ROW 1 : HEADER

                         ?????????????????????????????????????????? -->

                    <tr>

                        <td colspan="2" style="border-bottom:1.5px solid #000; padding:0;">

                            <table style="width:100%; border-collapse:collapse;">

                                <tr>

                                    <td>

                                        <img src="bew.png?a=2" style="height:180px; width:780px; display:block; margin:0 auto 4px auto;">

                                    </td>

                                </tr>

                            </table>

                        </td>

                    </tr>



                    <!-- ??????????????????????????????????????????

                         ROW 2 : BILL REFERENCE INFO

                         ?????????????????????????????????????????? -->

                    <tr>

                        <td colspan="2" style="border-bottom:1px solid #000; padding:0;">

                            <table style="width:100%; border-collapse:collapse; font-size:10pt;">

                                <tr>

                                    <!-- Left: Bill identifiers -->

                                    <td style="width:50%; vertical-align:top; padding:5px 12px; border-right:1px solid #000;">

                                        <table style="width:100%; border-collapse:collapse;">

                                            <tr>

                                                <td style="width:38%; padding:2px 0; font-weight:bold;">Bill No.</td>

                                                <td style="padding:2px 0;">

                                                    : &nbsp;BEW <?= str_pad($getdata['invno'], 6, '0', STR_PAD_LEFT); ?>/<?= date('y') . '-' . (date('y') + 1); ?>

                                                </td>

                                            </tr>

                                            <tr>

                                                <td style="padding:2px 0; font-weight:bold;">Work Order No.</td>

                                                <td style="padding:2px 0;">: &nbsp;<?= htmlspecialchars($getdata['ordno']); ?></td>

                                            </tr>

                                            <tr>

                                                <td style="padding:2px 0; font-weight:bold;">Your Challan No.</td>

                                                <td style="padding:2px 0;">: &nbsp;<?= htmlspecialchars($getdata['gatepass']); ?></td>

                                            </tr>

                                            <tr>

                                                <td style="padding:2px 0; font-weight:bold;">Our Challan No.</td>

                                                <td style="padding:2px 0;">: &nbsp;<?= htmlspecialchars($getdata['chalno']); ?></td>

                                            </tr>

                                        </table>

                                    </td>

                                    <!-- Right: Dates -->

                                    <td style="width:50%; vertical-align:top; padding:5px 12px;">

                                        <table style="width:100%; border-collapse:collapse;">

                                            <tr>

                                                <td style="width:46%; padding:2px 0; font-weight:bold;">Bill Date</td>

                                                <td style="padding:2px 0;">: &nbsp;<?= $getdata['mydt']; ?></td>

                                            </tr>

                                            <tr>

                                                <td style="padding:2px 0; font-weight:bold;">Order Date</td>

                                                <td style="padding:2px 0;">: &nbsp;<?= $getdata['myorddt']; ?></td>

                                            </tr>

                                            <tr>

                                                <td style="padding:2px 0; font-weight:bold;">Challan Date</td>

                                                <td style="padding:2px 0;">: &nbsp;<?= $getdata['mychaldt']; ?></td>

                                            </tr>

                                            <tr>

                                                <td style="padding:2px 0; font-weight:bold;">Date</td>

                                                <td style="padding:2px 0;">: &nbsp;<?= $getdata['mydt']; ?></td>

                                            </tr>

                                        </table>

                                    </td>

                                </tr>

                            </table>

                        </td>

                    </tr>



                    <!-- ??????????????????????????????????????????

                         ROW 3 : PARTY DETAILS

                         ?????????????????????????????????????????? -->

                    <tr>

                        <td colspan="2" style="border-bottom:1px solid #000; padding:6px 12px;">

                            <table style="width:100%; border-collapse:collapse;">

                                <tr>

                                    <td style="width:60%; vertical-align:top; font-size:10pt;">

                                        <span style="font-weight:bold; font-size:11pt;">M/s. <?= htmlspecialchars($showparty['name']); ?></span><br>

                                        <span style="font-size:9.5pt;">

                                            <?= htmlspecialchars($showparty['add1']); ?>

                                            <?php if (!empty($showparty['add2'])): ?><br><?= htmlspecialchars($showparty['add2']); ?><?php endif; ?>

                                            <?php if (!empty($showparty['city'])): ?>

                                                <br><?= htmlspecialchars($showparty['city']); ?><?php if (!empty($showparty['pin'])): ?> - <?= htmlspecialchars($showparty['pin']); ?><?php endif; ?>

                                            <?php endif; ?>

                                        </span>

                                    </td>

                                    <td style="width:40%; vertical-align:top; padding-left:12px; font-size:10pt;">

                                        <b>GST NO. :</b> <?= htmlspecialchars($showparty['gstno']); ?><br>

                                        <b>PAN NO. :</b> <?= htmlspecialchars($showparty['pan'] ?? ''); ?>

                                    </td>

                                </tr>

                            </table>

                        </td>

                    </tr>



                    <!-- ??????????????????????????????????????????

                         ROW 4 : ITEMS TABLE

                         ?????????????????????????????????????????? -->

                    <tr>

                        <td colspan="2" style="border-bottom:1px solid #000; padding:0;">

                            <table class="items-tbl">

                                <thead>

                                    <tr>

                                        <th class="col-sr" style="text-align:center;">Sr.</th>

                                        <th class="col-desc" style="text-align:left; padding-left:6px;">Description</th>

                                        <th class="col-hsn">HSN/SAC<br>Code</th>

                                        <th class="col-qty" style="text-align:right;">Qty.</th>

                                        <th class="col-rate" style="text-align:right;">Rate</th>

                                        <th class="col-amt" style="text-align:right;">Amount</th>

                                    </tr>

                                </thead>

                                <tbody>

                                    <?php
                                    $ctr = 0;

                                    $totqty = 0;

                                    $gettran = mysqli_query($conn, "SELECT * FROM `saletran` WHERE `vouchno`='$vouch' ORDER BY srno"
                                            ) or die(mysqli_error($conn));

                                    while ($showtran = mysqli_fetch_array($gettran)):

                                        if ($showtran['amount'] > 0) {

                                            $ctr++;

                                            $totqty += $showtran['qty'];
                                        }
                                        ?>

                                        <tr class="item-row">

                                            <td class="col-sr"><?= ($showtran['amount'] > 0) ? $ctr : ''; ?></td>

                                            <td class="col-desc" style="font-size:10pt;"><?= htmlspecialchars($showtran['descrip']); ?></td>

                                            <td class="col-hsn"><?= htmlspecialchars($showtran['hsncode']); ?></td>

                                            <td class="col-qty"><?= ($showtran['qty'] > 0) ? number_format($showtran['qty']) : ''; ?></td>

                                            <td class="col-rate"><?= ($showtran['rate'] > 0) ? number_format($showtran['rate'], 2) : ''; ?></td>

                                            <td class="col-amt"><?= ($showtran['amount'] > 0) ? number_format($showtran['amount'], 2) : ''; ?></td>

                                        </tr>

                                        <?php
                                    endwhile;

// Padding rows � minimum 10 lines

                                    for ($i = $ctr; $i < 10; $i++):
                                        ?>

                                        <tr style="height:24px;">

                                            <td class="col-sr">&nbsp;</td>

                                            <td class="col-desc">&nbsp;</td>

                                            <td class="col-hsn">&nbsp;</td>

                                            <td class="col-qty">&nbsp;</td>

                                            <td class="col-rate">&nbsp;</td>

                                            <td class="col-amt">&nbsp;</td>

                                        </tr>

                                    <?php endfor; ?>

                                </tbody>

                            </table>

                        </td>

                    </tr>



                    <!-- ??????????????????????????????????????????

                         ROW 5 : BANK DETAILS (left) | TOTALS (right)

                         ?????????????????????????????????????????? -->

                    <tr>

                        <td colspan="2" style="border-bottom:1px solid #000; padding:0;">

                            <table style="width:100%; border-collapse:collapse;">

                                <tr>

                                    <!-- Bank details -->

                                    <td style="width:50%; vertical-align:top; border-right:1px solid #000; padding:7px 10px;">

                                        <table class="bank-tbl">

                                            <tr><td class="lbl">Bank Name</td><td><?= htmlspecialchars($getcmpname['bankname']); ?></td></tr>

                                            <tr><td class="lbl">Branch</td><td><?= htmlspecialchars($getcmpname['branchname']); ?></td></tr>

                                            <tr><td class="lbl">Account No.</td><td><?= htmlspecialchars($getcmpname['acno']); ?></td></tr>

                                            <tr><td class="lbl">IFSC Code</td><td><?= htmlspecialchars($getcmpname['bankcode']); ?></td></tr>

                                        </table>

                                        <div style="margin-top:8px; font-size:9.5pt; line-height:1.8;">

                                            <b>GST NO. :</b> <?= htmlspecialchars($getcmpname['gst']); ?><br>

                                            <b>PAN NO. :</b> <?= htmlspecialchars($getcmpname['panno']); ?>

                                        </div>

                                    </td>



                                    <!-- Totals -->

                                    <td style="width:50%; vertical-align:top; padding:0;">

                                        <table class="totals-tbl">

                                            <tr style="border-bottom:1px solid #ccc;">

                                                <td class="tt-label">Sub Total</td>

                                                <td class="tt-val"><?= number_format($getdata['subtotal'], 2); ?></td>

                                            </tr>
                                            <?php if ($getdata['lesother'] > 0) { ?>

                                                <tr style="border-bottom:1px solid #ccc;">

                                                    <td class="tt-label">Advance </td>

                                                    <td class="tt-val"><?= $getdata['cdiscperc'] ?>% &nbsp; &nbsp;&nbsp;&nbsp;<?= number_format($getdata['lesother'], 2); ?></td>

                                                </tr>
                                                
                                            <?php } ?>

                                            <tr style="border-bottom:1px solid #ccc;">

                                                <td class="tt-label">Taxable Value</td>

                                                <td class="tt-val"><?= number_format($getdata['tottaxamt'] > 0 ? $getdata['tottaxamt'] : $getdata['subtotal'], 2); ?></td>

                                            </tr>



                                            <?php
                                            $slabs = [
                                                '5' => ['cgst' => $getdata['cgst5'], 'sgst' => $getdata['sgst5'], 'igst' => $getdata['igst5'], 'tot' => $getdata['totgst5']],
                                                '12' => ['cgst' => $getdata['cgst12'], 'sgst' => $getdata['sgst12'], 'igst' => $getdata['igst12'], 'tot' => $getdata['totgst12']],
                                                '18' => ['cgst' => $getdata['cgst18'], 'sgst' => $getdata['sgst18'], 'igst' => $getdata['igst18'], 'tot' => $getdata['totgst18']],
                                                '28' => ['cgst' => $getdata['cgst28'], 'sgst' => $getdata['sgst28'], 'igst' => $getdata['igst28'], 'tot' => $getdata['totgst28']],
                                            ];

                                            $anySlabData = array_sum(array_column($slabs, 'tot')) > 0;

                                            if ($anySlabData):

                                                foreach ($slabs as $pct => $g):

                                                    if ($g['tot'] <= 0)
                                                        continue;

                                                    $half = $pct / 2;

                                                    if ($isIGST):
                                                        ?>

                                                        <tr style="border-bottom:1px solid #ccc;">

                                                            <td class="tt-label">Add IGST <?= $pct ?>.00 %</td>

                                                            <td class="tt-val"><?= number_format($g['igst'], 2); ?></td>

                                                        </tr>

                                                    <?php else: ?>

                                                        <tr style="border-bottom:1px solid #ccc;">

                                                            <td class="tt-label">Add CGST <?= $half ?> %</td>

                                                            <td class="tt-val"><?= number_format($g['cgst'], 2); ?></td>

                                                        </tr>

                                                        <tr style="border-bottom:1px solid #ccc;">

                                                            <td class="tt-label">Add SGST <?= $half ?> %</td>

                                                            <td class="tt-val"><?= number_format($g['sgst'], 2); ?></td>

                                                        </tr>

                                                    <?php
                                                    endif;

                                                endforeach;

                                            elseif ($getdata['totcgst'] > 0 || $getdata['totsgst'] > 0 || $getdata['totigst'] > 0):

                                                if ($isIGST):
                                                    ?>

                                                    <tr style="border-bottom:1px solid #ccc;">

                                                        <td class="tt-label">Add IGST <?= $getdata['igstperc'] ?> %</td>

                                                        <td class="tt-val"><?= number_format($getdata['totigst'], 2); ?></td>

                                                    </tr>

                                                <?php else: ?>

                                                    <tr style="border-bottom:1px solid #ccc;">

                                                        <td class="tt-label">Add CGST <?= $getdata['cgstperc'] ?> %</td>

                                                        <td class="tt-val"><?= number_format($getdata['totcgst'], 2); ?></td>

                                                    </tr>

                                                    <tr style="border-bottom:1px solid #ccc;">

                                                        <td class="tt-label">Add SGST <?= $getdata['sgstperc'] ?> %</td>

                                                        <td class="tt-val"><?= number_format($getdata['totsgst'], 2); ?></td>

                                                    </tr>

                                                <?php
                                                endif;

                                            endif;
                                            ?>



                                            <?php if ($getdata['taxdisc'] > 0): ?>

                                                <tr style="border-bottom:1px solid #ccc;">

                                                    <td class="tt-label">Less Discount</td>

                                                    <td class="tt-val"><?= number_format($getdata['taxdisc'], 2); ?></td>

                                                </tr>

                                            <?php endif; ?>



                                            <tr class="bill-amt">

                                                <td class="tt-label">Bill Amount</td>

                                                <td class="tt-val"><?= number_format($getdata['netamt'], 2); ?></td>

                                            </tr>

                                        </table>

                                    </td>

                                </tr>

                            </table>

                        </td>

                    </tr>



                    <!-- ??????????????????????????????????????????

                         ROW 6 : AMOUNT IN WORDS

                         ?????????????????????????????????????????? -->

                    <tr>

                        <td colspan="2" style="border-bottom:1px solid #000; padding:5px 12px; font-size:10pt;">

                            Rupees <b><?= rsword(round($getdata['netamt'])); ?></b> Only

                        </td>

                    </tr>



                    <!-- ??????????????????????????????????????????

                         ROW 7 : FOOTER

                         ?????????????????????????????????????????? -->

                    <tr>

                        <td colspan="2" style="padding:0;">

                            <table style="width:100%; border-collapse:collapse;">

                                <tr>

                                    <!-- Left: E&OE + declaration -->

                                    <td style="width:65%; vertical-align:top; padding:6px 12px; border-right:1px solid #000; font-size:8.5pt; line-height:1.6;">

                                        <div style="font-size:9pt;"><b>E.&amp; O.E.</b></div>

                                        <div style="font-size:9pt;">Subject to Mumbai Jurisdiction</div>

                                        <br>

                                        <div>

                                            I/We hereby certify that my/our registration certificate under Rule 1 of GST invoice rules 2017

                                            is in force on the date on which the sale of the goods specified in this tax invoice is made by

                                            me/us and that the transaction of sale covered by this tax invoice has been affected by me/us and it

                                            shall be accounted for in the turnover of sales while filing of return and the due tax, if any, payable

                                            on the sales has been paid or shall be paid.

                                        </div>

                                    </td>

                                    <!-- Right: Company + signatory -->

                                    <td style="width:35%; vertical-align:top; text-align:center; padding:6px 10px; font-size:10pt;">
                                        <div style="font-weight:bold; font-size:10pt;">
                                            FOR <?= strtoupper(htmlspecialchars($getcmpname['pname'])); ?>
                                        </div>
                                        <img src="bewsign.png?a=2" style="height:90px; width: 200px;display:block; margin:4px auto;">
                                        <div style="font-size:9.5pt;">Authorised Signatory</div>
                                    </td>

                                </tr>

                            </table>

                        </td>

                    </tr>



                </tbody>

            </table>

        </div>



        <?php include("includes/linksfoot.php"); ?>

        <script>
//
//            $(function () {
//
//                formGeneral();
//
//            });

        </script>

    </body>

</html>

