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

$formname = "Salary Process";
$action = "perfreg_fr.php";

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

$zyear = right($zdb, 4);
$mfrdate = getzdates($zyear, 'S');
$mtodate = getzdates($zyear, 'E');
$zroot = $_SESSION["zdir"];
$salary = "salary";

$myyear = $_SESSION[$myyear];
mysqli_select_db($conn,$zdb);

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

    $count = $_POST["items"];
    $year = $_POST["year"];
    $monno = $_POST["monno"];
    $staff = $_POST["staff"];
    $vouchdt = $_POST["frmdt"];

    $staffcond = "";
    if ($staff != '') {
        $staffcond = "and staff='$staff'";
    }

    mysqli_query($conn,"DELETE FROM `$salary` WHERE `year`='$yearno' and `monno`='$monno' $staffcond")or die(mysqli_error($conn));
    for ($i = 0; $i < $count; $i++) {
        $srno = $_POST["srno_$i"];
        $staffname = $_POST["staffname_$i"];
        $desig = $_POST["desig_$i"];
        $salary = $_POST["salary_$i"];
        $daypres = $_POST["daypres_$i"];
        $hrspers = $_POST["hrspers_$i"];
        $salhrs = $_POST["salhrs_$i"];
        $monsal = $_POST["monsal_$i"];
        $advance = $_POST["advance_$i"];
        $netsal = $_POST["netsal_$i"];

        mysqli_query($conn,"INSERT INTO `$salary` (`srl`, `year`, `vouchdt`, `month`, `1staff`, `desig`,  `salary`, `daypres`, 
                                            `hrspres`, `monsal`, `advance`, `netsal`, `staffname`, `salhrs`, `hrsperday`, `saltype`)
                                     VALUES('$i', '$year','$vouchdt', '$monno', '$staffname', '$desig', '$salary','$daypres','$hrspers','$salhrs','$monsal',
                                            '$advance', '$netsal')") or die(mysqli_error($conn));
    }
    
}

include("includes/linkshead.php");
?>
<style>.td{font-size: 20px;}</style>
<script src="MyScript/salproc.js" type="text/javascript"></script>
</head>
<body>
    <form action='' method='POST'>
        <!-- Default box -->
        <div class="row">
            <div class="col-lg-10">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title"><?php Echo $formname; ?></h3>
                        <div class="actions pull-right">
                            <i class="fa fa-chevron-down"></i>
                            <i class="fa fa-times"></i>
                        </div>
                    </div>
                    <div class="panel-body sc">
                        <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">Staff</label>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-6 col-xs-6 pad">
                                <select class='staff form-control select2' name='staff'>
                                    <option value=""></option>
                                    <?php
//                                    $mown = mysqli_query($conn,"SELECT code, `user` FROM `passmain` WHERE `grpcode`='010' ORDER BY `name`") or die(mysqli_error($conn));
                                    $mown = mysqli_query($conn,"SELECT `code`, `name` FROM `staff` WHERE `active` = 1 ORDER BY `name`") or die(mysqli_error($conn));
                                    while ($trow = mysqli_fetch_array($mown)) {
                                        echo "<option value='$trow[code]'>$trow[name]</option>";
                                    }
                                    ?>
                                </select>
                            </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">From Date</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-3 col-xs-3 pad">
                                <input type="date" id="mfrdt" name="frmdt" class="form-control frmdt" value="<?php Echo $mfrdate; ?>" />
                            </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">To Date</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-3 col-xs-3 pad">
                                <input type="date" id="mfrdt" name="todt" class="form-control todt" value="<?php Echo $mtodate; ?>" />
                            </div>
                        </div>
                    </div>

                    <div class="panel-footer text-center sc">
                        <button type="button" name="salproc" class="btn btn-success salproc">Process Salary</button>
                    </div>
                    <div class="panel-body fr" style="display: none;">
                        <div class="table-responsive">
                            <table id="" class="table table-striped table-condensed table-bordered" width="100%">
                                <thead>
                                    <tr class="mySearchTr" style="background-color: #64CCFC">
                                        <th style="width:4%">Srno</th>
                                        <th style="width:10%">Staff</th>
                                        <th style="width:10%">Designation</th>
                                        <th style="width:10%">Salary</th>
                                        <th style="width:4%">Day</th>
                                        <th style="width:4%">Hours</th>
                                        <th style="width:8%">Sal/Hrs.</th>
                                        <th style="width:8%">Month Salary</th>
                                        <th style="width:15%">Advance</th>
                                        <th style="width:10%">Net Salary</th>
                                    </tr>
                                </thead>
                                <tbody class="sTbody">
                                </tbody>
                            </table>
                        </div>
                    </div>
                    <div class="panel-footer text-center fr" style="display: none;">
                        <input type="submit" name="submit" class="btn mySearchTr" style="color: white;" value="Save">
                    </div>
                </div>
            </div>
        </div>
    </form>
    <?php include("includes/linksfoot.php"); ?>
</body>
</html>