<?php
require_once("dompdf/dompdf_config.inc.php");
include_once("class/class.MySQL.php");
$db		= new MySQL();

if(isset($_POST['form'])){
	
$arr=array();
//echo "<pre>";
//print_r($_REQUEST);

if(isset($_POST['month_wages_id']) && $_POST['month_wages_id']!='null')
	   {
	   $month=$_POST['month_wages_id'];
	   }
	   else
	   {
	   $month = date('m');
	   //$month = 11;
	   }
	   if(isset($_POST['year_wages_id']) && $_POST['year_wages_id']!='null')
	   {
	   $year=$_POST['year_wages_id'];
	   }
	   else
	   {
	   $year = date('Y');
	   }
	   if(isset($_POST['branch_id']))
	   {
	   
	   
	   $branch_id=$_POST['branch_id'];
	  
	   $branchqry =$db->fetchQuery("select branch_name from branches where branch_id ='$branch_id'");
	//  print_r($branchqry);
	   $branchname=$branchqry[0]['branch_name'];
	   
	   }
	   else
	   {
	   $branch_id = ''; $branchname='';
	   }
	    $firstmonthdate='01-'.$month.'-'.$year;
		$d=cal_days_in_month(CAL_GREGORIAN,$month,$year);
		$lastmonthdate=$d.'-'.$month.'-'.$year;
		
$monthname=date("F", mktime(0, 0, 0, $month, 10));

//date("g:i:s A D, F jS Y",$firstmonthdate);
 
$wagesview ='<style type="text/css">
		body {font-family: "Trebuchet MS";color: #202020;font-size: 12px;line-height: 16px; background: #fff;}
		.content {
		  padding: 20px 15px;
		  overflow: auto;
		}
		h1,
		h2,
		h3,
		h4,
		h5,
		h6,
		.h1,
		.h2,
		.h3,
		.h4,
		.h5,
		.h6 {
		  font-family: "Source Sans Pro", sans-serif;
		}
		.table{
			border-collapse:collapse;
			margin-bottom:15px;
		}
		.table td{border:2px #F4F4F4 solid; padding-left:5px;}
		.table tr{height:30px;color:#202020;font-size:12px; line-height:20px;}
		.title{font-weight:bold;}
		</style>
<table width="100%">
  
  <tr>
    <td><table width="100%" border="1" cellspacing="2" cellpadding="2" class="table">
      
      <tr>
        <td colspan="16" align="center" valign="top" class="title">SBC/WR/007/2011</td>
        </tr>
      <tr>
        <td colspan="16" align="center" valign="top" class="title">MWV</td>
        </tr>
      <tr>
        <td colspan="2" align="left" valign="top" class="title">Name of Establistment: Group Mukkadan</td>
        <td colspan="11" align="center" valign="top" class="title">Wage Period from&hellip;&hellip;'.date("jS F Y", strtotime($firstmonthdate)).'...To&hellip;'.date("jS F Y", strtotime($lastmonthdate)).'&hellip;&hellip;&hellip;&hellip;.. </td>
        <td colspan="3" align="right" valign="top" class="title">Form V<br />
          Rule (29) (1)</td>
        </tr>
      
      <tr>
        <td colspan="16" align="center" valign="top" class="title">Place:'.$branchname.'</td>
        </tr>
      <tr>
        <td align="left" valign="top" class="title">&nbsp;</td>
        <td align="left" valign="top" class="title">&nbsp;</td>
        <td align="left" valign="top" class="title">&nbsp;</td>
        <td align="left" valign="top" class="title">&nbsp;</td>
        <td align="left" valign="top" class="title">&nbsp;</td>
        <td align="left" valign="top" class="title">&nbsp;</td>
        <td align="left" valign="top" class="title">&nbsp;</td>
        <td align="left" valign="top" class="title">&nbsp;</td>
        <td colspan="4" align="center" valign="top" class="title">Deduction</td>
        <td align="left" valign="top" class="title">&nbsp;</td>
        <td align="left" valign="top" class="title">&nbsp;</td>
        <td align="left" valign="top" class="title">&nbsp;</td>
        <td align="left" valign="top" class="title">&nbsp;</td>
      </tr>
      <tr>
        <td width="4%" align="left" valign="top" class="title">1</td>
        <td width="4%" align="left" valign="top" class="title">2</td>
        <td width="5%" align="left" valign="top" class="title">3</td>
        <td width="4%" align="left" valign="top" class="title">4</td>
        <td width="3%" align="left" valign="top" class="title">5</td>
        <td width="5%" align="left" valign="top" class="title">6</td>
        
        <td width="5%" align="left" valign="top" class="title">7</td>
        
        <td width="5%" align="left" valign="top" class="title">8</td>
        <td width="5%" align="left" valign="top" class="title">9</td>
        <td width="4%" align="left" valign="top" class="title">10</td>
        <td width="4%" align="left" valign="top" class="title">11</td>
        <td width="4%" align="left" valign="top" class="title">12</td>
        <td width="4%" align="left" valign="top" class="title">13</td>
        <td width="3%" align="left" valign="top" class="title">14</td>
        <td width="18%" align="left" valign="top" class="title">15</td>
        <td width="8%" align="left" valign="top" class="title">16</td>
      </tr>
      <tr>
        <td align="left" valign="top" class="title">&nbsp;</td>
        <td align="left" valign="top" class="title">&nbsp;</td>
        <td align="left" valign="top" class="title">&nbsp;</td>
        <td align="left" valign="top" class="title">&nbsp;</td>
        <td align="left" valign="top" class="title">&nbsp;</td>
        <td align="left" valign="top" class="title">&nbsp;</td>
        
        <td align="left" valign="top" class="title">&nbsp;</td>
        <td align="left" valign="top" class="title">&nbsp;</td>
        <td align="left" valign="top" class="title">&nbsp;</td>
        <td colspan="2" align="left" valign="top" class="title">Employees Contribution</td>
        <td align="left" valign="top" class="title">&nbsp;</td>
       
        <td align="left" valign="top" class="title">&nbsp;</td>
        <td align="left" valign="top" class="title">&nbsp;</td>
        <td align="left" valign="top" class="title">&nbsp;</td>
        <td align="left" valign="top" class="title">&nbsp;</td>
      </tr>
      <tr>
        <td align="left" valign="top" class="title">Sr. No. </td>
        <td align="left" valign="top" class="title">Name of Employees</td>
        <td align="left" valign="top" class="title">Father Name</td>
        <td align="left" valign="top" class="title">Designation</td>
        <td align="left" valign="top" class="title">Basic </td>
        <td align="left" valign="top" class="title">DA</td>
        
        <td align="left" valign="top" class="title">Total Attendance/ Days of work done</td>
        
        <td align="left" valign="top" class="title">Gross Salary Payable</td>
        <td align="left" valign="top" class="title">PF</td>
        <td align="left" valign="top" class="title">ESI</td>
        <td align="left" valign="top" class="title">HRA</td>
        <td align="left" valign="top" class="title">Other Deductions</td>
        <td align="left" valign="top" class="title">Total Deductions</td>
        <td align="left" valign="top" class="title">Wages Paid</td>
        <td align="left" valign="top" class="title">Date of Payment</td>
        <td align="left" valign="top" class="title">Signature or Thumb Impression of Employee</td>
      </tr>';
	  
	  /*echo "SELECT * FROM employee as em join payroll_salary_pay as paysal ON em.e_id = paysal.userid join designation as des on des.desig_id = em.e_post join pf_esi_details
 as pfesi on pfesi.emp_id = em.e_id  where em.branchid = '123' and month(paysal.pay_period) = '".$month."' and year(paysal.pay_period) ='".$year."'and month(pfesi.pay_period) = '".$month."' and year(pfesi.pay_period) ='".$year."'";*/
	  
	 //$getres =  $db->fetchQuery("SELECT * FROM employee as em join payroll_salary_pay as paysal ON em.e_id = paysal.userid join designation as des on des.desig_id = em.e_post  where em.branchid = '123' and MONTH(payment_date) = '$month' and YEAR(payment_date) = '$year'");
	 
	 /*echo "SELECT * FROM employee as em join payroll_salary_pay as paysal ON em.e_id = paysal.userid join designation as des on des.desig_id = em.e_post join pf_esi_details as pfesi on pfesi.emp_id = em.e_id  where em.branchid = '$branch_id' and month(paysal.pay_period) = '".$month."' and year(paysal.pay_period) ='".$year."'and month(pfesi.pay_period) = '".$month."' and year(pfesi.pay_period) ='".$year."'";*/
	 
	/* echo "SELECT * FROM employee as em join payroll_salary_pay as paysal ON em.e_id = paysal.userid join designation as des on des.desig_id = em.e_post join pf_esi_details as pfesi on pfesi.emp_id = em.e_id  where em.branchid = '123' and month(paysal.pay_period) = '".$month."' and year(paysal.pay_period) ='".$year."'and month(pfesi.pay_period) = '".$month."' and year(pfesi.pay_period) ='".$year."'";*/
	
	
	 $getres =  $db->fetchQuery("SELECT *,dtl_basic,dtl_da,dtl_hra,dtl_pf,dtl_esi,dtl_allowance,dtl_incentive FROM employee as em join payroll_salary_pay as paysal ON em.e_id = paysal.userid join designation as des on des.desig_id = em.e_post left join pf_esi_details as pfesi on pfesi.emp_id = em.e_id 
left join (select payment_id,amount as dtl_basic from payroll_salary_pay_details where name like '%basic%')as saldtl on paysal.id=saldtl.payment_id 
 left join (select payment_id,amount as dtl_da from payroll_salary_pay_details where name like '%da%')as saldtl1 on paysal.id=saldtl1.payment_id 
left join (select payment_id,amount as dtl_hra from payroll_salary_pay_details where name like '%hra%')as saldtl2 on paysal.id=saldtl2.payment_id 
left join (select payment_id,amount as dtl_pf from payroll_salary_pay_details where name like '%pf%')as saldtl3 on paysal.id=saldtl3.payment_id 
left join (select payment_id,amount as dtl_esi from payroll_salary_pay_details where name like '%esi%')as saldtl4 on paysal.id=saldtl4.payment_id 
left join (select payment_id,amount as dtl_allowance from payroll_salary_pay_details where name like '%allowance%')as saldtl5 on paysal.id=saldtl5.payment_id 
left join (select payment_id,amount as dtl_incentive from payroll_salary_pay_details where name like '%incentive%')as saldtl6 on paysal.id=saldtl6.payment_id 
where em.branchid = '$branch_id' and month(paysal.pay_period) = '".$month."' and year(paysal.pay_period) ='".$year."' ");
	 
	/* echo "SELECT * FROM employee as em join payroll_salary_pay as paysal ON em.e_id = paysal.userid join designation as des on des.desig_id = em.e_post join pf_esi_details as pfesi on pfesi.emp_id = em.e_id  where em.branchid = '123' and month(paysal.pay_period) = '".$month."' and year(paysal.pay_period) ='".$year."'and month(pfesi.pay_period) = '".$month."' and year(pfesi.pay_period) ='".$year."'";*/
	
	
	//print_r($getres);
	 $i=1;
	 
	 foreach($getres as $resval)
	 {
	 $arr[] = date("H:i:s");
	 
	 //	 $getded =  $db->fetchQuery("SELECT * FROM payroll_user_deductions as deduct join payroll_benifits_deductions 
 //as paydedbenfit ON deduct.deduction_id = paydedbenfit.id  where deduct.employee_id = '".$resval['e_id']."' ");
 
 	
	//echo "Select count(distinct(e_date)) as fullday from time as A where e_id='".$resval['e_id']."' and MONTH(e_date) = '04' and YEAR(e_date) = '2014' and (select total_worked_hours from time as B where B.e_id=A.e_id and B.e_date = A.e_date order by B.id desc limit 1) > '06:00:00'";
	$fulldaydetails = $db->fetchQuery("Select count(distinct(e_date)) as fullday from time as A where e_id='".$resval['e_id']."' and MONTH(e_date) = '$month' and YEAR(e_date) = '$year' and (select total_worked_hours from time as B where B.e_id=A.e_id and B.e_date = A.e_date order by B.id desc limit 1) > '06:00:00'");
	 
	$fulldays=$fulldaydetails[0]['fullday'];
	
	
	 

	  $halfdaysapplied = $db->fetchQuery("Select count(distinct(e_date)) as halfday from time as A where e_id='".$resval['e_id']."' and MONTH(e_date) = '$month' and YEAR(e_date) = '$year' and (select total_worked_hours from time as B where B.e_id=A.e_id and B.e_date = A.e_date order by B.id desc limit 1) < '06:00:00' and (select total_worked_hours from time as B where B.e_id=A.e_id and B.e_date = A.e_date order by B.id desc limit 1) > '04:00:00'");
	
	 $halfdays=$halfdaysapplied[0]['halfday'];

     $noofdaysworked=$fulldays +($halfdays*0.5);
	 
	 $daqry = $db->fetchQuery("Select * from payroll_benifits_deductions emp_id='".$resval['e_id']."' and name='ALLOWANCE'");
	 if(!empty($daqry))
	 {
	 $daamtqry=$db->fetchQuery("Select amount from payroll_benifits_deductions where name='ALLOWANCE'");
	 $daamt=$daamtqry[0]['amount'];
	 }
	 else
	 {
	 $daamt=0;
	 }
	 
	 $pfqry = $db->fetchQuery("Select * from payroll_benifits_deductions where emp_id='".$resval['e_id']."' and name='PF'");
	 if(!empty($pfqry))
	 {
	 $pfamtqry=$db->fetchQuery("Select amount from payroll_benifits_deductions where name='PF'");
	 $pfamt=$pfamtqry[0]['amount'];
	 }
	 else
	 {
	 $pfamt=0;
	 }
	 
	 $esiqry = $db->fetchQuery("Select * from payroll_benifits_deductions where emp_id='".$resval['e_id']."' and name='ESI'");
	 if(!empty($esiqry))
	 {
	 
	 $esiamtqry=$db->fetchQuery("Select amount from payroll_benifits_deductions where name='ESI'");
	 $esiamt=$esiamtqry[0]['amount'];
	 }
	 else
	 {
	 $esiamt=0;
	 }
	 /*$hraqry = $db->fetchQuery("Select * from payroll_user_deductions where employee_id='".$resval['e_id']."' and deduction_id=28");
	 if(!empty($hraqry))
	 {
	 $hraamtqry=$db->fetchQuery("Select amount from payroll_benifits_deductions where id=28");
	 $hraamt=$hraamtqry[0]['amount'];
	 }
	 else
	 {
	 $hraamt=0;
	 }
	 $othrdedqry = $db->fetchQuery("Select deduction_id from payroll_user_deductions where employee_id='".$resval['e_id']."' and deduction_id!=28 and deduction_id!=27 and deduction_id!=13");
	 if(!empty($othrdedqry))
	 {$othrdedamt=0;
	 foreach($othrdedqry as $othrdedqrys)
	 {
	 $othrdedamtqry=$db->fetchQuery("Select amount from payroll_benifits_deductions where id='".$othrdedqrys['deduction_id']."' and deduction_benefit=1");
	 $othrdedamt=$othrdedamt+$othrdedamtqry[0]['amount'];
	 }
	 
	 }
	 else
	 {
	 $othrdedamt=0;
	 }*/
	 
      $wagesview .='<tr>
        <td align="left" valign="top" class="title">'.$i.'</td>
        <td align="left" valign="top" class="title">'.$resval['e_fname'].' '.$resval['e_lname'].'</td>
        <td align="left" valign="top" class="title">'.$resval['e_mname'].'</td>
        <td align="left" valign="top" class="title">'.$resval['desig_name'].'</td>
        
        <td align="left" valign="top" class="title">'.$resval['dtl_basic'].'</td>
        <td align="left" valign="top" class="title">'.$resval['dtl_da'].'</td>
        <td align="left" valign="top" class="title">'.$resval['worked_days'].'</td>
        <td align="left" valign="top" class="title">'.($resval['dtl_basic']+$resval['dtl_allowance']+$resval['dtl_incentive']).'</td>
        <td align="left" valign="top" class="title">'.$resval['dtl_pf'].'</td>
        <td align="left" valign="top" class="title">'.$resval['dtl_esi'].'</td>
        
        <td align="left" valign="top" class="title">'.$resval['dtl_hra'].'</td>
        <td align="left" valign="top" class="title">'.round($resval['leave_deduction'],2).'</td>
        <td align="left" valign="top" class="title">'.round($resval['total_deduction'],2).'</td>
        <td align="left" valign="top" class="title">'.round($resval['total_paid'],2).'</td>
        <td align="left" valign="top" class="title">'.date('d-m-Y',strtotime($resval['payment_date'])).'</td>
        <td align="left" valign="top" class="title">&nbsp;</td>
      </tr>';
	  $i++;
      }

    $wagesview .='</table></td>
  </tr>
  <tr>
    <td height="70">&nbsp;</td>
  </tr>
</table>

';
	 	
	$html  .= '';
	$dompdf = new DOMPDF();
	$dompdf->load_html($wagesview);
	$dompdf->render();
	$dompdf->stream("Wage Register.pdf");
}





?>