<?php
require 'includes/dbconnect.php';
include 'includes/function.php';
include 'includes/check_auth_user.php';
//echo'abc';
$formname = 'Table Transfer';
$maindb = 'tabletran';
$trandb = 'kotmain';
$accdb = '';
$cashdb = '';
$bankdb = '';

$file = 'tabletrans.php';
$trandbexist = 0;
$keyfld = "vouchno";
$focusfield = "";
$keycond = "";
$keycond1 = "";
$printfile = "";

//browse($zdir);
$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);

include("includes/vouchno_generate.php");
include("includes/prev&next.php");
$para = $_GET["para"];
$zroot = $_SESSION["zdbroot"];
$admin = $_SESSION["admin_" . $dbname];



$username = $_SESSION["username_" . $dbname];

//$zroot = $_SESSION["zdb"];

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

    $gettab = mysqli_query($conn, "SELECT code,name FROM `tablemast` WHERE code='$get[oldtable]'") or die(mysqli_error($conn));
    $fettab = mysqli_fetch_array($gettab);

    $gettab2 = mysqli_query($conn, "SELECT code,name FROM `tablemast` WHERE code='$get[newtable]'") or die(mysqli_error($conn));
    $fettab2 = mysqli_fetch_array($gettab2);

    $getwai = mysqli_query($conn, "SELECT code,user FROM $zroot.`passmain` WHERE code='$get[waiter]'") or die(mysqli_error($conn));
    $fetwai = mysqli_fetch_array($getwai);
}

if (isset($_POST["submit"])) {

    $vouchdt = $_POST["vouchdt"];
    $oldtable = $_POST["oldtable"];
    $newtable = $_POST["newtable"];
    $waiter = $_POST["waiter"];
    $kotno = $_POST["kotno"];
    $remark = $_POST["remark"];

    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;

    mysqli_query($conn, "Update `kotmain` set `tableno`='$newtable' where `vouchno`='$kotno' ") or die(mysqli_error($conn));
//    mysqli_query($conn, "Update `kottable` set `tableno`='$newtable' where `vouchno`='$kotno' and srno = '1' ") or die(mysqli_error($conn));
    mysqli_query($conn, "Update `tablemast` set `book`='1' where `code`='$newtable'") or die(mysqli_error($conn));
    mysqli_query($conn, "Update `tablemast` set `book`=''  where `code`='$oldtable'") or die(mysqli_error($conn));

    if ($addflag == 1) {

        mysqli_query($conn, "INSERT INTO `$maindb`(`vouchno`, `vouchdt`, `vouchtime`, `oldtable`, `newtable`,`waiter`, `kotno`, `remark` )
                                              VALUES ('$qvouch', '$vouchdt', CURRENT_TIME(), '$oldtable', '$newtable', '$waiter', '$kotno', '$remark' )") OR die(mysqli_error($conn));
        browse('Table Transferred Successfully');
        header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
    } else {

        mysqli_query($conn, "Update `$maindb` set `vouchdt`='$vouchdt', `oldtable`='$oldtable', `newtable`='$newtable', `waiter`='$waiter',
                                                   `kotno`='$kotno', remark='$remark' where vouchno='$mvouch'") or die(mysqli_error($conn));

        updmess();
        header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
    }
}

$senable = "";
$cenable = "";

if (isset($_GET["delete"])) {

    $delcond = " vouchno='$getvouch'";
    delnew($zdb, "$maindb", "$delcond", "$file?mode=last", $conn);
}


include("includes/linkshead.php");
?>
<script type="text/javascript" src="MyScript/tabletrans.js?a=2"></script>
<style type="text/css">
    .padrow{
        margin-right:-3px;
        margin-left: 1px;
    }
</style>
</head>
<body>
    <form action='' method='POST' autocomplete="off">
        <!-- Default box -->
        <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 pad">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title" style="color:#BE99E9"><?php Echo $formname; ?><b class="pull-right"><?= $get['vouchtime'] ?></b></h3>

                </div>
                <div class="panel-body">

                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control marg">No.</label>
                        </div>
                        <div class="col-lg-1 col-md-1 col-sm-2 col-xs-2 pad">
                            <input type="text" class="form-control form vouchno" name="vouchno" value="<?php Echo ($addflag != 1) ? $get['vouchno'] : "$qvouch"; ?>" readonly>
                            <input type="hidden" class="form-control form admin" name="admin" value="<?= $admin ?>">
                        </div>
                        <div class="col-lg-1 col-md-1 col-sm-2 col-xs-2 text-right pad">
                            <label class="label-control marg">Date</label>
                        </div>
                        <div class="col-lg-2 col-md-2 col-sm-4 col-xs-4 pad">
                            <input type="date" class="form-control form vouchdt" name="vouchdt" value="<?php Echo ($addflag != 1) ? $get['vouchdt'] : date("Y-m-d"); ?>" >
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control marg">Old Table</label>
                        </div>
                        <div class="col-lg-4 col-md-4 col-sm-8 col-xs-8 pad">
                            <select class="form-control form oldtable" name="oldtable" id="oldtable" autofocus>
                                <option value="<?php echo ($addflag != 1) ? $get['oldtable'] : "" ?>"><?php echo ($addflag != 1) ? $fettab['name'] : "" ?></option>
                                <?php
                                if ($addflag == 1) {
                                    $gettbl1 = mysqli_query($conn, "Select code, name from `tablemast` where book = '1' order by name") or die(mysqli_error($conn));
                                    while ($fettbl1 = mysqli_fetch_array($gettbl1)) {
                                        echo "<option value='$fettbl1[code]'>$fettbl1[name]</option>";
                                    }
                                }
                                ?>
                            </select>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3 col-md-2 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control marg">New Table</label>
                        </div>
                        <div class="col-lg-4 col-md-4 col-sm-8 col-xs-8 pad">
                            <select class="form-control form newtable" name="newtable" id="newtable">
                                <option value="<?php echo ($addflag != 1) ? $get['newtable'] : "" ?>"><?php echo ($addflag != 1) ? $fettab2['name'] : "" ?></option>
                                <?php
                                if ($addflag == 1) {
                                    $gettbl2 = mysqli_query($conn, "Select code, name from `tablemast` where book != '1' order by name") or die(mysqli_error($conn));
                                    while ($fettbl2 = mysqli_fetch_array($gettbl2)) {
                                        echo "<option value='$fettbl2[code]'>$fettbl2[name]</option>";
                                    }
                                }
                                ?>
                            </select>                
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3 col-md-2 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control marg">KOT No.</label>
                        </div>
                        <div class="col-lg-3 col-md-2 col-sm-3 col-xs-3 pad">
                            <input type="text" class="form-control form kotno" name="kotno" value="<?php Echo ($addflag != 1) ? $get['kotno'] : ""; ?>" readonly>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3 col-md-2 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control marg">Waiter</label>
                        </div>
                        <div class="col-lg-4 col-md-4 col-sm-8 col-xs-8  pad">
                            <select class="form-control form waiter" name="waiter" id="waiter">
                                <option value="<?php echo ($addflag != 1) ? $get['waiter'] : "" ?>"><?php echo ($addflag != 1) ? $get['waiter'] : "" ?></option>
                                <?php
                                if ($addflag == 1) {
                                    $getwai2 = mysqli_query($conn, "Select code, user from $zroot.`passmain` where usertype = 'W' order by user") or die(mysqli_error($conn));
                                    while ($fetwai2 = mysqli_fetch_array($getwai2)) {
                                        echo "<option value='$fetwai2[user]'>$fetwai2[user]</option>";
                                    }
                                }
                                ?>
                            </select>                   
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3 col-md-2 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control marg">Reason</label>
                        </div>
                        <div class="col-lg-4 col-md-4 col-sm-8 col-xs-8 pad">
                            <textarea class="form-control form remark" name="remark" rows="4"><?php Echo ($addflag != 1) ? $get['remark'] : ""; ?></textarea>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-9">
                            <?php include("includes/buttons.php"); ?>
                        </div>
                        <div class="col-lg-2">
                            <input type="button" id="finishTab" class="btn btn-primary pull-right form" style="background-color:#78909C;color: #fff;" value="Check & Finish">
                        </div>
                    </div>
                </div>


            </div>
            <!--</div>-->

    </form>

    <div class="modal fade bs-example-modal-lg searchModal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <button class="close" data-dismiss="modal"><span>&times;</span></button>
                    <h4 class="modal-title" id="myLargeModalLabel">Search <? Echo $formname; ?></h4>
                </div>
                <div class="modal-body">
                    <div class="row">
                        <div class="col-lg-2">
                            <select class="form-control sfield">
                                <option value="1">Voucher</option>
                                <option value="2">Date</option>
                                <option value="3">Kot No.</option>
                                <option value="4">Reason</option>
                                <option value="5">Waiter</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control soperator">
                                <option value="">Type of Search</option>
                                <option value="%">Like</option>
                                <option value="=">Exactly</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control sortBy">
                                <option value="1">Voucher</option>
                                <option value="2">Date</option>
                                <option value="3">Kot No.</option>
                                <option value="4">Reason</option>
                                <option value="5">Waiter</option>
                            </select> 
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control orderAs">
                                <option value="desc">Desc</option>
                                <option value="asc">Asc</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <input type="text" value="" class="form-control ssearch" placeholder="Search Here">
                        </div>
                        <div class="col-lg-2">
                            <input type="button" class="btn btn-primary sload" value="Search" >
                        </div>
                    </div>
                    <div class="col-lg-12 sloader text-center">
                        <!--<img src="images/search.gif" alt="loading">-->       
                    </div>
                    <div class="col-lg-12 snoRecord text-center">
                        <h3>NO RECORDS FOUND</h3>
                    </div>
                    <div class="table-responsive" style="max-height: 400px;overflow: auto;">
                        <table id="" class="table table-striped table-condensed table-bordered" width="100%">
                            <thead>
                                <tr style="background-color:#B9408F;">
                                    <th width="10%">Voucher</th>
                                    <th width="10%">Date</th>
                                    <th width="10%">Old Table</th>
                                    <th width="10%">New Table</th>
                                    <th width="10%">KOT No.</th>
                                    <th width="20%">Waiter</th>
                                    <th width="20%">Reason</th>
                                    <th width="10%">Action</th>
                                </tr>
                            </thead>
                            <tbody class="sTbody">
                            </tbody>
                        </table>
                    </div>
                    <div class="row">
                        <div class="col-xs-12 text-right search-action-bar">
                            <input type="button"  class="btn btn-primary sprev" value="Previous">
                            <input type="button"  class="btn btn-primary snext" value="Next">
                        </div>
                    </div>

                </div>
                <div class="modal-footer"></div>
            </div>
        </div>
    </div>
    <style type="text/css">
        @media screen and (min-width: 768px) {
            .modal-dialog {
                width: 700px; /* New width for default modal */
            }
            .modal-sm {
                width: 350px; /* New width for small modal */
            }
        }
        @media screen and (min-width: 992px) {
            .modal-lg {
                width: 950px; /* New width for large modal */
            }
        }
    </style>
    <?php include("includes/linksfoot.php"); ?>
</body>
</html> 