<?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 fdtfinds(pr)
{

var vl=document.getElementById("dtfinds").value;
var dta=document.getElementById("dtda");
var dtb=document.getElementById("dtdb");
var cn=dta.rows.length;
var vls=vl.toLowerCase();

for(var cnt=2;cnt<cn;cnt++){
	
var tmp=dta.rows[cnt].cells[0].innerHTML;
var tmpa=tmp.toLowerCase();
var tmp=dta.rows[cnt].cells[1].innerHTML;
var tmpb=tmp.toLowerCase();
var sta=tmpa.indexOf(vls);
var stb=tmpb.indexOf(vls);

if((sta>=0 || stb>=0 ) && vls.length>0){
dta.rows[cnt].style.display="table-row";
dtb.rows[cnt].style.display="table-row";}
else{
dta.rows[cnt].style.display="none";
dtb.rows[cnt].style.display="none";}

if(vls.length==0){
dta.rows[cnt].style.display="table-row";
dtb.rows[cnt].style.display="table-row";}
}

}


</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">Market return region trend</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";}

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


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

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

if(isset($_REQUEST['mractions'])){
$mractions=$_REQUEST['mractions'];}
else{
$mractions="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;}

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

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

$cnt=mysqli_num_rows($seqry);

while($serst=mysqli_fetch_row($seqry)){
$cnt--;

$seflt=$seflt." ( agent_names is not null ";

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]."' ";}

$seflt=$seflt.") ";

if($cnt>0){
$seflt=$seflt." or ";}
}

$seflt=$seflt.") ";

#$flt=$seflt." and dates>='".$_REQUEST['starts']."' and dates<='".$_REQUEST['ends']."' ";

$flt=$seflt;
if(strtolower($zones)!='all'){
$flt=$flt." and zones='".$zones."' ";}
if(strtolower($regions)!='all'){
$flt=$flt." and regions='".$regions."' ";}
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."' ";}
if(strtolower($items)!='all'){
$flt=$flt." and item_names='".$items."' ";}
if(strtolower($agenttypes)!='all'){
$flt=$flt." and agent_types='".$agenttypes."' ";}
if(strtolower($mrtypes)!='all'){
$flt=$flt." and mr_types='".$mrtypes."' ";}
if(strtolower($mrreasons)!='all'){
$flt=$flt." and reasons='".$mrreasons."' ";}
if(strtolower($mractions)!='all'){
$flt=$flt." and actions='".$mractions."' ";}



$rlt=" where agent_name is not null ";
if(strtolower($regions)!='all'){
$rlt=$rlt." and region='".$regions."' ";}
if(strtolower($zones)!='all'){
$rlt=$rlt." and zone='".$zones."' ";}
if(strtolower($agents)!='all'){
$rlt=$rlt." and agent_name='".$agents."' ";}


mrshowshps();

reportdets("Region trend");

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

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



printf("<div class='frf'>Search</div><div class='frg'><input type='text' size=20 id='dtfinds' autocomplete='off' onkeydown=\"fdtfinds(event)\"></div></form>",$starts,$ends);


$conn=mysqli_connect("localhost","primarysales","S5L7lF01EQZvQrGK","primarysales_dianaold");
printf("<div class='tdt' style='width:100%%;'>");


if($_REQUEST['dtd']=='dtdaa' || strtolower($_REQUEST['dtd'])=='all'){

print("<div style='display:block;font-size:13;' id='tdta'>");
printf("<br>");
printf("<table id='dtda' style='font-size:13;width:100%%;'><th bgcolor='silver' colspan='12' align='center'><b>Market return region treand - Value</th>",$exchrf);

printf("<tr class='cls'  bgcolor='grey' style='color:#E6E6E6;'><td padding=1%% align='center' style='border:1px solid white;'>Region</td>");


$arr=array();

$months=date('m');
$days=date('t');
$years=date('Y');
 
$sqlst="select mranges";
$rsqlst="";
 
for($cnt=0;$cnt<6;$cnt++){
$arr[$cnt]=0;	
if($months==0){
$months=12;
$years--;}

$dates=date_create($years.'-'.$months);
printf("<td padding=1%% align='center'  style='border:1px solid white;'>%s</td>",date_format($dates,'M-Y'));
 
$sqlst=$sqlst.",xx".$months.$years.".vls";
 
$rsqlst=$rsqlst." left join (select regions tranges,sum(pcs_qty*pcs_price*(100-disocunt_rates)/100) as vls from market_returns".$flt." and dates>='".$years."-".$months."-1' and
dates<='".$years."-".$months."-".cal_days_in_month(CAL_GREGORIAN,$months,$years)."' group by regions) as xx".$months.$years." on xx".$months.$years.".tranges=mitems.mranges ";
$months--;        
}
 
$sqlst=$sqlst." from (select distinct regions as mranges from market_returns  ".$flt." ) as mitems ";
$sqlst=$sqlst.$rsqlst." order by mranges ";
 
#echo $sqlst;

printf("<td padding=1%% align='center' style='border:1px solid white'>Total</td>");
printf("<td padding=1%% align='center' style='border:1px solid white'>Reasons</td>");
printf("<td padding=1%% align='center' style='border:1px solid white'>Agents</td>");
printf("<td padding=1%% align='center' style='border:1px solid white'>Ranges</td>");
printf("<td padding=1%% align='center' style='border:1px solid white'>Items</td>");
printf("</tr>");

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

while($rst=mysqli_fetch_row($qry)){
$ttv=0;

$tmpvl=0;
for($cn=1;$cn<$fcn;$cn++){
$tmpvl=$tmpvl+$rst[$cn];}

if($tmpvl!=0){
printf("<tr class='cls'><td padding=1%%>%s</td>",$rst[0]);

$hrfreasons="<a href='mrhsreasonsummary.php?rpsty=agentsummarys&dtd=all&starts=".$starts."&ends=".$ends."&regions=".$rst[0]."&agents=".$agents."&territorys=all&ranges=all&zones=".$zones."&rsms=All&executives=All&reps=All&items=All&mrtypes=".$mrtypes."&mrreasons=".$mrreasons."&mractions=".$mractions."' target='_blank'>Reasons</a>";

$hrfagents="<a href='mrhsagentsummary.php?rpsty=agentsummarys&dtd=all&starts=".$starts."&ends=".$ends."&regions=".$rst[0]."&agents=".$agents."&territorys=all&ranges=all&zones=".$zones."&rsms=All&executives=All&reps=All&items=All&mrtypes=".$mrtypes."&mrreasons=".$mrreasons."&mractions=".$mractions."' target='_blank'>Agents</a>";

$hrfranges="<a href='mrhsrangesummary.php?rpsty=agentsummarys&dtd=all&starts=".$starts."&ends=".$ends."&regions=".$rst[0]."&agents=".$agents."&territorys=all&ranges=all&zones=".$zones."&rsms=All&executives=All&reps=All&items=All&mrtypes=".$mrtypes."&mrreasons=".$mrreasons."&mractions=".$mractions."' target='_blank'>Ranges</a>";

$hrfitems="<a href='mrhsitemsummary.php?rpsty=agentsummarys&dtd=all&starts=".$starts."&ends=".$ends."&regions=".$rst[0]."&agents=".$agents."&territorys=all&ranges=all&zones=".$zones."&rsms=All&executives=All&reps=All&items=All&mrtypes=".$mrtypes."&mrreasons=".$mrreasons."&mractions=".$mractions."' target='_blank'>Items</a>";


for($cn=1;$cn<$fcn;$cn++){
$arr[$cn]=$arr[$cn]+$rst[$cn];
$ttv=$ttv+$rst[$cn];
	
printf("<td align='right' padding=1%%>%s</td>",number_format($rst[$cn],0,'.',','));}

printf("<td align='right' padding=1%%>%s</td><td align='center'>%s</td><td align='center'>%s</td><td align='center'>%s</td><td align='center'>%s</td></tr>",number_format($ttv,0,'.',','),$hrfreasons,$hrfagents,$hrfranges,$hrfitems);
}
}

$hrfreasons="<a href='mrhsreasonsummary.php?rpsty=agentsummarys&dtd=all&starts=".$starts."&ends=".$ends."&regions=".$regions."&agents=".$agents."&territorys=all&ranges=all&zones=".$zones."&rsms=All&executives=All&reps=All&items=All&mrtypes=".$mrtypes."&mrreasons=".$mrreasons."&mractions=".$mractions."' target='_blank'>Reasons</a>";

$hrfagents="<a href='mrhsagentsummary.php?rpsty=agentsummarys&dtd=all&starts=".$starts."&ends=".$ends."&regions=".$regions."&agents=".$agents."&territorys=all&ranges=all&zones=".$zones."&rsms=All&executives=All&reps=All&items=All&mrtypes=".$mrtypes."&mrreasons=".$mrreasons."&mractions=".$mractions."' target='_blank'>Agents</a>";

$hrfranges="<a href='prhrangesummary.php?rpsty=agentsummarys&dtd=all&starts=".$starts."&ends=".$ends."&regions=".$regions."&agents=".$agents."&territorys=all&ranges=all&zones=".$zones."&rsms=All&executives=All&reps=All&items=All&mrtypes=".$mrtypes."&mrreasons=".$mrreasons."&mractions=".$mractions."' target='_blank'>Ranges</a>";

$hrfitems="<a href='mrhsitemsummary.php?rpsty=agentsummarys&dtd=all&starts=".$starts."&ends=".$ends."&regions=".$regions."&agents=".$agents."&territorys=all&ranges=all&zones=".$zones."&rsms=All&executives=All&reps=All&items=All&mrtypes=".$mrtypes."&mrreasons=".$mrreasons."&mractions=".$mractions."' target='_blank'>Items</a>";

$ttv=0;
printf("<tr class='cls' style='font-weight:bold;'><td>Total</td>");
for($cn=1;$cn<$fcn;$cn++){
$ttv=$ttv+$arr[$cn];
printf("<td align='right' padding=1%%>%s</td>",number_format($arr[$cn],0,'.',','));}
printf("<td align='right' padding=1%%>%s</td>",number_format($ttv,0,'.',','));

printf("<td align='center'>%s</td><td align='center'>%s</td><td align='center'>%s</td><td align='center'>%s</td></tr></table>",$hrfreasons,$hrfagents,$hrfranges,$hrfitems);
}

////////////////////////////////////////

if($_REQUEST['dtd']=='dtdcc' || strtolower($_REQUEST['dtd'])=='all'){

printf("<BR><BR>");

print("<div style='display:block;font-size:13;' id='tdta'>");
printf("<br>");
printf("<table id='dtdb' style='font-size:13;width:100%%;'><th bgcolor='silver' colspan='12' align='center'><b>Market return region trend - tonnage(Kg)</th>
",$exchrf);

printf("<tr class='cls'  bgcolor='grey' style='color:#E6E6E6;'><td padding=1%% align='center' style='border:1px solid white;'>Region</td>");


$arr=array();

$months=date('m');
$days=date('t');
$years=date('Y');
 
$sqlst="select mranges";
$rsqlst="";
 
for($cnt=0;$cnt<6;$cnt++){
$arr[$cnt]=0;	
if($months==0){
$months=12;
$years--;}

$dates=date_create($years.'-'.$months);
printf("<td padding=1%% align='center' style='border:1px solid white;'>%s</td>",date_format($dates,'M-Y'));
 
$sqlst=$sqlst.",xx".$months.$years.".vls";
 
$rsqlst=$rsqlst." left join (select regions as tranges,sum(pcs_qty*packet_weights)/1000 as vls from market_returns ".$flt." and  dates>='".$years."-".$months."-1' and
dates<='".$years."-".$months."-".cal_days_in_month(CAL_GREGORIAN,$months,$years)."' group by regions) as xx".$months.$years." on xx".$months.$years.".tranges=mitems.mranges ";
$months--;        
}
 
$sqlst=$sqlst." from (select distinct regions as mranges from market_returns ".$flt." ) as mitems ";
$sqlst=$sqlst.$rsqlst." order by mranges";
 
#echo $sqlst;
printf("<td padding=1%% align='center' style='border:1px solid white'>Total</td>");
printf("<td padding=1%% align='center' style='border:1px solid white'>Reasons</td>");
printf("<td padding=1%% align='center' style='border:1px solid white'>Agents</td>");
printf("<td padding=1%% align='center' style='border:1px solid white'>Ranges</td>");
printf("<td padding=1%% align='center' style='border:1px solid white'>Items</td>");
printf("</tr>");

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

while($rst=mysqli_fetch_row($qry)){
$ttv=0;

$hrfreasons="<a href='mrhsreasonsummary.php?rpsty=agentsummarys&dtd=all&starts=".$starts."&ends=".$ends."&regions=".$rst[0]."&agents=".$agents."&territorys=all&ranges=all&zones=".$zones."&rsms=All&executives=All&reps=All&items=All&mrtypes=".$mrtypes."&mrreasons=".$mrreasons."&mractions=".$mractions."' target='_blank'>Reasons</a>";

$hrfagents="<a href='mrhsagentsummary.php?rpsty=agentsummarys&dtd=all&starts=".$starts."&ends=".$ends."&regions=".$rst[0]."&agents=".$agents."&territorys=all&ranges=all&zones=".$zones."&rsms=All&executives=All&reps=All&items=All&mrtypes=".$mrtypes."&mrreasons=".$mrreasons."&mractions=".$mractions."' target='_blank'>Agents</a>";

$hrfranges="<a href='mrhsrangesummary.php?rpsty=agentsummarys&dtd=all&starts=".$starts."&ends=".$ends."&regions=".$rst[0]."&agents=".$agents."&territorys=all&ranges=all&zones=".$zones."&rsms=All&executives=All&reps=All&items=All&mrtypes=".$mrtypes."&mrreasons=".$mrreasons."&mractions=".$mractions."' target='_blank'>Ranges</a>";

$hrfitems="<a href='mrhsitemsummary.php?rpsty=agentsummarys&dtd=all&starts=".$starts."&ends=".$ends."&regions=".$rst[0]."&agents=".$agents."&territorys=all&ranges=all&zones=".$zones."&rsms=All&executives=All&reps=All&items=All&mrtypes=".$mrtypes."&mrreasons=".$mrreasons."&mractions=".$mractions."' target='_blank'>Items</a>";

$tmpvl=0;
for($cn=1;$cn<$fcn;$cn++){
$tmpvl=$tmpvl+$rst[$cn];}

if($tmpvl!=0){
printf("<tr class='cls'><td padding=1%%>%s</td>",$rst[0]);

for($cn=1;$cn<$fcn;$cn++){
$arr[$cn-1]=$arr[$cn-1]+$rst[$cn];
$ttv=$ttv+$rst[$cn];
	
printf("<td align='right' padding=1%%>%s</td>",number_format($rst[$cn],0,'.',','));
}

printf("<td align='right' padding=1%%>%s</td><td align='center'>%s</td><td align='center'>%s</td><td align='center'>%s</td><td align='center'>%s</td></tr>",number_format($ttv,0,'.',','),$hrfreasons,$hrfagents,$hrfranges,$hrfitems);
}
}

$hrfreasons="<a href='mrhsreasonsummary.php?rpsty=agentsummarys&dtd=all&starts=".$starts."&ends=".$ends."&regions=".$regions."&agents=".$agents."&territorys=all&ranges=all&zones=".$zones."&rsms=All&executives=All&reps=All&items=All&mrtypes=".$mrtypes."&mrreasons=".$mrreasons."&mractions=".$mractions."' target='_blank'>Reasons</a>";

$hrfagents="<a href='mrhsagentsummary.php?rpsty=agentsummarys&dtd=all&starts=".$starts."&ends=".$ends."&regions=".$regions."&agents=".$agents."&territorys=all&ranges=all&zones=".$zones."&rsms=All&executives=All&reps=All&items=All&mrtypes=".$mrtypes."&mrreasons=".$mrreasons."&mractions=".$mractions."' target='_blank'>Agents</a>";

$hrfranges="<a href='prhrangesummary.php?rpsty=agentsummarys&dtd=all&starts=".$starts."&ends=".$ends."&regions=".$regions."&agents=".$agents."&territorys=all&ranges=all&zones=".$zones."&rsms=All&executives=All&reps=All&items=All&mrtypes=".$mrtypes."&mrreasons=".$mrreasons."&mractions=".$mractions."' target='_blank'>Ranges</a>";

$hrfitems="<a href='mrhsitemsummary.php?rpsty=agentsummarys&dtd=all&starts=".$starts."&ends=".$ends."&regions=".$regions."&agents=".$agents."&territorys=all&ranges=all&zones=".$zones."&rsms=All&executives=All&reps=All&items=All&mrtypes=".$mrtypes."&mrreasons=".$mrreasons."&mractions=".$mractions."' target='_blank'>Items</a>";


$ttv=0;
printf("<tr class='cls' style='font-weight:bold;'><td>Total</td>");
for($cn=1;$cn<$fcn;$cn++){
$ttv=$ttv+$arr[$cn];
printf("<td align='right' padding=1%%>%s</td>",number_format($arr[$cn-1],0,'.',','));}
printf("<td align='right' padding=1%%>%s</td>",number_format($ttv,0,'.',','));

printf("<td align='center'>%s</td><td align='center'>%s</td><td align='center'>%s</td><td align='center'>%s</td></tr></table>",$hrfreasons,$hrfagents,$hrfranges,$hrfitems);

}

printf("</div>");



?>