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

$formname = 'Hall Booking';
$maindb = 'hallbook';
$printfile = "hallbook_pdf.php";
$file = 'hallbooking.php';
$trandbexist = 0;
$grid = '';
$fseries = $_SESSION["series_" . $dbname];
$keycond = " and `series`='$fseries'";
$keyfld = "vouchno";
$focusfield = "name";
$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"];
    $vouchdt = $_POST["vouchdt"];
    $hall = $_POST["hall"];
    $eventdt = $_POST["eventdt"];
    $formem = $_POST["formem"];
    $memcode = $_POST["memcode"];
    $name = $_POST["name"];
    $add1 = $_POST["add1"];
    $add2 = $_POST["add2"];
    $add3 = $_POST["add3"];
    $mobile = $_POST["mobile"];
    $mobile2 = $_POST["mobile2"];
    $amount = $_POST["amount"];
    $paytype = $_POST["paytype"];
    $advance = $_POST["advance"];
    $balance = $_POST["balance"];
    $staff = $_POST["staff"];
    $remark1 = $_POST["remark1"];
    $remark2 = $_POST["remark2"];
    $decorator = $_POST["decorator"];
    $decoamt = $_POST["decoamt"];
    $extdecoamt = $_POST["extdecoamt"];
    
    $query2 = "SELECT vouchno, hall, DATE_FORMAT(`eventdt`, '%d-%m-%Y')as eventdt FROM `hallbook` WHERE hall ='$hall' and eventdt  = '$eventdt' 
                      and vouchno != '$qvouch' " ;
    $result2 = mysqli_query($conn, $query2) or die(mysqli_error($conn));
    $r = mysqli_num_rows($result2);
    if ($r > 0 ) {
        $fet2 = mysqli_fetch_array($result2);
        browse("Hall Date: " . $fet2['eventdt']  . " For This Hall Is Already Booked For Voucher No." +  $fet2['vouchno'] );
        $a = 0 ;
    }
    
    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;

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

            if (mysqli_num_rows($genb) > 0) {
                $vvouchb = mysqli_fetch_assoc($genb);
                $increvouchb = $vvouchb["maxcode"] + 1;
            } else {
                $increvouchb = '1';
            }
            $bookno = $increvouchb;
        }
        $mvouch = $qvouch;

        if ($addflag == 1) {


            mysqli_query($conn, "INSERT INTO `$maindb`(`vouchno`, bookno, `vouchdt`, `hall`, `eventdt`, `formem`, `memcode`, `name`, `add1`,
                                                       `add2`, `add3`, `mobile`, `mobile2`, `amount`, `paytype`, `advance`, `balance`, `staff`,
                                                       `remark1`, `remark2`,`series`, `decorator`, `decoamt`, `extdecoamt`)
                                                VALUES('$qvouch', '$bookno', '$vouchdt', '$hall', '$eventdt', '$formem', '$memcode', '$name', 
                                                '$add1', '$add2', '$add3', '$mobile', '$mobile2','$amount', '$paytype', '$advance', '$balance',
                                                '$staff', '$remark1', '$remark2', '$fseries', '$decorator', '$decoamt', '$extdecoamt')") or die(mysqli_error($conn));
            insmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit"); 
        } else {
            $mvouch = $qvouch;
            mysqli_query($conn, "UPDATE `$maindb` SET `vouchdt`='$vouchdt', `hall`='$hall', `eventdt`='$eventdt', 
                                                     `formem`='$formem', `memcode`='$memcode', `name`='$name', `add1`='$add1', `add2`='$add2',
                                                     `add3`='$add3', `mobile`='$mobile', `mobile2`='$mobile2',
                                                     `paytype`='$paytype', `advance`='$advance', `balance`='$balance', `staff`='$staff',
                                                     `remark1`='$remark1',`remark2`='$remark2', `amount`='$amount',
                                                     `decorator`='$decorator', `decoamt`='$decoamt', `extdecoamt`='$extdecoamt'  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 hallname WHERE code = '$get[hall]' ") or die(mysqli_error($conn));
    $fhall = mysqli_fetch_object($ghall);

    $getdeco = mysqli_query($conn, "SELECT `accode`,`name` FROM accmast WHERE accode = '$get[decorator]' ") or die(mysqli_error($conn));
    $fetdeco = mysqli_fetch_array($getdeco);
}

$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/hallbook.js" type="text/javascript"></script>
<script src="MyScript/hallbook_search.js" type="text/javascript"></script>
</head>

<body>
    <form action='' method='POST' enctype="multipart/form-data">
        <!-- Default box -->
        <div class="row">
            <div class="col-lg-8">
                <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-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Voucher</label>
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" name="bookno" class="form-control form bookno" value="<?php echo ($addflag != 1) ? $get["bookno"] : "New"; ?>" readonly>
                                <input type="hidden" name="vouchno" class="form-control form vouchno" value="<?php echo ($addflag != 1) ? $get["vouchno"] : "$qvouch"; ?>" readonly>
                            </div>

                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Booking 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 focusfield form vouchdt " value="<?php echo ($addflag != 1) ? $get["vouchdt"] : date('Y-m-d'); ?>" required>
                            </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">Hall</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 pad">
                                <select class="form-control form hall  select2" name="hall" id="hall" required>
                                    <option value="<?php echo ($addflag != 1) ? $get['hall'] : ""; ?>"><?php echo ($addflag != 1) ? $fhall->name : ""; ?></option>
                                    <?php
                                    $gethall = mysqli_query($conn, "SELECT * FROM `hallname` where series ='$fseries' ") or die(mysqli_error($conn));
                                    while ($fethall = mysqli_fetch_object($gethall)) {
                                        echo "<option value='$fethall->code'>$fethall->name</option>";
                                    }
                                    ?>
                                </select>
                            </div>

                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad" style="background-color: lightpink;border-radius:5px">
                                <label class="label-control marg" >Event Date</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 pad" style="border: 1px solid lightpink;border-radius:5px" required>
                                <input type="date" name="eventdt" min="<?php echo date('Y-m-d'); ?>" class="form-control form eventdt" style="color: #B01542;" value="<?php echo ($addflag != 1) ? $get['eventdt'] : ""; ?>" required>
                            </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">For</label>
                            </div>
                            <div class="col-lg-2 col-md-8 col-sm-8 col-xs-8 pad">
                                <select name="formem" class="form-control form formem select2">
                                    <option value="<?php echo ($addflag != 1) ? $get['formem'] : ""; ?>"><?php echo ($addflag != 1) ? $get['formem'] : ""; ?></option>
                                    <option  value='Member'>Member</option>
                                    <option  value='Non-member'>Non-Member</option>
                                </select>
                            </div>
                        </div>
                        <?php
                        $vis = "";
                        if ($get['formem'] != 'Member') {
                            $vis = "style ='display:none'";
                        }
                        ?>
                        <div class="row membercode" <?= $vis ?>>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Member Code</label>
                            </div>
                            <div class="col-lg-2 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form  memcode" name="memcode" value="<?php echo ($addflag != 1) ? $get["memcode"] : ""; ?>" >
                            </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">Name</label>
                            </div>
                            <div class="col-lg-6 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form  name" maxlength="40" name="name" value="<?php echo ($addflag != 1) ? $get["name"] : ""; ?>" required>
                            </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">Address </label>
                            </div>
                            <div class="col-lg-6 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="text" class="form-control form add1"  maxlength="50" name="add1" value="<?php echo ($addflag != 1) ? $get["add1"] : ""; ?>" required>
                                <input type="text" class="form-control form add2"  maxlength="50" name="add2" value="<?php echo ($addflag != 1) ? $get["add2"] : ""; ?>">
                                <input type="text" class="form-control form add3"  maxlength="50" name="add3" value="<?php echo ($addflag != 1) ? $get["add3"] : ""; ?>">
                            </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">Mobile</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" name="mobile" class="form-control form mobile" value="<?php echo ($addflag != 1) ? $get["mobile"] : ""; ?>" required>
                            </div>

                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Mobile2</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" name="mobile2" class="form-control form mobile2" value="<?php echo ($addflag != 1) ? $get["mobile2"] : ""; ?>">
                            </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">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"] : "0"; ?>">
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Pay Type</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 pad">
                                <select class="form-control form paytype select2" name="paytype" id="paytype" >
                                    <option value="<?php echo ($addflag != 1) ? $get['paytype'] : ""; ?>"><?php echo ($addflag != 1) ? $get['paytype'] : ""; ?></option>
                                    <option value="Cash">Cash</option>
                                    <option value="Cheque">Cheque</option>
                                    <option value="NEFT">NEFT</option>
                                </select>
                            </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">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"] : "0"; ?>">
                            </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">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"] : "0"; ?>" readonly>
                            </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">Booked By Staff</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                                <select class="form-control form staff select2" name="staff" id="staff" required>
                                    <option value="<?php echo ($addflag != 1) ? $get["staff"] : ""; ?>"><?php echo ($addflag != 1) ? $get["staff"] : ""; ?></option>
                                    <?php
                                    $getstaff = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `grpcode`='044'") or die(mysqli_error($conn));
                                    while ($fetstaff = mysqli_fetch_object($getstaff)) {
                                        echo "<option value='$fetstaff->name'>$fetstaff->name</option>";
                                    }
                                    ?>
                                </select>
                            </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">Decorator</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                                <select class="form-control form decorator select2" name="decorator" >
                                    <option value="<?php echo ($addflag != 1) ? $fetdeco["accode"] : ""; ?>"><?php echo ($addflag != 1) ? $fetdeco["name"] : ""; ?></option>
                                    <?php
                                    $getd = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `grpcode`='009'") or die(mysqli_error($conn));
                                    while ($fetd = mysqli_fetch_object($getd)) {
                                        echo "<option value='$fetd->accode'>$fetd->name</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                            <div class="col-lg-1 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="decoamt" class="form-control text-right form decoamt" value="<?php echo ($addflag != 1) ? $get["decoamt"] : "0"; ?>" >
                            </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">Extra Decorator Amount</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" name="extdecoamt" class="form-control text-right form extdecoamt" value="<?php echo ($addflag != 1) ? $get["extdecoamt"] : "0"; ?>" >
                            </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">Remarks</label>
                            </div>
                            <div class="col-lg-6 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-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg"></label>
                            </div>
                            <div class="col-lg-6 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">Voucher</option>
                                <option value="2">Booking Date</option>
                                <option value="3">Hall</option>
                                <option value="4">Event Date</option>
                                <option value="5">For</option>
                                <option value="6">Name</option>
                                <option value="7">Address</option>
                                <option value="8">Mobile</option>
                                <option value="9">Amount</option>
                                <option value="10">Booked By</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">Voucher</option>
                                <option value="2">Booking Date</option>
                                <option value="3">Hall</option>
                                <option value="4">Event Date</option>
                                <option value="5">For</option>
                                <option value="6">Name</option>
                                <option value="7">Address</option>
                                <option value="8">Mobile</option>
                                <option value="9">Amount</option>
                                <option value="10">Booked By</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="3%">Voucher No</th>
                                    <th width="7%">Booking Date</th>
                                    <th width="5%">Hall</th>
                                    <th width="7%">Event Date</th>
                                    <th width="5%">For</th>
                                    <th width="10%">Name</th>
                                    <th width="10%">Address</th>
                                    <th width="6%">Mobile</th>
                                    <th width="5%">Amount</th>
                                    <th width="10%">Booked By</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>