<?php //echo $_SESSION['AssignedSites'];
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']."'";



if(isset($_POST['save']))
{  
	$date = date("Y");
	$date1 = date("Y")+1;
	$month=array("04","05","06","07","08","09","10","11","12","01","02","03");
	$month1=array($date,$date,$date,$date,$date,$date,$date,$date,$date,$date1,$date1,$date1);
	
	for($i=0;$i<$_REQUEST['count1'];$i++){
	$mst_usersid=$_REQUEST['mst_usersid'][$i];
	$target_value=$_REQUEST['target_amm'][$i]/12;
	//$achieve_ammount=$_REQUEST['target_achieve'][$i];
	//$Diffr=$achieve_ammount-$target_value[$i];
	for($k=0;$k<12;$k++){
	$datafetch=DB_query("select * from crmsales_target where mst_usersid='".$mst_usersid."' and CompanyId='".$_SESSION['ACGCompanyId']."' and fin_year='".$date."-".$date1."' and month='".$month[$k]."' and yearmonth='".$month1[$k]."'",$db);
	$n= DB_num_rows($datafetch);
	if($n<1){		
	$insert = "insert into crmsales_target set 
	mst_usersid='".$mst_usersid."',
	fin_year='".$date."-".$date1."',
	target='".$target_value."',
	published_data='".date('Y-m-d')."',
	created_by='".$_SESSION['UserID']."',
	branch='".$_REQUEST['site']."',
	CompanyId='".$_SESSION['ACGCompanyId']."',
	month='".$month[$k]."',
	yearmonth='".$month1[$k]."'";
	$result_user_target = DB_query($insert,$db);
	}
	}
	}
	$s->pageLocation("index.php?route=CRM/salescrm_target_view".$rurl);
}
if($_REQUEST['action']='edit')
{
	$GetSQL=DB_query("select sum(target) AS TotalAS from crmsales_target where mst_usersid='".$_REQUEST['id']."'",$db);
	$GetData=DB_fetch_array($GetSQL);
}?>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1>Users Target Assigments</h1>
<!-- Start Table 1 -->
<form name="searchform" method="post" action="">
<table width="100%" border="0" cellpadding="3" cellspacing="0">
  <tr class="row1">
  <?php ?>
    <td class="tabledata1">Financial year</td>
    <td><select name="fin_year" style="width:150px;">
	<option value="<?php echo convertSQLDate($_SESSION['financialYrStart']).'-'.convertSQLDate($_SESSION['financialYrEnd']); ?>"><?php echo convertSQLDate($_SESSION['financialYrStart']).' to '.convertSQLDate($_SESSION['financialYrEnd']); ?></option>
	</select></td>
	<td class="tabledata1">Month</td>
    <td><select name="month" style="width:150px;">
	<option value="All">All</option>
	</select></td>
    <td class="tabledata1">Branch</td>
	<td><select name="site" id="site" style="width:150px;">
<option value="0">---Select---</option>
<?php 
$site="select loccode,locationname from locations where loccode IN ('".$_SESSION['AssignedBranch']."')";
$result=DB_query($site,$db);		
while($mrow=DB_fetch_array($result)){?>
<option value="<?php echo $mrow['loccode']?>" <?php if($_REQUEST['site']==$mrow['loccode']) echo "selected";?>><?php echo $mrow['locationname']?></option>
<?php } ?>
</select></td>
  </tr>
 <!-- <tr bgcolor="#e7e7e7">
    <td class="tabledata">Start date</td>
    <td><input name="fdate" type="text" style="width:150px;" class="datepicker" /></td>
    <td class="tabledata">End date</td>
    <td><input name="todate" type="text"  style="width:150px;" class="datepicker" /></td>
 </tr>-->
 </table>
<!-- Close Table 1 -->
<div style="clear:both;"></div>
<!-- Start Table 1 -->
<?php 
//echo "SELECT * FROM deptmaster WHERE deptcode='MKT' OR deptcode='sal' and CompanyId='".$_SESSION['ACGCompanyId']."'";
$Dept=DB_query("SELECT * FROM deptmaster WHERE DEPTNAME LIKE '%sale%' and CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
if(DB_num_rows($Dept))
{
while($DeptRow=DB_fetch_array($Dept))
{
$DCode[]=$DeptRow['id'];
}
$Dept_id=implode(',',$DCode);
 $Dept_id;
$query_target = DB_query("select id,userid,realname from www_users where realname!='Admin' and companyid='".$_SESSION['ACGCompanyId']."' and blocked=0 and department in('".$Dept_id."') and id in($lowerlist) and assigned_sites in(".$_SESSION['AssignedSites'].")",$db);
 //echo "select id,userid,realname from www_users where realname!='Admin' and companyid='".$_SESSION['ACGCompanyId']."' and blocked=0 and id in($SKEList)";
 // $query_target = DB_query("select id,userid,realname from www_users where realname!='Admin' and companyid='".$_SESSION['ACGCompanyId']."' and blocked=0 and id in($SKEList)",$db);
if(DB_num_rows($query_target)>0){?>
<table width="100%" border="1" cellpadding="3" cellspacing="0">
<tr class="row1">
	<td class="tabledata"><strong>User Name</strong></td>
	<td class="tabledata"><strong>Real Name</strong></td>
	<td class="tabledata"><strong>Target amount</strong></td>
	<!--<td class="tabledata">Target Achieved</td>
	<td class="tabledata">Difference</td>-->
</tr>
<?php $i=0;
while($query_targetRow=DB_fetch_array($query_target)){
$TargetDAMTSQL=DB_query("select SUM(target)AS ToatalTarget,mst_usersid from crmsales_target where mst_usersid='".$query_targetRow['id']."'",$db);
$TargetDAMT=DB_fetch_array($TargetDAMTSQL);
$sel='';
$val='';
if($TargetDAMT['mst_usersid']==$query_targetRow['id'])
$sel='readonly';
$val=round($TargetDAMT['ToatalTarget']);?>
<tr class="row1">
	<td><?php echo $query_targetRow['userid']; ?><input name="mst_usersid[<?php echo $i; ?>]" readonly type="hidden" value="<?php echo $query_targetRow['id']; ?>" />
	<input name="user_name[<?php echo $i; ?>]" readonly type="hidden" value="<?php echo $query_targetRow['userid']; ?>" /></td>
	<td><?php echo $query_targetRow['realname']; ?></td>
	<td><input name="target_amm[<?php echo $i; ?>]" <?php echo $sel; ?> type="text" value="<?php echo $val; ?>" style=" width:150px;" /></td>
</tr>
<?php $i++;}?>
<input type="hidden" name="count1" value="<?php echo $i; ?>" />
</table>
<p align="center"><input name="save" type="submit" value="Save" class="search" /></p>
<?php } ?>
</form>
<?php }?>
</div><!-- Close Table 1 -->
</from>
</div></div>
<script>
function showdata(i)
{
document.getElementById("targetamm_"+i).removeAttribute('readOnly');	
}	
</script>