<?php
date_default_timezone_set("Asia/Colombo");
require "showshp.php";
session_start();
if(!isset($_SESSION['titles']) || !isset($_SESSION['positions'])|| !isset($_SESSION['usernames'])){
header("location:index.php");
}
?>
<html>
<title>Prasanna Mallawa</title>
<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 showsdt(pr)
{
if(pr=="ranges"){
document.getElementById("dvszones").style.display="none";
document.getElementById("dvsranges").style.display="block";}
if(pr=="zones"){
document.getElementById("dvsranges").style.display="none";
document.getElementById("dvszones").style.display="block";}
}
</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">Primary-Sales</div>
<div class="homes"><a href="index.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_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]."' ";}
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." and dates>='".$_REQUEST['starts']."' and dates<='".$_REQUEST['ends']."' ";
$flt=$seflt;

showshps();

$sqlst="select max(dates) from rc_invoices where dates<'".date('Y-m-d')."'";
$qry=mysqli_query($conn,$sqlst);
$rst=mysqli_fetch_row($qry);
$todates=$rst[0];

#$todays=date("Y-m-d");
#$todates=date_create($todays);
#date_sub($todates,date_interval_create_from_date_string("1 days"));

$hrfitems="<a href='pritemsummarys.php?rpsty=agentsitems&starts=".$todates."
&ends=".$todates."&regions=all&agents=all&territorys=all&ranges=all' target='_blank'>".$todates." - Invoiced items</a>";

$hrfagents="<a href='pragentsummarys.php?rpsty=agentsitems&starts=".$todates."
&ends=".$todates."&regions=all&agents=all&territorys=all&ranges=all' target='_blank'>".$todates." - Invoiced agents</a>";

$hrfregions="<a href='przonesummarys.php?rpsty=agentsitems&starts=".$todates."
&ends=".$todates."&regions=all&agents=all&territorys=all&ranges=all' target='_blank'>".$todates." - Invoiced Zones</a>";

printf("<div class='dtsdashbs'><table style='font-size:13;color:#2E2E2E;width:100%%;'><tr><td colspan='2'>%s</td></tr><tr><td colspan='2'>%s</td></tr><tr><td colspan='2'>%s<br><br></td></tr>
<tr style='color:white;font-weight:bold;font-size:13;background:grey;border:1px solid grey;'><td colspan='2'>Daily sales</td></tr><tr class'cls' style='background:silver;color:#2E2E2E;border:1px solid silver;'><td align='center'>Date</td><td align='right'>Sales value</td></tr>",$hrfitems,$hrfagents,$hrfregions);

$sqlst="select dates,sum(box_qty*box_price) from rc_invoices ".$flt." and dates>='".$starts."' and dates<='".$ends."' group by dates order by dates";
$qry=mysqli_query($conn,$sqlst);
while($rst=mysqli_fetch_row($qry)){
printf("<tr class='cls' style='border:1px solid silver;'><td align='center'>%s</td><td align='right'>%s</td></tr>",$rst[0],number_format($rst[1],0,'.',','));}

printf("</table><div style='color:green;font-size:13;'><br><a href='mrdashbs.php' target='_blank'><b>Goto Market returns</b></a></div>");
printf("</table><div style='color:green;font-size:13;'><br><a href='rddashbs.php' target='_blank'><b>Goto RD sales</b></a></div>");
printf("</table><div style='color:green;font-size:13;'><br><a href='prprstarts.php' target='_blank'><b>Goto New Primary Dashboard</b></a></div>");
printf("</table><div style='color:green;font-size:13;'><br><a href='#' target='_blank'><b>Currently System is updating the figures of new chanel sepration</b></a></div></div>");
#printf("</table><div style='color:green;font-size:13;'><br><a href='wptrzonesummarys.php' target='_blank'><b>Goto P2R sales</b></a></div></div>");

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

$conn=mysqli_connect("localhost","primarysales","S5L7lF01EQZvQrGK","primarysales_dianaold");

#$todays=date("Y-m-d");
#$todates=date_create($todays);
#date_sub($todates,date_interval_create_from_date_string("1 days"));
$dyears=date('Y');
$dmonths=date('m');
$ddates=date('Y-m-d');

if($dmonths<=3){
$dstarts=($dyears-1)."-04-01";}
else{
$dstarts=$dyears."-04-01";}

$tsqlst="select invts,vlb,vla,vld from (select distinct invoice_types as invts from rc_invoices where dates>='2015-08-01' ) as invs left join 
(select invoice_types as invta,sum(box_qty*box_price) as vla from rc_invoices ".$flt." and dates>='".$starts."' and dates<='".$ends."'group by invoice_types) 
as ss  on invts=invta left join (select invoice_types as invtb,sum(box_qty*box_price) as vlb from rc_invoices ".$flt." and dates='".$todates."' group by invoice_types) as tt 
on invtb=invts left join (select invoice_types as invtd,sum(box_qty*box_price) as vld from rc_invoices ".$flt." and dates>=DATE_SUB('".$starts."',INTERVAL 1 YEAR) and dates<=DATE_SUB('".$ends."',INTERVAL 1 YEAR) group by invoice_types) as pt on invtd=invts order by vla DESC";

#echo $tsqlst;

$qry=mysqli_query($conn,$tsqlst);
$rnms=mysqli_num_rows($qry);

$achvd=0;

$sqlst="select actv,tgtvfrom (select sum(box_qty*box_price) as tgtv from rc_targets where dates>='".$starts."' and dates<='".$ends."') as tgs, (select sum(box_qty*box_price) as actv from rc_invoices ".$flt." and dates>='".$starts."' and dates<='".$ends."') as acs";


$qry=mysqli_query($conn,$tsqlst);
$rnms=mysqli_num_rows($qry);

$achvd=0;

$sqlst="select actv,tgtv from (select sum(box_qty*box_price) as tgtv from rc_targets where dates>='".$starts."' and dates<='".$ends."') as tgs, (select sum(box_qty*box_price) as actv from rc_invoices ".$flt." and dates>='".$starts."' and dates<='".$ends."') as acs ";

$qry=mysqli_query($conn,$sqlst);
$rst=mysqli_fetch_row($qry);
if($rst[1]!=0){
$achvd=$rst[0]*100/$rst[1];}
else{
$achvd=100;}

if($rst[1]==0){
$achvd="Budget not set";}



print("<div style='display:block;font-size:14;' id='tdta'>");

printf("<table id='dtd' style='font-size:13;width:100%%;color:#2E2E2E;'>");

printf("<tr style='border:0px solid silver;;font-size:14;'><td style='width:60%%;' colspan='4'><b>Summary</b></td>");
if($rst[1]==0){
printf("<td rowspan='%s' align='center' style='color:#0066CC;font-size:20;'><b>%s</b><br></td></tr>",$rnms+3,$achvd);}
else{
printf("<td rowspan='%s' align='center' style='color:#0066CC;font-size:24;'><b>%s</b>%%<br>Achieved</td></tr>",$rnms+3,number_format($achvd,0,'.',','));}


$pyears=date("Y")-1;

printf("<tr style='border:1px solid silver;background:silver;'><td>Invoice type</td><td align='right'>%s</td><td align='right'>%s</td><td align='right' style='border-right:1px solid silver;'>%s</td></tr>",$todates,date("M-Y"),date("M-".$pyears));

$tvs=0;
$tts=0;
$pts=0;
$yys=0;
$yps=0;


$qry=mysqli_query($conn,$tsqlst);
while($rst=mysqli_fetch_row($qry)){
$tvs=$tvs+$rst[1];
$tts=$tts+$rst[2];
$pts=$pts+$rst[3];
$yys=$yys+$rst[4];
$yps=$yps+$rst[5];

printf("<tr class='cls' style='border:1px solid silver;'><td>%s</td><td align='right'>%s</td><td align='right'>%s</td><td align='right' style='border-right:1px solid silver;'>%s</td></tr>",$rst[0],number_format($rst[1],0,'.',','),number_format($rst[2],0,'.',','),number_format($rst[3],0,'.',','));}

printf("<tr style='border:1px solid silver;font-weight:bold;'><td>Total</td><td align='right'>%s</td><td align='right'>%s</td><td align='right' style='border-right:1px solid silver;'>%s</td></tr></table>",number_format($tvs,0,'.',','),number_format($tts,0,'.',','),number_format($pts,0,'.',','));

printf("</div>");


$flt=$seflt." and dates>='".$starts."' and dates<='".$ends."' ";
if(strtolower($regions)!='all'){
$flt=$flt." and regions='".$regions."' ";}
if(strtolower($agents)!='all'){
$flt=$flt." and agent_names='".$agents."' ";}


$asqlst=" left join (select zones 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 zones) as targets on tgagent_names=magent_names ";

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

$rsqlst="select magent_names as region ";
$rsqlst=$rsqlst.",tgvls,acvls,prdis,acmargins ";

$rsqlst=$rsqlst." from (select distinct zone as magent_names,zone_order as zoneorder from agentsmrd where agent_name is not null  ";
if(strtolower($regions)!='all'){
$rsqlst=$rsqlst." and region='".$regions."'";}
if(strtolower($agents)!='all'){
$rsqlst=$rsqlst." and agent_name='".$agents."'";}

$rsqlst=$rsqlst.") as rgs ";
$sqlst=$rsqlst.$asqlst." order by zoneorder";
#echo $sqlst;
#printf("<div class='tdt'>");
printf("<br>");
$qry=mysqli_query($conn,$sqlst);

$exchrf="<a href='excprtgzonesummary.php?rpsty=agentsummarys&starts=".$starts."&ends=".$ends."&regions=".$regions."&agents=".$agents."&territorys=all&ranges=all&zones=".$zones."&rsms=All&executives=All&reps=All&items=All' target='_blank'><b>download</b></a>";

print("<div style='display:none;font-size:14;' id='dvszones'>");
printf("<table id='dtd' style='font-size:13;width:100%%;color:#2E2E2E;'><th style='color:white;font-size:13;background:grey;border:1px solid grey;' colspan='8' align='left'><b>Zone Primary-achievement (in Millions) - %s</th>
<th onclick=\"showsdt('ranges')\" align='center' colspan='2' style='color:#E6E6E6;font-size:13;background:grey;border:1px solid grey;font-weight:normal;'>Show ranges (click)</th>",date("M Y"));

printf("<tr class='cls'  style='background:silver;border:1px solid silver;' ><td margin=15%% align='center'>Zone</td><td margin=15%% align='center'>Target</td><td margin=15%% align='center'>Gross-sales</td>
<td margin=5%% align='center'>Ach:%%</td><td margin=15%% align='center'>Sales variance</td><td margin=15%% align='center'>Discount</td>
<td align='center'>%%</td><td margin=15%% align='center'>Free-Issue</td><td align='center'>%%</td><td margin=15%% align='center'>Net-Sales</td></tr>");

$tgqt=0;
$acqt=0;
$tgvt=0;
$acvt=0;
$tgmg=0;
$acmg=0;
$marg=0;


while($rst=mysqli_fetch_row($qry)){
$tgqt=$tgqt+$rst[1];
$acqt=$acqt+$rst[2];
$tgvt=$tgvt+$rst[3];
$acvt=$acvt+$rst[4];

$itemshrf="<a href='prtgitemsummary.php?rpsty=agentsummarys&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'>Item list</a>";
$agentshrf="<a href='prtgagentsummary.php?rpsty=agentsummarys&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'>Agent list</a>";
$regionhrf="<a href='prtgregionsummary.php?rpsty=agentsummarys&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'>Region list</a>";

if($rst[2]!=0 && $rst[3]!=0){
$tgmg=$rst[3]*100/$rst[2];
$tgq=$rst[1];
$tgv=$rst[3];}
else{
$tgmg=0;
$tgq=$rst[2];
$tgv=$rst[4];}

if($tgv!=0 && $tgq!=0 && $rst[2]!=0 && $rst[4]!=0){
$pcn=$rst[2]/$tgq*100;}
else{
$pcn=0;}

if($rst[2]!=0 && $magr!=0){
$acmg=$magr*100/$rst[2];}
else{
$acmg=0;}

	

printf("<tr class='cls' style='border:1px solid silver;'><td margin=15%%>%s</td><td align='right'   style='padding-right:2%%'>%s</td><td align='right'   style='padding-right:2%%'>%s</td>
<td align='center'>%s%%</td><td align='right'   style='padding-right:2%%'>%s</td><td align='right'   style='padding-right:2%%'>%s</td><td align='center'>%s%%</td><td align='right'   style='padding-right:2%%'>%s</td><td align='center'>%s%%</td>
<td align='right'   style='padding-right:2%%'>%s</td></tr>",$rst[0],
number_format($rst[1],2,'.',','),number_format($rst[2],2,'.',','),number_format($pcn,0,'.',','),
number_format(($rst[2]-$rst[1]),2,'.',','),number_format($rst[3],2,'.',','),number_format($tgmg,0,'.',','),number_format($magr,2,'.',','),
number_format($acmg,0,'.',','),number_format($rst[2]+$rst[3],2,'.',','), $regionhrf,$agentshrf,$itemshrf);
}	


if($tgqt!=0 && $tgvt!=0){
$pcn=$acqt*100/$tgqt;
$tgmg=$tgvt*100/$tgqt;}
else{
$pcn=0;
$tgmg=0;
}

if($acqt!=0 && $acvt!=0){
$acmg=$acvt*100/$acqt;}
else{
$acmg=0;}


$itemshrf="<a href='prtgitemsummary.php?rpsty=agentsummarys&starts=".$starts."&ends=".$ends."&regions=".$regions."&agents=".$agents."&territorys=all&ranges=all&zones=all&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=".$agents."&territorys=all&ranges=all&zones=all&rsms=All&executives=All&reps=All&items=All' target='_blank'>Agent list</a>";
$regionhrf="<a href='prtgregionsummary.php?rpsty=agentsummarys&starts=".$starts."&ends=".$ends."&regions=".$regions."&agents=".$agents."&territorys=all&ranges=all&zones=all&rsms=All&executives=All&reps=All&items=All' target='_blank'>Region list</a>";


printf("<tr class='cls' style='font-weight:bold;border:1px solid silver;'><td margin=15%%>Total</td><td align='right'   style='padding-right:2%%'>%s</td><td align='right'   style='padding-right:2%%'>%s</td>
<td align='center'>%s%%</td><td align='right'   style='padding-right:2%%'>%s</td><td align='right'   style='padding-right:2%%'>%s</td><td align='center'>%s%%</td><td align='right'   style='padding-right:2%%'>%s</td><td align='center'>%s%%</td>
<td align='right'   style='padding-right:2%%'>%s</td></tr>",
number_format($tgqt,2,'.',','),number_format($acqt,2,'.',','),number_format($pcn,0,'.',','),
number_format(($acqt-$tgqt),2,'.',','),number_format($tgvt,2,'.',','),number_format($tgmg,0,'.',','),
number_format($magr,2,'.',','),number_format($magr,0,'.',','),number_format($acqt+$tgvt,2,'.',','), $regionhrf,$agentshrf,$itemshrf);
printf("</tr></table></div>");



$flt=$seflt." and dates>='".$starts."' and dates<='".$ends."' ";
if(strtolower($regions)!='all'){
$flt=$flt." and regions='".$regions."' ";}
if(strtolower($agents)!='all'){
$flt=$flt." and agent_names='".$agents."' ";}



$asqlst=" left join (select chanell 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 chanell) as targets on tgagent_names=magent_names ";

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


$rsqlst="select magent_names as region ";

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

$rsqlst=$rsqlst." from (select distinct chanelname as magent_names from agentsmrd where chanelname is not null  ";
if(strtolower($regions)!='all'){
$rsqlst=$rsqlst." and region='".$regions."'";}
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);

$exchrf="<a href='excprtgzonesummary.php?rpsty=agentsummarys&starts=".$starts."&ends=".$ends."&regions=".$regions."&agents=".$agents."&territorys=all&ranges=all&zones=".$zones."&rsms=All&executives=All&reps=All&items=All' target='_blank'><b>download</b></a>";

print("<div style='display:block;font-size:14;' id='dvsranges'>");
printf("<table id='dtd' style='font-size:13;width:100%%;color:#2E2E2E;'><th style='color:white;font-size:13;background:grey;border:1px solid grey;' colspan='8' align='left'><b>Channel Primary-Achievement (in Millions) - %s</th>
<th onclick=\"showsdt('zones')\" align='center' colspan='2' style='color:#E6E6E6;font-size:13;background:grey;border:1px solid grey;font-weight:normal;'>Show zones (click)</th>",date("M Y"));

printf("<tr class='cls'  style='background:silver;border:1px solid silver;' ><td margin=15%% align='center'>Channel</td><td margin=15%% align='center'>Target</td><td margin=15%% align='center'>Gross-sales</td>
<td margin=5%% align='center'>Ach:%%</td><td margin=15%% align='center'>Sales-variance</td><td margin=15%% align='center'>Discount</td>
<td align='center'>%%</td><td margin=15%% align='center'>Free-Issue</td><td align='center'>%%</td><td margin=15%% align='center'>Net-Sales</td></tr>");

$tgqt=0;
$acqt=0;
$tgvt=0;
$acvt=0;
$tgmg=0;
$acmg=0;


while($rst=mysqli_fetch_row($qry)){

$tgqt=$tgqt+$rst[1];
$acqt=$acqt+$rst[2];
$tgvt=$tgvt+$rst[3];
$acvt=$acvt+$rst[4];

$itemshrf="<a href='prtgitemsummary.php?rpsty=agentsummarys&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'>Item list</a>";
$agentshrf="<a href='prtgagentsummary.php?rpsty=agentsummarys&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'>Agent list</a>";
$regionhrf="<a href='prtgregionsummary.php?rpsty=agentsummarys&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'>Region list</a>";

if($rst[1]!=0 || $rst[2]!=0){

if($rst[2]!=0 && $rst[3]!=0){
$tgmg=$rst[3]*100/$rst[2];
$tgq=$rst[1];
$tgv=$rst[3];}
else{
$tgmg=0;
$tgq=$rst[2];
$tgv=$rst[4];}

if($tgv!=0 && $tgq!=0 && $rst[2]!=0 && $rst[4]!=0){
$pcn=$rst[2]/$tgq*100;}
else{
$pcn=0;}

if($rst[2]!=0 && $magr!=0){
$acmg=$magr*100/$rst[2];}
else{
$acmg=0;}

	

printf("<tr class='cls' style='border:1px solid silver;'><td margin=15%%>%s</td><td align='right'   style='padding-right:2%%'>%s</td><td align='right'   style='padding-right:2%%'>%s</td>
<td align='center'>%s%%</td><td align='right'   style='padding-right:2%%'>%s</td><td align='right'   style='padding-right:2%%'>%s</td><td align='center'>%s%%</td><td align='right'   style='padding-right:2%%'>%s</td><td align='center'>%s%%</td>
<td align='right'   style='padding-right:2%%'>%s</td></tr>",$rst[0],
number_format($rst[1],2,'.',','),number_format($rst[2],2,'.',','),number_format($pcn,0,'.',','),
number_format(($rst[2]-$rst[1]),2,'.',','),number_format($rst[4],2,'.',','),number_format($tgmg,0,'.',','),number_format($magr,2,'.',','),
number_format($acmg,0,'.',','),number_format($rst[2]+$rst[4],2,'.',','), $regionhrf,$agentshrf,$itemshrf);
}	
}

if($tgqt!=0 && $tgvt!=0){
$pcn=$acqt*100/$tgqt;
$tgmg=$tgvt*100/$tgqt;}
else{
$pcn=0;
$tgmg=0;
}

if($acqt!=0 && $magr!=0){
$acmg=$acvt*100/$acqt;}
else{
$acmg=0;}


$itemshrf="<a href='prtgitemsummary.php?rpsty=agentsummarys&starts=".$starts."&ends=".$ends."&regions=".$regions."&agents=".$agents."&territorys=all&ranges=all&zones=all&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=".$agents."&territorys=all&ranges=all&zones=all&rsms=All&executives=All&reps=All&items=All' target='_blank'>Agent list</a>";
$regionhrf="<a href='prtgregionsummary.php?rpsty=agentsummarys&starts=".$starts."&ends=".$ends."&regions=".$regions."&agents=".$agents."&territorys=all&ranges=all&zones=all&rsms=All&executives=All&reps=All&items=All' target='_blank'>Region list</a>";


printf("<tr class='cls' style='font-weight:bold;border:1px solid silver;'><td margin=15%%>Total</td><td align='right'   style='padding-right:2%%'>%s</td><td align='right'   style='padding-right:2%%'>%s</td>
<td align='center'>%s%%</td><td align='right'   style='padding-right:2%%'>%s</td><td align='right'   style='padding-right:2%%'>%s</td><td align='center'>%s%%</td><td align='right'   style='padding-right:2%%'>%s</td><td align='center'>%s%%</td>
<td align='right'   style='padding-right:2%%'>%s</td></tr>",
number_format($tgqt,2,'.',','),number_format($acqt,2,'.',','),number_format($pcn,0,'.',','),
number_format(($acqt-$tgqt),2,'.',','),number_format($acvt,2,'.',','),number_format($tgmg,0,'.',','),
number_format($magr,2,'.',','),number_format($acmg,0,'.',','),number_format($acqt+$acvt,2,'.',','), $regionhrf,$agentshrf,$itemshrf);
printf("</tr></table></div>");


printf("<BR>");



$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." group by item_ranges) as targets on tgagent_names=magent_names ";

$asqlst=$asqlst." left join (select item_ranges as acagent_names,sum(box_qty*box_price) as acvls,sum((box_qty*box_price)*discount_rates) as acmargins
from rc_invoices ".$flt." 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 where item_name is not null  ";
if(strtolower($regions)!='all'){
$rsqlst=$rsqlst." and region='".$regions."'";}
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);






print("<div style='display:block;font-size:14;' id='dvsranges'>");
printf("<table id='dtd' style='font-size:13;width:100%%;color:#2E2E2E;'><th style='color:white;font-size:13;background:grey;border:1px solid grey;' colspan='8' align='left'><b>Range Primary-Achievement (in Millions) - %s</th>
<th onclick=\"showsdt('zones')\" align='center' colspan='2' style='color:#E6E6E6;font-size:13;background:grey;border:1px solid grey;font-weight:normal;'>Show zones (click)</th>",date("M Y"));

printf("<tr class='cls'  style='background:silver;border:1px solid silver;' ><td margin=15%% align='center'>Range</td><td margin=15%% align='center'>Target</td><td margin=15%% align='center'>ACT sales</td>
<td margin=5%% align='center'>Ach:%%</td><td margin=15%% align='center'>Sales variance</td><td margin=15%% align='center'>Discount</td>
<td align='center'>%%</td><td margin=15%% align='center'>Free-Issue</td><td align='center'>%%</td><td margin=15%% align='center'>Net-Sales</td></tr>");

$tgqt=0;
$acqt=0;
$tgvt=0;
$acvt=0;
$tgmg=0;
$acmg=0;


while($rst=mysqli_fetch_row($qry)){

$tgqt=$tgqt+$rst[1];
$acqt=$acqt+$rst[2];
$tgvt=$tgvt+$rst[3];
$acvt=$acvt+$rst[4];

$itemshrf="<a href='prtgitemsummary.php?rpsty=agentsummarys&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'>Item list</a>";
$agentshrf="<a href='prtgagentsummary.php?rpsty=agentsummarys&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'>Agent list</a>";
$regionhrf="<a href='prtgregionsummary.php?rpsty=agentsummarys&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'>Region list</a>";

if($rst[1]!=0 || $rst[2]!=0){

if($rst[2]!=0 && $rst[3]!=0){
$tgmg=$rst[3]*100/$rst[2];
$tgq=$rst[1];
$tgv=$rst[3];}
else{
$tgmg=0;
$tgq=$rst[2];
$tgv=$rst[4];}

if($tgv!=0 && $tgq!=0 && $rst[2]!=0 && $rst[4]!=0){
$pcn=$rst[2]/$tgq*100;}
else{
$pcn=0;}

if($rst[2]!=0 && $magr!=0){
$acmg=$magr*100/$rst[2];}
else{
$acmg=0;}

	

printf("<tr class='cls' style='border:1px solid silver;'><td margin=15%%>%s</td><td align='right'   style='padding-right:2%%'>%s</td><td align='right'   style='padding-right:2%%'>%s</td>
<td align='center'>%s%%</td><td align='right'   style='padding-right:2%%'>%s</td><td align='right'   style='padding-right:2%%'>%s</td><td align='center'>%s%%</td><td align='right'   style='padding-right:2%%'>%s</td><td align='center'>%s%%</td>
<td align='right'   style='padding-right:2%%'>%s</td></tr>",$rst[0],
number_format($rst[1],2,'.',','),number_format($rst[2],2,'.',','),number_format($pcn,0,'.',','),
number_format(($rst[2]-$rst[1]),2,'.',','),number_format($rst[4],2,'.',','),number_format($tgmg,0,'.',','),number_format($magr,2,'.',','),
number_format($acmg,0,'.',','),number_format($rst[2]+$rst[4],2,'.',','), $regionhrf,$agentshrf,$itemshrf);
}	
}

if($tgqt!=0 && $tgvt!=0){
$pcn=$acqt*100/$tgqt;
$tgmg=$tgvt*100/$tgqt;}
else{
$pcn=0;
$tgmg=0;
}

if($acqt!=0 && $magr!=0){
$acmg=$acvt*100/$acqt;}
else{
$acmg=0;}


$itemshrf="<a href='prtgitemsummary.php?rpsty=agentsummarys&starts=".$starts."&ends=".$ends."&regions=".$regions."&agents=".$agents."&territorys=all&ranges=all&zones=all&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=".$agents."&territorys=all&ranges=all&zones=all&rsms=All&executives=All&reps=All&items=All' target='_blank'>Agent list</a>";
$regionhrf="<a href='prtgregionsummary.php?rpsty=agentsummarys&starts=".$starts."&ends=".$ends."&regions=".$regions."&agents=".$agents."&territorys=all&ranges=all&zones=all&rsms=All&executives=All&reps=All&items=All' target='_blank'>Region list</a>";


printf("<tr class='cls' style='font-weight:bold;border:1px solid silver;'><td margin=15%%>Total</td><td align='right'   style='padding-right:2%%'>%s</td><td align='right'   style='padding-right:2%%'>%s</td>
<td align='center'>%s%%</td><td align='right'   style='padding-right:2%%'>%s</td><td align='right'   style='padding-right:2%%'>%s</td><td align='center'>%s%%</td><td align='right'   style='padding-right:2%%'>%s</td><td align='center'>%s%%</td>
<td align='right'   style='padding-right:2%%'>%s</td></tr>",
number_format($tgqt,2,'.',','),number_format($acqt,2,'.',','),number_format($pcn,0,'.',','),
number_format(($acqt-$tgqt),2,'.',','),number_format($acvt,2,'.',','),number_format($tgmg,0,'.',','),
number_format($magr,2,'.',','),number_format($acmg,0,'.',','),number_format($acqt+$acvt,2,'.',','), $regionhrf,$agentshrf,$itemshrf);
printf("</tr></table></div>");


printf("<BR>");


print("<div style='display:block;font-size:14;left:5%%;' id='tdta'>");
printf("<table id='dtda' style='font-size:13;width:100%%;color:#2E2E2E;'><th style='color:white;font-size:13;background:grey;border:1px solid grey;' colspan='7' align='left'><b>Zone Primary-Sales trend</th>",$exchrf);

printf("<tr class='cls' style='border:1px solid silver;color:#2E2E2E;'><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--;}

$flt=$seflt;

$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("</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' style='border:1px solid silver;'><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("</tr>");

$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;border:1px solid silver;'><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("</tr></table>",$itemshrf,$agentshrf,$regionhrf);



printf("<BR>");

print("<div style='display:block;font-size:14;left:5%%;' id='tdta'>");
printf("<table id='dtda' style='font-size:13;width:100%%;color:#2E2E2E;'><th style='color:white;font-size:13;background:grey;border:1px solid grey;' colspan='7' align='left'><b>Range primary-Sales trend</th>",$exchrf);

printf("<tr class='cls' style='border:1px solid silver;color:#2E2E2E;'><td padding=1%% align='center' bgcolor='silver' >Range</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 item_ranges 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 item_ranges) as xx".$months.$years." on xx".$months.$years.".tranges=mitems.mranges ";
$months--;        
}
 
$sqlst=$sqlst." from (select distinct item_ranges as mranges from itemsmrd ".$rlt.") as mitems ";
$sqlst=$sqlst.$rsqlst." order by mranges ";
 
#echo $sqlst;

printf("</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=".$rst[0]."&zones=".$zones."&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=".$rst[0]."&zones=".$zones."&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=".$rst[0]."&zones=".$zones."&rsms=All&executives=All&reps=All&items=All' target='_blank'>Regions</a>";



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

if($ttv!=0){
printf("<tr class='cls' style='border:1px solid silver;'><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("</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=".$zones."&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=".$zones."&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=".$zones."&rsms=All&executives=All&reps=All&items=All' target='_blank'>Regions</a>";
}
}

$ttv=0;
printf("<tr class='cls' style='font-weight:bold;border:1px solid silver;'><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("</tr></table>",$itemshrf,$agentshrf,$regionhrf);

printf("</div>");

?>