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

$formname = 'Bill';
$maindb = 'checkout';
$printfile = "checkout_pdf.php";
$file = 'checkout.php';
$trandbexist = 0;
$grid = '';
$fseries = $_SESSION["series_" . $dbname];
$keycond = " and `series`='$fseries'";
$keyfld = "vouchno";
$username = $_SESSION["username_" . $dbname];
$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);

include("includes/vouchno_generate.php");
include("includes/prev&next.php");

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

    $a = 1;
    $qvouch = $_POST["vouchno"];
    $code = $_POST["code"];
    $vouchdt = $_POST["vouchdt"];
    $roomno = $_POST["roomno"];
    $bookno = $_POST["bookno"];
    $name = $_POST["name"];
    $chkin = $_POST["chkin"];
    $chkout = $_POST["chkout"];
    $daysno = $_POST["daysno"];
    $rate = $_POST["rate"];
    $amount = $_POST["amount"];
    $extra = $_POST["extra"];
    $half = $_POST["half"];
    $totbill = $_POST["totbill"];
    $ldisc = $_POST["ldisc"];
    $addoth = $_POST["addoth"];
    $billamt = $_POST["billamt"];
    $advance = $_POST["advance"];
    $balance = $_POST["balance"];
    $paytype = $_POST["paytype"];
    $remark1 = $_POST["remark1"];
    $remark2 = $_POST["remark2"];

    if ($a == 1) {

        if ($addflag == 1) {

            $gen1 = mysqli_query($conn, "SELECT MAX($keyfld) AS maxcode FROM `$maindb` WHERE $keyfld !=''  LIMIT 0,1 ") or die(mysqli_error($conn));

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

        if ($addflag == 1) {


            mysqli_query($conn, "INSERT INTO `$maindb`(`vouchno`, `code`, `vouchdt`, `roomno`, `bookno`, `name`, `chkin`, `chkout`,
                                                       `daysno`, `rate`, `amount`, `extra`, `half`, `totbill`, `ldisc`, `addoth`, `billamt`,
                                                       `advance`,`series`, `balance`, `paytype`, `remark1`, `remark2`)
                                                VALUES('$qvouch', '$code', '$vouchdt', '$roomno', '$bookno', '$name', '$chkin', '$chkout', 
                                                '$daysno', '$rate', '$amount', '$extra','$half', '$totbill', '$ldisc', '$addoth',
                                                '$billamt', '$advance', '$fseries', '$balance', '$paytype' , '$remark1','$remark2')") or die(mysqli_error($conn));
            insmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        } else {
            $mvouch = $qvouch;
            mysqli_query($conn, "UPDATE `$maindb` SET `code`='$code', `vouchdt`='$vouchdt', `roomno`='$roomno', `bookno`='$bookno', `name`='$name', 
                                                     `chkin`='$chkin', `chkout`='$chkout', `daysno`='$daysno', `rate`='$rate',
                                                     `amount`='$amount', `extra`='$extra', `half`='$half',`totbill`='$totbill', `ldisc`='$ldisc', 
                                                     `addoth`='$addoth', `billamt`='$billamt', `advance`='$advance',`series`='$fseries',
                                                    `balance`='$balance', `paytype`='$paytype', `remark1`='$remark1', `remark2`='$remark2'  where vouchno = '$mvouch' $keycond") or die(mysqli_error($conn));

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

if ($addflag != 1) {

    $getvouch = '';
    if ($addflag == 0) {
        $getvouch = $_GET[$keyfld];
    } else {
        $getvouch = $qvouch;
    }

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

    $ghall = mysqli_query($conn, "SELECT * FROM hotmain WHERE vouchno != ''") or die(mysqli_error($conn));
    $fhall = mysqli_fetch_object($ghall);
}

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

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

    $delcondn = " $keyfld = '$_GET[$keyfld]' $keycond";

    delnew($zdb, "$maindb", "$delcondn", "$file?mode=last&series=$fseries");
}

include("includes/linkshead.php");
?>

<script src="MyScript/checkout.js" type="text/javascript"></script>
<script src="MyScript/checkout_search.js" type="text/javascript"></script>
<style>
    .maincal {
        background-color: #F0E68C;
        border: 1px solid black;
    }
</style>
</head>

<body>
    <form action='' method='POST' enctype="multipart/form-data">
        <!-- Default box -->
        <div class="row">
            <div class="col-lg-6">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title"><?php echo $formname; ?></h3>

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

                        <div class="row">
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Date</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="date" name="vouchdt" class="form-control form vouchdt" value="<?php echo ($addflag != 1) ? $get["vouchdt"] : date('Y-m-d'); ?>" required>
                            </div>

                            <div class="col-lg-4 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Bill No.</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" name="vouchno" class="form-control form vouchno" value="<?php echo ($addflag != 1) ? $get["vouchno"] : $qvouch; ?>" readonly>
                                <input type="hidden" class="form-control form code" name="code" value="<?php echo ($addflag != 1) ? $get["code"] : ""; ?>">
                                <input type="hidden" class="form-control form addflg" name="addflg" value="<?php echo $addflag; ?>">
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Booking No.</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 pad" required>
                                <input type="text" name="bookno" class="form-control form bookno focusfield" style="color: #B01542;" value="<?php echo ($addflag != 1) ? $get['bookno'] : ""; ?>">
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Room No.</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" name="roomno" class="form-control form roomno" style="color: #B01542;" value="<?php echo ($addflag != 1) ? $get['roomno'] : ""; ?>" readonly tabindex="-1">
                            </div>
                        </div>


                        <div class="row">
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Party Name</label>
                            </div>
                            <div class="col-lg-4 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form  name" name="name" value="<?php echo ($addflag != 1) ? $get["name"] : ""; ?>" required tabindex="-1">
                            </div>
                        </div>
                        
                        <div class="row">
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Check-In Date </label>
                            </div>
                            <div class="col-lg-4 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="datetime-local" class="form-control form chkin" tabindex="-1" name="chkin" value="<?php echo ($addflag != 1) ? $get["chkin"] : ""; ?>" readonly tabindex="-1">
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Check-Out Date </label>
                            </div>
                            <div class="col-lg-4 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="datetime-local" class="form-control form chkout" name="chkout" value="<?php echo ($addflag != 1) ? $get["chkout"] : ""; ?>">
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">No. Of Days</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="text" class="form-control text-right form daysno" tabindex="-1" name="daysno" value="<?php echo ($addflag != 1) ? $get["daysno"] : ""; ?>" readonly tabindex="-1">
                            </div>
                        </div>

                        <div class="maincal">
                            <div class="row">
                                <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                    <label class="label-control marg">Rate Per Day</label>
                                </div>
                                <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                    <input type="text" class="form-control text-right form rate" name="rate" value="<?php echo ($addflag != 1) ? $get["rate"] : ""; ?>">
                                </div>
                                <!-- <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-center pad">
                                <label class="label-control marg">X</label>
                                </div> -->
                                <div class="col-lg-4 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                    <label class="label-control marg">Amount</label>
                                </div>
                                <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 pad">
                                    <input type="text" name="amount" class="form-control text-right form amount" value="<?php echo ($addflag != 1) ? $get["amount"] : ""; ?>" readonly tabindex="-1">
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                    <label class="label-control marg">Extra Charges</label>
                                </div>
                                <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 pad">
                                    <input type="text" name="extra" class="form-control text-right form extra" value="<?php echo ($addflag != 1) ? $get["extra"] : ""; ?>">
                                </div>
                                <div class="col-lg-4 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                    <label class="label-control marg">Add Other</label>
                                </div>
                                <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 pad">
                                    <input type="text" name="addoth" class="form-control text-right form addoth" value="<?php echo ($addflag != 1) ? $get["addoth"] : "0"; ?>">
                                </div>
                            </div>
                            <div class="row">
                                <!-- <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                    <label class="label-control marg">Half Day</label>
                                </div>
                                <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 pad">
                                    <input type="text" name="half" class="form-control text-right form half" value="<?php echo ($addflag != 1) ? $get["half"] : ""; ?>">
                                </div> -->
                                <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                    <label class="label-control marg">Less Discount</label>
                                </div>
                                <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 pad">
                                    <input type="text" name="ldisc" class="form-control text-right form ldisc" value="<?php echo ($addflag != 1) ? $get["ldisc"] : ""; ?>">
                                </div>
                                <div class="col-lg-4 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                    <label class="label-control marg">Bill Amount</label>
                                </div>
                                <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 pad">
                                    <input type="text" name="billamt" class="form-control text-right form billamt" value="<?php echo ($addflag != 1) ? $get["billamt"] : ""; ?>" readonly tabindex="-1">
                                </div>
                            </div>
                            <hr style="border:2px solid black">
                            <div class="row">
                                <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                    <label class="label-control marg">Advance</label>
                                </div>
                                <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 pad">
                                    <input type="text" name="advance" class="form-control text-right form advance" value="<?php echo ($addflag != 1) ? $get["advance"] : ""; ?>" readonly tabindex="-1">
                                </div>
                                <div class="col-lg-4 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                    <label class="label-control marg">Balance</label>
                                </div>
                                <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 pad">
                                    <input type="text" name="balance" class="form-control text-right form balance" value="<?php echo ($addflag != 1) ? $get["balance"] : ""; ?>" readonly tabindex="-1">
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Payment Type</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 pad">
                                <select class="form-control form select2 paytype" name="paytype" id="paytype">
                                    <option value=" <?php echo ($addflag != 1) ? $get['paytype'] : ""; ?>"><?php echo ($addflag != 1) ? $get['paytype'] : ""; ?></option>
                                    <option value=""></option>
                                    <option value="Cash">Cash</option>
                                    <option value="Bank Payment">Bank Payment</option>
                                    <option value="UPI ID">UPI ID</option>
                                </select>
                                <!--<input type="text" name="paytype" class="form-control form paytype" value="<?php echo ($addflag != 1) ? $get["paytype"] : ""; ?>">-->
                            </div>
                        </div>
                        <div class="row">

                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Remarks</label>
                            </div>
                            <div class="col-lg-4 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" name="remark1" class="form-control form remark1" value="<?php echo ($addflag != 1) ? $get["remark1"] : ""; ?>">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg"></label>
                            </div>
                            <div class="col-lg-4 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" name="remark2" class="form-control form remark2" value="<?php echo ($addflag != 1) ? $get["remark2"] : ""; ?>">
                            </div>
                        </div>
                    </div>
                    <div class="panel-footer">
                        <?php include("includes/buttons.php"); ?>
                    </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">Bill No.</option>
                                <option value="2">Date</option>
                                <option value="3">Booking No.</option>
                                <option value="4">Room No.</option>
                                <option value="6">Party Name</option>
                                <option value="5">Bill Amount</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="">Sort By</option>
                                <option value="1">Bill No.</option>
                                <option value="2">Date</option>
                                <option value="3">Booking No.</option>
                                <option value="4">Room No.</option>
                                <option value="6">Party Name</option>
                                <option value="5">Bill Amount</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" style="border-radius: 5px" 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 class="mySearchTr">
                                    <th width="5%">Bill No.</th>
                                    <th width="7%">Date</th>
                                    <th width="7%">Booking No.</th>
                                    <th width="7%">Room No.</th>
                                    <th width="15%">Party Name</th>
                                    <th width="7%">Payment Type</th>
                                    <th width="7%" style="text-align:right">Bill Amount</th>
                                    <th width="5%">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" style="border-radius: 5px" class="btn btn-primary sprev" value="Previous">
                            <input type="button" style="border-radius: 5px" class="btn btn-primary snext" value="Next">
                        </div>
                    </div>

                </div>
                <div class="modal-footer"></div>
            </div>
        </div>
    </div>
    <?php include("includes/linksfoot.php"); ?>
</body>

</html>