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

$formname = 'HR';
$maindb = 'hrmain';
$trandb = 'hrtran';
$accdb = 'actran';
$visadb = 'visasacn';
$printfile = "hrreg.php";
$file = 'hr_ent.php';
$trandbexist = 1;
$grid = '';
$keyfld = "vouchno";
$focusfield = "name";
$source = "HR";
// $salecode = 30;

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];

// $fseries = $_SESSION["series_" . $dbname];
$keycond = "";

$adduser = $_SESSION['username_' . $dbname];
date_default_timezone_set("Asia/Kolkata");
$adddate = date("Y-m-d");
$addtime = date("h:i:sa");
$usertype = $_SESSION["usertype"];

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"];
    $customer = $_POST["customer"];
    $nofpass = $_POST["nofpass"];
    $totamt = $_POST["totamt"];
    $visano = $_POST["visano"];
    $idno = $_POST["idno"];
    $embassy = $_POST["embassy"];
    $totpax = $_POST["totpax"];
    $totavlbl = $_POST["totavlbl"];
    // $country = strtoupper($_POST["country"]);

    $count = $_POST["items"];

    $mvouch = $qvouch;


    if ($a == 1) {

        mysqli_query($conn, "LOCK TABLE `$maindb` WRITE,`$trandb` WRITE, `catgmast` WRITE, $visadb WRITE");

        ############################## MAIN  ######################################################
        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);
                $qvouch = $vvouch["maxcode"] + 1;
            } else {
                $qvouch = '1';
            }
            $mvouch = $qvouch;

            mysqli_query($conn, "INSERT INTO `$maindb`(`vouchno`, `vouchdt`, `customer`, `nofpass`, `totamt`,`visano`,`idno`,`embassy`,`totpax`,`totavlbl`)
                                                VALUES('$qvouch', '$vouchdt', '$customer', '$nofpass', '$totamt','$visano','$idno','$embassy','$totpax','$totavlbl')") or die(mysqli_error($conn));
            insmess();
            $mvouch = $qvouch; //1
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        } else {
            $mvouch = $qvouch; //1

            mysqli_query($conn, "UPDATE `$maindb` SET  `vouchdt`='$vouchdt', `customer`='$customer', `nofpass`='$nofpass', `totamt`='$totamt',`visano`='$visano' ,
                                                        `idno`='$idno',`embassy`='$embassy',`totpax`='$totpax', `totavlbl`='$totavlbl' WHERE `vouchno` = '$mvouch' ") or die(mysqli_error($conn));
            updmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        }


        ############################## VISATRAN  ######################################################
        mysqli_query($conn, "DELETE FROM `$trandb` WHERE vouchno='$mvouch' ") or die(mysqli_error($conn));
        mysqli_query($conn, "DELETE FROM `$visadb` WHERE vouchno='$mvouch' AND source='$source' ") or die(mysqli_error($conn));

        for ($i = 0; $i <= $count; $i++) {
            $cnt++;
            $msrno = $_POST["srno_$i"];
            $paxno = $_POST["paxno_$i"];
            $category = $_POST["category_$i"];
            $cateng = $_POST["cateng_$i"];
            $nationality = $_POST["nationality_$i"];
            $visaiss = $_POST["visaiss_$i"];
            $avlbl = $_POST["avlbl_$i"];
            
             $gcat = mysqli_query($conn, "SELECT code,name FROM `catgmast` WHERE `name`='$cateng'") or die(mysqli_error($conn));
        if (mysqli_num_rows($gcat) > 0) {
            $fcat = mysqli_fetch_array($gcat);
            $catcode = $fcat['code'];
        } else {
            $gensz1 = mysqli_query($conn, "SELECT MAX(code) AS maxcode FROM `catgmast` where code != '' LIMIT 0,1 ") or die(mysqli_error($conn));
            if (mysqli_num_rows($gensz1) > 0) {
                $gsz1 = mysqli_fetch_assoc($gensz1);
                $catcode = $gsz1["maxcode"] + 1;
            } else {
                $catcode = '1';
            }
            if (!empty($cateng)){
            mysqli_query($conn, "INSERT INTO `catgmast`(`code`, `name`)
                           VALUES('$catcode',  '$cateng')") or die(mysqli_error($conn));
            }
        }

            if (!empty($paxno)) {
                mysqli_query($conn, "INSERT INTO `$trandb`(`srno`,paxno, category, cateng, nationality, `visaiss`, `avlbl`, `vouchno`, `vouchdt`)
                                                    VALUES('$cnt', '$paxno', N'$category', '$cateng', '$nationality', '$visaiss', '$avlbl', '$mvouch', '$vouchdt')") or die(mysqli_error($conn));

                mysqli_query($conn, "INSERT INTO `$visadb` (`vouchdt`,`vouchno`, `source`, visano, idno, `accode`,  `category`, `cateng`, `qty`)
                                                    VALUES('$vouchdt', '$mvouch', '$source', '$visano', '$idno', '$customer', N'$category', '$cateng','$paxno')") or die(mysqli_error($conn));
            }
        }

        ############################## ACTRAN  ######################################################
        // mysqli_query($conn, "DELETE FROM `$accdb` WHERE vouchno='$mvouch' and source = '$source' ") or die(mysqli_error($conn));
        // if ($totamt > 0) {
        //     $minus_amt = ($totamt * -1);
        //     // sale part
        //     mysqli_query($conn, "INSERT INTO `$accdb`(`date`, vouchno, source, visano, idno, category, amount)
        //                                      VALUES ('$vouchdt','$mvouch','$source','$visano', '$idno', '$customer', '005', '$totamt' )") or die(mysqli_error($conn));
        //     // party part
        //     mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, accode, grpcode, amount)
        //                                      VALUES ('$vouchdt','$mvouch','$source', '002', '$agent', '010', '$minus_amt' )") or die(mysqli_error($conn));
        // }

        mysqli_query($conn, "UNLOCK TABLES");
    }
}

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);
    if ($get['embassy'] == "M") {
        $emb = "Mumbai";
    } else {
        $emb = "New-Delhi";
    }

    $findparty = mysqli_query($conn, "SELECT `name` FROM `accmast` WHERE `accode`='$get[customer]'") or die(mysqli_error($conn));
    $lparty = mysqli_fetch_array($findparty);

    $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno` = '$get[vouchno]' ORDER BY `srno`") or die(mysqli_error($conn));

    $rd = "readonly";
}

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

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

    $delcondn = " $keyfld = '$_GET[$keyfld]' ";
    $delcondn2 = " $keyfld = '$_GET[$keyfld]' AND source = '$source' ";

    delnew($zdb, "$maindb", "$delcondn", "$file?mode=last");
    delnew($zdb, "$trandb", "$delcondn", "$file?mode=last");
    delnew($zdb, "$visadb", "$delcondn2", "$file?mode=last");
}

include("includes/linkshead.php");
?>
<!-- <script src="MyScript/v_search.js" type="text/javascript"></script> -->
<script src="MyScript/hr_ent.js" type="text/javascript"></script>
<script src="MyScript/hr_ent_search.js" type="text/javascript"></script>

</head>

<body>

    <form action='' method='POST'>
        <!-- Default box -->
        <div class="row">
            <div class="col-lg-12">
                <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-1 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-1 col-sm-1 col-xs-1 pad ">
                                <input type="text" class="form-control form vouchno" name="vouchno" value="<?php echo ($addflag != 1) ? $get["vouchno"] : $qvouch; ?>" tabindex="-1" readonly>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 text-right pad">
                                <label class="label-control marg">Date</label>
                            </div>
                            <div class="col-lg-1 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="date" class="form-control form vouchdt" name="vouchdt" value="<?php echo ($addflag != 1) ? $get["vouchdt"] : date("Y-m-d"); ?>" required>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Company / Customer </label>
                            </div>
                            <div class="col-lg-3 col-md-8 col-sm-8 col-xs-8 pad">
                                <select name="customer" class="form-control form select2 customer" style="width: 90%">
                                    <option value="<?php echo ($addflag != 1) ? $get["customer"] : ""; ?>"><?php echo ($addflag != 1) ? $lparty["name"] : ""; ?></option>
<?php
$gparty = mysqli_query($conn, "SELECT * FROM `accmast` WHERE grpcode = '010' ORDER BY name") or die(mysqli_error($conn));
while ($party = mysqli_fetch_array($gparty)) {
    echo "<option value='$party[accode]'>$party[name]</option>";
}
?>
                                </select>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Visa No</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 pad ">
                                <input type="text" class="form-control form visano" name="visano" value="<?php echo ($addflag != 1) ? $get["visano"] : ''; ?>">
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 text-right pad">
                                <label class="label-control marg">ID No</label>
                            </div>
                            <div class="col-lg-1 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="text" class="form-control form idno" name="idno" value="<?php echo ($addflag != 1) ? $get["idno"] : ""; ?>">
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Embassy </label>
                            </div>
                            <div class="col-lg-3 col-md-8 col-sm-8 col-xs-8 pad">
                                <select name="embassy" class="form-control form select2 embassy" style="width: 90%">
                                    <option value="<?php echo ($addflag != 1) ? $get["embassy"] : ""; ?>"><?php echo ($addflag != 1) ? $emb : ""; ?></option>
                                    <option value='M'>Mumbai</option>
                                    <option value='N'>New Delhi</option>
                                </select>
                            </div>
                        </div>

                        <div class="row">
                            <div class="panel panel-default">

                                <div class="panel-body">
                                    <div class='table-responsive' style="overflow-y:scroll; height: 350px">
                                        <table class="table table-striped table-condensed table-bordered" width="80%">
                                            <thead class="mySearchTr">
                                                <tr>
                                                    <th style="width: 2%"></th>
                                                    <th style="width: 4%;text-align: center;">Sr.</th>
                                                    <th style="width: 10%;text-align: right">Person Count</th>
                                                    <th style="width: 20%;">Occupation / Category</th>
                                                    <th style="width: 20%;">Category in English</th>
                                                    <th style="width: 20%;">Nationality</th>
                                                    <th style="width: 10%;">Visa Issue</th>
                                                    <!-- <th style="width: 10%;">Availability</th> -->
                                                </tr>
                                            </thead>
                                            <tbody id="tabledata">
<?php
if ($addflag != 1) {
    $ctr = 0;
    while ($tran = mysqli_fetch_array($gettran)) {
        echo "<tr>";
        echo "<td align='center' valign='top'><span class='remove fa fa-times ' name='remove' value='remove' style='display:none;'></span></td>";
        echo "<td><input type='text' value='$tran[srno]'  name='srno_$ctr'  class='form-control form text-right' style='text-align:center;' readonly></td>";
        echo "<td><input type='number' value='$tran[paxno]' name='paxno_$ctr' maxlength='4' class='form-control text-right form paxno' ></td>";
        echo "<td><input type='text' value='$tran[category]' name='category_$ctr' class='form-control form category' ></td>";
//        echo "<td><input type='text' value='$tran[cateng]' name='cateng_$ctr' class='form-control form cateng'></td>";
        echo "<td><input list='cateng' name='cateng_$ctr' value='$tran[cateng]' class=' form-control form ' autocomplete='off'>";
                                echo "<datalist id='cateng'>";
                                    echo "<option value=''>";
                                        $gcat = mysqli_query($conn, "SELECT `name` FROM `catgmast` where code!=''") or die(mysqli_error($conn));
                                        while ($fcat = mysqli_fetch_array($gcat)) {
                                            echo "<option value = '$fcat[name]' > ";
                                        }
                                echo "</datalist>";
                                echo "</td>";
        echo "<td><input type='text' value='$tran[nationality]' name='nationality_$ctr' class='form-control form nationality'  ></td>";
        echo "<td><input type='date' value='$tran[visaiss]' name='visaiss_$ctr' class='form-control form visaaiss'  ></td>";
        // echo "<td><input type='number' value='$tran[avlbl]' name='avlbl_$ctr' maxlength='4' class='form-control form avlbl' style='text-align:right;'  ></td>";
        echo "</tr>";
        $ctr++;
    }
} else {
    $pctr = 0;
    $yctr = 0;

    $yctr++;
    echo "<tr>";
    echo "<td align='center'><span class='remove fa fa-times ' name='remove' value='remove' style=''></span></td>";
    echo "<td  align='left'><input type='text' size='6' maxlength='6' name='srno_$pctr' class='sno form-control form' style='text-align:center' value='$yctr' readonly></td>";

    echo "<td><input type='number'   name='paxno_$pctr' class='paxno form-control form' maxlength='4' style='text-align:right;' value='' /></td>";
    echo "<td><input type='text'   name='category_$pctr' class='category form-control form' style='text-align:left;' value=''  /></td>";
//    echo "<td><input type='text'   name='cateng_$pctr' class='cateng form-control form' style='text-align:left;' value=''></td>";
    echo "<td><input list='cateng' name='cateng_$pctr' value='' class='cateng form-control form ' autocomplete='off'>";
                                echo "<datalist id='cateng'>";
                                    echo "<option value=''>";
                                        $gcat = mysqli_query($conn, "SELECT `name` FROM `catgmast` where code!=''") or die(mysqli_error($conn));
                                        while ($fcat = mysqli_fetch_array($gcat)) {
                                            echo "<option value='$fcat[name]' > ";
                                        }
                                echo "</datalist>";
                                echo "</td>";
    echo "<td><input type='text'   name='nationality_$pctr' class='nationality form-control form' style='text-align:left;' value='' /></td>";
    echo "<td><input type='date'   name='visaiss_$pctr' class='visaiss form-control form' style='text-align:left;' value=''/></td>";
    // echo "<td><input type='number' name='avlbl_$pctr' class='avlbl form-control form' maxlength='4' style='text-align:right;' value=''/></td>";
    echo "</tr>";
    $pctr++;
}
?>
                                            </tbody>
                                        </table>
                                    </div>
                                    <button type="button" class="btn btn-success btn-square form" id="addnew">
                                        <i class="fa fa-plus" style="color:#ffffff" data-toggle='tooltip' title='Add Row'></i>
                                    </button>
                                    <input type="hidden" id="items" name="items" value="<?php echo ($addflag != 1) ? $ctr : 1; ?>" />
                                    <div class="col-lg-12 col-xs-12 pad row">
                                        <div class="col-lg-8 pad"></div>
                                        <div class="col-lg-3 col-xs-5 pad text-right">
                                            <label class="label-control marg text-right">Total Pax</label>
                                        </div>
                                        <div class="col-lg-1 col-xs-3 pad">
                                            <input type="number" step="any" name="totpax" class="form-control form text-right totpax" value="<?php echo ($addflag != 1) ? $get['totpax'] : "0"; ?>" readonly>
                                        </div>
                                        <!-- <div class="col-lg-1 col-xs-1 pad text-right">
                                            <label class="label-control marg">Total Availability</label>
                                        </div>
                                        <div class="col-lg-2 col-xs-3 pad">
                                            <input type="number" step="any" name="totavlbl" class="form-control form text-right totavlbl" value="<?php echo ($addflag != 1) ? $get['totavlbl'] : "0"; ?>" readonly>
                                        </div> -->
                                    </div>
                                </div>
                            </div>
                        </div>

                    </div>

                    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 pad">
                        <div class="panel panel-default">
                            <div class="panel-body">
                                <!-- <div class="col-lg-3 col-xs-2 pad"></div>
                                <div class="col-lg-1 col-xs-2 pad" style="margin-top: 7px;">
                                    <a href="entinvoice_pdf.php?<?= $keyfld . "=" . $qvouch; ?>" target="_blank" class="btn btn-primary"  tabindex="-1">PDF</a>
                                </div> -->

                                <div class="col-lg-12 pad text-right">
<?php include("includes/buttons.php"); ?>
                                </div>



                            </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">Date</option>
                                <option value="3">Company</option>
                                <option value="4">Visa No</option>
                                <option value="5">ID No</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">Date</option>
                                <option value="3">Company</option>
                                <option value="4">Visa No</option>
                                <option value="5">ID No</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" style="border-radius: 5px" 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="10%">Voucher</th>
                                    <th width="10%">Date</th>
                                    <th width="10%">Company</th>
                                    <th width="10%">Visa No</th>
                                    <th width="10%">ID No</th>
                                    <th width="10%">Embassy</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" style="border-radius: 5px" value="Previous">
                            <input type="button" class="btn btn-primary snext" style="border-radius: 5px" value="Next">
                        </div>
                    </div>
                </div>
                <div class="modal-footer"></div>
            </div>
        </div>
    </div>
<?php include("includes/linksfoot.php"); ?>
</body>

</html>