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

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

$maindb = 'kotmain';
$trandb = 'kottran';
$file = 'kotpent.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);

include("includes/vouchno_generate.php");
include("includes/linkshead.php");

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

    $vouchdt = $_POST['vouchdt'];
    $table = $_POST['tableno'];
    $totqty = $_POST['totqty'];
    $delvcomp = $_POST['delvcomp'];
    $custmob = $_POST['custmob'];
    $custname = $_POST['custname'];
    $custaddress = $_POST['custaddress'];
    $count = $_POST['items2'];


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

    if ($delvcomp == '') {
        browse("Delivery Cannot Be Empty");
        header("Location: javascript:history.back(-1)");
        return false;
    }

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

    $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;

//    if (!empty($custmob)) {
//    mysqli_query($conn, "DELETE FROM `$trandb` WHERE `vouchno`='$mvouch'") or die(mysqli_error($conn));
    $mctr = 0;
    $tqty = 0;
    $tamount = 0;
    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`, `srno`, `item`, `qty`, `rate`, `amount`,total)
                                                  VALUES('$mvouch', '$mctr', '$item', '$qty', '$rate', '$amount','$amount')") or die(mysqli_error($conn));
        }
        $tqty = $tqty + $qty;
        $tamount = $tamount + $amount;
    }

    if ($addflag == 1) {

        $mvouch = $qvouch;

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

        browse("KOT has been Transfered");
    } else {
        $mvouch = $qvouch;

        mysqli_query($conn, "UPDATE `$maindb` SET `tableno` = '$table', `totitems`='$mctr', `totqty`='$tqty',`netamt`='$tamount', `subtotal`='$tamount',
                                `delvcomp`='$delvcomp',`custmob`='$custmob',`custname`='$custname'`,`custaddress`='$custaddress',
                                `parcel`='Y' where $keyfld = '$mvouch' ") or die(mysqli_error($conn));
        browse("KOT has been Updated");
    }
//    }
    mysqli_query($conn, "UNLOCK TABLES");
    header("refresh: 1; $menuaction");
}
$addftotqty = $addftotamt = 0;

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

    $getsumqty = mysqli_query($conn, "SELECT sum(qty) as totqty, sum(amount) as totamt FROM `kottran` WHERE $keyfld='$getvouch'") or die(mysqli_error($conn));
    $fsumqty = mysqli_fetch_array($getsumqty);

    $addftotqty = $fsumqty['totqty'];
    $addftotamt = $fsumqty['totamt'];
}
?>
<script src="MyScript/kotpent.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;
    }
    .plus{
        color:green;
    }
    table td {
        font-size: 12px;
    }
    .itemdesc{
        color: red;
    }
    .btn-default{
        background-color: transparent;
    }
    .navtable{
        margin-bottom: 0px;
    }
    .fa-heart{
        color: #b9408f;
    }
    .favspan{
        display: none;
    }
</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-2 col-md-2 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>
                    <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">
                            <label class="label-control">Contact</label>
                        </div>
                        <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 pad">
                            <input type="text" class="form-control" pattern=".{10,10}" maxlength="10" name="custmob" value="<?php Echo ($addflag != 1) ? "" : $get['custmob']; ?>"  >
                        </div>
                        <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 pad">
                            <a href="#"><i class="fa fa-chevron-circle-down toggleadd" style="font-size:24px"></i></a>
                        </div>
                    </div>
                    <!--<div class="row" style="display: none;">-->
                    <div class="parceladd" style="display: none;">
                        <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">
                                <label class="label-control">Name</label>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 pad">
                                <input type="text" class="form-control" name="custname" value="<?php Echo ($addflag != 1) ? "" : $get['custname']; ?>">
                            </div>
                        </div>
                        <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">
                                <label class="label-control">Address</label>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 pad">
                                <textarea class="form-control" rows="3" name="custaddress" value="<?php Echo ($addflag != 1) ? "" : $get['custaddress']; ?>"></textarea>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad"></div>
                        <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 text-right pad">
                            <label class="label-control">Type</label>
                        </div>
                        <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 pad">
                            <select class="form-control form select2 delvcomp" name="delvcomp" style="width:100%" required> 
                                <option value=""></option>
                                <?php
                                $getdelvcomp = mysqli_query($conn, "SELECT code,name FROM `delvcomp`")or die(mysqli_error($conn));
                                while ($delvcomp = mysqli_fetch_array($getdelvcomp)) {
                                    Echo "<option value='$delvcomp[code]'>$delvcomp[name]</option>";
                                }
                                ?>
                            </select>
                        </div>
                    </div>
                    <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-10 col-xs-10 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-2 col-xs-2 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="30%" 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'>";
                                            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' class='tritem' name='itemcode_$ctr2' value='$item[code]'> <br>";
                                            Echo "<span class='itemdesc'>$item[descrip]<span></td>";
                                            Echo "<td class='text-right'><label class='label-control lblrate'>$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"><?= $addftotqty ?></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'><?= $addftotamt ?></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 modaldelvcomp" 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>