<?php include("parent/callMe.php"); ?>
<?php  include("sessioncheck.php"); ?>
<?php

$m->where("session_id", $_REQUEST['session']);
$getYearAgainstSession = $m->get("edu_session_config");

$getYear=(explode("-",$getYearAgainstSession[0]['session_year']));

  $branshID=$_SESSION['branch'];

$sql = " select designation_id,department_id,user_id,user_salary,user_fname,user_lname,user_security,advance_salary,emp_trav,emp_reimbersement,emp_other from  edu_user_config where branch_id='$branshID' and status='0' ";

if($_REQUEST['month']!=NULL){

 if($_REQUEST['designation']=="All"){
      $sql;
    } else {
    $sql .= " AND designation_id = '" .$_REQUEST['designation']. "' ";

    }

  if($_REQUEST['department']=="All"){
      $sql;

    } else {

    $sql .= " AND department_id = '" .$_REQUEST['department']. "' ";

    }  

  }

  $sql .= " order by user_id desc";

//print_r($sql);

$WorkAssignListings = $m->query($sql);


$default;
    
    if($_GET["month"]!=NULL ){
      $default= $_GET["month"];
      $default = sprintf("%02d", $default);
    }
    else{
     /* $default= date("m");*/
    }

 $sessionIDs;
 
  if($_GET["session"]!=NULL ){

      $sessionIDs= $_GET["session"];
    }
    else{
      $sessionIDs= $_SESSION['current_session'];
    }
  

        $m->where("Month_Id", $default);
        $month_name=$m->get("eduportal_month_config");


$salaries;

if(isset($_REQUEST['session']) && isset($_REQUEST['month'])){

 $m->where("branch_id", $_SESSION['branch']);
 $m->where("session_id", $_REQUEST['session']);
 $m->where("month_id", $default);

 $salaries = $m->get("employee_payroll"); 
}
 
     ?>


<!DOCTYPE html>
<html lang="en">
  <head>
    
    <title>Creat Employees PayRoll | EduPortal</title>
    <?php include('RootMe/css_links.php');?>
   
  <style>

.cellcolor{background-color:lightgray;border:1px solid black;}


.center {
  margin-left: auto; 
  margin-right: auto;
}

</style>
  </head>
  




  <body class="nav-md">
    <div class="container body">
      <div class="main_container">
        <div class="">
     
            <?php //include('RootMe/sidebar.php');?>

              <?php //include('RootMe/header.php');?>
     
        <!-- page content -->
          <div class="" role="main">
            <br>
             <a style="margin-left: 10px;" href="<?php echo $_SESSION['dashboard'];?>">Dashboard </a> &nbsp; <i style="" class="fa fa-angle-double-right"></i>  &nbsp; 
            Create PayRoll
          <br><br>
          <div class="" style="background-color: white;">
           
  <section class="add_sub_agent" id="table_sub_agent">
  <div class="container">

 <div class="" style="margin-top:10px;">
        
     
      <div class="clearfix"></div>
    </div>




  <form class="form-style-7">

    
  <div style="overflow-x:auto;">
            
              
              <div class="col-md-2 col-sm-12 col-xs-12" style="padding: 8px;">
                <div class="form-group ">
                  <label class="required">Year</label>
                    <select name="session" class="form-control"  style="height:40px;">

                               
                        <?php

                       $match;
                        if(!isset($_REQUEST['session'])){
                          $match=$_SESSION['current_session'];
                        }else { $match=$_REQUEST['session']; }
                       
                                 
                        $WorkAssignList = $m->get("edu_session_config");
                        foreach($WorkAssignList as $WorkAssignListVal)
                        { 

                        $Year=(explode("-",$WorkAssignListVal['session_year']));
                          ?>

                           <option <?php if($match==$WorkAssignListVal['session_id']){echo 'selected';}?> value="<?php echo $WorkAssignListVal['session_id']; ?>"><?php echo $Year[0]; ?></option>


                                <?php
                        } ?>

                    </select>
                </div>
              </div>


               <div class="col-md-2 col-sm-12 col-xs-12" style="padding: 8px;">
                <div class="form-group ">
                <label class="required">Month</label>
                <select style="height:40px;" name="month" id="month" class="form-control"  required>

                              <option value="">Select Month</option>
                        

                        <?php
                        $WorkAssignList = $m->get("eduportal_month_config");
                        foreach($WorkAssignList as $WorkAssignListVal)
                        { ?>

                           <option <?php if($default==$WorkAssignListVal['Month_Id']){echo 'selected';}?> value="<?php echo $WorkAssignListVal['Month_Id']; ?>"><?php echo $WorkAssignListVal['Month_Name']; ?></option>


                                <?php
                        } ?>

                    </select>
                </div>
              </div>


              <div class="col-md-2 col-sm-12 col-xs-12" style="padding: 8px;">
                <div class="form-group ">
                <label class="required">Designation</label>
                <select style="height:40px;" name="designation" id="designation" class="form-control"  required>

                              <option value="All">All</option>
                        

                        <?php
                  $query="select * from eduportal_designation_config where designation_id!='14' and designation_id!='15'";
                  $WorkAssignList = $m->query($query);
                  foreach($WorkAssignList as $WorkAssignListVal)
                  { ?>

                         <option <?php if($_REQUEST['designation']==$WorkAssignListVal['designation_id']){echo "selected";} ?>  value="<?php echo $WorkAssignListVal['designation_id']; ?>"><?php echo $WorkAssignListVal['designation_name']; ?></option>


                  <?php } ?>

                    </select>
                </div>
              </div>


              <div class="col-md-2 col-sm-12 col-xs-12" style="padding: 8px;">
                <div class="form-group ">
                <label class="required">Department</label>
                <select style="height:40px;" name="department" id="department" class="form-control"  required>

                              <option value="All">All</option>
                        

                        <?php
                  $query="select * from eduportal_hrm_department_config";
                  $Departments = $m->query($query);
                  foreach($Departments as $Department)
                  { ?>

                         <option <?php if($_REQUEST['department']==$Department['department_id']){echo "selected";} ?>  value="<?php echo $Department['department_id']; ?>"><?php echo $Department['department_name']; ?></option>


                  <?php } ?>

                    </select>
                </div>
              </div>
 

              <div class="col-md-2 " style="padding:2px;">

              <button id="send" type="submit" class="btn btn-primary" style="margin-top: 34px;"> 
                Search</button>
    
            
            <a href="dashboard.php" id="subuser" class="btn btn-success" style="margin-top: 34px;">Dashboard </a>

               <!--  <?php if (isset($_REQUEST['month'])) { ?>
                  
                  <a href="payroll_report.php" id="subuser" class="btn btn-primary" style="margin-top: 34px;">Print Report </a>

               <?php }  ?>  -->            
                
                

              </div>
</form>

  


  <form action="<?php echo DOMAIN.AgentDirectory;?>parent/InsertMe.php" method="post">
  

<?php 
  if(isset($_REQUEST['emp'])){
?>
  
    <input type="hidden" name="action"  value="EditEmployeeSalary">  

    <?php } else {?>

  <input type="hidden" name="action" value="AddEmployeeSalary">

<?php } ?>


  <input type="hidden" id="session"  name="session" value="<?php echo $_GET['session']; ?>" />
  <input type="hidden" id="date"  name="month" value="<?php echo $_GET['month']; ?>" />

  <?php 


if(isset($_GET['month'])&& (isset($_REQUEST['emp'])) ){
   
  ?>
  
<input type="submit" name="submit" value="Update Salaries" class="pull-right" />


<?php  

} else if(isset($_GET['month']) ){ ?>

   <input type="submit" name="submit" value="Save Salaries" class=" btn btn-primary" style="margin-top: 36px;" />
<?php  }  ?>
  



  <?php 

if(isset($_GET['month']) ){
   
  ?>

  <table data-page-length='100' class="table table-striped table-bordered center" style="width:98%;font-size:12px;">
          
          <thead>
          <caption style="font-size:20px;font-weight:bold;"><center>Employee PayRoll</center></caption>
            <tr>
              <th colspan="2" style="font-size:14px;"><center>Emp Detail</center></th>
              
                
                <th colspan="5" style="font-size:14px;background-color:#00afef;color: white;"><center>Deductions</center></th>
                <th colspan="5" style="font-size:14px;background-color:#00afef;color: white;"><center>Allowances</center></th>
                <th colspan="3"></th>
                                
            </tr>

            <tr>
                <th width="" >Sr.</th>
                <th width="" >Emp Name</th>
                <th width="" class="cellcolor">Basic sal</th>
                <th width="" >P.Bal</th>   
                <th width="" >Security</th>
                <th width="" >Advance</th>             
                <th width="" >Other</th>
                <th width="" >Absent</th>                               
                <th width="" class="cellcolor" >Total</th>
                <th width="" >Absent</th>
                <th width="" >Traveling</th>
                <th width="" >Reimb..</th>
                <th width="" >Other</th>
                <th width="" class="cellcolor">Total</th> 
                <th width="" class="cellcolor">Net Total</th>

            </tr>
        </thead>
        <tbody>
     
     <?php
     
     $count=1; 

      foreach($WorkAssignListings as $WorkAssignListVal)
      { 

        $m->where("designation_id", $WorkAssignListVal['designation_id']);
        $designation=$m->get("eduportal_designation_config");

        $m->where("department_id", $WorkAssignListVal['department_id']);
        $department=$m->get("eduportal_hrm_department_config");

        $m->where("branch_id", $_SESSION['branch']);
        $m->where("session_id",$sessionIDs);
        $m->where("user_id", $WorkAssignListVal['user_id']);
        $m->where("month_id", $default);

        $param = array('other_deductions','total_deduction','leave_encashment','total_allowances');
       $payrolldata = $m->get("employee_payroll",null,$param);

       // $payrolldata=$m->get("employee_payroll");


        if(count($payrolldata)>0){
          continue;
        }


        $sal_id= $WorkAssignListVal['user_id'];
        $Bid=$_SESSION['branch'];
        $qr="SELECT paid_status,net_total,remaining FROM employee_payroll WHERE user_id = '$sal_id' and branch_id='$Bid' ORDER BY payroll_id DESC LIMIT 1";
        //print_r($qr);
        $previous_data=$m->query($qr);

        $preBal;
        if ($previous_data[0]['paid_status']==0) {
          
            $preBal=$previous_data[0]['net_total'];
        } else {
            $preBal=$previous_data[0]['remaining'];
        }

        // Start Absent Code

        $branch=$_SESSION['branch'];
        $date=$getYear[0]."-".$default;
        $user=$WorkAssignListVal['user_id'];

        $qr1="SELECT count(*) as total FROM eduportal_hrm_emp_attendance WHERE attendance='A'  and branch_id='$branch' and emp_id='$user' and date like '$date%' ";

       //print_r($qr1);
         $result=$m->query($qr1);
         
         if($result[0]['total']>0 && $brid[0]['Absent_deduction']==1){
     
          $emp_absent=$result[0]['total'];
          $PerDaySalary=$WorkAssignListVal['user_salary']/30;
          $empLeaveDec= $emp_absent * $PerDaySalary;
          } else { $empLeaveDec=""; }

         // End Absent Code

         // Start Leave Code

           $qr3="SELECT count(*) as total FROM eduportal_hrm_emp_attendance WHERE attendance='L'  and branch_id='$branch' and emp_id='$user' and date like '$date%' ";

       
         $result2=$m->query($qr3);

         $emp_Leave_absentPopup=$result2[0]['total'];

         $totalLeaveNumbers=$result2[0]['total']-$brid[0]['no_of_leaves'];
         
         if($totalLeaveNumbers>0 && $brid[0]['Leave_deduction']==1) {
     
          $emp_Leave_absent=$totalLeaveNumbers;
          $PerDaySalary2=$WorkAssignListVal['user_salary']/30;
          $perdayPercent=$PerDaySalary2*$brid[0]['Leave_Percentage']/100;
          $empLeaveDec2= $emp_Leave_absent * $perdayPercent;

         } else { $empLeaveDec2=""; }

// End Leave Code


// Start Late Arrival Code

         $qr4="SELECT count(*) as total FROM eduportal_hrm_emp_attendance WHERE attendance='LA'  and branch_id='$branch' and emp_id='$user' and date like '$date%' ";

       
         $result4=$m->query($qr4);

         $emp_Late_arivalPopup=$result4[0]['total'];

         $totalLateArrivalNumbers=$result4[0]['total']-$brid[0]['no_of_late_arrival'];
         
         if($totalLateArrivalNumbers>0 && $brid[0]['late_arrival_deduction']==1){
     
          $emp_Late_Arival=$totalLateArrivalNumbers;
          $PerDaySalary3=$WorkAssignListVal['user_salary']/30;
          $perdayLateArrivalPercent=$PerDaySalary3*$brid[0]['late_arrival_percentage']/100;
          $empLeaveDec3= $emp_Late_Arival * $perdayLateArrivalPercent;

         } else { $empLeaveDec3=""; }

// End Late Arrival Code

$totalLeaveDeduction=$empLeaveDec+$empLeaveDec2+$empLeaveDec3;
      ?>


<tr>
        
     
  <td><?php echo $count++;?></td>
  <td style="padding:5px;">
    <a data-toggle="modal" href="#" data-target="#absentdetail<?php echo $WorkAssignListVal['user_id'];?>">
    <?php echo $WorkAssignListVal['user_fname'];?>
    <input name="employee[]" id="employee[]" type="hidden" value="<?php echo $WorkAssignListVal['user_id'];?>">
    </a>

     <!-- Modal -->
      <div id="absentdetail<?php echo $WorkAssignListVal['user_id'];?>" class="modal fade" role="dialog">
        <div class="modal-dialog">

          <!-- Modal content-->
          <div class="modal-content" style="width: 900px;margin-left: -25%;">
            <div class="modal-header">
              <button type="button" class="close" data-dismiss="modal">&times;</button>
              <h4 class="modal-title"> Employee Leave Details  </h4>
            </div>


            <div class="modal-body">



               <p> <?php echo $WorkAssignListVal['user_fname'];?>  <?php echo $WorkAssignListVal['user_lname'];?>  </b><br>
                (Number Of Absent:-  <b><?php echo $emp_absent;?></b>) <br>
                (Number Of Leaves:-  <b><?php echo $emp_Leave_absentPopup;?></b>)
              </p>


            </div>


          <br>
     

            <div class="modal-footer">

             <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>


            </div>

          </div>

        </div>
      </div>

      <!-- ending modal -->        
                     
  </td>

      
      
      <td class="cellcolor" style="padding:6px 0px 0px 0px;"> <input type="text" id="basic<?php echo $WorkAssignListVal['user_id'];?>" onkeyup="net_totals(<?php echo $WorkAssignListVal['user_id'];?>)" name="basic_salary[]"   value="<?php echo $WorkAssignListVal['user_salary'];?>"  style="width:55px;" > </td>

      <td style="padding:6px 0px 0px 0px;"> <input type="text" id="previous<?php echo $WorkAssignListVal['user_id'];?>" 
       onkeyup="net_totals(<?php echo $WorkAssignListVal['user_id'];?>)" name="pre_balance[]"value="<?php echo $preBal;?>"style="width:55px;" > </td>

       <td> <input type="text"autocomplete="off" id="ded_security<?php echo $WorkAssignListVal['user_id'];?>" name="security_deductions[]" value="<?php echo $WorkAssignListVal['user_security'];?>"
       onkeyup="total_deductions(<?php echo $WorkAssignListVal['user_id'];?>)" style="width:55px;" > </td>

       <td> <input type="text"autocomplete="off" id="ded_advance<?php echo $WorkAssignListVal['user_id'];?>" name="advance_deductions[]" value="<?php echo $WorkAssignListVal['advance_salary'];?>"
       onkeyup="total_deductions(<?php echo $WorkAssignListVal['user_id'];?>)" style="width:55px;" > </td>


      <td> <input type="text"autocomplete="off" id="ded_othrdec<?php echo $WorkAssignListVal['user_id'];?>" name="other_deductions[]" value="<?php echo $payrolldata[0]['other_deductions'];?>"
       onkeyup="total_deductions(<?php echo $WorkAssignListVal['user_id'];?>)" style="width:55px;" > </td>
      
       <td> <input type="text"autocomplete="off" id="ded_leavdec<?php echo $WorkAssignListVal['user_id'];?>" name="leave_deduction[]" 
        value="<?php echo sprintf('%0.1f', $totalLeaveDeduction);?>"
      onkeyup="total_deductions(<?php echo $WorkAssignListVal['user_id'];?>)" style="width:55px;" > </td>

      
      
       <td class="cellcolor" style="padding:8px 0px 0px 0px;"> <input type="text" id="totaldec<?php echo $WorkAssignListVal['user_id'];?>" name="total_deduction[]" value="<?php echo $payrolldata[0]['total_deduction'];?>" readonly style="width:55px;" > </td>
      


      
       <td> <input type="text"autocomplete="off" id="alw_levcash<?php echo $WorkAssignListVal['user_id'];?>" name="leave_encashment[]" onkeyup="total_allowances(<?php echo $WorkAssignListVal['user_id'];?>)" value="<?php echo $payrolldata[0]['leave_encashment'];?>" style="width:55px;" > </td>

       <td> <input type="text"autocomplete="off" id="alw_trav<?php echo $WorkAssignListVal['user_id'];?>" name="emp_trav[]" onkeyup="total_allowances(<?php echo $WorkAssignListVal['user_id'];?>)" value="<?php echo $WorkAssignListVal['emp_trav'];?>" style="width:55px;" > </td>


       <td> <input type="text"autocomplete="off" id="alw_reimbr<?php echo $WorkAssignListVal['user_id'];?>" name="emp_reimbersement[]" onkeyup="total_allowances(<?php echo $WorkAssignListVal['user_id'];?>)" value="<?php echo $WorkAssignListVal['emp_reimbersement'];?>" style="width:55px;" > </td>
               
        
       <td> <input type="text"autocomplete="off" id="alw_othrallowans<?php echo $WorkAssignListVal['user_id'];?>" name="other_allowances[]" value="<?php echo $WorkAssignListVal['emp_other'];?>" onkeyup="total_allowances(<?php echo $WorkAssignListVal['user_id'];?>)" style="width:55px;" > </td>
     
     
      <td class="cellcolor" style="padding:8px 0px 0px 0px;"> <input type="text" id="totalallow<?php echo $WorkAssignListVal['user_id'];?>" name="total_allowances[]" value="<?php echo $payrolldata[0]['total_allowances'];?>" readonly style="width:55px;" > </td>

     
      <td class="cellcolor" style="padding:8px 0px 0px 0px; "> <input type="text" id="totalamt<?php echo $WorkAssignListVal['user_id'];?>" name="net_total[]" value="<?php echo $WorkAssignListVal['user_salary']+$previous_data[0]['remaining'];?>" readonly  required style="width:55px;" > </td>



      
</tr>

<?php
      $result=""; 
$emp_absent="";
$emp_Leave_absent="";
    } ?>

<script>

   function total_deductions(id) {
        
        var ded_security=document.getElementById("ded_security"+id).value;
        var ded_advance=document.getElementById("ded_advance"+id).value;
        var ded_othrdec=document.getElementById("ded_othrdec"+id).value;
        var ded_leavdec=document.getElementById("ded_leavdec"+id).value;
                
        
        var total_ded= Number(ded_othrdec)+Number(ded_leavdec)+Number(ded_security)+Number(ded_advance);
        
        document.getElementById("totaldec"+id).value=total_ded;

       net_totals(id); 
      }


     function total_allowances(id) {
        
      
        var cash_alw=document.getElementById("alw_levcash"+id).value;
        var alw_trav=document.getElementById("alw_trav"+id).value;
        var alw_reimbr=document.getElementById("alw_reimbr"+id).value;
        var othr_alw=document.getElementById("alw_othrallowans"+id).value;

        
        
        var total_alw= Number(cash_alw) + Number(alw_trav) + Number(alw_reimbr) + Number(othr_alw);
        
        document.getElementById("totalallow"+id).value=total_alw;

        net_totals(id);
      }

      function net_totals(id) {
        
        
        var basic_slar=document.getElementById("basic"+id).value;
        var prev_slar=document.getElementById("previous"+id).value;
        var total_alw=document.getElementById("totalallow"+id).value;
        var total_ded=document.getElementById("totaldec"+id).value;
       

        
        
        var total_amount= (Number(basic_slar) + Number(total_alw) +  Number(prev_slar))- Number(total_ded);
        
        document.getElementById("totalamt"+id).value=total_amount;



        
      }




</script>

           
            
           
        </tbody>
        
    </table>
<?php  } else {?>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br>
  <div  style="display:block;" class="alert alert-danger alert-dismissible">
      <a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
       <strong>Warning !</strong> Please select month and serarch to creat PayRoll  !!!
  </div>
  <?php }?>

</form>
</div>

    <br><br>


  </div>
  <div class="clearfix"></div>
</section>          
    

           
          </div>
        </div>

            </div>
          </div>
        </div>
        <!-- /page content -->
  

    
  <?php include('RootMe/js_linksscript.php');?>
  </body>
</html>
