<?php
ob_start();
session_name('CAKEPHP'); session_start();
error_reporting(FALSE);

	$PageSecurity = 3;
	include ('includes/session.inc');
	include ('includes/function_lib.php');
	include('includes/fun-php.php');
	include('includes/fun-HRClass.php');
    global $db; 
 	
	?>
<script type="text/javascript" language="javascript">
  function toggle_visibility(id) {
       var e = document.getElementById(id);
	       e.style.display = 'none';
		       //alert(e.style.display);
			   window.print();
    }
  
</script>
    <div align="center" style="width: 950px; margin-left:auto; margin-right:auto; border: #000000 solid 1px;">Users Target V/S Achievement</div>
	
	<div style="width:950px; margin-left:auto; margin-right:auto;">
	
	<table width="100%" border="1" cellspacing="1" cellpadding="2" align="left" style="border-collapse: collapse;">
  <tr>
    <th><center>User Name</center></th>
    <th><center>Target amount</center></th>
    <th><center>Target Achieved</center></th>
    <th><center>Achieved %</center></th>
    <th><center>Difference</center></th>
  </tr>  
  <?php 
  $j=0;
   $sql1=$_SESSION['query'];
   $sql=DB_query($sql1,$db);
  	while($Crm_sales=DB_fetch_array($sql))
	{  	
	?>
  <tr bgcolor="#e7e7e7">
  <td><center> 
  <?php echo $s->GetFieldValue('www_users',"id='".$Crm_sales['mst_usersid']."'",'userid'); ?></center></td>
  <td><center><?php echo $Crm_sales['target'] ?></center></td>
 <?php 
 $t=0;
  $Sales1=DB_query("select * from salesorder where Sale_Key_Exec='".$Crm_sales['mst_usersid']."' and month(orderDate)='".$Crm_sales['month']."' and month(orderDate)='".$Crm_sales['month']."'",$db);
 while($Sales1Row=DB_fetch_array($Sales1))
 {
 $invoice=DB_query("SELECT SUM(Amount) AS Total FROM salesorder_details WHERE masterId='".$Sales1Row['id']."' and ApprovalStatus='Approved' ",$db);
 while($invoiceRow1=DB_fetch_array($invoice))
 {?>
	
	<?php $t+= $invoiceRow1['Total'];?>
	<?php  }
 }
 ?>
  
  <td><center><?php echo $t;?></center></td>
  <?php 
	if($t!=0)
	$per=($Crm_sales['target']/$t)*100;
	else
	$per='';	
     ?>
  
 <td><?php echo round($per,2); ?></td>
  <?php 
	
	if($t>$Crm_sales['target'])
	{
	$DiffRow=$t-$Crm_sales['target'];
	$val="Pen";
	}
	else
	{
	$DiffRow=$Crm_sales['target']-$t;
	$val="App";	
	}
	?>
 <td>
	<center><?php echo $DiffRow; ?> &nbsp;<?php echo $val; ?></center></td>
  </tr>
  
  <?php $j++; } ?>
  
  
 
</table>

	</div>	<!-- main div close -->
	<br>
<div id="foo">
  <p align="center"> <a href="#" onclick="javascript:toggle_visibility('foo');" id="foo" >Print</a> </p>
</div>
