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

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

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

$deptcode = $_REQUEST["deptcode"];
$divcode = $_REQUEST["divcode"];
$monname = $_REQUEST["monname"];
$yearno = $_REQUEST["yearno"];
$vouchno = $_REQUEST["vouchno"];
$addflag = $_REQUEST["addflag"];

$daysinmonth = getDaysInMonth($monname, $yearno);


if ($deptcode != '' && $monname != '') {
    if ($addflag == 1) {
        $getacc = mysqli_query($conn, "SELECT accode,deptcode, name, total as salary, basic, da, hra, ta, oa, `p_tax`, `pfund`, `esic` 
                                       FROM accmast WHERE deptcode = '$deptcode' and active = '1' ORDER BY deptcode,`custcode`")or die(mysqli_error($conn));
//        echo 1;
    } 

//    print table($getacc);
//    die();
    $pctr = mysqli_num_rows($getacc);
    $ctr = 0;
    $cctr = 1;

    while ($acc = mysqli_fetch_array($getacc)) {

        $getnights = mysqli_query($conn, "SELECT sum(nonights) as nights  from nighttran where karigar = '$acc[accode]' and approved != '' and monno = '$monname' and yearno ='$yearno' ")or die(mysqli_error($conn));
        $shonights = mysqli_fetch_array($getnights);
        if ($shonights['nights'] > 0) {
            $nights = $shonights['nights'];
        } else {
            $nights = 0;
        }

        $getadv = mysqli_query($conn, "SELECT sum(amount) as adv  from cashgive where accode = '$acc[accode]' and MONTH(vouchdt) = '$monname' and YEAR(vouchdt) ='$yearno'")or die(mysqli_error($conn));
        $shoadv = mysqli_fetch_array($getadv);
   
        if ($shoadv['adv'] > 0) {
            $advance = $shoadv['adv'];
        } else {
            $advance = 0;
        }

        $getdep = mysqli_query($conn, "SELECT code,name FROM dept  WHERE code = '$acc[deptcode]' ")or die(mysqli_error($conn));
        $fetdep = mysqli_fetch_array($getdep);
        $deptcode = $acc['code'];
        $deptname = $fetdep['name'];




        // $getpresent = mysqli_query($conn, "SELECT `accode` from `attend`  where `accode` = '$acc[accode]' 
        //                                        and month(`attdate`) = '$monname' and yearno = '$yearno' and noofhrs > 5 group by attdate ")or die(mysqli_error($conn));

        // $fpresent = mysqli_num_rows($getpresent);

        // $gethalf = mysqli_query($conn, "SELECT `accode` from `attend`  where `accode` = '$acc[accode]' 
        //                                        and month(`attdate`) = '$monname' and yearno = '$yearno' and  noofhrs < 5 group by attdate ")or die(mysqli_error($conn));

        // $fhalf = mysqli_num_rows($gethalf);
//        } else {
//            $fpresent = $acc['totdays'];
//        }

        // if ($fpresent > 0) {

            echo "<tr>";
//            echo "<td align='center' valign='top'><span      class='remove fa fa-times' name='remove'  value='remove' $hide_var></span></td>";
            Echo "<td><input type='text' name='srno_$ctr'    class='form-control form'             value='$cctr'        readonly></td>";
            Echo "<td><input type='text' name='deptname_$ctr'  class='form-control form'             value='$deptname'   readonly></td>";
            Echo "<td><input type='text' name='accode_$ctr'  class='form-control form'             value='$acc[name]'   readonly></td>";
            Echo "<td><input type='text' name='basic_$ctr'   class='form-control form text-right'  value='$acc[basic]'  readonly>
                     <input type='hidden' name='salary_$ctr'  class='form-control form text-right'  value='$acc[salary]' readonly>
                     <input type='hidden' name='da_$ctr'      class='form-control form text-right'  value='$acc[da]'     readonly>
                     <input type='hidden' name='hra_$ctr'     class='form-control form text-right'  value='$acc[hra]'    readonly>
                     <input type='hidden' name='ta_$ctr'      class='form-control form text-right'  value='$acc[ta]'     readonly>
                     <input type='hidden' name='oa_$ctr'      class='form-control form text-right'  value='$acc[oa]'     readonly></td>";
            Echo "<td><input type='text' name='ab_$ctr'      class='form-control form text-right ab'  value='0' ></td>";
            Echo "<td><input type='text' name='hf_$ctr'      class='form-control form text-right hf'  value='0' ></td>";
            Echo "<td><input type='text' name='sun_$ctr'     class='form-control form text-right sun' value='0'></td>";
            Echo "<td><input type='text' name='ho_$ctr'      class='form-control form text-right ho'  value='0'></td>";
            Echo "<td><input type='text' name='pr_$ctr'      class='form-control form text-right pr'  value='$daysinmonth' >
                      <input type='hidden' name='totpr_$ctr'   class='form-control form text-right totpr'  value='0'>
                      <input type='hidden' name='ot_$ctr'      class='form-control form text-right ot'  value='$nights'>
                      <input type='hidden' name='othrs_$ctr'   class='form-control form text-right othrs'  value='0'></td>";
            Echo "<td><input type='text' name='pd_$ctr'      class='form-control form text-right pd'  value='0'></td>";
            Echo "<td><input type='text' name='advance_$ctr' class='form-control form text-right advance' value='$advance'>
                      <input type='hidden' name='food_$ctr' class='form-control form text-right foodamt' value='0'></td>";
            Echo "<td><input type='text' name='totdays_$ctr' class='form-control form text-right totdays' value='0' readonly>
                <input type='hidden' name='p_tax_$ctr'   class='form-control form text-right p_tax' value='$acc[p_tax]'>
                <input type='hidden' name='pfund_$ctr'    class='form-control form text-right pfund' value='$acc[pfund]'>
                <input type='hidden' name='esic_$ctr'     class='form-control form text-right esic' value='$acc[esic]'></td>";
            Echo "</tr>";

            $ctr++;
            $cctr++;
        // }
    }
}
echo "<input type='hidden' id='items' name='items' value='$pctr'>  ";
?>