<?php

require "showshp.php";

session_start();
if(!isset($_SESSION['titles']) || !isset($_SESSION['positions'])|| !isset($_SESSION['usernames'])){
header("location:prdlogings.php");
}


?>


<html>


<head>

<link rel="stylesheet" type="text/css" href="rdsty.css">
<link rel="stylesheet" type="text/css" media="all" href="jsDatePick_ltr.min.css" />
<link rel="stylesheet" type="text/css" media="all" href="jsDatePick_ltr.css" />
<script type="text/javascript" src="jsDatePick.min.1.3.js"></script>
<script src="datesps.js"></script>

<script>

function mnh(mn,bn)
{



}


function mch(mn)
{
if(mn.style.background!='silver'){
mn.style.background='orange';}
}


function mnc(mn)
{
if(mn.style.background!='silver'){
mn.style.background='white';}

}


function agentsn()
{
var rg=document.getElementById('regions');
var ag=document.getElementById('agents');

var rvl=rg.options[rg.selectedIndex].text;
ag.length=0;

var httpr= new XMLHttpRequest();
httpr.onreadystatechange=function(){
if(httpr.readyState==4 && httpr.status==200){
var vl=httpr.responseText;
var tmp=vl.split("###");
for(cn=0;cn<tmp.length;cn++){
var ops=document.createElement("option");
ops.text=tmp[cn];
ag.add(ops,null);}
}}
var ops=document.createElement("option");
ops.text="All";
ag.add(ops,null);

httpr.open("POST","aj.php?actp=agentsn&sparams="+rvl,true);
httpr.send();
}

</script>

</head>



<body style="font-family:calibri;font-size:small" onload="mnh('amnc','agentsb')">
<div class="hdn"></div>

<?php
printf("<div class='ses'>%s (%s)</div>",$_SESSION['titles'],$_SESSION['positions']);
?>

<div class="tops" align="center">Target Agent range summary</div>
<div class="dashbs"><a href="prdashbs.php" style='color:orange;'><b>Dashboard</b></a></div>
<div class="homes"><a href="prdlogings.php" style='color:#A9BCF5;'>Sign out</a></div>

</body>

</html>




<?php

$grt=0;
$grq=0;
$arr=array();
$arvl=array();
$tvt=0;

$starts=$_REQUEST['starts'];
$ends=$_REQUEST['ends'];

if(isset($_REQUEST['zones'])){
$zones=$_REQUEST['zones'];}
else{
$zones="All";}

if(isset($_REQUEST['regions'])){
$regions=$_REQUEST['regions'];}
else{
$regions="All";}

if(isset($_REQUEST['territorys'])){
$territorys=$_REQUEST['territorys'];}
else{
$territorys="All";}

if(isset($_REQUEST['agents'])){
$agents=$_REQUEST['agents'];}
else{
$agents="All";}

if(isset($_REQUEST['rsms'])){
$rsms=$_REQUEST['rsms'];}
else{
$rsms="All";}

if(isset($_REQUEST['executives'])){
$executives=$_REQUEST['executives'];}
else{
$executives="All";}

if(isset($_REQUEST['reps'])){
$reps=$_REQUEST['reps'];}
else{
$reps="All";}

if(isset($_REQUEST['ranges'])){
$ranges=$_REQUEST['ranges'];}
else{
$ranges="All";}

if(isset($_REQUEST['subranges'])){
$subranges=$_REQUEST['subranges'];}
else{
$subranges="All";}

if(isset($_REQUEST['items'])){
$items=$_REQUEST['items'];}
else{
$items="All";}

$rpsty="regions=".$regions."&agents=".$agents."&territorys=".$territorys."&ranges=".$ranges."&zones=".$zones."&rsms=".$rsms."&executives=".$executives."&reps=".$reps
."&items=".$items."&subranges=".$subranges; 


$months=date('m');
$days=date('t');
$years=date('Y');

if($starts==0 || $starts==''){
$starts=$years."-".$months."-01";}
if($ends==0 || $ends==''){
$ends=$years."-".$months."-".$days;}


showshps();

printf("<div class='dts'><table style='font-size:14'><tr style='border-bottom:1px solid silver;'><td width=200><b><font color=#0066cc>Report details</font></td></tr</table>");
printf("<table><table style='font-size:13'>");
printf("<tr><td>Title:</td><td>Agent range target achievement</td></tr>");
printf("<tr><td>From:</td><td>%s</td></tr>",$starts);
printf("<tr><td>To:</td><td>%s</td></tr>",$ends);

if(isset($_REQUEST['zones'])){
$zones=$_REQUEST['zones'];
printf("<tr><td>Zone:</td><td>%s</td></tr>",$zones);}
else{
$zones="All";}

if(isset($_REQUEST['regions'])){
$regions=$_REQUEST['regions'];
printf("<tr><td>Region:</td><td>%s</td></tr>",$regions);}
else{
$regions="All";}

if(isset($_REQUEST['territorys'])){
$territorys=$_REQUEST['territorys'];
printf("<tr><td>Territory:</td><td>%s</td></tr>",$territorys);}
else{
$territorys="All";}

if(isset($_REQUEST['agents'])){
$agents=$_REQUEST['agents'];
printf("<tr><td>Agent:</td><td>%s</td></tr>",$agents);}
else{
$agents="All";}

if(isset($_REQUEST['rsms'])){
$rsms=$_REQUEST['rsms'];
printf("<tr><td>RSM:</td><td>%s</td></tr>",$rsms);}
else{
$rsms="All";}

if(isset($_REQUEST['executives'])){
$executives=$_REQUEST['executives'];
printf("<tr><td>Executive:</td><td>%s</td></tr>",$executives);}
else{
$executives="All";}

if(isset($_REQUEST['reps'])){
$reps=$_REQUEST['reps'];
printf("<tr><td>Rep:</td><td>%s</td></tr>",$reps);}
else{
$reps="All";}

if(isset($_REQUEST['ranges'])){
$ranges=$_REQUEST['ranges'];
printf("<tr><td>Range:</td><td>%s</td></tr>",$ranges);}
else{
$ranges="All";}

if(isset($_REQUEST['subranges'])){
$subranges=$_REQUEST['subranges'];
printf("<tr><td>Sub-range:</td><td>%s</td></tr>",$subranges);}
else{
$subranges="All";}

if(isset($_REQUEST['items'])){
$items=$_REQUEST['items'];
printf("<tr><td>Item:</td><td>%s</td></tr>",$items);}
else{
$items="All";}

printf("</table></div>");


printf("<div class='srg'></div>");
printf("<div class='mains'>");


$conn=mysqli_connect("localhost","primarysales","S5L7lF01EQZvQrGK","primarysales_dianaold");
$seflt=" where agent_names is not null ";

$sesqlst="select * from acs where user_names='".$_SESSION['usernames']."'";
$seqry=mysqli_query($conn,$sesqlst);

while($serst=mysqli_fetch_row($seqry)){
if(strtolower($serst[1])!='all'){
$seflt=$seflt." and zones='".$serst[1]."' ";}
if(strtolower($serst[2])!='all'){
$seflt=$seflt." and regions='".$serst[2]."' ";}
if(strtolower($serst[3])!='all'){
$seflt=$seflt." and territory_names='".$serst[3]."' ";}
if(strtolower($serst[4])!='all'){
$seflt=$seflt." and agent_names='".$serst[4]."' ";}
if(strtolower($serst[5])!='all'){
$seflt=$seflt." and item_names='".$serst[5]."' ";}
if(strtolower($serst[6])!='all'){
$seflt=$seflt." and item_ranges='".$serst[6]."' ";}
if(strtolower($serst[7])!='all'){
$seflt=$seflt." and sub_ranges='".$serst[7]."' ";}
if(strtolower($serst[8])!='all'){
$seflt=$seflt." and executive_names='".$serst[8]."' ";}
if(strtolower($serst[9])!='all'){
$seflt=$seflt." and chanell='".$serst[9]."' ";}
}

#echo $seflt;


printf("<form method='post' action='prtgagentrangesummary.php?%s'>",$rpsty);
printf("<div class='fra'>From</div><div class='frb'>
<input type='text' size=10 class='starts'  name='starts' value='%s' id='inputField'></div><div class='frc'>To</div><div class='frd'><input type='text class='ends' size=10 name='ends' value='%s' id='inputField2'></div>
<div class='fre'><input type='submit' value='go' style='width:70'></div><div class='frf'>Search</div><div class='frg'>
<input type='text' size=20 id='dtfinds' autocomplete='off' onkeydown=\"fdtfinds(event)\"></div></form>",$starts,$ends);


#printf("<div style='color:#045FB4' class='frk'><input type='radio' name='chsh' checked onclick=\"document.getElementById('tdta').style.display='block';document.getElementById('tdtb').style.display='block';\">Values</div>");
#printf("<div style='color:#045FB4'  class='frm'><input type='radio' name='chsh'  onclick=\"document.getElementById('tdta').style.display='none';document.getElementById('tdtb').style.display='block';\"> %%</div>");
#printf("<div  style='color:#045FB4' class='frn'><input type='radio' name='chsh'  onclick=\"document.getElementById('tdta').style.display='block';document.getElementById('tdtb').style.display='block';\">Both</div>");


$flt=$seflt." and dates>='".$_REQUEST['starts']."' and dates<='".$_REQUEST['ends']."' ";
if(strtolower($regions)!='all'){
$flt=$flt." and regions='".$regions."' ";}
if(strtolower($zones)!='all'){
$flt=$flt." and zones='".$zones."' ";}
if(strtolower($agents)!='all'){
$flt=$flt." and agent_names='".$agents."' ";}
if(strtolower($territorys)!='all'){
$flt=$flt." and territory_names='".$territorys."' ";}
if(strtolower($ranges)!='all'){
$flt=$flt." and item_ranges='".$ranges."' ";}
if(strtolower($subranges)!='all'){
$flt=$flt." and sub_ranges='".$subranges."' ";}




$asqlst=" left join (select agent_names as tgagent_names,sum((pcs_qty*pcs_price)+(box_qty*box_price)) as tgvls,sum(margin_rates*(pcs_qty*pcs_price)+(box_qty*box_price)) as tgmargins
 from rc_targets ".$flt." group by agent_names) as targets on tgagent_names=magent_names ";

$asqlst=$asqlst." left join (select agent_names as acagent_names,sum((pcs_qty*pcs_price)+(box_qty*box_price)) as acvls,sum((pcs_qty*pcs_price)+(box_qty*box_price)*discount_rates) as acmargins 
from rc_invoices ".$flt." group by agent_names) as acts on acagent_names=magent_names ";


$rsqlst="select magent_names as region ";

$rsqlst=$rsqlst.",tgvls,acvls,tgmargins,acmargins ";

$rsqlst=$rsqlst." from (select distinct agent_name as magent_names from agentsmrd where agent_name is NOT NULL ";
if(strtolower($regions)!='all'){
$rsqlst=$rsqlst."and  region='".$regions."'";}
if(strtolower($zones)!='all'){
$rsqlst=$rsqlst." and zone='".$zones."'";}
if(strtolower($agents)!='all'){
$rsqlst=$rsqlst." and agent_name='".$agents."'";}

$rsqlst=$rsqlst.") as rgs ";


$sqlst=$rsqlst.$asqlst;
#echo $sqlst;

#printf("<div class='tdt'>");
printf("<br>");


$qry=mysqli_query($conn,$sqlst);

printf("<BR>");

print("<div style='display:block;font-size:14;' id='tdta'>");
printf("<br>");
printf("<table style='font-size:13;width:100%%'><th bgcolor='silver' colspan='10' align='center'><b>Agent range target achievement</th><tr  style='color:#E6E6E6' bgcolor='grey'>");

printf("<tr class='cls'><td margin=15%% align='center'>Agent</td><td margin=15%% align='center'>Target sales</td><td margin=15%% align='center'>ACT sales</td>
<td margin=15%% align='center'>Salles variance</td><td margin=15%% align='center'>Target margin</td><td margin=15%% align='center'>ACT margin</td>
<td margin=15%% align='center'>Margin variance</td><td margin=15%% align='center'>Agent list</td><td margin=15%% align='center'>Item list</td></tr>");

while($rst=mysqli_fetch_row($qry)){
if(($rst[1]+$rst[2])!=0){

$itemshrf="<a href='prtgitemsummary.php?rpsty=agentsummarys&starts=".$starts."&ends=".$ends."&regions=".$regions."&agents=".urlencode($rst[0])."&territorys=all&ranges=all&zones=".$zones."&rsms=All&executives=All&reps=All&items=All' target='_blank'>Item list</a>";
$agentshrf="<a href='prtgagentsummary.php?rpsty=agentsummarys&starts=".$starts."&ends=".$ends."&regions=".$regions."&agents=".urlencode($rst[0])."&territorys=all&ranges=all&zones=".$zones."&rsms=All&executives=All&reps=All&items=All' target='_blank'>Agent list</a>";
	
printf("<tr class='cls' style='background:silver;'><td margin=15%%>%s</td><td align='right' margin=15%%>%s</td><td align='right' margin=15%%>%s</td><td align='right'>%s</td>
<td align='right' margin=15%%>%s</td><td align='right' margin=15%%>%s</td><td align='right' margin=15%%>%s</td><td align='center'>%s</td><td margin=15%% align='center'>%s</td></tr>",
$rst[0],number_format($rst[1],0,'.',','),number_format($rst[2],0,'.',','),number_format($rst[1]-$rst[2],0,'.',','),number_format($rst[3],0,'.',','),number_format($rst[4],0,'.',','),
number_format($rst[3]-$rst[4],0,'.',','),$itemshrf,$agentshrf);

$asqlst=" left join (select item_ranges as tgagent_names,sum((pcs_qty*pcs_price)+(box_qty*box_price)) as tgvls,sum(margin_rates*(pcs_qty*pcs_price)+(box_qty*box_price)) as tgmargins
 from rc_targets ".$flt." and agent_names='".$rst[0]."' group by item_ranges) as targets on tgagent_names=magent_names ";

$asqlst=$asqlst." left join (select item_ranges as acagent_names,sum((pcs_qty*pcs_price)+(box_qty*box_price)) as acvls,sum((pcs_qty*pcs_price)+(box_qty*box_price)*margin_rates) as acmargins 
from rc_invoices ".$flt." and agent_names='".$rst[0]."' group by item_ranges) as acts on acagent_names=magent_names ";


$rsqlst="select magent_names as region ";

$rsqlst=$rsqlst.",tgvls,acvls,tgmargins,acmargins ";

$rsqlst=$rsqlst." from (select distinct item_ranges as magent_names from itemsmrd ";
if(strtolower($ranges)!='all'){
$rsqlst=$rsqlst." where item_ranges='".$ranges."'";}

$rsqlst=$rsqlst.") as rgs ";


$sqlst=$rsqlst.$asqlst;
#echo $sqlst;

$rqry=mysqli_query($conn,$sqlst);

while($qrst=mysqli_fetch_row($rqry)){

$itemshrf="<a href='prtgitemsummary.php?rpsty=agentsummarys&starts=".$starts."&ends=".$ends."&regions=".$regions."&agents=".$rst[0]."&territorys=all&ranges=".$qrst[0]."&zones=".$zones."&rsms=All&executives=All&reps=All&items=All' target='_blank'>Item list</a>";
$agentshrf="<a href='prtgagentsummary.php?rpsty=agentsummarys&starts=".$starts."&ends=".$ends."&regions=".$regions."&agents=".$rst[0]."&territorys=all&ranges=".$qrst[0]."&zones=".$zones."&rsms=All&executives=All&reps=All&items=All' target='_blank'>Agent list</a>";
	
printf("<tr class='cls'><td margin=15%%>%s</td><td align='right' margin=15%%>%s</td><td align='right' margin=15%%>%s</td><td align='right'>%s</td>
<td align='right' margin=15%%>%s</td><td align='right' margin=15%%>%s</td><td align='right' margin=15%%>%s</td><td align='center'>%s</td><td margin=15%% align='center'>%s</td></tr>",
$qrst[0],number_format($qrst[1],0,'.',','),number_format($qrst[2],0,'.',','),number_format($qrst[1]-$qrst[2],0,'.',','),number_format($qrst[3],0,'.',','),
number_format($qrst[4],0,'.',','),number_format($qrst[3]-$qrst[4],0,'.',','),$itemshrf,$agentshrf);
}
printf("<tr><td colspan=8>-</td></tr>");
}

}	



printf("</tr></table></div>");



?>