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

ini_set('display_errors', 1);
error_reporting(E_ERROR);

$formname = 'Auto Attendance Entry - Monthwise';
$maindb = 'attdmon';
$trandb = 'attdtran';
$keyfld = "vouchno";
$file = 'autoattendmonth.php';

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$zroot = $_SESSION["zdbroot"];
$myyear = substr($_SESSION["zdbyear_" . $dbname], -4);

mysqli_select_db($conn, $zdb);
include("includes/vouchno_generate.php");
include("includes/prev&next.php");

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

    $vochno = $_POST["vochno"];
    $mon = $_POST["mon"];
    $monname = $_POST["monname"];
    $deptcode = $_POST["deptcode"];
    $process = $_POST["process"];
    $sunday = $_POST["sun"];
    $holiday = $_POST["ho"];
    $divcode = $_POST["divcode"];
    $yearno = $_POST["yearno"];

    $items = $_POST["items"];

//browse($ho);
    $mvouch = $qvouch;

    if ($a == 1) {

        #################################TRAN PART #################################
        mysqli_query($conn, "DELETE FROM $trandb WHERE vouchno = $mvouch ");
        $ctn = 0;
        for ($i = 0; $i < $items; $i++) {

            $ctn++;

            $srno = $_POST["srno_$i"];
            $accode = $_POST["accode_$i"];
            $basic = $_POST["basic_$i"];
            $da = $_POST["da_$i"];
            $hra = $_POST["hra_$i"];
            $ta = $_POST["ta_$i"];
            $oa = $_POST["oa_$i"];
            $ab = $_POST["ab_$i"];
            $hf = $_POST["hf_$i"];
            $sun = $_POST["sun_$i"];
            $ho = $_POST["ho_$i"];
            $pr = $_POST["pr_$i"];
            $ot = $_POST["ot_$i"];
            $othrs = $_POST["othrs_$i"];
            $pd = $_POST["pd_$i"];
            $advance = $_POST["advance_$i"];
            $totdays = $_POST["totdays_$i"];
            $p_tax = $_POST["p_tax_$i"];
            $esic = $_POST["esic_$i"];
            $pfund = $_POST["pfund_$i"];
            $food = $_POST["food_$i"];

            $salary = $basic;

            $getname = mysqli_query($conn, "SELECT accode, name FROM accmast where name = '$accode'") or die(mysqli_error($conn));
            $showname = mysqli_fetch_array($getname);
            $accode1 = $showname['accode'];

            if (!empty($salary)) {
                mysqli_query($conn, "INSERT INTO `$trandb`(`vouchno`,   `srno`, `accode`,  `salary`, `basic`, `da`, `hra`, `ta`, `oa`, 
                                                    `ab`, `hf`, `sun`, `ho`, `pr`, `ot`, `othrs`, `pd`, `advance`, `deptcode`,process,
                                                    `mon`, `totdays`, `p_tax`, `esic`, `pfund`, `divcode`,`yearno`,food)
                                          VALUES('$mvouch', '$ctn', '$accode1', '$salary', '$basic', '$da', '$hra', '$ta', '$oa', 
                                                 '$ab', '$hf', '$sun', '$ho', '$pr', '$ot', '$othrs', '$pd', '$advance', '$deptcode','$process',
                                                 '$monname', '$totdays', '$p_tax', '$esic', '$pfund', '$divcode','$yearno','$food')") or die(mysqli_error($conn));
            }
        }
        if ($addflag == 1) {

            mysqli_query($conn, "INSERT INTO `$maindb`(`vouchno`, `mon`, `monname`, `deptcode`, `divcode`, `sun`, `ho`, yearno, process) 
                                    VALUES ( '$qvouch', '$mon', '$monname', '$deptcode', '$divcode', '$sunday', '$holiday', '$yearno','$process')")or die(mysqli_error($conn));
        }

//############################## MAIN  ######################################################
        if ($addflag == 1) {
            insmess();
            $mvouch = $qvouch; //1
            header("refresh: 1; $file?mode=last");
        } else {
            $mvouch = $qvouch; //1
            updmess();
            header("refresh: 1; $file?mode=last");
        }
    }
}

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

    $getmon = mysqli_query($conn, "SELECT code, name FROM `dept` where code = '$get[deptcode]'") or die(mysqli_error($conn));
    $showmon = mysqli_fetch_array($getmon);
    // $getproc = mysqli_query($conn, "SELECT code, name FROM `process` where code = '$get[process]'") or die(mysqli_error($conn));
    // $shoproc = mysqli_fetch_array($getproc);

    $getmonno = mysqli_query($conn, "SELECT * FROM $zroot.`month` where moncode='$get[monname]'") or die(mysqli_error($conn));
    $showmonth = mysqli_fetch_array($getmonno);
}

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

if (isset($_GET["delete"])) {
    delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
    delete($zdb, "$trandb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
}

include("includes/linkshead.php");
?>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.21/css/jquery.dataTables.css">
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.21/js/jquery.dataTables.js"></script>

<script src="MyScript/autoattendmonth.js?a=<?= mt_rand() ?>" type="text/javascript"></script>
<script src="MyScript/autoattend_search.js?b=<?= mt_rand() ?>" type="text/javascript"></script>
<style>
    
    .button-29 {
        align-items: center;
        appearance: none;
        background: transparent;
        border: 2px solid #0b4c78;
        border-radius: 6px;
        box-sizing: border-box;
        color: #0b4c78;
        cursor: pointer;
        display: inline-flex;
        font-family: "JetBrains Mono", monospace;
        height: 28px;
        width: 120px;
        justify-content: center;
        line-height: 1;
        list-style: none;
        overflow: hidden;
        padding-left: 16px;
        padding-right: 16px;
        position: relative;
        text-align: left;
        text-decoration: none;
        transition: box-shadow .15s, transform .15s;
        user-select: none;
        -webkit-user-select: none;
        touch-action: manipulation;
        white-space: nowrap;
        will-change: box-shadow, transform;
        font-size: 18px;
    }


    .button-29:hover {
        box-shadow: rgba(45, 35, 66, .4) 0 4px 8px, rgba(45, 35, 66, .3) 0 7px 13px -3px, #0b4c78 0 -3px 0 inset;
        transform: translateY(-2px);
    }

    .button-29:active {
        box-shadow: #0b4c78 0 3px 7px inset;
        transform: translateY(2px);
    }
</style>
</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" style="color :#037D95;"><?php Echo $formname; ?></h3>

                    </div>
                    <div class="panel-body">
                        <div class="row">
                            <div class="col-lg-2">
                                <input type="hidden" name="vouchno" class="form-control form vouchno" value="<?php echo ($addflag != 1 ) ? $get["vouchno"] : "$qvouch"; ?>">
                                <input type="hidden" name="addflag" class="form-control form addflag" value="<?php echo ($addflag != 1 ) ? $addflag : "$addflag"; ?>">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-3 col-xs-3 pad text-right">
                                <label class="label-control marg">Department Name</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3">
                                <select class="form-control deptcode form select2" tabindex="1" name='deptcode' required >
                                    <option value="<?php echo ($addflag != 1) ? $showmon["code"] : ""; ?>"> <?php echo ($addflag != 1 ) ? $showmon["name"] : ""; ?></option>
                                    <?php
                                    if ($addflag == 1) {
                                        $getcode = mysqli_query($conn, "SELECT name,code FROM `dept` order by name")or die(mysqli_error($conn));
                                        while ($fetcode = mysqli_fetch_array($getcode)) {
                                            Echo "<option value='$fetcode[code]'>$fetcode[name]</option>";
                                        }
                                    }
                                    ?>
                                </select>
                            </div>
              
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 pad text-right">
                                <label class="label-control marg">Month</label>
                            </div>
                            <div class="col-lg-1 col-md-2 col-sm-2 col-xs-2">
                                <select name="monname" class="form-control form mon monname select2" tabindex="2">
                                    <option value="<?php echo ($addflag != 1) ? $showmonth["moncode"] : ""; ?>"> <?php echo ($addflag != 1 ) ? $showmonth["monname"] : ""; ?></option>
                                    <?php
                                    if ($addflag == 1) {
                                        $getmonth = mysqli_query($conn, "SELECT * FROM $zroot.`month` order by moncode")or die(mysqli_error($conn));
                                        while ($fetmonth = mysqli_fetch_array($getmonth)) {
                                            Echo "<option value='$fetmonth[moncode]'>$fetmonth[monname]</option>";
                                        }
                                    }
                                    ?>
                                </select>
                            </div>
                            
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 pad text-right">
                                <label class="label-control marg">Year</label>
                            </div>
                            <div class="col-lg-1 col-md-2 col-sm-2 col-xs-2">
                                <input type='text' class='form-control form yearno' tabindex="3" id="yearno" name='yearno' style="" value="<?php echo ($addflag != 1) ? $get["yearno"] : ""; ?>" required>
                            </div>
                            <?php if ($addflag == 1) { ?>
                            <div class="col-lg-1">
                                <button type="button" class="button-29 form autoattend">AUTO</button>
                            </div>
                            <div class="col-lg-1">
                                <button type="button" class="button-29 form manualattend">MANUAL</button>
                            </div>
                            <?php } ?>
                        </div>
                                

                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad text-right">
                                <label class="label-control marg">Weekly Off</label>
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3">
                                <input type='text' class='form-control form sun msun number text-right' maxlength="1" tabindex="4" id="sun" name='sun' style="color:red" value="<?php echo ($addflag != 1) ? $get["sun"] : "0"; ?>">
                            </div>

                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 pad text-right">
                                <label class="label-control marg">No. of Holiday</label>
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3">
                                <input type='text' class='form-control form ho mho number text-right' maxlength="2" tabindex="5" id="ho" name='ho' value="<?php echo ($addflag != 1) ? $get["ho"] : "0"; ?>">
                            </div>
                        </div>



                        <div class="table-responsive" style="max-height: 1710px;overflow-y: auto;">
                            <table class="table table-striped table-condensed table-bordered"  style="width: 100%">
                                <thead>
                                    <tr class="mySearchTr">
                                        <th width="3%">Sr.</th>
                                        <th width="15%">Department</th>
                                        <th width="15%">Name</th>
                                        <!-- <th width="5.5%" class="text-right">Salary</th> -->
                                        <th width="5.5%" class="text-right">Salary</th>
                                        <!-- <th width="5.5%" class="text-right">TA</th> -->
                                        <th width="3.5%" class="text-right">AB</th>
                                        <th width="3.5%" class="text-right">HF</th>
                                        <th width="3.5%" class="text-right">SUN</th>
                                        <th width="3.5%" class="text-right">HO</th>
                                        <th width="3.5%" class="text-right">Days</th>
                                        <!-- <th width="3.5%" class="text-right">OT/Nights</th> -->
                                        <th width="3.5%" class="text-right">PD</th>
                                        <th width="6%"   class="text-right">Advance</th>
                                        <!-- <th width="6%" class="text-right">Food</th> -->
                                        <th width="5%"   class="text-right">PR</th>
                                    </tr>
                                </thead>

                                <tbody class="tabledata tbody">
                                    <?php
                                    if ($addflag != 1) {
                                        $gettran = mysqli_query($conn, "SELECT * FROM $trandb WHERE vouchno = '$get[vouchno]' ORDER BY `srno`")or die(mysqli_error($conn));
                                        $ctr = 0;
                                        while ($showtran = mysqli_fetch_array($gettran)) {
                                            $fpresent = $showtran['pr'];

                                            $getemp = mysqli_query($conn, "SELECT * FROM accmast  WHERE accode = '$showtran[accode]' ")or die(mysqli_error($conn));
                                            $semp = mysqli_fetch_array($getemp);
                                            $getdep = mysqli_query($conn, "SELECT code,name FROM dept  WHERE code = '$semp[deptcode]' ")or die(mysqli_error($conn));
                                            $fetdep = mysqli_fetch_array($getdep);
                                            $deptcode = $showtran['deptcode'];
                                            $deptname = $fetdep['name'];
                                            $accode = $showtran['accode'];
                                            $mon = $showtran['mon'];


                                            Echo "<tr>";
                                            Echo "<td><input type='text' name='srno_$ctr'     class='form-control form' value='$showtran[srno]' readonly></td>";
                                            Echo "<td><input type='text' name='deptname_$ctr'   class='form-control form' value='$fetdep[name]' readonly></td>";
                                            Echo "<td><input type='text' name='accode_$ctr'   class='form-control form' value='$semp[name]' readonly>
                                                      <input type='hidden' name='salary_$ctr'   class='form-control form text-right' value='$showtran[salary]' readonly></td>";
                                            Echo "<td><input type='text' name='basic_$ctr'    class='form-control form text-right basic' value='$showtran[basic]' readonly>
                                                      <input type='hidden' name='da_$ctr'       class='form-control form text-right da' value='$showtran[da]' readonly>
                                                      <input type='hidden' name='hra_$ctr'      class='form-control form text-right hra' value='$showtran[hra]' readonly>
                                                      <input type='hidden' name='ta_$ctr'       class='form-control form text-right ta' value='$showtran[ta]' readonly>
                                                      <input type='hidden' name='oa_$ctr'       class='form-control form text-right oa' value='$showtran[oa]' readonly></td>";
                                            Echo "<td><input type='text' name='ab_$ctr'       class='form-control form text-right ab' value='$showtran[ab]'></td>";
                                            Echo "<td><input type='text' name='hf_$ctr'       class='form-control form text-right hf' value='$showtran[hf]'></td>";
                                            Echo "<td><input type='text' name='sun_$ctr'      class='form-control form text-right sun' value='$showtran[sun]' ></td>";
                                            Echo "<td><input type='text' name='ho_$ctr'       class='form-control form text-right ho' value='$showtran[ho]' ></td>";
                                            Echo "<td><input type='text' name='pr_$ctr'       class='form-control form text-right pr' value='$showtran[pr]' readonly>
                                                    <input type='hidden' name='totpr_$ctr'    class='form-control form text-right totpr' value='$fpresent' readonly>
                                                    <input type='hidden' name='ot_$ctr'       class='form-control form text-right ot' value='$showtran[ot]'>
                                                    <input type='hidden' name='othrs_$ctr'    class='form-control form text-right othrs' value='$showtran[othrs]'></td>";
                                            Echo "<td><input type='text' name='pd_$ctr'       class='form-control form text-right pd' value='$showtran[pd]'></td>";
                                            Echo "<td><input type='text' name='advance_$ctr'  class='form-control form text-right advance' value='$showtran[advance]'>
                                                      <input type='hidden' name='food_$ctr'  class='form-control form text-right food' value='$showtran[food]' ></td>";
                                            Echo "<td><input type='text' name='totdays_$ctr'  class='form-control form text-right totdays' value='$showtran[totdays]' readonly></td>";
                                            $ctr++;
                                        }
                                    }
                                    echo "<input type = 'hidden' id = 'items' name = 'items' value = '$ctr'>";
                                    ?>
                                </tbody>
                            </table>
                        </div>

                    </div>

                    <div class="col-lg-12 col-xs-12 pad">

                        <div class="col-lg-3 pad text-right">
                        </div>

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

                        <div class="col-lg-3 pad">
                            <input type="button" id="finishTab" class="finish tab btn btn-primary form mySearchTr" style="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">Department Name</option>
                                <option value="2">Month</option>
                                <option value="3">Year</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">P.O No</option>
                                <option value="2">Date</option>
                                <option value="3">Supplier</option>
                                <option value="4">Delivery Date</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 class="mySearchTr">
                                    <th style="color: white;"width="5%">Department Name</th>
                                    <th style="color: white;"width="7%">Month</th>
                                    <th style="color: white;"width="7%">Year</th>
                                    <th style="color: white;"width="1%">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>
    <?php include("includes/linksfoot.php"); ?>
</body>
</html>
