<link rel="stylesheet" href="css/new_mxerp.css">

<?php 
if(isset($_POST['save']))
{  
$Del=DB_query("delete from crmsales_target where mst_usersid='".$_REQUEST['id']."'",$db);
//echo $_REQUEST['id'];
//exit;
  $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);
	


	$mst_usersid=$_REQUEST['mst_usersid'];
	$target_value=$_REQUEST['target_amm']/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);
								 	}
	}
								
								
	

echo "<script>location.href='index.php?route=CRM/salescrm_target_view&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
	
}


if($_REQUEST['action']='edit')
{
	$GetSQL=DB_query("SELECT SUM(target)AS TotalAS,fin_year,branch as BRAN from crmsales_target where mst_usersid='".$_REQUEST['id']."'",$db);
	$GetData=DB_fetch_array($GetSQL);
	
}


?>

<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>
	<?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);
	 ?>
    <td><select name="fin_year" style="width:150px;">
	<option value="000">Select</option>	
	<option value="<?php echo $FinYRow['SYear'].'-'.$FinYRow['EYEAR']; ?>"<?php if($GetData['fin_year']==$FinYRow['SYear'].'-'.$FinYRow['EYEAR']){?>selected="selected" <?php } ?> ><?php echo $FinYRow['SYear'].'-'.$FinYRow['EYEAR']; ?></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 CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in('".$_SESSION['AssignedBranch']."')";
$result=DB_query($site,$db);		
while($mrow=DB_fetch_array($result)){
?>
<option value="<?php echo $mrow['loccode']?>" <?php echo($GetData['BRAN']==$mrow['loccode']?'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 -->


<table width="100%" border="1" cellpadding="3" cellspacing="0">
  <tr class="row1">
    <td class="tabledata">User Name</td>
    <td class="tabledata">Target amount</td>
    <!--<td class="tabledata">Target Achieved</td>
    <td class="tabledata">Difference</td>-->
  </tr>

<tr class="row1">
    <td><input name="mst_usersid" readonly type="hidden" value="<?php echo $_REQUEST['id']; ?>" />
	<input name="user_name]" readonly type="text" value="<?php echo $s->GetFieldValue('www_users',"id='".$_REQUEST['id']."'",'userid');; ?>" /></td>
	<td><input name="target_amm" type="text" value="<?php echo round($GetData['TotalAS']); ?>" style=" width:150px;" /></td>
</tr>
</table>

<p align="center"><input name="save" type="submit" value="Save" class="search" /></p>


</form>

</div><!-- Close Table 1 -->




</from>

</div></div>
<script>
function showdata(i)
{
document.getElementById("targetamm_"+i).removeAttribute('readOnly');	
}	
	
	
	
</script>