<?php //include('CRM/functions.php');
	//$fin_year=$_REQUEST['fin_year'];
        $fin_year=$_SESSION['YearStart'].'-'.$_SESSION['YearEnd1'];
        $month[0]='All';
	//$fromdate=ConvertSQLDate($_REQUEST['fdate']);
	//$todate=ConvertSQLDate($_REQUEST['todate']);
        if(isset($_POST['GO']))
            {
	$month=explode('-',$_REQUEST['month']);
        //$month['0']='All';
	$filter=array();
	if($fin_year!='000')
	{
	$filter[]="fin_year='".$fin_year."'";
	}
	//if($_REQUEST['todate']!='' && $_REQUEST['fdate']!='' )
	//{
	//$filter[]="published_data between '".$fromdate."' and '".$todate."'";
	//}
	if($month[0]!='All')
	{ 
	$filter[]="month='".$month[0]."'";
	$filter[]="fin_year like '".$month[1]."%'";
	}
	
	if(count($filter)>0)
	{
	if($month[0]!='All'){
	$searchdata=implode(" and ", $filter);
	/// $sql="select * from crmsales_target where $searchdata and mst_usersid in($lowerlist)";
       $sql="select * from crmsales_target where $searchdata";
	}
	else
	{ 
	$searchdata=implode(" and ", $filter);
	// $sql="select DISTINCT(mst_usersid),fin_year from crmsales_target where $searchdata and mst_usersid in($lowerlist)";
      $sql="select DISTINCT(mst_usersid),fin_year from crmsales_target where $searchdata ";
	}
	}
	else
	{
	//$sql="select * from crmsales_target where month='".date("m")."' and mst_usersid in($lowerlist)";
          $sql="select DISTINCT(mst_usersid),fin_year from crmsales_target where $searchdata";
	}
    }else{
          $sql="select DISTINCT(mst_usersid),fin_year from crmsales_target where fin_year='".$fin_year."' ";  
    }
	//$_SESSION['query']=$sql;

?>
<form name="searchform" method="post" action="">
<table width="100%" border="1" cellspacing="0" cellpadding="3" style="border-collapse:collapse;">
<tr>
    <td colspan="5" style="padding:0px;"><table width="80%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td align="left" valign="top"><strong>Financial year</strong></td>
    <td align="left" valign="top"><?php //$FinY=DB_query("SELECT *,YEAR(BookPeriodStart) AS SYear,YEAR(BookPeriodEnd) AS EYEAR FROM companies where CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
//$FinYRow=DB_fetch_array($FinY);?>
        <select name="fin_year" style="width:100px;">
	<option value="<?php echo $fin_year; ?>"<?php if($_REQUEST['fin_year']==$fin_year){?>selected="selected" <?php } ?>     ><?php echo $fin_year; ?></option>
	</select></td>
    <td align="left" valign="top"><strong>Month</strong></td>
    <td align="left" valign="top"><select name="month" style="width:150px;">
	<option value="All" <?php echo($_REQUEST['month']=='All'?'selected':''); ?>>All</option>
<?php $diffdata=($FinYRow['EYEAR']-$FinYRow['SYear'])+1;
	  for($i=0;$i<$diffdata;$i++){
	  if($i==0) { ?>
	<option value="04-<?php echo $_SESSION['YearStart'] ?>"<?php if($_REQUEST['month']=='04'.'-'.$_SESSION['YearStart']){?>selected="selected"<?php }?>>April-<?php echo $_SESSION['YearStart']?></option>
	<option value="05-<?php echo $_SESSION['YearStart'] ?>"<?php if($_REQUEST['month']=='05'.'-'.$_SESSION['YearStart']){?>selected="selected"<?php }?>>May-<?php echo $_SESSION['YearStart'] ?></option>
	<option value="06-<?php echo $_SESSION['YearStart'] ?>"<?php if($_REQUEST['month']=='06'.'-'.$_SESSION['YearStart']){?>selected="selected"<?php }?>>June-<?php echo $_SESSION['YearStart'] ?></option>
	<option value="07-<?php echo $_SESSION['YearStart'] ?>"<?php if($_REQUEST['month']=='07'.'-'.$_SESSION['YearStart']){?>selected="selected"<?php }?>>July-<?php echo $_SESSION['YearStart']?></option>
	<option value="08-<?php echo $_SESSION['YearStart']?>"<?php if($_REQUEST['month']=='08'.'-'.$_SESSION['YearStart']){?>selected="selected"<?php }?>>August-<?php echo $_SESSION['YearStart'] ?></option>
	<option value="09-<?php echo $_SESSION['YearStart']?>"<?php if($_REQUEST['month']=='09'.'-'.$_SESSION['YearStart']){?>selected="selected"<?php }?>>September-<?php echo $_SESSION['YearStart'] ?></option>
	<option value="10-<?php echo $_SESSION['YearStart']?>"<?php if($_REQUEST['month']=='10'.'-'.$_SESSION['YearStart']){?>selected="selected"<?php }?>>October-<?php echo $_SESSION['YearStart'] ?></option>
	<option value="11-<?php echo $_SESSION['YearStart']?>"<?php if($_REQUEST['month']=='11'.'-'.$_SESSION['YearStart']){?>selected="selected"<?php }?>>November-<?php echo $_SESSION['YearStart']?></option>
	<option value="12-<?php echo $_SESSION['YearStart']?>"<?php if($_REQUEST['month']=='12'.'-'.$_SESSION['YearStart']){?>selected="selected"<?php }?>>December-<?php echo $_SESSION['YearStart'] ?></option>
	<?php } else {?>
	<option value="01-<?php echo $_SESSION['YearEnd1'] ?>"<?php if($_REQUEST['month']=='01'.'-'.$_SESSION['YearEnd1']){?>selected="selected"<?php }?>>January-<?php echo $_SESSION['YearEnd1'] ?></option>
	<option value="02-<?php echo $_SESSION['YearEnd1']?>"<?php if($_REQUEST['month']=='02'.'-'.$_SESSION['YearEnd1']){?>selected="selected"<?php }?>>February-<?php echo $_SESSION['YearEnd1'] ?></option>
	<option value="03-<?php echo $_SESSION['YearEnd1']?>"<?php if($_REQUEST['month']=='03'.'-'.$_SESSION['YearEnd1']){?>selected="selected"<?php }?>>March-<?php echo $_SESSION['YearEnd1'] ?></option>
	<?php } } ?>	
	</select></td>
    <td align="left" valign="top"><input type="submit" value="GO"  name="GO" class="search" /></td>
  </tr>
</table>
</td>
  </tr>
 
  <tr>
    <td width="21%" class="tabledata" style="font-size:10px;">Username</td>
    <td width="19%" class="tabledata" style="font-size:10px;">Month</td>
    <td width="21%" class="tabledata" style="font-size:10px;">Assigned Target </td>
    <td width="19%" class="tabledata" style="font-size:10px;">Target Achieved </td>
	<td width="20%" class="tabledata" style="font-size:10px;">Diff</td>
  </tr>
<?php 
if($month[0]!='All'){
$query_target=DB_query($sql,$db);
if(DB_num_rows($query_target)>0){
$j=0;
$sql1=DB_query($sql,$db);
while($Crm_sales=DB_fetch_array($sql1))
{
$timestamp = mktime(0, 0, 0, $Crm_sales['month'], 1, $Crm_sales['yearmonth']);
$monthdata= date("M", $timestamp);
?>

<tr class="row1">
  <td><?php echo $s->GetFieldValue("www_users","id='".$Crm_sales['mst_usersid']."'","realname"); ?></td>
  <td><?php echo $monthdata; ?>&nbsp;<?php echo $Crm_sales['yearmonth']; ?></td>
  <td><?php echo $Crm_sales['target']; ?></td>
<?php $t=0;
$Sales1=DB_query("select * from salesorder where Sale_Key_Exec='".$Crm_sales['mst_usersid']."' 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='2' || ApprovalStatus='3') ",$db);
while($invoiceRow1=DB_fetch_array($invoice)){?>
<?php $t+= $invoiceRow1['Total'];?>
<?php  }}?>
  <td><?php echo $t;?></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="+";
	  }
	  else
	  {
	  $DiffRow=$Crm_sales['target']-$t;
	  $val="-";	
	  }?>
 <td><?php echo $val; ?>&nbsp;<?php echo $DiffRow; ?></td>
</tr>
<?php $j++; }  }
  else {?>
<tr class="row1">
  <td colspan="6" style="color:#FF0000;">No Records</td>	
</tr>
<?php } ?>
<?php } else {?>
<?php
$query_target=DB_query($sql,$db);
if(DB_num_rows($query_target)>0){
$j=0;
$sql1=DB_query($sql,$db);
while($Crm_sales=DB_fetch_array($sql1))
{
$TotalTarSQL=DB_query("select sum(target) as GrandTotal from crmsales_target where mst_usersid='".$Crm_sales['mst_usersid']."'",$db);
$TotalTarAMT=DB_fetch_array($TotalTarSQL);
$FinalGrandAMT=round($TotalTarAMT['GrandTotal']);
$timestamp = mktime(0, 0, 0, $Crm_sales['month'], 1, $Crm_sales['yearmonth']);
$monthdata= date("M", $timestamp);
?>

<tr class="row1">
  <td><?php echo $s->GetFieldValue('www_users',"id='".$Crm_sales['mst_usersid']."'",'realname'); ?></td>
  <td><?php echo 'All Month'; ?>&nbsp;<?php echo $Crm_sales['fin_year']; ?></td>
  <td><?php echo $FinalGrandAMT; ?></td>
<?php $t=0;
$Sales1=DB_query("select * from salesorder where Sale_Key_Exec='".$Crm_sales['mst_usersid']."'",$db);
while($Sales1Row=DB_fetch_array($Sales1)){
$invoice=DB_query("SELECT SUM(Amount) AS Total FROM salesorder_details WHERE masterId='".$Sales1Row['id']."' and (ApprovalStatus='2' || ApprovalStatus='3') ",$db);
while($invoiceRow1=DB_fetch_array($invoice)){?>
<?php $t+= $invoiceRow1['Total'];?>
<?php  }}?>
  <td><?php echo $t;?></td>
<?php if($t!=0)
	$per=($Crm_sales['target']/$t)*100;
	else
	$per='';?>
 <!--<td><?php echo round($per,2); ?></td>-->
<?php if($t>$FinalGrandAMT){
	  $DiffRow=$t-$FinalGrandAMT;
	  $val="+";
	  }
	  else
	  {
	  $DiffRow=$FinalGrandAMT-$t;
	  $val="-";	
	  }?>
 <td><?php echo $val; ?>&nbsp;<?php echo $DiffRow; ?> </td>
</tr>
<?php $j++; }  }
  else {?>
<tr class="row1">
  <td colspan="6" style="color:#FF0000;">No Records</td>	
</tr>
<?php } ?>



<?php } ?>

</table>
</form>
<!--<div  align="right" class="seemore" ><a href="#" onclick='popup("barchartshow.php?id=<?php echo $_SESSION['UserID'] ?>",880,360)'>View Chart</a></div>-->