<?php

require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include ('includes/function.php');
$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn,$zdb);
$admin = $_SESSION["admin_" . $dbname];

if (isset($_REQUEST["frmdt"])) {

    $frmdt = $_REQUEST["frmdt"];
    $todt = $_REQUEST["todt"];
    $staff = $_REQUEST["staff"];
    $fmonth = date("F", strtotime($frmdt));
    $monthday = $todt - $frmdt + 1;
    $staffcond = "";
    if ($staff != '') {
        $staffcond = "and b.code = '$staff'";
    }


//    $gattend = mysqli_query($conn,"SELECT zyear as year, m.monno as month, mfrdate as vouchdt, a.staff, b.name as staffname, ;
//                            b.desig, b.salary, b.salhrs, b.hrsperday, b.saltype, coun(*) as daypres, sum(a.hours) as hrspres, ;
//                            0000000.00 as monsal, sum(a.advance) as advance, 0000000.00 as netsal, ;
//                            spac(1) as xkey from attend a, staff b ;
//                            where a.staff = b.srno and a.vouchdt >= mfrdate and a.vouchdt <= mtodate and b.active = 1  ;
//                            &pcond grou by a.staff orde by b.name into dbf &saltmp")or die(mysqli_error($conn));
    $ctr = 0;
    $get_salproc = mysqli_query($conn,"SELECT a.staff, b.name as staffname, b.desig, b.salary, b.salaryph, b.day,  b.type, COUNT(*) as daypres,  
                                sum(a.hours) as hrspres, 0000000.00 as monsal, SUM(a.advance) as advance, 0000000.00 as netsal
                                FROM attend a, staff b WHERE a.staff = b.code and a.vouchdt >= '$frmdt' and a.vouchdt <= '$todt'
                                and b.active = 1 $staffcond GROUP BY a.staff ORDER BY b.name")or die(mysqli_error($conn));
    while ($salproc = mysqli_fetch_array($get_salproc)) {

        if ($salproc['saltype'] == "M") {
            $salhrs = round(($salproc['salary'] / $monthday / $salproc['day']), 2);
            $monsal = $salproc['hrspres'] * ($salproc['salary'] / $monthday / $salproc['hrspres']);
            $netsal = $monsal - $salproc['advance'];
        } else {
            $salhrs = round(($salproc['salary'] / $salproc['day']), 2);
            $monsal = $salproc['hrspres'] * $salproc['day'];
            $netsal = $monsal - $salproc['advance'];
        }

        $ctr++;
        Echo "<tr>";
        Echo "<td><input type='text' class='form-control form text-right srno' name='srno_$ctr'      value='$ctr' readonly tabindex='-1'></td>";
        Echo "<td><input type='text' class='form-control form text-right staffname' name='staffname_$ctr'      value='$salproc[staffname]' readonly tabindex='-1'></td>";
        Echo "<td><input type='text' class='form-control form text-right desig' name='desig_$ctr'      value='$salproc[desig]' readonly tabindex='-1'></td>";
        Echo "<td><input type='number' class='form-control form text-right salary' name='salary_$ctr'      value='$salproc[salary]' readonly tabindex='-1'></td>";
        Echo "<td><input type='number' class='form-control form text-right daypres' name='daypres_$ctr'      value='$salproc[daypres]' readonly tabindex='-1'></td>";
        Echo "<td><input type='number' class='form-control form text-right hrspres' name='hrspers_$ctr'      value='$salproc[hrspres]' readonly tabindex='-1'></td>";
        Echo "<td><input type='number' class='form-control form text-right' name='salhrs_$ctr'      value='$salhrs' readonly tabindex='-1'></td>";
        Echo "<td><input type='number' class='form-control form text-right monsal' name='monsal_$ctr'      value='$monsal' readonly tabindex='-1'></td>";
        Echo "<td><input type='number' class='form-control form text-right advance' name='advance_$ctr'      value='$salproc[advance]'></td>";
        Echo "<td><input type='number' class='form-control form text-right netsal' name='netsal_$ctr'      value='$netsal' readonly tabindex='-1'></td>";
        Echo "</tr>";
    }

    $ctr++;
    Echo "<tr style='display: none;'>";
    Echo "<td colspan='20'>
            <input type='text' class='form-control form text-right year'      name='year'      value='' readonly tabindex='-1'>
            <input type='text' class='form-control form text-right monno'      name='monno'      value='' readonly tabindex='-1'>
            <input type='text' class='form-control form text-right items'      name='items'      value='$ctr' readonly tabindex='-1'>
         </td>";
    Echo "</tr>";
} else {
    echo "ERROR";
}
?>