<?php
if(basename($_SERVER['PHP_SELF']) == 'payc.php') 
  $pp=" class='current'";
include'header.php';
include'connection.php';


 $doit=mysqli_query($conn, "SELECT *FROM `payrolls` WHERE `Level`='2' ORDER BY `Number` DESC");
if($foit=mysqli_num_rows($doit)){
		$roit=mysqli_fetch_assoc($doit);
			$dat=$roit['Ending'];
}
else
$dat=$Date;

		$dato = strtotime("+1 day", strtotime("$dat"));
				 $dato=date("d-m-Y", $dato);

		$datos = strtotime("+6 day", strtotime("$dato"));
				 $datos=date("d-m-Y", $datos);


if(isset($_POST['search']))
		{
			$dato=$_POST['dato'];
			$datos=$_POST['datos'];
			$currentp=$_POST['currentp'];
		}

		if($currentp)
			$cp="AND `Currentp`='$currentp'";
			else
				$cp='';

$do=mysqli_query($conn, "SELECT *FROM `employees` WHERE `Eid`!='1001' AND `Status`='0' AND `Depart`='2' $cp ORDER BY `Fname` ASC");
		$fo=mysqli_num_rows($do);

?>

<div class="container-fluid main-content">
<div class="page-title">
 
 <h1 style='margin-top:-20px; margin-bottom: 5px;'>Casual Payroll</h1>


                 </div>
<div class="row"></div>   
     
        <div class="row">             
           
           
       
        <div class="col-lg-10">
                  <div class="row">
           <div class="col-lg-6"> 
        <input name="searchkeyword" value="" type="hidden">
        <input name="column_name" value="" type="hidden">   
                 
    
        </form>
           
           
              </div>
          
          
           <div class="col-lg-6" style='float:right; border:0px solid blue;'> </div>
		   
		   
		   <div class="col-lg-12" style='float:right; border:0px solid blue;'> <div align='right'>           

        <form action="" method="post" class="form-horizontal ">
              <table style='margin-top:-40px; margin-right:-40px;' border='0'>
			  <tr><td> <div class="col-md-4" style='width:140px; padding-top:5px;'> 
			  
			  <select class="form-control" name="currentp" style='width:140px; margin-right:-10px; margin-top:-5px;'>
			  <option value="0">Select&nbsp;Position</option>
			  <?php
		
			$de=mysqli_query($conn, "SELECT *FROM `position` WHERE `Status` = '0' ORDER BY `Postname` ASC");
			  while($re=mysqli_fetch_assoc($de)){
					$ne=$re['Postid'];
					$dep=$re['Postname'];
					$tit=$re['Owner'];
					if($ne==$currentp)
						$sed="selected=selected'";
					else
						$sed="";
			echo"<option value='$ne' $sed title='$tit'>$dep</option>";
			  }
			  ?>
                            </select></div></td><td>  <div class="col-md-4" style='width:142px;'> 
           <div class="input-group date datepicker" style='width:140px;'>
      <input class="form-control form-center" id="from" name="dato" type="text" value="<?php echo $dato ?>" onkeypress='return isNumberKey(event)' required>
	  <span class="input-group-addon"><i class="lnr lnr-calendar-full"></i></span>
			</div></div>
		</td><td>

		  <div class="col-md-4" style='width:142px;'> 
           <div class="input-group date datepicker" style='width:140px;'>	
      <input class="form-control form-center" id="to" name="datos" type="text" value="<?php echo $datos ?>" onkeypress='return isNumberKey(event)' required>
	  <span class="input-group-addon"><i class="lnr lnr-calendar-full"></i></span>
              </div></div> 
          
		  </td><td> <div class="col-md-4" style='width:100px; padding-top:5px;'>  
			
                      <span class="input-group-btn">
                        <button class="btn  btn-primary" type="submit" name="search" style='border-radius:5px;'><i class="lnr lnr-magnifier"></i> Search</button>
                      </span>
				</div>
					  </td></tr></table>
                       </div>
            </form> 
           </div> </div>             
               
           </div></div>
		   <?php
		   $days = date(' t ', strtotime($mt) );
		   ?>

             <div class="row"><form action='slips.php' method='post' name='myform'>
            <div class="col-lg-12" style='text-align:left; margin-top:-20px; border:0px solid black;'>
             <span>Total Records Found : <b><?php echo" $fo "; ?></b></span>
			 <div style='text-align:right; margin-top:-20px; padding-right:20px;'>
<li>
         <a href="javascript:void();" onclick="javascript:checkAll('myform', true);">Check All</a> | 
         <a href="javascript:void();" onclick="javascript:checkAll('myform', false);">UnCheck All</a>
</li></div>
             <div class="widget-container fluid-height clearfix">
            <div class="widget-content padded clearfix">
                
                                <table class="table table-striped">     
                                      <thead>
                    <tr role="row">
                     <th class="hidden-xs"> S.NO</th>
			<th class="hidden-xs"> EMPLOYEE </th><th class="hidden-xs" align='left'> POSITION </th>
					  <th class="hidden-xs" align='left'> AMOUNT </th>
					  <?php
					$date=$dato;
					  while(strtotime($date) <= strtotime($datos)){
						   $pieces = explode("-", $date);
								$i=$pieces[0];
						  echo"<th> $i </th>";
				 $date = strtotime("+1 day", strtotime("$date"));
				 $date=date("d-m-Y", $date);
								 }
								 ?>
						
                     </tr>
                    </thead>
                                        <tbody>
					<?php
					$n=$t=1;
						while($ro=mysqli_fetch_assoc($do)){
$code=$ro['Eid'];
$fna=$ro['Fname'];
$lna=$ro['Lname'];
$sal=$ro['Salary'];
$curp=$ro['Currentp'];

$dei=mysqli_query($conn, "SELECT *FROM `position` WHERE `Postid`='$curp' ORDER BY `Postname` ASC");
			 if($fei=mysqli_num_rows($dei)){
				 $rei=mysqli_fetch_assoc($dei);
					$post=$rei['Postname'];
			 }
			 else
				 $post="N/A";

$salo=number_format($sal);
$mto = date('m', strtotime("$mt"));
        
		print("<tr>
                        <td class=hidden-xs align='center'>$t</td>
                        <td> $fna $lna </td> <td class='hidden-xs' align='left'> $post </td> <td class='hidden-xs' align='center'> $salo </td>");
 $date=$dato; 
 while(strtotime($date) <= strtotime($datos)){
			 $pieces = explode("-", $date);
					$i=$pieces[0];
					$mt=$pieces[1];
					 $mt = date("F", mktime(0, 0, 0, $mt, 10));
					   $yr=$pieces[2];

	 if($i%2==0)
		 $clr="";
	 else
		 $clr="";

	 $doi=mysqli_query($conn, "SELECT *FROM `payrolls` WHERE `Employee`='$code' AND `Month`='$mt' AND `Year`='$yr' AND `D$i`='1'");
		$foi=mysqli_num_rows($doi);
		if($foi)
			$sl="checked";
		else
			$sl="";

//if(!$foi)
//$sl="checked";

	$dto=$date;

						  echo"<td title='$fna $lna // $i $mt $yr
			Salary: $salo
			$dto' style='background-color: $clr;'>  
  <input type='checkbox' class='input-assumpte' id='input-confidencial' name='cheko$n' value='1' $sl>  
		<input type='hidden' name='code$n' value='$code'><input type='hidden' name='sal$n' value='$sal'>
		<input type='hidden' name='curp$n' value='$curp'><input type='hidden' name='date$n' value='$date'></td>";
					$date = strtotime("+1 day", strtotime("$date"));
				 $date=date("d-m-Y", $date);
						    $n++;
 }
						
                    print("</tr>");	
						$t++;
						}
						?>
						
                    </tbody>   
                  </table> <HR STYLE='margin-top:-8px;'>
                                      <div class="row">
                  <div class="col-md-12">
                  <div class="pull-right" style='width:280px; padding-right:40px;'>
              <?php
					echo"<input type='hidden' name='n' value='$n'><input type='hidden' name='dato' value='$dato'>
					<input type='hidden' name='datos' value='$datos'>";

		if($dato!=$datos){
					?>
                       <button class="btn btn-lg btn-block btn-success" type="submit" name="submit">
					   <i class="lnr lnr-checkmark-circle"></i> Submit </button> 
					   
					   <?php
		}
					   $set=mysqli_query($conn, "UPDATE `payrolls` SET `Total`=`D01` + `D02` + `D03` + `D04` + `D05` + `D06` + `D07` 
					   + `D08` + `D09` + `D10` + `D11` + `D12` + `D13` + `D14` + `D15` + `D16` + `D17` + `D18` + `D19` + `D20` 
					   + `D21` + `D22` + `D23` + `D24` + `D25` + `D26` + `D27` + `D28` + `D29` + `D30` + `D31`
					   WHERE `Total`='0' AND `Level`='2' ORDER BY `Number` DESC LIMIT 100");
					  
					   ?>
              </div></div></div></form>
                                
              </div>

<?php
include'footer.php';
?>