<?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 = 'Attendance Entry';
$maindb = 'attend';
$keyfld = "vouchno";
$file = 'attendance.php';

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

mysqli_select_db($conn, $zdb);

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

    $vouchdt = $_POST["vouchdt"];
    $timestamp = strtotime($vouchdt);
    $y = date("Y", $timestamp);
    $dept = $_POST["dept"];
    $proces = $_POST["proces"];
    $count = $_POST["items"];
    $totmins = 0;
    for ($i = 0; $i < $count; $i++) {
        $accode = $_POST['accode_' . $i];
        $intime = $_POST['intime_' . $i];
        $outtime = $_POST['outtime_' . $i];
        $nohrs = $_POST['nohrs_' . $i];
        $nomins = $_POST['nomins_' . $i];
        $totmins = ($nohrs * 60) + $nomins;
        $gtct = mysqli_query($conn, "SELECT custcode FROM accmast WHERE accode = '$accode'") or die(mysqli_error($conn));
        $ftct = mysqli_fetch_array($gtct);
        $custcode = $ftct['custcode'];
        mysqli_query($conn, "DELETE FROM `$maindb` WHERE accode = '$accode' and `attdate` = '$vouchdt'") or die(mysqli_error($conn));
        if (!empty($accode) && !empty($intime) && !empty($outtime)) {
            mysqli_query($conn, "INSERT INTO `$maindb` (`accode`, `empcode`, `dept`, `attdate`, `intime`, `outtime`, `noofhrs`, `noofmin`, `totmin`,yearno, proces)
                                     VALUES ('$accode', '$custcode', '$dept', '$vouchdt', '$intime', '$outtime', '$nohrs', '$nomins', '$totmins','$y','$proces')") or die(mysqli_error($conn));
        }
    }

    insmess();
    header("refresh: 1; $file");
}

include("includes/linkshead.php");
?>
<script src="MyScript/attendance.js?a=<?= mt_rand() ?>" type="text/javascript"></script>
</head>
<form method='POST' action="<?= $action; ?>">
    <!-- Default box -->
    <!--<div>-->
    <div class="col-lg-7 col-md-12 col-sm-12 col-xs-12  pad">
        <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">Date</label>
                    </div>
                    <div class="col-lg-3 col-md-3 col-sm-5 col-xs-5 pad">
                        <input type="date" id="mtodt" name="vouchdt" class="form-control vouchdt" value="<?php echo date("Y-m-d"); ?>" />  
                    </div>
                </div>

                <div class="row">
                    <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                        <label class="label-control">Department</label>
                    </div>
                    <div class="col-lg-6 col-md-6 col-sm-5 col-xs-5 pad">
                        <select name="dept" class="form-control select2 dept" required>
                            <option value=""></option>
                            <?php
                            $getdept = mysqli_query($conn, "SELECT * FROM `dept` ORDER BY `name`") or die(mysqli_error($conn));
                            while ($fetdept = mysqli_fetch_array($getdept)) {
                                echo "<option value='$fetdept[code]'>$fetdept[name]</option>";
                            }
                            ?>
                        </select>
                    </div>
                </div>

            </div>

            <div class="panel-footer">
                <div class="text-center pad">
                    <input type="button" class="empproceed btn btn-primary" value="Proceed">
                </div>                       
            </div>


            <div class="panel-body daily_attend">

            </div>
        </div>
    </div>
    <!--</div>-->
</form>
<?php include("includes/linksfoot.php"); ?>
</body>
</html>
