<?php
ob_start();
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include('includes/function.php');

$formname = "K.O.T.";

$maindb = 'kotmain';
$trandb = 'kottran';
$file = 'kotent.php';
$keyfld = "vouchno";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
$username = $_SESSION["username_" . $dbname];
$usertype = $_SESSION["usertype_" . $dbname];

$menuaction = "";
if ($usertype != "W") {
    $menuaction = "menu.php";
} else {
    $menuaction = "menu2.php";
}
mysqli_select_db($conn, $zdb);
$abdul = $_GET['abdul'];
include("includes/vouchno_generate.php");
include("includes/linkshead.php");

if (isset($_POST["submit"])) {
    $a = 0;

    $vouchdt = $_POST['vouchdt'];
    $table = $_POST['tableno'];
    $totqty = $_POST['totqty'];
    $count = $_POST['items2'];

    if ($totqty <= 0) {
        echo "<script>alert('Qty Should be Greater Than Zero')</script>";
        header("Location: javascript:history.back(-1)");
        return false;
    }

    
    mysqli_query($conn, "LOCK TABLES `$maindb` WRITE, `$trandb` WRITE, `tablemast` WRITE, `kottable` WRITE") or die(mysqli_error($conn));

    $mvouch = $qvouch;

    if (!empty($table)) {
        if ($addflag == 1) {
            $gen1 = mysqli_query($conn, "SELECT MAX($keyfld) AS maxcode FROM `$maindb` LIMIT 0,1 ") or die(mysqli_error($conn));

            if (mysqli_num_rows($gen1) > 0) {
                $vvouch = mysqli_fetch_assoc($gen1);
                $qvouch = $vvouch["maxcode"] + 1;
            } else {
                $qvouch = '1';
            }
            $mvouch = $qvouch;
        }






        $mctr = $tqty = $tamount = $subvouch = 0;
        $getvouchsub = mysqli_query($conn, "SELECT `subvouch` FROM `$trandb` WHERE `vouchno` = '$mvouch' ") or die(mysqli_error($conn));
        $fsubvouch = mysqli_fetch_array($getvouchsub);
        if ($fsubvouch['subvouch'] <= 0) {
            $subvouch = 1;
        } else {
            $subvouch = $fsubvouch['subvouch'] + 1;
        }

//        mysqli_query($conn, "DELETE FROM `$trandb` WHERE `vouchno`='$mvouch'") or die(mysqli_error($conn));
        for ($i = 0; $i <= $count; $i++) {
            $item = $_POST["itemcode_$i"];
            $rate = $_POST["rate_$i"];
            $qty = $_POST["qty_$i"];
            $amount = $qty * $rate;

            if ($qty > 0) {

                $mctr++;
                mysqli_query($conn, "INSERT INTO `$trandb`(`vouchno`, `subvouch`, `srno`, `item`, `qty`, `rate`, `amount`, `total`)
                                                  VALUES('$mvouch', '$subvouch', '$mctr', '$item', '$qty', '$rate', '$amount', '$amount')") or die(mysqli_error($conn));
            }
            $tqty = $tqty + $qty;
            $tamount = $tamount + $amount;
        }






        if ($addflag == 1) {
//--------------------------------------kottable start
            $multable = 0;
            if (!empty($_POST['tablenomul'])) {
                $multable = 1;
                foreach ($_POST['tablenomul'] as $check2) {
                    mysqli_query($conn, "INSERT INTO `kottable`(`vouchno`, `vouchdt`, `vouchtime`, `tableno`, `user`, `multable`)
                                      VALUES('$mvouch', '$vouchdt', CURRENT_TIME(), '$check2', '$username', '$multable')") or die(mysqli_error($conn));
                    mysqli_query($conn, "UPDATE `tablemast` SET `book` = '1'
                                where code = '$check2' ") or die(mysqli_error($conn));
                }
            }
            mysqli_query($conn, "INSERT INTO `kottable`(`vouchno`, `vouchdt`, `vouchtime`, `tableno`, `user`, `multable`)
                                      VALUES('$mvouch', '$vouchdt', CURRENT_TIME(), '$table', '$username', '$multable')") or die(mysqli_error($conn));
//--------------------------------------kottable end







            mysqli_query($conn, "INSERT INTO `$maindb`(`vouchno`, `vouchdt`, `vouchtime`, `tableno`, `user`, `totitems`, 
                                                       `totqty`, `netamt`, `subtotal`, `cgstperc`, `sgstperc`, `gstperc`)
                                      VALUES('$mvouch', '$vouchdt', CURRENT_TIME(), '$table', '$username','$mctr', 
                                             '$tqty', '$tamount', '$tamount', '2.5', '2.5', '5')") or die(mysqli_error($conn));

            mysqli_query($conn, "UPDATE `tablemast` SET `book` = '1'
                                where code = '$table' ") or die(mysqli_error($conn));
//            browse("KOT has been Transfered");
        } else {
            $mvouch = $qvouch;
//            $getamt = mysqli_query($conn, "Select sum(amount) as stotal, sum(total) as ttotal, sum(totqty) as tqty where vouchno = '$mvouch'") or die(mysqli_error($conn));
//            $fetamt = mysqli_fetch_array($getamt);
//            $tqty = $fetamt['tqty'];
//            $tamount = $fetamt['ttotal'];
            mysqli_query($conn, "UPDATE `$maindb` SET `totitems`='$mctr', `totqty`='$tqty',`netamt`='$tamount', `subtotal`='$tamount'
                                where $keyfld='$mvouch'") or die(mysqli_error($conn));
//            browse("KOT has been Updated");
        }
    }
    mysqli_query($conn, "UNLOCK TABLES");
//    echo "<script>window.location.href='pendkot_print.php?vouchno=$qvouch'</script>";
    header("refresh: 1; $menuaction");
}
$addftotqty = $addftotamt = 0;
$readtable = "";
if ($addflag != 1) {
    $getvouch = '';
    if ($addflag == 0) {
        $getvouch = $_GET[$keyfld];
    } else {
        $getvouch = $qvouch;
    }

    $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE $keyfld='$getvouch'") or die(mysqli_error($conn));
    $get = mysqli_fetch_array($result);

    $gettables = mysqli_query($conn, "SELECT * FROM `tablemast` WHERE `code` ='$get[tableno]'") or die(mysqli_error($conn));
    $ftables = mysqli_fetch_array($gettables);

    $readtable = "readonly";
}
?>
<script src="MyScript/kotent.js" type="text/javascript"></script>
<style>
    .itemhead{
        text-align: center;
        background-color: black;
        color: white;
    }

    .select2-container .select2-selection--single {
        height: 25px;
    }

    .table>tbody>tr>td{padding: 5px;}
    .btn{padding: 3px 5px;}
    .qty{
        width: 40% !important;
        display: inline;
    }

    #ItemSearch{
        border: 0px solid #000000;
        border-bottom-width: 1px;
        background-color: transparent;
    }

    .navbar {
        position: fixed;
        bottom: 0;
        left: 0px;
    }
    @media only screen and (min-width: 992px){
        .navbar{
            width: 50%;
        }
    }
    .minus{
        color: red;
        font-size: 25px;
    }
    .plus{
        color:green;
        font-size: 25px;
    }
    table td {
        font-size: 12px;
    }
    .itemdesc{
        color: red;
    }
    .btn-default{
        background-color: transparent;
    }
    .navtable{
        margin-bottom: 0px;
    }
    .fa-heart{
        color: #b9408f;
    }
    .favspan{
        display: none;
    }
    .groupsrch{
        visibility: hidden;
    }
</style>
</head>
<body>
    <form method='POST'>
        <!-- Default box -->
        <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 pad">
            <div class="row">
                <div style="float: right;">
                    <a href="<?= $menuaction; ?>">
                        <button type="button" class="btn btn-warning btn-md" style="background-color: #f7b64e">Go Back</button>
                    </a>
                </div>
            </div>
            <div class="panel panel-default">
                <div class="panel-heading" style="padding: 0px;">
                    <h3 class="panel-title"><?php Echo $formname; ?><span style="font-size: 10px;color:red">&nbsp;&nbsp;&nbsp;(Note: For Description Click on Item)</span></h3>
                </div>
                <div class="panel-body">
                    <div class="row">
                        <!--                        <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 pad">
                                                </div>-->
                        <div class="col-lg-1 col-md-1 col-sm-2 col-xs-2 pad">
                            <input type="text" class="form-control text-right vouch" name="vouchno" value="<?php Echo ($addflag != 1) ? $qvouch : $qvouch; ?>" required readonly>
                        </div>
                        <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 pad">
                            <input type="date" class="form-control vouchdt" name="vouchdt" value="<?php Echo ($addflag != 1) ? $get["vouchdt"] : date("Y-m-d"); ?>" required>
                        </div>
                        <div class="col-lg-3 col-md-3 col-sm-6 col-xs-6 pad">
                            <select class="form-control select2 tableno" name="tableno" required>
                                <option value="<?php Echo ($addflag != 1) ? $ftables['code'] : ""; ?>"><?php Echo ($addflag != 1) ? $ftables['name'] : ""; ?></option>
                                <?php
                                $gettable = mysqli_query($conn, "SELECT `code`, `name` FROM `tablemast` where book = '' order by `name`+0") or die(mysqli_error($conn));
                                while ($table = mysqli_fetch_array($gettable)) {
                                    echo "<option value='$table[code]'>$table[name]</option>";
                                }
                                ?>
                            </select>
                        </div>
                        <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                            <label class="label-control text-right">Joint</label>
                        </div>
                        <div class="col-lg-2 col-md-2 col-sm-4 col-xs-4 pad">
                            <input type="checkbox" class="form-control" id="jointtblchk" name="jointtblchk" style="margin-top: 0px;">
                        </div>
                        <div class="col-lg-1 col-md-1 pad">
                        </div>
                        <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 pad multbldiv" style="width: 40%;">
                        </div>
                    </div><br>
                    <div class="row">
                        <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 text-right pad">
                        </div>
                        <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 pad">
                            <input type="text" class="form-control" id="ItemSearch" name="itemsearch" placeholder="Search Itemname" autofocus>
                        </div>
                        <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 text-right pad">
                            <label class="label-control text-right">Fav</label>
                        </div>
                        <div class="col-lg-2 col-md-2 col-sm-4 col-xs-4 text-right pad">
                            <input type="checkbox" class="form-control" id="favitem" name="favitem" style="margin-top: 0px;">
                        </div>
                    </div>
                    <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 text-right pad">
                    </div>
                    <div class="row">
                        <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                            <table id="" class="table table-bordered" style="width: 100%;">
                                <thead class="thead2">
                                    <tr class="mySearchTr">
                                        <!--<th width="7%"></th>-->
                                        <th width="5%" class=''>Fav</th>
                                        <th width="45%" class=''>Item</th>
                                        <th width="10%" class='text-right'>Rate</th>
                                        <th width="37%" class='text-right'>Qty</th>
                                        <th width="3%"></th>
                                    </tr>
                                </thead>
                                <tbody class="tbody2">
                                    <?php
                                    $ctr2 = 0;
                                    $grpctr = 0;
                                    $gettran2 = mysqli_query($conn, "SELECT * FROM `itemgrp` WHERE `code` != '' ORDER BY `code`")or die(mysqli_error($conn));
                                    while ($tran2 = mysqli_fetch_array($gettran2)) {


                                        Echo "<tr id='$grpctr' class='itemgrptr'>";
                                        Echo "<td colspan='6' class='itemhead'>$tran2[name]</td>";
                                        Echo "</tr>";


                                        $getitem = mysqli_query($conn, "SELECT * FROM `item` WHERE `itemgrp` = '$tran2[code]' ORDER BY `code`")or die(mysqli_error($conn));
                                        while ($item = mysqli_fetch_array($getitem)) {



                                            $qtyval = 0;
//                                            if ($addflag != '1') {
//                                                $getitemqty = mysqli_query($conn, "SELECT qty FROM `$trandb` WHERE `vouchno` ='$qvouch'
//                                                                                   and item=$item[code] ") or die(mysqli_error($conn));
//                                                $itemqty = mysqli_fetch_array($getitemqty);
//                                                if ($itemqty['qty'] > 0) {
//                                                    $qtyval = $itemqty['qty'];
//                                                } else {
//                                                    $qtyval = 0;
//                                                }
//                                            }

                                            Echo "<tr class='item_$grpctr colrrow'>";
//                                            Echo "<td class='text-right'><i class='fa fa-close remove'></i></td>";
                                            Echo "<td class='text-center'>";
                                            if ($item['favourite'] != '') {
                                                echo "<span class='favspan'>favitem</span><i class='fa fa-heart' aria-hidden='true'></i>";
                                            }
                                            Echo "</td><td class='itemname'>";
                                            echo "<label class='label-control lblitemname'>$item[name]</label><input type='hidden' name='itemcode_$ctr2' value='$item[code]'> <br>";
                                            Echo "<span class='itemdesc'>$item[descrip]<span><span class='groupsrch'>$tran2[name]</span></td>";
                                            Echo "<td class='text-right'><label class='label-control'>$item[rate]</label><input type='hidden' step='any' class = 'form-control text-right rate' name='rate_$ctr2' value='$item[rate]'></td>";
                                            Echo "<td colspan='2'><button type='button' class='btn btn-default fa fa-minus minus'></button>";
                                            echo "<input type='number' step='any' class='form-control text-right qty' name='qty_$ctr2' value='$qtyval'><button type='button' class='btn btn-default fa fa-plus plus'></button></td>";
                                            Echo "</tr>";
                                            $ctr2++;
                                        }
                                        $grpctr++;
                                    }
                                    ?>
                                </tbody>
                                <input type="hidden" id="items2" name="items2" value="<?php echo $ctr2; ?>" />
                            </table>
                            <nav class="navbar fixed-bottom navbar-light bg-light" style="margin-bottom: 0px;background-color: #ffcc22">
                                <table id="" class="table table-bordered navtable" style="width: 100%;">
                                    <thead>
                                        <tr style="background-color: #f7b64e">
                                            <th width="10%">
                                            </th>
                                            <th width="30%" class='text-right'>Total Qty</th>
                                            <th width="10%"><span class="totqty"></span><input type="hidden" name="totqty" class="totqty2" value="<?php Echo ($addflag != 1) ? $addftotqty : 0; ?>"></th>
                                            <th width="20%" class='text-right'>Amount</th>
                                            <th width="30%" class='totamt'></th>
                                            <th width="0%"></th>
                                        </tr>
                                        <tr style="background-color: green">
                                            <th colspan="3" class="text-center">
                                                <input type="button" name="detail" value="Detail" class="form-control ordrep">
                                            </th>
                                            <th colspan="3" class="text-center">
                                                <input type="submit" name="submit" value="Checkout" class="form-control" >
                                            </th>
                                        </tr>
                                    </thead>
                                </table>
                            </nav>
                        </div>
                    </div>
                </div>
                <div class="panel-footer">
                </div>
            </div>
        </div>
    </form>

    <div class="modal" id="Orderrep">
        <div class="modal-dialog modal-dialog-scrollable">
            <div class="modal-content">

                <!-- Modal Header -->
                <div class="modal-header">
                    <div class="row">
                        <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 text-right pad">
                        </div>
                        <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                            <input type="text" class="form-control modalvouch" readonly>
                        </div>
                        <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                            <input type="text" class="form-control modalvouchdt" readonly>
                        </div>
                        <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                            <input type="text" class="form-control modaltable" readonly>
                        </div>
                    </div>
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                </div>
                <!-- Modal body -->
                <div class="modal-body">
                    <table id="" class="table table-bordered" style="width: 100%;">
                        <thead class="thead4">
                            <tr class="mySearchTr">
                                <th width="7%"></th>
                                <th width="40%" class=''>Item</th>
                                <th width="15%" class='text-right'>Qty</th>
                                <th width="10%" class='text-right'>Rate</th>
                                <th width="25%" class='text-right'>Amount</th>
                                <th width="3%"></th>
                            </tr>
                        </thead>
                        <tbody class="tbody4">
                        </tbody>
                    </table>
                </div>

                <!-- Modal footer -->
                <div class="modal-footer">
                    <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
                </div>

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