<?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 dtc=document.getElementById("dtdc");
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+1].style.display="table-row";
dtc.rows[cnt].style.display="table-row";}
else{
dta.rows[cnt].style.display="none";
dtb.rows[cnt+1].style.display="none";
dtc.rows[cnt].style.display="none";}

if(vls.length==0){
dta.rows[cnt].style.display="table-row";
dtb.rows[cnt+1].style.display="table-row";
dtc.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">Target zone summary</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;}

$conn=mysqli_connect("localhost","primarysales","S5L7lF01EQZvQrGK","primarysales_diana");
$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]."' ";}
if(strtolower($serst[8])!='all'){
$seflt=$seflt." and executive_names='".$serst[8]."' ";}
if(strtolower($serst[9])!='all'){
$seflt=$seflt." and chanell='".$serst[9]."' ";}


$seflt=$seflt.") ";

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

$seflt=$seflt.") ";

$flt=$seflt;
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($ranges)!='all'){
$flt=$flt." and item_ranges='".$ranges."' ";}
if(strtolower($items)!='all'){
$flt=$flt." and item_names='".$items."' ";}
if(strtolower($subranges)!='all'){
$flt=$flt." and sub_ranges='".$subranges."' ";}
if(strtolower($territorys)!='all'){
$flt=$flt." and territory_names='".$territorys."' ";}

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>Trend zone 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'>");



printf("<div class='frf'>Search</div><div class='frg'><input type='text' size=20 id='dtfinds' autocomplete='off' onkeydown=\"fdtfinds(event)\"></div>",$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>");


$conn=mysqli_connect("localhost","primarysales","S5L7lF01EQZvQrGK","primarysales_diana");
if($_REQUEST['dtd']=='dtdbb' || strtolower($_REQUEST['dtd'])=='all'){

printf("<BR>");

print("<div style='display:block;font-size:14;' id='tdta'>");
printf("<br><br>");
printf("<table id='dtda' style='font-size:13;width:100%%;'><th bgcolor='grey' colspan='12' align='center'><b>Zone Sales value trend</th>",$exchrf);

printf("<tr class='cls'><td padding=1%% align='center' bgcolor='silver' >Zone</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='background:silver;'>%s</td>",date_format($dates,'M-Y'));
 
$sqlst=$sqlst.",xx".$months.$years.".vls";
 
$rsqlst=$rsqlst." left join (select zones as tranges,sum(box_qty*box_price) as vls from rc_invoices ".$flt." and dates>='".$years."-".$months."-1' and
dates<='".$years."-".$months."-".cal_days_in_month(CAL_GREGORIAN,$months,$years)."' group by zones) as xx".$months.$years." on xx".$months.$years.".tranges=mitems.mranges ";
$months--;        
}
 
$sqlst=$sqlst." from (select distinct zone as mranges, zone_order as zoneorders from agentsmrd) as mitems ";
$sqlst=$sqlst.$rsqlst." order by zoneorders ";
 
#echo $sqlst;
printf("<td padding=1%% align='center' style='background:silver;'>Total</td>");
printf("<td padding=1%% align='center' style='background:silver;'>Items</td>");
printf("<td padding=1%% align='center' style='background:silver;'>Agents</td>");
printf("<td padding=1%% align='center' style='background:silver;'>Regions</td></tr>");

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

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

$itemshrf="<a href='prhsitemsummary.php?rpsty=agentsummarys&dtd=dtdaa&starts=".$starts."&ends=".$ends."&regions=".$regions."&agents=".$agents."&territorys=all&ranges=all&zones=".$rst[0]."&rsms=All&executives=All&reps=All&items=All' target='_blank'>Items</a>";
$agentshrf="<a href='prhsagentsummary.php?rpsty=agentsummarys&dtd=dtdaa&starts=".$starts."&ends=".$ends."&regions=".$regions."&agents=".$agents."&territorys=all&ranges=all&zones=".$rst[0]."&rsms=All&executives=All&reps=All&items=All' target='_blank'>Agents</a>";
$regionhrf="<a href='prhsregionsummary.php?rpsty=agentsummarys&dtd=dtdaa&starts=".$starts."&ends=".$ends."&regions=".$regions."&agents=".$agents."&territorys=all&ranges=all&zones=".$rst[0]."&rsms=All&executives=All&reps=All&items=All' target='_blank'>Regions</a>";


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

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></tr>",number_format($ttv,0,'.',','),$itemshrf,$agentshrf,$regionhrf);

$itemshrf="<a href='prhsitemsummary.php?rpsty=agentsummarys&dtd=dtdaa&starts=".$starts."&ends=".$ends."&regions=".$regions."&agents=".$agents."&territorys=all&ranges=all&zones=all&rsms=All&executives=All&reps=All&items=All' target='_blank'>Items</a>";
$agentshrf="<a href='prhsagentsummary.php?rpsty=agentsummarys&dtd=dtdaa&starts=".$starts."&ends=".$ends."&regions=".$regions."&agents=".$agents."&territorys=all&ranges=all&zones=all&rsms=All&executives=All&reps=All&items=All' target='_blank'>Agents</a>";
$regionhrf="<a href='prhsregionsummary.php?rpsty=agentsummarys&dtd=dtdaa&starts=".$starts."&ends=".$ends."&regions=".$regions."&agents=".$agents."&territorys=all&ranges=all&zones=all&rsms=All&executives=All&reps=All&items=All' target='_blank'>Regions</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></tr></table>",$itemshrf,$agentshrf,$regionhrf);
}

if($_REQUEST['dtd']=='dtdbb' || strtolower($_REQUEST['dtd'])=='all'){
printf("<BR>");

print("<div style='display:block;font-size:14;' id='tdta'>");
printf("<br><br>");
printf("<table id='dtdb' style='font-size:13;width:100%%;'><th bgcolor='grey' colspan='25' align='center'><b>Zone sales achievement (in Millions)</th>",$exchrf);

printf("<tr class='cls'><td padding=1%% align='center' bgcolor='silver' ></td>");


$tarr=array();
$aarr=array();

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

$sqlst="select mranges";
$rsqlst="";

 
for($cnt=0;$cnt<6;$cnt++){
$tarr[$cnt]=0;	
$aarr[$cnt]=0;	
if($months==0){
$months=12;
$years--;}

$dates=date_create($years.'-'.$months);
printf("<td padding=1%% align='center' colspan='3' style='background:silver;'>%s</td>",date_format($dates,'M-Y'));
 
$sqlst=$sqlst.",tt".$months.$years.".vls".",xx".$months.$years.".vls";
 
$rsqlst=$rsqlst." left join (select zones as tranges,sum(box_qty*box_price) as vls from rc_targets ".$flt." and  dates>='".$years."-".$months."-1' and
dates<='".$years."-".$months."-".cal_days_in_month(CAL_GREGORIAN,$months,$years)."' group by zones) as tt".$months.$years." on tt".$months.$years.".tranges=mitems.mranges ";

$rsqlst=$rsqlst." left join (select zones as tranges,sum(box_qty*box_price) as vls from rc_invoices ".$flt." and dates>='".$years."-".$months."-1' and
dates<='".$years."-".$months."-".cal_days_in_month(CAL_GREGORIAN,$months,$years)."' group by zones) as xx".$months.$years." on xx".$months.$years.".tranges=mitems.mranges ";

$months--;        
}

printf("<td padding=1%% align='center' style='background:silver;' colspan='3'>Total</td>
<td padding=1%% align='center' ' colspan='3'  style='background:silver;'></td></tr>

<tr><td padding=1%% align='center' >Zone</td>");

$tclr="transparent";

for($cnt=0;$cnt<6;$cnt++){

if($tclr=="silver"){
$tclr="transparent";}
else{
$tclr="silver";}

printf("<td padding=1%% bgcolor=%s align='center' >Target</td>",$tclr);
printf("<td padding=1%% bgcolor=%s align='center' >ACT</td>",$tclr);
printf("<td padding=1%% bgcolor=%s align='center' >%%</td>",$tclr);
}

if($tclr=="silver"){
$tclr="transparent";}
else{
$tclr="silver";}

printf("<td padding=1%% bgcolor=%s align='center' >Target</td>",$tclr);
printf("<td padding=1%% bgcolor=%s align='center' >ACT</td>",$tclr);
printf("<td padding=1%% bgcolor=%s align='center' >%%</td>",$tclr);

if($tclr=="silver"){
$tclr="transparent";}
else{
$tclr="silver";}

printf("<td padding=1%% bgcolor=%s align='center' >Items</td>",$tclr);
printf("<td padding=1%% bgcolor=%s align='center' >Agents</td>",$tclr);
printf("<td padding=1%% bgcolor=%s align='center' >Regions</td></tr>",$tclr);

$tclr="transparent";

 
$sqlst=$sqlst." from (select distinct zone as mranges,zone_order as zoneorders from agentsmrd) as mitems ";
$sqlst=$sqlst.$rsqlst." order by zoneorders ";
 
#echo $sqlst;

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

$gtv=0;
$gav=0;


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

$itemshrf="<a href='prhsitemsummary.php?rpsty=agentsummarys&dtd=dtdbb&starts=".$starts."&ends=".$ends."&regions=".$regions."&agents=".$agents."&territorys=all&ranges=all&zones=".$rst[0]."&rsms=All&executives=All&reps=All&items=All' target='_blank'>Items</a>";
$agentshrf="<a href='prhsagentsummary.php?rpsty=agentsummarys&dtd=dtdbb&starts=".$starts."&ends=".$ends."&regions=".$regions."&agents=".$agents."&territorys=all&ranges=all&zones=".$rst[0]."&rsms=All&executives=All&reps=All&items=All' target='_blank'>Agents</a>";
$regionhrf="<a href='prhsregionsummary.php?rpsty=agentsummarys&dtd=dtdbb&starts=".$starts."&ends=".$ends."&regions=".$regions."&agents=".$agents."&territorys=all&ranges=all&zones=".$rst[0]."&rsms=All&executives=All&reps=All&items=All' target='_blank'>Regions</a>";

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

$nct=0;

for($cn=1;$cn<$fcn;$cn=$cn+2){
if($tclr=="silver"){
$tclr="transparent";}
else{
$tclr="silver";}

printf("<td bgcolor=%s align='center' padding=1%%>%s</td>",$tclr,number_format($rst[$cn]/1000000,0,'.',','));
printf("<td bgcolor=%s align='center' padding=1%%>%s</td>",$tclr,number_format($rst[$cn+1]/1000000,0,'.',','));

if($rst[$cn]==0){
$pcn=100;}
else{
$pcn=$rst[$cn+1]*100/$rst[$cn];}

printf("<td  bgcolor=%s align='center' padding=1%%>%s%%</td>",$tclr,number_format($pcn,0,'.',','));

$tarr[$nct]=$tarr[$nct]+$rst[$cn];
$aarr[$nct]=$aarr[$nct]+$rst[$cn+1];
$ttv=$ttv+$rst[$cn];
$gtv=$gtv+$rst[$cn];
$acv=$acv+$rst[$cnt+1];
$gav=$gav+$rst[$cn+1];
$nct++;

}

if($tclr=="silver"){
$tclr="transparent";}
else{
$tclr="silver";}

printf("<td bgcolor=%s align='center' padding=1%%>%s</td>",$tclr,number_format($ttv/1000000,0,'.',','));
printf("<td bgcolor=%s align='center' padding=1%%>%s</td>",$tclr,number_format($atv/1000000,0,'.',','));

if($ttv==0){
$pcn=100;}
else{
$pcn=$atv*100/$ttv;}
printf("<td bgcolor=%s align='center' padding=1%%>%s%%</td>",$tclr,number_format($pcn,0,'.',','));

$tclr="transparent";

printf("<td align='center'>%s</td><td align='center'>%s</td><td align='center'>%s</td></tr>",$itemshrf,$agentshrf,$regionhrf);

}

printf("<tr class='cls' style='font-weight:bold;'><td>Total</td>");

for($cn=0;$cn<$nct;$cn++){
if($tclr=="silver"){
$tclr="transparent";}
else{
$tclr="silver";}

printf("<td bgcolor=%s align='center' padding=1%%>%s</td>",$tclr,number_format($tarr[$cn]/1000000,0,'.',','));
printf("<td bgcolor=%s align='center' padding=1%%>%s</td>",$tclr,number_format($aarr[$cn]/1000000,0,'.',','));

if($tarr[$cn]==0){
$pcn=100;}
else{
$pcn=$aarr[$cn]*100/$tarr[$cn];}

printf("<td align='center' bgcolor=%s padding=1%%>%s%%</td>",$tclr,number_format($pcn,0,'.',','));
}

if($tclr=="silver"){
$tclr="transparent";}
else{
$tclr="silver";}

printf("<td bgcolor=%s align='center' padding=1%%>%s</td>",$tclr,number_format($gtv/1000000,0,'.',','));
printf("<td bgcolor=%s align='center' padding=1%%>%s</td>",$tclr,number_format($gav/1000000,0,'.',','));

if($gtv==0){
$pcn=100;}
else{
$pcn=$gav*100/$gtv;}
printf("<td bgcolor=%s align='center' padding=1%%>%s%%</td>",$tclr,number_format($pcn,0,'.',','));

$tclr="silver";

$itemshrf="<a href='prhsitemsummary.php?rpsty=agentsummarys&dtd=dtdbb&starts=".$starts."&ends=".$ends."&regions=".$regions."&agents=".$agents."&territorys=all&ranges=all&zones=all&rsms=All&executives=All&reps=All&items=All' target='_blank'>Items</a>";
$agentshrf="<a href='prhsagentsummary.php?rpsty=agentsummarys&dtd=dtdbb&starts=".$starts."&ends=".$ends."&regions=".$regions."&agents=".$agents."&territorys=all&ranges=all&zones=all&rsms=All&executives=All&reps=All&items=All' target='_blank'>Agents</a>";
$regionhrf="<a href='prhsregionsummary.php?rpsty=agentsummarys&dtd=dtdbb&starts=".$starts."&ends=".$ends."&regions=".$regions."&agents=".$agents."&territorys=all&ranges=all&zones=all&rsms=All&executives=All&reps=All&items=All' target='_blank'>Regions</a>";

printf("<td align='center'>%s</td><td align='center'>%s</td><td align='center'>%s</td></tr></table>",$itemshrf,$agentshrf,$regionhrf);
}


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

printf("<BR>");

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

printf("<tr class='cls'><td padding=1%% align='center' bgcolor='silver' >Zone</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='background:silver;'>%s</td>",date_format($dates,'M-Y'));
 
$sqlst=$sqlst.",xx".$months.$years.".vls";
 
$rsqlst=$rsqlst." left join (select zones as tranges,sum(box_qty*packet_weights*packets_per_box/1000) as vls from rc_invoices ".$flt." and  dates>='".$years."-".$months."-1' and
dates<='".$years."-".$months."-".cal_days_in_month(CAL_GREGORIAN,$months,$years)."' group by zones) as xx".$months.$years." on xx".$months.$years.".tranges=mitems.mranges ";
$months--;        
}
 
$sqlst=$sqlst." from (select distinct zone as mranges,zone_order as zoneorders from agentsmrd) as mitems ";
$sqlst=$sqlst.$rsqlst." order by zoneorders ";
 
#echo $sqlst;
printf("<td padding=1%% align='center' style='background:silver;'>Total</td>");
printf("<td padding=1%% align='center' style='background:silver;'>Items</td>");
printf("<td padding=1%% align='center' style='background:silver;'>Agents</td>");
printf("<td padding=1%% align='center' style='background:silver;'>Regions</td></tr>");

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

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

$itemshrf="<a href='prhsitemsummary.php?rpsty=agentsummarys&dtd=dtdcc&starts=".$starts."&ends=".$ends."&regions=".$regions."&agents=".$agents."&territorys=all&ranges=all&zones=".$rst[0]."&rsms=All&executives=All&reps=All&items=All' target='_blank'>Items</a>";
$agentshrf="<a href='prhsagentsummary.php?rpsty=agentsummarys&dtd=dtdcc&starts=".$starts."&ends=".$ends."&regions=".$regions."&agents=".$agents."&territorys=all&ranges=all&zones=".$rst[0]."&rsms=All&executives=All&reps=All&items=All' target='_blank'>Agents</a>";
$regionhrf="<a href='prhsregionsummary.php?rpsty=agentsummarys&dtd=dtdcc&starts=".$starts."&ends=".$ends."&regions=".$regions."&agents=".$agents."&territorys=all&ranges=all&zones=".$rst[0]."&rsms=All&executives=All&reps=All&items=All' target='_blank'>Regions</a>";

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

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></tr>",number_format($ttv,0,'.',','),$itemshrf,$agentshrf,$regionhrf);
}

$itemshrf="<a href='prhsitemsummary.php?rpsty=agentsummarys&dtd=dtdcc&starts=".$starts."&ends=".$ends."&regions=".$regions."&agents=".$agents."&territorys=all&ranges=all&zones=all&rsms=All&executives=All&reps=All&items=All' target='_blank'>Items</a>";
$agentshrf="<a href='prhsagentsummary.php?rpsty=agentsummarys&dtd=dtddd&starts=".$starts."&ends=".$ends."&regions=".$regions."&agents=".$agents."&territorys=all&ranges=all&zones=all&rsms=All&executives=All&reps=All&items=All' target='_blank'>Agents</a>";
$regionhrf="<a href='prhsregionsummary.php?rpsty=agentsummarys&dtd=dtdcc&starts=".$starts."&ends=".$ends."&regions=".$regions."&agents=".$agents."&territorys=all&ranges=all&zones=all&rsms=All&executives=All&reps=All&items=All' target='_blank'>Regions</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></tr></table>",$itemshrf,$agentshrf,$regionhrf);
}

printf("</div>");

?>