<?php 
include('functions.php');
$s=new db_query();
//GET Heirarchy from users wise
$deptid=  $s->GetFieldValue('www_users'," id='".$_SESSION['UserID']."' " ,'mst_dpt_heirarchy');
$r=GETUpperDeptUser($_SESSION['UserID'],$deptid,$_SESSION['ACGCompanyId']);
$userlist=  UserDeptList($r);
  if(count($userlist)>0)
$userlist=implode(",",$userlist).",".$_SESSION['UserID'];
else
$userlist="'".$_SESSION['UserID']."'";

$LowerDeptList=GETLowerDeptUser($_SESSION['UserID'],$deptid,$_SESSION['ACGCompanyId']);

 $lowerlist=  UserDeptList($LowerDeptList);
  //echo "<br>--------222--------<br>";
 //print_r($LowerDeptList);
 if(count($lowerlist)>0)
$lowerlist=implode(",",$lowerlist).",".$_SESSION['UserID'];
else
$lowerlist="'".$_SESSION['UserID']."'";



$Client1=array();
$ZoneSQL=DB_query("select * from www_users where id in($lowerlist) and assigned_sites in(".$_SESSION['AssignedSites'].")",$db);

while($ZoneClient=DB_fetch_array($ZoneSQL))
{
	$Client1[]=$ZoneClient['id'];
}
$Client2=implode(",",$Client1);
 
//*************************DELETE FROM TABLES*********AS**************START HERE****************04-12-2012************************
if($_REQUEST['Delete'])
	{
	for($i=0;$i<count($_REQUEST['rowid']);$i++)
	{
	$del=DB_query("delete from crmsales_target where mst_usersid='".$_REQUEST['rowid'][$i]."'",$db);
	$s->pageLocation("index.php?route=CRM/salescrm_target_view".$rurl);
	}
}
//*************************DELETE FROM TABLES***********************END HERE****************04-12-2012************************

?>
<!----------------------------JS FILES----------------------------START HERE-------------------------------------------------->
<script type="text/javascript" language="javascript">
function LeadData()
{	
	myCheckboxArray = document.myform.elements["leadid[]"];
	var t=myCheckboxArray.length;
	if(document.getElementById('leadid').checked==true)
	{	
	if(t==undefined)
	document.getElementById('leadid_'+1).checked=true;		
	for (var i = 1; i <= t; i++)
	{
	document.getElementById('leadid_'+i).checked=true;	
	}	
	}
	else
	{
	if(t==undefined)		
	document.getElementById('leadid_'+1).checked=false;		
	for (var i = 1; i <= t; i++)
	{
	document.getElementById('leadid_'+i).checked=false;	
	}
	}
}
</script>
<!----------------------------JS FILES----------------------------END HERE-------------------------------------------------->
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1>Users Target Assigments View</h1>
<!-- Start Table 1 -->
<!-- Close Table 1 -->

<form name="myform" method="post">
<p align="left">
<?php if($_SESSION['frmAdd']==0){ ?>
< <input type="button" value="Add New Users Targets" alt="Not Allowed" title="Not Allowed" class="search" />
<?php } else {?>
<a href="index.php?route=CRM/salescrm_target&modid=<?php echo $_REQUEST['modid']; ?>&frmid=<?php echo $_REQUEST['frmid']; ?>"> <input type="button" value="Add New Users Targets" class="search" /></a>
<?php } ?>
<?php if($_SESSION['frmDelete']==0){ ?>
<input type="button" name="Delete" value="Delete" alt="Not Allowed" title="Not Allowed" class="search" />
<?php } else {?>
<input type="submit" name="Delete" value="Delete" class="search" />
<?php } ?>
</p>	
<div style="clear:both;"></div>
<!-- Start Table 1 -->
<?php 
//echo "select * from crmsales_target order by month";
//echo "SELECT DISTINCT(mst_usersid) FROM crmsales_target where mst_usersid in($lowerlist) order by id ";
  $query_target = DB_query("SELECT DISTINCT(mst_usersid) FROM crmsales_target where mst_usersid in($Client2) order by id ",$db);
  
?>

<table width="100%" border="1" cellpadding="3" cellspacing="0" style="border-collapse:collapse; border:#666666 solid 1px;">
  <tr>
  <td width="7%" class="tabledata"><input name="leadid" type="checkbox"   id="leadid" onclick="LeadData()"    /></td>
    <td width="47%" class="tabledata"><strong>User Name</strong></td>
    <td width="22%" class="tabledata"><strong>Target amount</strong></td>
	<td width="21%" class="tabledata"><strong>Month</strong></td>
	<td width="21%" class="tabledata"><strong>Financial Year</strong></td>
  </tr>
  <?php 
 if(DB_num_rows($query_target)>0)
  {
  $i=0;
  while($query_targetRow=DB_fetch_array($query_target))
  {
  $TotalASQL=DB_query("SELECT SUM(target) TargetAMT, fin_year FROM crmsales_target WHERE mst_usersid='".$query_targetRow['mst_usersid']."'",$db);
 $TotalAS=DB_fetch_array($TotalASQL);
   $timestamp = mktime(0, 0, 0, $query_targetRow['month'], 1, $query_targetRow['yearmonth']);
    $monthdata= date("M", $timestamp);
 ?>
<tr bgcolor="#c1c1c1">
  <td>
  <input name="leadid[]" type="checkbox" id="leadid_<?php echo $i+1; ?>"   value="" />
  
  <input name="rowid[]" type="hidden"    value="<?php  echo $query_targetRow['mst_usersid'];?>" />
  <a href="index.php?route=CRM/salescrm_target_Edit&modid=<?php echo $_REQUEST['modid']; ?>&frmid=<?php echo $_REQUEST['frmid']; ?>&id=<?php echo $query_targetRow['mst_usersid']; ?>&action=edit" ><img src="images/icon_edit.gif" width="12px"  height="12" /></a>
  
  
  	<!--<a href="index.php?route=CRM/salescrm_target_view&modid=<?php echo $_REQUEST['modid']; ?>&frmid=<?php echo $_REQUEST['frmid']; ?>&id=<?php echo $query_targetRow['id']; ?>&action=delete">Delete</a>-->
	</td>
    <td><?php echo $s->getField('www_users','id',$query_targetRow['mst_usersid'],'realname') ; ?></td>
    <td><?php echo round($TotalAS['TargetAMT']); ?></td>
	<td><?php echo "All Months"; //echo $monthdata ."". $query_targetRow['yearmonth']; ?></td>
	<td><?php echo $TotalAS['fin_year'];?></td>
  </tr>
  <?php $i++;  } } else {?>
<tr bgcolor="#c1c1c1">
 <td colspan="4">No Records</td>	
 	
 </tr>
  
 <?php } ?> 
  	
</table>
</form>
</div></div>

