<?
session_start();
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='ind.php'; </script>";
 return;
}

include("../opendb.php");
include("../test.php");
include('mysqli_table.php');
$psres=mysqli_query($db,"select brpass from br_master where brname='$br'");
$psrow=mysqli_fetch_array($psres);
 if($psrow[brpass]!=$pswd)
 {
 print "<center><blink><font color=red> Invalid Password</font>";
   print"<script>history.go(-1);</script>";
 }




function monthName($month_int) {

$month_int = (int)$month_int;

$months = array("","Jan", "Feb", "Mar", "April", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec");
return $months[$month_int];
}

if(isset($_POST['deleterec']) && ($_POST['deleterec']!=''))
{
$id = array();
  $id = $_POST['del'];
  $delvar=0;
   if (count($id) > 0)
  {
     foreach ($del as $id2)
     {
       
	

$dateValue = 	date("y-m-d");
$year = date('y',strtotime($dateValue));

$month = date('F',strtotime($dateValue));
$m1=date("m");
$y1=date('Y',strtotime($dateValue));


$tbl="paytmp".$brcode;
$tbl1="payrolltmp".$brcode;
$sql="delete from $tbl1 where divi='$divi' and br='$br' and month(wgto)='$monthfrm' and year(wgto)='$yearfrm'";
//mysqli_query($db,$sql)or die('err'.mysqli_error());
$sql="create table $tbl1(empcode varchar(100) primary key,brcode varchar(100),ename varchar(100),design varchar(100),wgfrom date,wgto date,dayspaid int(5),wdays int(3),leave int(2),basicda double(14,2),ebasicda  double(14,2),hra double(14,2), ehra double(14,2),ta double(14,2),eta double(14,2),divi varchar(100), br varchar(100),rent double(14,2))";
//mysqli_query($db,$sql);

$tbl2="allow_payrolltmp".$brcode;
$sq="delete from $tbl2 where divi='$divi' and br='$br' and month(wdatefrm)='$monthfrm' and year(wdatefrm)='$yearfrm' and month(wdateto)='$month' and year(wdateto)='$yearfrm'";

//mysqli_query($db,$sq) or die('delall err'.mysqli_error());
$sql="create table $tbl2(empcode varchar(100),brcode varchar(100),alloname varchar(100),alloamt double(14,2),ealloname varchar(100),ealloamt double(14,2),divi varchar(100), br varchar(100),wdatefrm date,wdateto date)";
//mysqli_query($db,$sql) ;

$tbl3="ded_payrolltmp".$brcode;
$sq="delete from $tbl3 where divi='$divi' and br='$br' and month(wdateto)='$monthfrm' and year(wdateto)='$yearfrm'";

//mysqli_query($db,$sq)or die('del ded '.mysqli_error());

$sql="create table $tbl3(empcode varchar(100),brcode varchar(100),dedname varchar(100),dedamt double(14,2),pf double(14,2),esi double(14,2),otherded double(14,2),divi varchar(100), br varchar(100),wdatefrm date,wdateto date,ptaxamt double(12,2))";
//mysqli_query($db,$sql);

$sq="select * from br_master where brname='$br'";

$re=mysqli_query($db,$sq) or die("er1:".mysqli_error());
$brow=mysqli_fetch_array($re) or die("er1:".mysqli_error());
$sq="select distinct * from $tbl where divi='$divi' and br='$br' and month(dto)='$monthfrm' and year(dto)='$yearfrm' and empcode='$id2' order by empcode, dfrom desc";


 $res_tmp=mysqli_query($db,$sq)or die('Err divi'.mysqli_error());
$gdate="$yearfrm-$monthfrm-30";
while($row_tmp=mysqli_fetch_array($res_tmp))
{
 $sq="select distinct * from emp_pay where empcode='$row_tmp[empcode]' and ondate<='$gdate' order by ondate desc,empcode limit 1";

$emppayres=mysqli_query($db,$sq) or die("er2:".mysqli_error());
if(mysqli_num_rows($emppayres)>0)
 {
while($empayrow=mysqli_fetch_array($emppayres))
{
$ebasic=0;
$eda=0;
$ehra=0;
$eta=0;
$al_pf=0;
$pf=0;
$al_esi=0;
$esi=0;
$totforesi=0;
$totded=0;
$totd=0;
$teall=0;
$tots=0;
$tots1=0;
$newtea=0;
//$rentfortot=$empayrow[rent];

 $empres=mysqli_query($db,"select distinct * from empmaster where empcode='$empayrow[empcode]'") or die("er3:".mysqli_error());
$sql="select distinct * from empmaster where empcode='$empayrow[empcode]'";

 $emprow=mysqli_fetch_array($empres);
 $trainee=$emprow[trainee];
 $ptax=$emprow[ptax];
 $allores=mysqli_query($db,"select ondata from emp_pay_allow where ondata='$empayrow[14]'  and  empcode='$empayrow[empcode]'order by ondata desc limit 1") or die("er4:".mysqli_error());
$sql="select ondata from emp_pay_allow where ondata='$empayrow[14]'  and  empcode='$empayrow[empcode]'order by ondata desc limit 1";



 
$allorowdt=mysqli_fetch_array($allores);

 $dedres=mysqli_query($db,"select * from emp_pay_ded where empcode='$empayrow[empcode]' and ondata<='$gdate' order by ondata desc limit 1") or die("er5:".mysqli_error());

 $dedow=mysqli_fetch_array($dedres);




$result=mysqli_query($db,"select distinct * from $tbl where divi='$divi' and br='$br' and month(dto)='$monthfrm' and year(dto)='$yearfrm' order by empcode, dfrom desc");
$number_of_products = mysqli_numrows($result);



$pdf = new FPDF();
$pdf->AddPage();
$image2="logo_04.png";

$pdf->SetFont('Arial','B',12);
$pdf->SetFont('','B');
$pdf->Cell(200,0,'PSN Automobiles Pvt. Ltd'.$pdf->Image($image2, 170, 5, 18.78),0,1,'C');
$pdf->SetFont('','BU');
$pdf->Cell(200,10,'Pay Slip',0,1,'C');
//$pdf->Cell( 0,10,$pdf->Image($image2, $pdf->GetX(), $pdf->GetY(), 23.78), 0, 0, 'R', false );

$pdf->SetFont('Arial','',11);
$stname=ucwords(strtolower($emprow[empname]));
$pdf->Cell(40,20,' Name                 :','LT');$pdf->Cell(60,20,$stname,'RT');$pdf->Cell(50,20,' Employee Code       : ','LT');$pdf->Cell(40,20,$emprow[empid],'RT');
$pdf->Ln(4);
$pdf->Ln(4);
$designation=	strtoupper($emprow[design]);
$strcnt=strlen($designation);

if($strcnt>25)
{
$designation= substr($designation,0,25);
}
$designation=ucwords(strtolower($designation));
$pdf->Cell(25,20,' Designation','');$pdf->Cell(15,20,'     :','');$pdf->Cell(60,20,$designation,0,0,'');$pdf->Cell(50,20,' PF No.                     :','');$pdf->Cell(40,20,$emprow[pf],'R');
$pdf->Ln(4);
$pdf->Ln(4);
$sq="select *,(dto-dfrom)Tdays from $tbl where empcode='$empayrow[empcode]' and divi='$divi' and br='$br' and month(dto)='$monthfrm' and year(dto)='$yearfrm' order by dto desc limit 1";
$tmpres=mysqli_query($db,$sq)or die('Err 1:'.mysqli_error());
 $tmprow=mysqli_fetch_array($tmpres);
if($trainee=='No')
{
$pdf->Cell(30,20,' Wages Period','L');$pdf->Cell(10,20,':','');$pdf->Cell(60,20,datetohtml($tmprow[dfrom]).' To '.datetohtml($tmprow[5]),'');$pdf->Cell(50,20,' ESI No.                    :','');$pdf->Cell(60,20,$emprow[esi],'');
$pdf->Ln(4);
$pdf->SetFont('Arial','',11);
$pdf->Cell(60,20,'','');$pdf->Cell(40,20,'','R');
$pdf->Ln(4);

}
else
{
$pdf->Cell(30,20,' Stipend Period','L');$pdf->Cell(10,20,':','');$pdf->Cell(60,20,datetohtml($tmprow[dfrom]).' To '.datetohtml($tmprow[5]),'');$pdf->Cell(50,20,' ESI No.                    :','');$pdf->Cell(60,20,$emprow[esi],'');
$pdf->Ln(4);
$pdf->SetFont('Arial','',11);
$pdf->Cell(60,20,'','');$pdf->Cell(40,20,'','R');
$pdf->Ln(4);

}
 
$totbasc=$empayrow[basic]+$empayrow[da];


 if($tmprow[leave]>$tmprow[aleave])
  $leavetaken=($tmprow[leave]-$tmprow[aleave]);
 else 
  $leavetaken=0;
 $totdays=$tmprow[Tdays]+1;

$w=($tmprow[att]-$tmprow[leave]);
if($w==0)
{
$totdays=0;
}


if($emprow[lop]!='y')
{
 $leavetaken=0;
$w=$tmprow[att]; 
}
if($leavetaken>0&&$totdays>0)
 {
  $ebasic=($empayrow[basic]/$totdays)*($totdays-$leavetaken);
  $eda=($empayrow[da]/$totdays)*($totdays-$leavetaken);
  $ehra=($empayrow[hra]/$totdays)*($totdays-$leavetaken);
  $eta=($empayrow[ta]/$totdays)*($totdays-$leavetaken);
 }

else if($totdays>0)
 {
  $ebasic=($empayrow[basic]);
  $eda=($empayrow[da]);
  $ehra=($empayrow[hra]);
  $eta=($empayrow[ta]);
  }

$eta=ceil($eta);
$ehra=ceil($ehra);


 $ddres=mysqli_query($db,"select to_days('$tmprow[dto]')-to_days('$tmprow[dfrom]') from $tbl") or die("er6:".mysqli_error());
 $ddrow=mysqli_fetch_row($ddres);
 $wdays=$ddrow[0]+1;
 if($tmprow[leave]>$tmprow[aleave])
  $wdays-=($tmprow[leave]-$tmprow[aleave]);





if($w>0)
	{
	//$pdf->Cell(40,20,'PF No.: ','L');$pdf->Cell(40,20,$emprow[pf],'R');
	//$pdf->Ln(4);

	//$pdf->Cell(40,20,'ESI No.: ','L');$pdf->Cell(40,20,$emprow[esi],'R');
	//$pdf->Ln(4);
	//$pdf->Cell(40,20,'DEDUCTIONS','L');
	//$pdf->Ln(4);
	$celres=mysqli_query($db,"select * from celmast where brcode='$brcode'") or die("er9:".mysqli_error());
	$celrow=mysqli_fetch_array($celres);
	$totforpf=$ebasic+$eda+$al_pf+$ehra+$eta;
	if($empayrow[tapf]=='y')
		$totforpf+=$eta;

		if($totforpf<$celrow[pf])
		$pf=($totforpf*$celrow[pfp])/100;
	else
		$pf=($celrow[pf]*$celrow[pfp])/100;
	$pf = ceil($pf);
//********* esi
	
	if($empayrow[taesi]=='y')
		$totforesi+=$eta;
	if($empayrow[hraesi]=='y')
		$totforesi+=$ehra;
	if($empayrow[baesi]=='y')
		$totforesi+=$ebasic;
	if($empayrow[daesi]=='y')
		$totforesi+=$eda;
	$totforesi+=$teall;
	if($tots1<=$celrow[esi])
 	{
 		$sq="select * from $tbl2 where empcode='$empayrow[empcode]' and month(wdatefrm)='$monthfrm' and year(wdatefrm)='$yearfrm' order by wdatefrm desc";
		$a1=mysqli_query($db,$sq)or die('err'.mysqli_error());
		while($a1row=mysqli_fetch_array($a1))
		{
			$a2=mysqli_query($db,"select * from allomast where allname='$a1row[alloname]' and brcode='$brcode'")or die(mysqli_error());
			$a2row=mysqli_fetch_array($a2);
			if($a2row[esi]=='')
    			{
				$totforesi-=$a1row[ealloamt];
			}
		}
		if($totforesi>=100)
		{
		$esi=($totforesi*$celrow[esip])/100;  
		}
	}

	else
	$esi=0;
	$esi=ceil($esi);
$esi=$esi+$mesi_inc1[newesi];
$dedres1=mysqli_query($db,"select * from emp_pay_ded where empcode='$empayrow[empcode]' and ondata<='$gdate' order by ondata desc limit 1") or die("er8:".mysqli_error());
while($dedow1=mysqli_fetch_array($dedres1))
{
// print "<tr><td>&nbsp;&nbsp;$dedow1[allow]</td><td>".putcoma($dedow1[amt])."</td></tr>";
// $totded+=$dedow1[amt];
/*
	$sql="insert into $tbl3 values('$empayrow[empcode]','$brcode','$dedow1[allow]','$dedow1[amt]','$pf','$esi','$tmprow[ded]','$divi','$br','$tmprow[dfrom]','$tmprow[5]','0')";
	//mysqli_query($db,$sql)or die(mysqli_error());
//print $sql;*/
}
if($brcode=='AUT009')
{
	$pf=round($pf);
	$esi=ceil($esi);
}

//$pdf->Cell(40,20,'PF','L');$pdf->Cell(40,20,putComa($pf),'R');
	//$pdf->Ln(4);
//$pdf->Cell(40,20,'ESI','L');$pdf->Cell(40,20,putComa($esi),'R');
	//$pdf->Ln(4);



if($ptax=='Yes')
{
	if($tots>=5000 && $tots<8000)
		$ptaxamt=0;
	else if($tots>=8000 && $tots<10000)
   		$ptaxamt=0;
	else if($tots>=10000 && $tots<15000)
   		$ptaxamt=150;
	else if($tots>=15000)
   	$ptaxamt=200;
}// end if ptax


$sql="insert into $tbl3 values('$empayrow[empcode]','$brcode','$dedow1[allow]','$dedow1[amt]','$pf','$esi','$tmprow[ded]','$divi','$br','$tmprow[dfrom]','$tmprow[5]','$ptaxamt')";
//mysqli_query($db,$sql)or die('tbl3 err:'.mysqli_error());

$madvances=$tmprow[ded]+$mesi_inc1[mob_ded]+$mesi_inc1[advance]+$mesi_inc1[tds]+$mesi_inc1[kshemanidhi]+$mesi_inc1[mediclaim]+$mesi_inc1[other_ded]+$mesi_inc1[laptop];
$totd=$totded+$pf+$esi+$ptaxamt+$madvances+$mesi_inc1[prof_tax];




$pdf->Cell(40,20,'--------------------------------------------------------------------------------------------------------------------------------------------------','');

$pdf->Ln(4);
$pdf->Cell(60,20,'','L');$pdf->Cell(40,20,'','R');$pdf->SetFont('Arial','BU',12);$pdf->Cell(50,20,' DEDUCTIONS','');$pdf->Cell(40,20,'','R');
$pdf->Ln(4);
$pdf->Ln(4);
$pdf->SetFont('Arial','',11);
$pdf->Cell(40,20,' No of working days','L');$pdf->Cell(10,20,':','');$pdf->Cell(50,20,$tmprow[att],'R');$pdf->Cell(40,20,' PF','');$pdf->Cell(10,20,':','');$pdf->Cell(50,20,putComa($pf),'');
$pdf->Ln(5);
$pdf->Cell(40,20,' No of days present','L');$pdf->Cell(10,20,':','');$pdf->Cell(50,20,($tmprow[att]-$tmprow[leave]),'R');$pdf->Cell(40,20,' ESI','');$pdf->Cell(10,20,':','');$pdf->Cell(40,20,putComa($esi),'');
$pdf->Ln(5);
$pdf->Cell(40,20,' No of Days paid','L');$pdf->Cell(10,20,':','');$pdf->Cell(50,20,$totdays,'R');$pdf->Cell(40,20,' Professional Tax','');$pdf->Cell(10,20,':','');
if($ptaxamt>0)
{
$pdf->Cell(40,20,putComa($ptaxamt),'R');
}
else
{
$pdf->Cell(40,20,'0.00','R');
}
$pdf->Ln(5);
$nwd=$tmprow[att]-$tmprow[leave];

$pdf->Cell(40,20,' No of Leave','L');$pdf->Cell(10,20,':','');$pdf->Cell(50,20,$tmprow[leave],'R');$pdf->Cell(40,20,' TDS','');$pdf->Cell(10,20,':','');$pdf->Cell(40,20,'0.00','');
$pdf->Ln(5);
$pdf->Cell(50,20,'','L');$pdf->Cell(50,20,'','R');$pdf->Cell(40,20,' Other Advances','');$pdf->Cell(10,20,':','');$pdf->Cell(40,20,putComa($madvances),'');
$pdf->Ln(4);
$pdf->Cell(50,20,'','L');$pdf->Cell(50,20,'','R');if($mesi_inc1[prof_tax]>0)
{
	$pdf->Cell(40,20,' Professional Tax','');$pdf->Cell(10,20,':','');$pdf->Cell(40,20,putComa($mesi_inc1[prof_tax]),'R');
	$pdf->Ln(4);
}
else
{
$pdf->Cell(40,20,'','');$pdf->Cell(50,20,'','R');
}
$pdf->Ln(4);
$pdf->SetFont('Arial','U',11);
$pdf->Cell(50,20,'','L');$pdf->Cell(25,20,'Actual','');$pdf->Cell(25,20,'Earned','');$pdf->Cell(50,20,'','');$pdf->Cell(40,20,'','R');
$pdf->SetFont('Arial','',11);
$pdf->Ln(5);
$calcforesi=$totbasc;
$toterned=ceil($ebasic+$eda);
$pdf->SetFont('Arial','',11);
$pdf->Cell(40,20,' Basic + DA','L');$pdf->Cell(10,20,':','');$pdf->Cell(25,20,putcoma($totbasc),'');$pdf->Cell(25,20,putcoma($toterned),'R');$pdf->SetFont('Arial','B',12);$pdf->Cell(40,20,' Total (B)','');$pdf->Cell(10,20,':','');$pdf->Cell(40,20,putComa($totd),'R');
$pdf->Ln(5);
$pdf->SetFont('Arial','',11);
$calcforesi+=$empayrow[hra];
$pdf->Cell(40,20,' CCA','L');$pdf->Cell(10,20,':','');$pdf->Cell(25,20,putcoma($empayrow[hra]),'');$pdf->Cell(25,20,putComa(ceil($ehra)),'');$pdf->Cell(50,20,'','');$pdf->Cell(40,20,'','R');
$pdf->Ln(5);
$calcforesi+=$empayrow[ta];
$pdf->Cell(40,20,' TA','L');$pdf->Cell(10,20,':','');$pdf->Cell(25,20,putcoma($empayrow[ta]),'');$pdf->Cell(25,20,putComa(ceil($eta)),'');$pdf->Cell(50,20,'','');$pdf->Cell(40,20,'','R');
$pdf->Ln(4);


$pdf->Cell(50,20,'','L');$pdf->Cell(50,20,'','R');$pdf->Cell(50,20,'','');$pdf->Cell(40,20,'','R');
$pdf->Ln(5);
$grsslry=$totbasc+$empayrow[hra]+$empayrow[ta];
$tots=$ebasic+$eda+$ehra+$eta+$teall;
$pdf->SetFont('Arial','B',10);
$pdf->Cell(40,20,' Gross Salary','L');$pdf->Cell(10,20,':','');$pdf->Cell(25,20,putcoma($grsslry),'');$pdf->Cell(25,20,putComa(ceil($tots)),'');$pdf->Cell(50,20,'','');$pdf->Cell(40,20,'','R');
$pdf->Ln(5);
$pdf->SetFont('Arial','',11);




$sql="insert into $tbl1 values('$empayrow[empcode]','$brcode','$emprow[empname]','$emprow[design]','$tmprow[dfrom]','$tmprow[5]','$wdays','$nwd','$tmprow[leave]','$totbasc','$toterned','$empayrow[hra]','$ehra','$empayrow[ta]','$eta', '$divi','$br','$empayrow[rent]')";
//mysqli_query($db,$sql)or die('ins err:'.$tbl1.mysqli_error());
$q="select distinct allow,amt from emp_pay_allow where empcode='$empayrow[empcode]' and ondata='$allorowdt[0]' order by ondata desc";

$allores1=mysqli_query($db,$q) or die("er7:".mysqli_error());
while($allorow1=mysqli_fetch_array($allores1))
{
$p="select pf,esi,lop from allomast where allname='$allorow1[allow]'";
	$all_res=mysqli_query($db,$p)or die('alll Error'.mysqli_error());
	$allowrow111=mysqli_fetch_array($all_res);
	if($leavetaken>0 &&$allowrow111[lop]=='y')
	{ 
		$eall=($allorow1[amt]/$totdays)*($totdays-$leavetaken);
 	}
	else 
	{
  		$eall=($allorow1[amt]);
	}
	$teall+=$eall;
	if($allowrow1[esi]!='y')
	{
		$newtea=$teall;
		// check ceiling
		$newtea-=$allorow1[amt];
	}
	if($allowrow1[pf]=='y')
		$al_pf+=$allorow1[amt];
	if($allowrow1[esi]=='y')
	{
		// check ceiling
		$al_esi+=$allorow1[amt];
	}
//**********************************************88888888
if ($newtea<0) 
	{
		$newtea=0;
	}	
	$tots=$ebasic+$eda+$ehra+$eta+$teall;
	$tots1=$ebasic+$eda+$ehra+$eta+$newtea;
	if ($allorow1[allow]=='RENT.'||$allorow1[allow]=='RENT')
	{
	$pdf->Cell(40,20,' HRA','L');$pdf->Cell(10,20,':','');$pdf->Cell(25,20,'','');$pdf->Cell(25,20,putComa($eall),'');$pdf->Cell(50,20,'','');$pdf->Cell(40,20,'','R');
	$pdf->Ln(5);
	
	}
	else
	{
		$pdf->Cell(50,20,$allorow1[allow],'');$pdf->Cell(50,20,putComa($allorow1[amt]),'R');$pdf->Cell(50,20,'','');$pdf->Cell(40,20,'','R');
		
		$pdf->Ln(4);
		$pdf->Cell(50,20,'Earned '.$allorow1[allow],'L');$pdf->Cell(50,20,putComa($eall),'R');$pdf->Cell(50,20,'','');$pdf->Cell(40,20,'','R');
		$pdf->Ln(5);
		
	}
	$eallow_name="Earned".$allorow1[allow];
	$sql="insert into $tbl2 values('$empayrow[empcode]','$brcode','$allorow1[allow]','$allorow1[amt]','$eallow_name','$eall','$divi','$br','$tmprow[dfrom]','$tmprow[5]')";
	//mysqli_query($db,$sql)or die('tbl2 err:'.mysqli_error());

}
$sq="select * from esiinc where month(fordate)='$monthfrm' and year(fordate)='$yearfrm' and divi='$divi' and br='$br' and brcode='$brcode' and ename='$emprow[empname]'";

	$mesi_inc=mysqli_query($db,$sq) or die("er2:".mysqli_error());	
$mesi_inc1=mysqli_fetch_array($mesi_inc);
$mallow = $mesi_inc1[ot]+$mesi_inc1[mob_all];
$tots=$tots+$mallow+$mesi_inc1[inc];
$tot_pay = $tots;
	$tots1=$ebasic+$eda+$ehra+$eta+$newtea;
	$tot_pay = $tots;
	$tots1=$ebasic+$eda+$ehra+$eta+$newtea;
$image1 = "sign.JPG";
$pdf->Cell(40,20,' Incentive','L');$pdf->Cell(10,20,':','');$pdf->Cell(25,20,'','');$pdf->Cell(25,20,putComa($mesi_inc1[inc]),'');$pdf->Cell(50,20,'','');$pdf->Cell(10,20,'','');$pdf->Cell( 30,20,$pdf->Image($image1, $pdf->GetX(), $pdf->GetY(), 23.78), 0, 0, 'R', false );
	$pdf->Ln(5);
	
	
	$pdf->Cell(40,20,' Allowances','L');$pdf->Cell(10,20,':','');$pdf->Cell(25,20,'','');$pdf->Cell(25,20,putComa($mallow),'R');$pdf->Cell(40,20,'','');$pdf->Cell(50,20,'','R');
	$pdf->Ln(4);
	$pdf->Ln(4);
	
	$pdf->SetFont('Arial','B',12);
	$pdf->Cell(40,20,' Total (A)','L');$pdf->Cell(10,20,':','');$pdf->Cell(25,20,'','');$pdf->Cell(25,20,putComa(ceil($tots)),'R');$pdf->SetFont('Arial','',11);if($trainee=='No')
{
$pdf->Cell(50,20,' Signature of Employee','');$pdf->Cell(50,20,'  Authorised signatory','');
	$pdf->Ln(4);
	}
	else
{
$pdf->Cell(50,20,' Signature of Apprentice','');$pdf->Cell(50,20,'  Authorised signatory','');
	$pdf->Ln(4);
}


$tots=0;
}

} // end while tmp
$spay=$tot_pay-$totd;
//$spay+=$rentfortot;
$spay=ceil($spay);
$pdf->Ln(4);
if($trainee=='No')
{
	$pdf->SetFont('Arial','B',12);
	$pdf->Cell(50,20,' Salary Payable (A - B)','L');$pdf->Cell(25,20,':','');$pdf->Cell(25,20,putComa($spay),'R');$pdf->SetFont('Arial','',11);$pdf->Cell(50,20,' Date :'.datetohtml($tmprow[5]),'');$pdf->Cell(40,20,'','R');
	$pdf->Ln(4);
	$pdf->Ln(4);
}

else
{
	$pdf->SetFont('Arial','B',12);
	$pdf->Cell(50,20,' Stipend Payable (A - B)','L');$pdf->Cell(50,20,'             '.putComa($spay),'R');$pdf->SetFont('Arial','',11);$pdf->Cell(50,20,' Date :'.datetohtml($tmprow[5]),'');$pdf->Cell(40,20,'','R');
	$pdf->Ln(4);
	$pdf->Ln(4);
}
$pdf->SetFont('Arial','',11);

	


$pdf->Cell(40,20,'_______________________________________________________________________________________','');
} // end if tmp

$ptaxamt=0;
}
//echo $empmail; 
$pdf->Output();
	
$from = 'salaryadmin@psnauto.com'; 
$qu="select * from empmaster where empcode='$id2'";
$rs=mysqli_query($db,$qu);
$row=mysqli_fetch_array($rs);
$emmail=$row[email];
$emmob=$row[mob];
	//echo "<br>";
$to =  $emmail;
//$to = 'bipinsunny@gmail.com';
$subject = "Salary Slip"; 
$headers = "From: ".$from."\r\n";  
$headers .= "Reply-To: ".$from."\r\n";  
$headers .= "Return-Path: ".$from."\r\n";

// a random hash will be necessary to send mixed content
$separator = md5(time());

// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// attachment name
$filename = "psnsalaryslip.pdf";

// encode data (puts attachment in proper format)
$pdfdoc = $pdf->Output("", "S");
$attachment = chunk_explode(base64_encode($pdfdoc));

// main header
$headers .= "MIME-Version: 1.0".$eol; 
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";

// no more headers after this, we start the body! //

$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
//$body .= "This is a MIME encoded message.".$eol;
$ddfdf=$tmprow[dfrom];
$ddfdf=explode('-',$ddfdf);
$month1=$ddfdf[1];
$month1= monthName($month1);

$message1 = '
<html>
<head>
  <title>Pay Slip</title>
</head>
<body>
  <table>
    <tr>
      <td>Dear Mr./Ms. '.strtoupper($emprow[empname]).',</td>
    </tr>
    <tr>
      <td>Your Salary Slip for the month of '.$month1.' '.$yearfrm.' is generated. Please find the attached slip along with this mail.</td>
    </tr>
    <tr>
      <td>Regards,</td>
    </tr>
	<tr>
      <td>Salary Admin</td>
    </tr>
<tr>
      <td>PSN Automobiles Pvt. Ltd</td>
    </tr>
  </table>
</body>
</html>
';





// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message1.$eol;

// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";

// send message
$flagsend=mail($to,$subject, $body,$headers);
		$delvar=$delvar+1;

					$username="psnauto";
					$api_password="afc46op45nftmu1kk";
					$sender="PSNVTS";
					$domain="sms.psnindia.com";
					$priority="11";// 1-Normal,2-Priority,3-Marketing
					$method="POST";
					
					$message="Your salaryslip has been sent to your email ".$emmail." , kindly check";
					$parameters="username=$username&api_password=$api_password&sender=$sender&to=$emmob&message=$message&priority=$priority";
				//	echo $parameters;
					$url="http://$domain/pushsms.php";
					$ch = curl_init($url);

					if($method=="POST")
					{
						curl_setopt($ch, CURLOPT_POST,1);
						curl_setopt($ch, CURLOPT_POSTFIELDS,$parameters);
					}
					else
					{
						$get_url=$url."?".$parameters;
						curl_setopt($ch, CURLOPT_POST,0);
						curl_setopt($ch, CURLOPT_URL, $get_url);
					}

					curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1); 
					curl_setopt($ch, CURLOPT_HEADER,0);  // DO NOT RETURN HTTP HEADERS 
					curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);  // RETURN THE CONTENTS OF THE CALL
					$return_val = curl_exec($ch);


					if($return_val=="")
					echo "Process Failed, Please check domain, username and password.";
					else
					$return_val=1;
					if($return_val==1)
					{

					$message2=$delvar." Message(s) Sent Successfully!";
					}

	

					

     }
	

	 if($flagsend){
	 
	 
	 $message3=$delvar." Mail(s) Sent Successfully!";
	 }
  }
}






?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>PSN TELCON</title>
<link href="css/style.php" type="text/css" />
</head>
<body>
<?php echo $message3; ?>
<form action="" name="mail_form" id="mail_form" method="post">
<?php

$ql="select * from empmaster where divi='$divi' and br='$br' and doret='0000-00-00' and email!='' order by br,empname";
//echo $ql;
//exit;
//$ql="select DISTINCT a.empcode,a.brcode,a.bname,b.empcode,b.brcode,b.divi,b.empname from $tbl22 a,empmaster b where b.brcode='$brcode' and b.divi='$divi' and b.br='$br' and a.empcode=b.empcode and a.brcode=b.brcode and b.email!='' order by b.br,b.empname";

$es=mysqli_query($db,$ql) or die('Err:'.mysqli_error());
$nn=mysqli_num_rows($es);
 if($nn>0)
  { ?>
<table width="80%" align="center" border="1">
  <tr>
    <td colspan="7" align="center">Employee Details</td>
    <div align="center"><font color="#FF0000"><?php echo $message2; ?></font></div>
  </tr>
<tr>
<td bgcolor="#D1D1A3">Select All&nbsp;<input name="selectall" value="0" onclick="un_check_category();" type="checkbox" /></td>
<td bgcolor="#D1D1A3">Sl No.</td>
<td bgcolor="#D1D1A3">Emp Name</td>
 <td bgcolor="#D1D1A3">Designation</td> 
 <td bgcolor="#D1D1A3">Branch</td>
  <td bgcolor="#D1D1A3">Email</td>  
<td bgcolor="#D1D1A3">Mobile</td>
  </tr>
<?php 
$slno=0;
while($row=mysqli_fetch_array($es)){ $slno++;?>
  <tr>
    <td><input type="checkbox" name="del[]" id="del[]" value="<?php echo $row[empcode];?>" /></td>
    <td><?php echo $slno; ?></td>
    <td><?php echo $row[empname]; ?><input type=hidden name=empcode id=empcode value=<?php echo $row[empcode]; ?>><input type=hidden name=br value=<?php echo $br; ?>><input type=hidden name=divi value=<?php echo $divi; ?>><input type=hidden name=monthfrm value=<?php echo $month; ?>><input type=hidden name=yearfrm value=<?php echo $yea; ?>><input type=hidden name=pswd value=<?php echo $pswd; ?>></td>
    <td><?php echo $row[design]; ?></td>
	<td><?php echo $row[br]; ?></td>
	 <td><?php echo $row[email]; ?><input type=hidden name=empmail id=empmail value=<?php echo $row[email]; ?>></td>
	<td><?php echo $row[mob]; ?><input type=hidden name=empmob id=empmob value=<?php echo $row[mob]; ?>></td>
  </tr>
  <?php } ?>
  
<tr>
<td align="center" colspan="7"><input type="hidden" name="deleterec" value=""/><input name="delete" id="delete" type="submit" value="Send Mail" onClick="return deleteRecords(document.mail_form);" /><?php if($flagsend){ ?><INPUT type="button" value="Close Window" onClick="window.close()"><?php } ?> </td>
</tr>
</table>
<?php }  ?>
</form>

</body>
</html>
<script>
function un_check_category()
{
  for (var i = 0; i < document.mail_form.elements.length; i++) {
    var e = document.mail_form.elements[i];
            if ((e.name != 'allbox') && (e.type == 'checkbox')) {
e.checked = document.mail_form.selectall.checked;
    }
  }
}
function deleteRecords(formname)
        {
        var chks = document.getElementsByName('del[]');

        var hasChecked = false;
        // Get the checkbox array length and iterate it to see if any of them is
//selected
        for (var i = 0; i < chks.length; i++)
        {
                if (chks[i].checked)
                {
                        hasChecked = true;
                        break;
                }
        }
        // if ishasChecked is false then throw the error message
        if (!hasChecked)
        {
                alert("Woops! You forgot to select record(s)");
                return false;
        }
        var agree=confirm("Are you sure you want to send message to the selected employee(s)?");
 if(agree==true)
 {
        formname.deleterec.value="delete";
    formname.submit();
 }
else
return false;
}
</script>