<?php
error_reporting(0);
require "racs.php";
require "vtshowsBBB.php";
session_start();
if(!isset($_SESSION['titles']) || !isset($_SESSION['positions'])|| !isset($_SESSION['usernames'])){
header("location:index.php");}
?>

<html>
<title>Diana-Regional Performance</title>

<head>
<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>
<link rel="stylesheet" type="text/css" href="vtxstyles.css">

<script>

function scopys(pr)
{
var dts=document.getElementById("dt");

var sels=window.getSelection();
sels.removeAllRanges();
var ranges=document.createRange();
ranges.selectNodeContents(dts);
sels.addRange(ranges);
document.execCommand("copy");
alert("Copied - successfull");
sels.removeAllRanges();
}


function srgs(pr)
{
var sv=document.getElementById('srctxt').value.toLowerCase();
var dt=document.getElementById('dt');
var rn=dt.rows.length;

for(var cnt=1;cnt<rn-1;cnt++){
tvl=dt.rows[cnt].cells[0].innerText.toLowerCase();
rvl=dt.rows[cnt].cells[1].innerText.toLowerCase();
if(sv.length>1 && (tvl.indexOf(sv)>=0 || rvl.indexOf(sv)>=0)){
dt.rows[cnt].style.display='table-row';}
else{
dt.rows[cnt].style.display='none';}
if(sv.length<=1){
dt.rows[cnt].style.display='table-row';}	
}

}
</script>
</head>
<body style='font-family:calibri;font-size:small;'>
<div style='width=30%;height:100%%;'>
<?php
$flt=racs(1);
if($flt=="access failed"){
printf("<div style='font-color:red;'>>Access failed\Contact Sales and Marketing division</div>");
}
else{
$conn=mysqli_connect("localhost","primarysales","S5L7lF01EQZvQrGK","primarysales_dianaold");
$sqlst="select group_dbid from acs where user_names='".$_SESSION['usernames']."' ";
$qry=mysqli_query($conn,$sqlst);

$rst=mysqli_fetch_row($qry);
if($rst[0]==1){
printf("<div style='position:absolute;top:100;left:200;font-size:16;'>Access Denied</div>");}
else{


if(isset($_REQUEST['starts'])){
$starts=$_REQUEST['starts'];}
else{
$starts=date('Y-m-01');}

if(isset($_REQUEST['ends'])){
$ends=$_REQUEST['ends'];}
else{
$ends=date('Y-m-t');}

if(isset($_REQUEST['chanell'])){
$chanell=$_REQUEST['chanell'];}
else{
$chanell='all';}

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

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

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

if(isset($_REQUEST['terrys'])){
$terrys=$_REQUEST['terrys'];}
else{
$terrys='all';}

if(isset($_REQUEST['subregions'])){
$subregions=$_REQUEST['subregions'];}
else{
$subregions='all';}

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

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

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

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

#$flt=racs(1);

if(strtolower($chanell)!='all'){
$flt=$flt." and chanell='".$chanell."' ";}
if(strtolower($zones)!='all'){
$flt=$flt." and zones='".$zones."' ";}
if(strtolower($regions)!='all'){
$flt=$flt." and regions='".$regions."' ";}
if(strtolower($subregions)!='all'){
$flt=$flt." and sub_regions='".$subregions."' ";}
if(strtolower($agents)!='all'){
$flt=$flt." and agent_names='".$agents."' ";}
if(strtolower($agenttypes)!='all'){
$flt=$flt." and agent_types='".$agenttypes."' ";}
if(strtolower($terrys)!='all'){
$flt=$flt." and territory_names='".$terrys."' ";}
if(strtolower($items)!='all'){
$flt=$flt." and item_names='".$items."' ";}
if(strtolower($ranges)!='all'){
$flt=$flt." and item_ranges='".$ranges."' ";}
if(strtolower($subranges)!='all'){
$flt=$flt." and sub_ranges='".$subranges."' ";}

stps(pr);
printf("<div class='sds'>");
rdentrydash(1);
printf("</div>");

printf("<div class='mnf'>");
printf("<div style='font-size:16;color:#006699;border-bottom:1px solid silver;width:30%%;'>Performance $starts - $ends</div>");

$tv=0;
$tz=0;
$arr=array();

printf("<BR>");
$sprt="&zones=".$zones."&regions=".$regions."&subregions=".$subregions."&agents=".$agents."&agenttypes=".$agenttypes."&terrys=".$terrys."&ranges=".$ranges."&items=".$items."&subranges=".$subranges;

$sqlst="select rgs from (select distinct zone_orders as rds,regions as rgs from rc_invoices union select distinct zone_orders,regions from rc_targets order by rds) as sbs";
$qry=mysqli_query($conn,$sqlst);

printf("<div style='width:100%%;height:5%%;border:0px solid silver;border-radius:2px;display:table;vertical-align:middle;'>");

printf("<form method='post' action='regionzones.php?actp=rpt%s'><table style='width:100%%;font-size:13;'>",$sprt);
printf("<tr><td width=5%%><input type='text' id='inputField' value='%s' name='starts' size=8 style='font-size:13;background:#CACACA;border:1px solid #006699;border-radius:1px;font-family:calibri;color:#006699;'></td><td width=1%%>-</td><td width=5%%><input type='text' id='inputField2' value='%s' name='ends' size=8 style='font-size:13;background:#CACACA;border:1px solid #006699;border-radius:1px;font-family:calibri;color:#006699;'></td>
<td width=20%%><select name='zones' style='width:95%%;font-size:13;background:#CACACA;border:1px solid #006699;border-radius:1px;font-family:calibri;color:#006699;'><option>%s</option><option>All</option>",$starts,$ends,$zones);
while($rst=mysqli_fetch_row($qry)){
printf("<option>%s</option>",$rst[0]);}	
printf("</select></td><td><input type='submit' value='Go' style='font-size:13;background:#CACACA;color:#006699;border:1.5px solid #006699;border-radius:1pxfont-size:13;'></td></form>",$starts,$ends);

printf("<td width=38%% align='right' style='color:grey;'>Search</td><td width=20%%>
<input type='text' id='srctxt' size=28 style='font-family:calibri;font-size:14;border:1px solid silver;border-radius:1.5px;' onkeyup=\"srgs(event)\"></td><td style='font-size:12;color:green;text-align:center;background:silver;border:1px solid white;' onclick=\"scopys(1)\">Copy</td></tr>");
printf("</table>");

printf("<BR>");
printf("<div style='width:100%%;height:5%%;border:0px solid silver;border-radius:2px;display:table;vertical-align:middle;'>");

//printf("<form method='post' action='rpmrdslregionl.php?actp=rpt%s'><table style='width:100%%;font-size:13;'>",$sprt);
//printf("<tr><td width=5%%><input type='text' id='inputField' value='%s' name='starts' size=8 style='font-size:13;background:#CACACA;border:1px solid #006699;border-radius:1px;font-family:calibri;color:#006699;'></td><td width=1%%>-</td><td width=5%%><input type='text' id='inputField2' value='%s' name='ends' size=8 style='font-size:13;background:#CACACA;border:1px solid #006699;border-radius:1px;font-family:calibri;color:#006699;'></td>
//<td width=20%%><select name='zones' style='width:95%%;font-size:13;background:#CACACA;border:1px solid #006699;border-radius:1px;font-family:calibri;color:#006699;'><option>%s</option><option>All</option>",$starts,$ends,$zones);
//while($rst=mysqli_fetch_row($qry)){
//printf("<option>%s</option>",$rst[0]);}	
//printf("</select></td><td><input type='submit' value='Go' style='font-size:13;background:#CACACA;color:#006699;border:1.5px solid #006699;border-radius:1pxfont-size:13;'></td></form>",$starts,$ends);

//printf("<td width=38%% align='right' style='color:grey;'>Search</td><td width=20%%>
//<input type='text' id='srctxt' size=28 style='font-family:calibri;font-size:14;border:1px solid silver;border-radius:1.5px;' onkeyup=\"srgs(event)\"></td><td style='font-size:12;color:green;text-align:center;background:silver;border:1px solid white;' onclick=\"scopys(1)\">Copy</td></tr>");
//printf("</table>");
//<td style='border:1px solid white;text-align:center;'>Order(with Cancel)</td>

//printf("<div style='font-size:16;color:green;border-bottom:1px solid silver;width:30%%;'>Region-01 - $prevdatestart - $prevdateend</div>");

printf("<div style='font-size:16;color:green;border-bottom:1px solid silver;width:30%%;'>Central Region - $starts - $ends</div>");

printf("<table id='dt' style='width:100%%;font-size:13;border-collapse:collapse;color:#006699;'>
<tr style='background:silver;border:1px solid white;text-align:right;'>
<td style='border:1px solid white;text-align:center;width:5%%;'>Area</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>Target Ton</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>RD Ton</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>Order Ton</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>Pending Inv Ton</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>Primary Ton</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>Target</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>Primary Order</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>Pending Invoice</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>Primary Sales</td>
<td style='border:1px solid white;text-align:center;width:2%%;'>Pri:%%</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>Primary Difict</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>RD-[DB+Dir]</td>
<td style='border:1px solid white;text-align:center;width:2%%;'>Rd:%%</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>RD-DB</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>Direct</td>
<td style='border:1px solid white;text-align:center;width:4%%;'>P-Calls</td>
<td style='border:1px solid white;text-align:center;width:4%%;'>W-Days</td>
<td style='background:silver;border:1px solid white;width:2%%;' align='center'>View</td>
<td style='background:silver;border:1px solid white;width:2%%;' align='center'>View</td>
<td style='background:silver;border:1px solid white;width:2%%;' align='center'>View</td>
</tr>");

$sqlst="select mzones,tgvl,acvl,odvl,rdvl,penodval,pcls,wdays,acvldir,tton,rdton,odton,prton,dirton,penodton from (select distinct rgs as mzones from (select distinct zone_orders as rds,regions as rgs from rc_invoices ".$flt." and dates>='".$starts."' and dates<='".$ends."' and chanell='GENERAL TRADE' and zones='CENTRAL REGION' union select distinct zone_orders,regions from rc_targets ".$flt."  and dates>='".$starts."' and dates<='".$ends."' and zones='CENTRAL REGION' union select distinct zone_orders,regions from rc_orders ".$flt."  and dates>='".$starts."' and dates<='".$ends."' and chanell='GENERAL TRADE' and zones='CENTRAL REGION' order by rds,rgs) as zns) as ags 

left join (select regions,sum(box_qty*box_price) as tgvl,sum(box_qty*packets_per_box*packet_weights/1000000) as tton from rc_targets ".$flt." and dates>='".$starts."' and dates<='".$ends."' and zones='CENTRAL REGION' group by regions) as tgs on tgs.regions=mzones 

left join (select regions,sum(box_qty*box_price) as acvl,sum(box_qty*packets_per_box*packet_weights/1000000) as prton from rc_invoices ".$flt." and dates>='".$starts."' and dates<='".$ends."' and zones='CENTRAL REGION' group by regions) 
as acs on acs.regions=mzones

left join (select regions,sum(box_qty*box_price) as odvl,sum(box_qty*packets_per_box*packet_weights/1000000) as odton from rc_orders ".$flt." and dates>='".$starts."' and dates<='".$ends."' and order_status<>'CANCELLED' and zones='CENTRAL REGION' group by regions) 
as acss on acss.regions=mzones 


left join (select regions,sum(box_qty*box_price) as penodval,sum(box_qty*packets_per_box*packet_weights/1000000) as penodton from rc_orders ".$flt." and dates>='".$starts."' and dates<='".$ends."' and order_status='PENDING' and zones='CENTRAL REGION' group by regions)
as acssod on acssod.regions=mzones 

left join (select regions,sum(pcs_qty*pcs_price) as rdvl,sum(pcalls) as pcls,sum(pcs_qty*pcs_price)/count(distinct(invoice_number))*count(distinct(agent_names)) as rdavgday,count(distinct(invoice_number)) as wdays,sum(pcs_qty*packet_weights/1000000) as rdton from rd_saleinv ".$flt." and dates>='".$starts."' and dates<='".$ends."' and zones='CENTRAL REGION' group by regions) 
as rds on rds.regions=mzones

left join (select regions,sum(box_qty*box_price) as acvldir,sum(box_qty*packets_per_box*packet_weights/1000000) as dirton from rc_invoices ".$flt." and dates>='".$starts."' and dates<='".$ends."' and agent_types='DIRECT' and zones='CENTRAL REGION' group by regions) 
as acsdr on acsdr.regions=mzones";
 
$tgv=0;
$acv=0;
$tgw=0;
$acw=0;
$odv=0;
$rdv=0;
$pcl=0;
$odvlc=0;
$rdavgday=0;
$wdays=0;
$tton=0;
$ordton=0;
$prton=0;
$rdton=0;
$pendton=0;
$penval=0;
$dbrd=0;
$dirtot=0;
 
$qry=mysqli_query($conn,$sqlst);
while($rst=mysqli_fetch_row($qry)){
$tgv=$tgv+$rst[1];
$odv=$odv+$rst[3];
$acv=$acv+$rst[2];
$rdv=$rdv+$rst[4]+$rst[8];
$pcl=$pcl+$rst[6];
//$odvlc=$odvlc+$rst[5];
//$rdavgday=$rdavgday+$rst[7];
$wdays=$wdays+$rst[7];
$tton=$tton+$rst[9];
$ordton=$ordton+$rst[11];
$prton=$prton+$rst[12];
$rdton=$rdton+$rst[10]+$rst[13];
$pendton=$pendton+$rst[14];
$penval=$penval+$rst[5];
$dbrd=$dbrd+$rst[4];
$dirtot=$dirtot+$rst[8];

if($rst[1]==0){
$pcs=0;}
else{
$pcs=$rst[2]*100/$rst[1];}

if($rst[1]==0){
$rdss=0;}
else{
$rdss=($rst[4]+$rst[9])*100/$rst[1];}
$reg1='RSM-RAJITHA';

printf("<tr style='border-bottom:1px solid silver;'><td>%s</td>
<td align='right'>%s</td>
<td align='right'>%s</td>
<td align='right'>%s</td>
<td align='right'>%s</td>
<td align='right'style='color:522109'bgcolor='C6F9D8'>%s</td>
<td align='right'>%s</td>
<td align='right'>%s</td>
<td align='right'style='color:522109'bgcolor='C6F9D8'>%s</td>
<td align='right'>%s</td>
<td align='right'style='color:522109'bgcolor='#DAF7A6'>%s%%</td>
<td align='right' style='color:black'>%s</td>
<td align='right' style='color:red'>%s</td>
<td align='right' style='color:black'bgcolor='#DAF7A6'>%s%%</td>
<td align='right' style='color:black'bgcolor='#D6EAF8'>%s</td>
<td align='right' style='color:purple'>%s</td>
<td align='right' style='color:green'>%s</td>
<td align='right' style='color:orange'>%s</td>
<td align='center'><a href='rpmrdagenttgsummary.php?starts=%s&ends=%s&regions=%s' target='_blank'>Agent</a></td>
<td align='center'><a href='rpmrangenew.php?starts=%s&ends=%s&regions=%s&zones=%s' target='_blank'>Range</a></td>
<td align='center'><a href='rpmtgttitemwisetgsummary.php?starts=%s&ends=%s&regions=%s' target='_blank'>items</a></td></tr>",
$rst[0],
number_format($rst[9],2,'.',','),
number_format($rst[10]+$srt[13],2,'.',','),
number_format($rst[11],2,'.',','),
number_format($rst[14],2,'.',','),
number_format($rst[12],2,'.',','),
number_format($rst[1],0,'.',','),
number_format($rst[3],0,'.',','),
number_format($rst[5],0,'.',','),
number_format($rst[2],0,'.',','),
number_format($pcs,1,'.',','),
number_format($rst[1]-$rst[2],0,'.',','),
number_format($rst[4]+$rst[8],0,'.',','),
number_format($rdss,1,'.',','),
number_format($rst[4],0,'.',','),
number_format($rst[8],0,'.',','),
number_format($rst[6],0,'.',','),
number_format($rst[7],0,'.',','),
$starts,$ends,$rst[0],$starts,$ends,$rst[0],$reg1,$starts,$ends,$rst[0]);
}


printf("<tr style='border-bottom:1px solid silver;font-weight:bold;'>
<td bgcolor='CED9D3'>Total</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'>%s%%</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'>%s%%</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='center'><a href='rpmrdagenttgsummary.php?starts=%s&ends=%s&zones=%s'' target='_blank'>Agent</a></td>
<td align='center'><a href='rpmrangenew.php?starts=%s&ends=%s&zones=%s' target='_blank'>Range</a></td>
<td align='center'><a href='rpmtgttitemwisetgsummary.php?starts=%s&ends=%s&zones=%s' target='_blank'>items</a></td>
</tr></table>",
number_format($tton,2,'.',','),
number_format($rdton,2,'.',','),
number_format($ordton,2,'.',','),
number_format($pendton,2,'.',','),
number_format($prton,2,'.',','),
number_format($tgv,0,'.',','),
number_format($odv,0,'.',','),
number_format($penval,0,'.',','),
number_format($acv,0,'.',','),
number_format($acv/$tgv*100,1,'.',','),
number_format($tgv-$acv,0,'.',','),
number_format($rdv,0,'.',','),
number_format($rdv/$tgv*100,1,'.',','),
number_format($dbrd,0,'.',','),
number_format($dirtot,0,'.',','),
number_format($pcl,0,'.',','),
number_format($wdays,0,',',','),
$starts,$ends,$reg1,$starts,$ends,$reg1,$starts,$ends,$reg1);


printf("<BR>");
printf("<div style='width:100%%;height:5%%;border:0px solid silver;border-radius:2px;display:table;vertical-align:middle;'>");

printf("<div style='font-size:16;color:green;border-bottom:1px solid silver;width:30%%;'>NC-East-North Region - $starts - $ends</div>");

printf("<table id='dt' style='width:100%%;font-size:13;border-collapse:collapse;color:#006699;'>
<tr style='background:silver;border:1px solid white;text-align:right;'>
<td style='border:1px solid white;text-align:center;width:5%%;'>Area</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>Target Ton</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>RD Ton</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>Order Ton</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>Pending Inv Ton</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>Primary Ton</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>Target</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>Primary Order</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>Pending Invoice</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>Primary Sales</td>
<td style='border:1px solid white;text-align:center;width:2%%;'>Pri:%%</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>Primary Difict</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>RD-[DB+Dir]</td>
<td style='border:1px solid white;text-align:center;width:2%%;'>Rd:%%</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>RD-DB</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>Direct</td>
<td style='border:1px solid white;text-align:center;width:4%%;'>P-Calls</td>
<td style='border:1px solid white;text-align:center;width:4%%;'>W-Days</td>
<td style='background:silver;border:1px solid white;width:2%%;' align='center'>View</td>
<td style='background:silver;border:1px solid white;width:2%%;' align='center'>View</td>
<td style='background:silver;border:1px solid white;width:2%%;' align='center'>View</td>
</tr>");

$sqlst="select mzones,tgvl,acvl,odvl,rdvl,penodval,pcls,wdays,acvldir,tton,rdton,odton,prton,dirton,penodton from (select distinct rgs as mzones from (select distinct zone_orders as rds,regions as rgs from rc_invoices ".$flt." and dates>='".$starts."' and dates<='".$ends."' and chanell='GENERAL TRADE' and rsm_names='RSM-PALITHA' union select distinct zone_orders,regions from rc_targets ".$flt."  and dates>='".$starts."' and dates<='".$ends."' and rsm_names='RSM-PALITHA' union select distinct zone_orders,regions from rc_orders ".$flt."  and dates>='".$starts."' and dates<='".$ends."' and chanell='GENERAL TRADE' and rsm_names='RSM-PALITHA' order by rds,rgs) as zns) as ags 

left join (select regions,sum(box_qty*box_price) as tgvl,sum(box_qty*packets_per_box*packet_weights/1000000) as tton from rc_targets ".$flt." and dates>='".$starts."' and dates<='".$ends."' and rsm_names='RSM-PALITHA' group by regions) as tgs on tgs.regions=mzones 

left join (select regions,sum(box_qty*box_price) as acvl,sum(box_qty*packets_per_box*packet_weights/1000000) as prton from rc_invoices ".$flt." and dates>='".$starts."' and dates<='".$ends."' and rsm_names='RSM-PALITHA' group by regions) 
as acs on acs.regions=mzones

left join (select regions,sum(box_qty*box_price) as odvl,sum(box_qty*packets_per_box*packet_weights/1000000) as odton from rc_orders ".$flt." and dates>='".$starts."' and dates<='".$ends."' and order_status<>'CANCELLED' and rsm_names='RSM-PALITHA' group by regions) 
as acss on acss.regions=mzones 


left join (select regions,sum(box_qty*box_price) as penodval,sum(box_qty*packets_per_box*packet_weights/1000000) as penodton from rc_orders ".$flt." and dates>='".$starts."' and dates<='".$ends."' and order_status='PENDING' and rsm_names='RSM-PALITHA' group by regions)
as acssod on acssod.regions=mzones 

left join (select regions,sum(pcs_qty*pcs_price) as rdvl,sum(pcalls) as pcls,sum(pcs_qty*pcs_price)/count(distinct(invoice_number))*count(distinct(agent_names)) as rdavgday,count(distinct(invoice_number)) as wdays,sum(pcs_qty*packet_weights/1000000) as rdton from rd_saleinv ".$flt." and dates>='".$starts."' and dates<='".$ends."' and rsm_names='RSM-PALITHA' group by regions) 
as rds on rds.regions=mzones

left join (select regions,sum(box_qty*box_price) as acvldir,sum(box_qty*packets_per_box*packet_weights/1000000) as dirton from rc_invoices ".$flt." and dates>='".$starts."' and dates<='".$ends."' and agent_types='DIRECT' and rsm_names='RSM-PALITHA' group by regions) 
as acsdr on acsdr.regions=mzones";
 
$tgv=0;
$acv=0;
$tgw=0;
$acw=0;
$odv=0;
$rdv=0;
$pcl=0;
$odvlc=0;
$rdavgday=0;
$wdays=0;
$tton=0;
$ordton=0;
$prton=0;
$rdton=0;
$pendton=0;
$penval=0;
$dbrd=0;
$dirtot=0;
 
$qry=mysqli_query($conn,$sqlst);
while($rst=mysqli_fetch_row($qry)){
$tgv=$tgv+$rst[1];
$odv=$odv+$rst[3];
$acv=$acv+$rst[2];
$rdv=$rdv+$rst[4]+$rst[8];
$pcl=$pcl+$rst[6];
//$odvlc=$odvlc+$rst[5];
//$rdavgday=$rdavgday+$rst[7];
$wdays=$wdays+$rst[7];
$tton=$tton+$rst[9];
$ordton=$ordton+$rst[11];
$prton=$prton+$rst[12];
$rdton=$rdton+$rst[10]+$rst[13];
$pendton=$pendton+$rst[14];
$penval=$penval+$rst[5];
$dbrd=$dbrd+$rst[4];
$dirtot=$dirtot+$rst[8];

if($rst[1]==0){
$pcs=0;}
else{
$pcs=$rst[2]*100/$rst[1];}

if($rst[1]==0){
$rdss=0;}
else{
$rdss=($rst[4]+$rst[9])*100/$rst[1];}
$reg2='RSM-PALITHA';

printf("<tr style='border-bottom:1px solid silver;'><td>%s</td>
<td align='right'>%s</td>
<td align='right'>%s</td>
<td align='right'>%s</td>
<td align='right'>%s</td>
<td align='right'style='color:522109'bgcolor='C6F9D8'>%s</td>
<td align='right'>%s</td>
<td align='right'>%s</td>
<td align='right'style='color:522109'bgcolor='C6F9D8'>%s</td>
<td align='right'>%s</td>
<td align='right'style='color:522109'bgcolor='#DAF7A6'>%s%%</td>
<td align='right' style='color:black'>%s</td>
<td align='right' style='color:red'>%s</td>
<td align='right' style='color:black'bgcolor='#DAF7A6'>%s%%</td>
<td align='right' style='color:black'bgcolor='#D6EAF8'>%s</td>
<td align='right' style='color:purple'>%s</td>
<td align='right' style='color:green'>%s</td>
<td align='right' style='color:orange'>%s</td>
<td align='center'><a href='rpmrdagenttgsummary.php?starts=%s&ends=%s&regions=%s' target='_blank'>Agent</a></td>
<td align='center'><a href='rpmrangenew.php?starts=%s&ends=%s&regions=%s&zones=%s' target='_blank'>Range</a></td>
<td align='center'><a href='rpmtgttitemwisetgsummary.php?starts=%s&ends=%s&regions=%s' target='_blank'>items</a></td></tr>",
$rst[0],
number_format($rst[9],2,'.',','),
number_format($rst[10]+$srt[13],2,'.',','),
number_format($rst[11],2,'.',','),
number_format($rst[14],2,'.',','),
number_format($rst[12],2,'.',','),
number_format($rst[1],0,'.',','),
number_format($rst[3],0,'.',','),
number_format($rst[5],0,'.',','),
number_format($rst[2],0,'.',','),
number_format($pcs,1,'.',','),
number_format($rst[1]-$rst[2],0,'.',','),
number_format($rst[4]+$rst[8],0,'.',','),
number_format($rdss,1,'.',','),
number_format($rst[4],0,'.',','),
number_format($rst[8],0,'.',','),
number_format($rst[6],0,'.',','),
number_format($rst[7],0,'.',','),
$starts,$ends,$rst[0],$starts,$ends,$rst[0],$reg2,$starts,$ends,$rst[0]);
}


printf("<tr style='border-bottom:1px solid silver;font-weight:bold;'>
<td bgcolor='CED9D3'>Total</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'>%s%%</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'>%s%%</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='center'><a href='rpmrdagenttgsummary.php?starts=%s&ends=%s&zones=%s'' target='_blank'>Agent</a></td>
<td align='center'><a href='rpmrangenew.php?starts=%s&ends=%s&zones=%s' target='_blank'>Range</a></td>
<td align='center'><a href='rpmtgttitemwisetgsummary.php?starts=%s&ends=%s&zones=%s' target='_blank'>items</a></td>
</tr></table>",
number_format($tton,2,'.',','),
number_format($rdton,2,'.',','),
number_format($ordton,2,'.',','),
number_format($pendton,2,'.',','),
number_format($prton,2,'.',','),
number_format($tgv,0,'.',','),
number_format($odv,0,'.',','),
number_format($penval,0,'.',','),
number_format($acv,0,'.',','),
number_format($acv/$tgv*100,1,'.',','),
number_format($tgv-$acv,0,'.',','),
number_format($rdv,0,'.',','),
number_format($rdv/$tgv*100,1,'.',','),
number_format($dbrd,0,'.',','),
number_format($dirtot,0,'.',','),
number_format($pcl,0,'.',','),
number_format($wdays,0,',',','),
$starts,$ends,$reg2,$starts,$ends,$reg2,$starts,$ends,$reg2);



printf("<BR>");
printf("<div style='width:100%%;height:5%%;border:0px solid silver;border-radius:2px;display:table;vertical-align:middle;'>");

printf("<div style='font-size:16;color:green;border-bottom:1px solid silver;width:30%%;'>Southern Region - $starts - $ends</div>");

printf("<table id='dt' style='width:100%%;font-size:13;border-collapse:collapse;color:#006699;'>
<tr style='background:silver;border:1px solid white;text-align:right;'>
<td style='border:1px solid white;text-align:center;width:5%%;'>Area</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>Target Ton</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>RD Ton</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>Order Ton</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>Pending Inv Ton</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>Primary Ton</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>Target</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>Primary Order</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>Pending Invoice</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>Primary Sales</td>
<td style='border:1px solid white;text-align:center;width:2%%;'>Pri:%%</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>Primary Difict</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>RD-[DB+Dir]</td>
<td style='border:1px solid white;text-align:center;width:2%%;'>Rd:%%</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>RD-DB</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>Direct</td>
<td style='border:1px solid white;text-align:center;width:4%%;'>P-Calls</td>
<td style='border:1px solid white;text-align:center;width:4%%;'>W-Days</td>
<td style='background:silver;border:1px solid white;width:2%%;' align='center'>View</td>
<td style='background:silver;border:1px solid white;width:2%%;' align='center'>View</td>
<td style='background:silver;border:1px solid white;width:2%%;' align='center'>View</td>
</tr>");

$sqlst="select mzones,tgvl,acvl,odvl,rdvl,penodval,pcls,wdays,acvldir,tton,rdton,odton,prton,dirton,penodton from (select distinct rgs as mzones from (select distinct zone_orders as rds,regions as rgs from rc_invoices ".$flt." and dates>='".$starts."' and dates<='".$ends."' and chanell='GENERAL TRADE' and rsm_names='RSM-MANOJ' union select distinct zone_orders,regions from rc_targets ".$flt."  and dates>='".$starts."' and dates<='".$ends."' and rsm_names='RSM-MANOJ' union select distinct zone_orders,regions from rc_orders ".$flt."  and dates>='".$starts."' and dates<='".$ends."' and chanell='GENERAL TRADE' and rsm_names='RSM-MANOJ' order by rds,rgs) as zns) as ags 

left join (select regions,sum(box_qty*box_price) as tgvl,sum(box_qty*packets_per_box*packet_weights/1000000) as tton from rc_targets ".$flt." and dates>='".$starts."' and dates<='".$ends."' and rsm_names='RSM-MANOJ' group by regions) as tgs on tgs.regions=mzones 

left join (select regions,sum(box_qty*box_price) as acvl,sum(box_qty*packets_per_box*packet_weights/1000000) as prton from rc_invoices ".$flt." and dates>='".$starts."' and dates<='".$ends."' and rsm_names='RSM-MANOJ' group by regions) 
as acs on acs.regions=mzones

left join (select regions,sum(box_qty*box_price) as odvl,sum(box_qty*packets_per_box*packet_weights/1000000) as odton from rc_orders ".$flt." and dates>='".$starts."' and dates<='".$ends."' and order_status<>'CANCELLED' and rsm_names='RSM-MANOJ' group by regions) 
as acss on acss.regions=mzones 


left join (select regions,sum(box_qty*box_price) as penodval,sum(box_qty*packets_per_box*packet_weights/1000000) as penodton from rc_orders ".$flt." and dates>='".$starts."' and dates<='".$ends."' and order_status='PENDING' and rsm_names='RSM-MANOJ' group by regions)
as acssod on acssod.regions=mzones 

left join (select regions,sum(pcs_qty*pcs_price) as rdvl,sum(pcalls) as pcls,sum(pcs_qty*pcs_price)/count(distinct(invoice_number))*count(distinct(agent_names)) as rdavgday,count(distinct(invoice_number)) as wdays,sum(pcs_qty*packet_weights/1000000) as rdton from rd_saleinv ".$flt." and dates>='".$starts."' and dates<='".$ends."' and rsm_names='RSM-MANOJ' group by regions) 
as rds on rds.regions=mzones

left join (select regions,sum(box_qty*box_price) as acvldir,sum(box_qty*packets_per_box*packet_weights/1000000) as dirton from rc_invoices ".$flt." and dates>='".$starts."' and dates<='".$ends."' and agent_types='DIRECT' and rsm_names='RSM-MANOJ' group by regions) 
as acsdr on acsdr.regions=mzones";
 
$tgv=0;
$acv=0;
$tgw=0;
$acw=0;
$odv=0;
$rdv=0;
$pcl=0;
$odvlc=0;
$rdavgday=0;
$wdays=0;
$tton=0;
$ordton=0;
$prton=0;
$rdton=0;
$pendton=0;
$penval=0;
$dbrd=0;
$dirtot=0;
 
$qry=mysqli_query($conn,$sqlst);
while($rst=mysqli_fetch_row($qry)){
$tgv=$tgv+$rst[1];
$odv=$odv+$rst[3];
$acv=$acv+$rst[2];
$rdv=$rdv+$rst[4]+$rst[8];
$pcl=$pcl+$rst[6];
//$odvlc=$odvlc+$rst[5];
//$rdavgday=$rdavgday+$rst[7];
$wdays=$wdays+$rst[7];
$tton=$tton+$rst[9];
$ordton=$ordton+$rst[11];
$prton=$prton+$rst[12];
$rdton=$rdton+$rst[10]+$rst[13];
$pendton=$pendton+$rst[14];
$penval=$penval+$rst[5];
$dbrd=$dbrd+$rst[4];
$dirtot=$dirtot+$rst[8];

if($rst[1]==0){
$pcs=0;}
else{
$pcs=$rst[2]*100/$rst[1];}

if($rst[1]==0){
$rdss=0;}
else{
$rdss=($rst[4]+$rst[9])*100/$rst[1];}
$reg3='RSM-MANOJ';

printf("<tr style='border-bottom:1px solid silver;'><td>%s</td>
<td align='right'>%s</td>
<td align='right'>%s</td>
<td align='right'>%s</td>
<td align='right'>%s</td>
<td align='right'style='color:522109'bgcolor='C6F9D8'>%s</td>
<td align='right'>%s</td>
<td align='right'>%s</td>
<td align='right'style='color:522109'bgcolor='C6F9D8'>%s</td>
<td align='right'>%s</td>
<td align='right'style='color:522109'bgcolor='#DAF7A6'>%s%%</td>
<td align='right' style='color:black'>%s</td>
<td align='right' style='color:red'>%s</td>
<td align='right' style='color:black'bgcolor='#DAF7A6'>%s%%</td>
<td align='right' style='color:black'bgcolor='#D6EAF8'>%s</td>
<td align='right' style='color:purple'>%s</td>
<td align='right' style='color:green'>%s</td>
<td align='right' style='color:orange'>%s</td>
<td align='center'><a href='rpmrdagenttgsummary.php?starts=%s&ends=%s&regions=%s' target='_blank'>Agent</a></td>
<td align='center'><a href='rpmrangenew.php?starts=%s&ends=%s&regions=%s&zones=%s' target='_blank'>Range</a></td>
<td align='center'><a href='rpmtgttitemwisetgsummary.php?starts=%s&ends=%s&regions=%s' target='_blank'>items</a></td></tr>",
$rst[0],
number_format($rst[9],2,'.',','),
number_format($rst[10]+$srt[13],2,'.',','),
number_format($rst[11],2,'.',','),
number_format($rst[14],2,'.',','),
number_format($rst[12],2,'.',','),
number_format($rst[1],0,'.',','),
number_format($rst[3],0,'.',','),
number_format($rst[5],0,'.',','),
number_format($rst[2],0,'.',','),
number_format($pcs,1,'.',','),
number_format($rst[1]-$rst[2],0,'.',','),
number_format($rst[4]+$rst[8],0,'.',','),
number_format($rdss,1,'.',','),
number_format($rst[4],0,'.',','),
number_format($rst[8],0,'.',','),
number_format($rst[6],0,'.',','),
number_format($rst[7],0,'.',','),
$starts,$ends,$rst[0],$starts,$ends,$rst[0],$reg3,$starts,$ends,$rst[0]);
}


printf("<tr style='border-bottom:1px solid silver;font-weight:bold;'>
<td bgcolor='CED9D3'>Total</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'>%s%%</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'>%s%%</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='center'><a href='rpmrdagenttgsummary.php?starts=%s&ends=%s&zones=%s'' target='_blank'>Agent</a></td>
<td align='center'><a href='rpmrangenew.php?starts=%s&ends=%s&zones=%s' target='_blank'>Range</a></td>
<td align='center'><a href='rpmtgttitemwisetgsummary.php?starts=%s&ends=%s&zones=%s' target='_blank'>items</a></td>
</tr></table>",
number_format($tton,2,'.',','),
number_format($rdton,2,'.',','),
number_format($ordton,2,'.',','),
number_format($pendton,2,'.',','),
number_format($prton,2,'.',','),
number_format($tgv,0,'.',','),
number_format($odv,0,'.',','),
number_format($penval,0,'.',','),
number_format($acv,0,'.',','),
number_format($acv/$tgv*100,1,'.',','),
number_format($tgv-$acv,0,'.',','),
number_format($rdv,0,'.',','),
number_format($rdv/$tgv*100,1,'.',','),
number_format($dbrd,0,'.',','),
number_format($dirtot,0,'.',','),
number_format($pcl,0,'.',','),
number_format($wdays,0,',',','),
$starts,$ends,$reg3,$starts,$ends,$reg3,$starts,$ends,$reg3);


printf("<BR>");
printf("<div style='width:100%%;height:5%%;border:0px solid silver;border-radius:2px;display:table;vertical-align:middle;'>");

printf("<div style='font-size:16;color:green;border-bottom:1px solid silver;width:30%%;'>Western - Wayamba - $starts - $ends</div>");

printf("<table id='dt' style='width:100%%;font-size:13;border-collapse:collapse;color:#006699;'>
<tr style='background:silver;border:1px solid white;text-align:right;'>
<td style='border:1px solid white;text-align:center;width:5%%;'>Area</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>Target Ton</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>RD Ton</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>Order Ton</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>Pending Inv Ton</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>Primary Ton</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>Target</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>Primary Order</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>Pending Invoice</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>Primary Sales</td>
<td style='border:1px solid white;text-align:center;width:2%%;'>Pri:%%</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>Primary Difict</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>RD-[DB+Dir]</td>
<td style='border:1px solid white;text-align:center;width:2%%;'>Rd:%%</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>RD-DB</td>
<td style='border:1px solid white;text-align:center;width:5%%;'>Direct</td>
<td style='border:1px solid white;text-align:center;width:4%%;'>P-Calls</td>
<td style='border:1px solid white;text-align:center;width:4%%;'>W-Days</td>
<td style='background:silver;border:1px solid white;width:2%%;' align='center'>View</td>
<td style='background:silver;border:1px solid white;width:2%%;' align='center'>View</td>
<td style='background:silver;border:1px solid white;width:2%%;' align='center'>View</td>
</tr>");

$sqlst="select mzones,tgvl,acvl,odvl,rdvl,penodval,pcls,wdays,acvldir,tton,rdton,odton,prton,dirton,penodton from (select distinct rgs as mzones from (select distinct zone_orders as rds,regions as rgs from rc_invoices ".$flt." and dates>='".$starts."' and dates<='".$ends."' and chanell='GENERAL TRADE' and rsm_names='RSM-PRABATH' union select distinct zone_orders,regions from rc_targets ".$flt."  and dates>='".$starts."' and dates<='".$ends."' and rsm_names='RSM-PRABATH' union select distinct zone_orders,regions from rc_orders ".$flt."  and dates>='".$starts."' and dates<='".$ends."' and chanell='GENERAL TRADE' and rsm_names='RSM-PRABATH' order by rds,rgs) as zns) as ags 

left join (select regions,sum(box_qty*box_price) as tgvl,sum(box_qty*packets_per_box*packet_weights/1000000) as tton from rc_targets ".$flt." and dates>='".$starts."' and dates<='".$ends."' and rsm_names='RSM-PRABATH' group by regions) as tgs on tgs.regions=mzones 

left join (select regions,sum(box_qty*box_price) as acvl,sum(box_qty*packets_per_box*packet_weights/1000000) as prton from rc_invoices ".$flt." and dates>='".$starts."' and dates<='".$ends."' and rsm_names='RSM-PRABATH' group by regions) 
as acs on acs.regions=mzones

left join (select regions,sum(box_qty*box_price) as odvl,sum(box_qty*packets_per_box*packet_weights/1000000) as odton from rc_orders ".$flt." and dates>='".$starts."' and dates<='".$ends."' and order_status<>'CANCELLED' and rsm_names='RSM-PRABATH' group by regions) 
as acss on acss.regions=mzones 


left join (select regions,sum(box_qty*box_price) as penodval,sum(box_qty*packets_per_box*packet_weights/1000000) as penodton from rc_orders ".$flt." and dates>='".$starts."' and dates<='".$ends."' and order_status='PENDING' and rsm_names='RSM-PRABATH' group by regions)
as acssod on acssod.regions=mzones 

left join (select regions,sum(pcs_qty*pcs_price) as rdvl,sum(pcalls) as pcls,sum(pcs_qty*pcs_price)/count(distinct(invoice_number))*count(distinct(agent_names)) as rdavgday,count(distinct(invoice_number)) as wdays,sum(pcs_qty*packet_weights/1000000) as rdton from rd_saleinv ".$flt." and dates>='".$starts."' and dates<='".$ends."' and rsm_names='RSM-PRABATH' group by regions) 
as rds on rds.regions=mzones

left join (select regions,sum(box_qty*box_price) as acvldir,sum(box_qty*packets_per_box*packet_weights/1000000) as dirton from rc_invoices ".$flt." and dates>='".$starts."' and dates<='".$ends."' and agent_types='DIRECT' and rsm_names='RSM-PRABATH' group by regions) 
as acsdr on acsdr.regions=mzones";
 
$tgv=0;
$acv=0;
$tgw=0;
$acw=0;
$odv=0;
$rdv=0;
$pcl=0;
$odvlc=0;
$rdavgday=0;
$wdays=0;
$tton=0;
$ordton=0;
$prton=0;
$rdton=0;
$pendton=0;
$penval=0;
$dbrd=0;
$dirtot=0;
 
$qry=mysqli_query($conn,$sqlst);
while($rst=mysqli_fetch_row($qry)){
$tgv=$tgv+$rst[1];
$odv=$odv+$rst[3];
$acv=$acv+$rst[2];
$rdv=$rdv+$rst[4]+$rst[8];
$pcl=$pcl+$rst[6];
//$odvlc=$odvlc+$rst[5];
//$rdavgday=$rdavgday+$rst[7];
$wdays=$wdays+$rst[7];
$tton=$tton+$rst[9];
$ordton=$ordton+$rst[11];
$prton=$prton+$rst[12];
$rdton=$rdton+$rst[10]+$rst[13];
$pendton=$pendton+$rst[14];
$penval=$penval+$rst[5];
$dbrd=$dbrd+$rst[4];
$dirtot=$dirtot+$rst[8];

if($rst[1]==0){
$pcs=0;}
else{
$pcs=$rst[2]*100/$rst[1];}

if($rst[1]==0){
$rdss=0;}
else{
$rdss=($rst[4]+$rst[9])*100/$rst[1];}
$reg4='RSM-PRABATH';

printf("<tr style='border-bottom:1px solid silver;'><td>%s</td>
<td align='right'>%s</td>
<td align='right'>%s</td>
<td align='right'>%s</td>
<td align='right'>%s</td>
<td align='right'style='color:522109'bgcolor='C6F9D8'>%s</td>
<td align='right'>%s</td>
<td align='right'>%s</td>
<td align='right'style='color:522109'bgcolor='C6F9D8'>%s</td>
<td align='right'>%s</td>
<td align='right'style='color:522109'bgcolor='#DAF7A6'>%s%%</td>
<td align='right' style='color:black'>%s</td>
<td align='right' style='color:red'>%s</td>
<td align='right' style='color:black'bgcolor='#DAF7A6'>%s%%</td>
<td align='right' style='color:black'bgcolor='#D6EAF8'>%s</td>
<td align='right' style='color:purple'>%s</td>
<td align='right' style='color:green'>%s</td>
<td align='right' style='color:orange'>%s</td>
<td align='center'><a href='rpmrdagenttgsummary.php?starts=%s&ends=%s&regions=%s' target='_blank'>Agent</a></td>
<td align='center'><a href='rpmrangenew.php?starts=%s&ends=%s&regions=%s&zones=%s' target='_blank'>Range</a></td>
<td align='center'><a href='rpmtgttitemwisetgsummary.php?starts=%s&ends=%s&regions=%s' target='_blank'>items</a></td></tr>",
$rst[0],
number_format($rst[9],2,'.',','),
number_format($rst[10]+$srt[13],2,'.',','),
number_format($rst[11],2,'.',','),
number_format($rst[14],2,'.',','),
number_format($rst[12],2,'.',','),
number_format($rst[1],0,'.',','),
number_format($rst[3],0,'.',','),
number_format($rst[5],0,'.',','),
number_format($rst[2],0,'.',','),
number_format($pcs,1,'.',','),
number_format($rst[1]-$rst[2],0,'.',','),
number_format($rst[4]+$rst[8],0,'.',','),
number_format($rdss,1,'.',','),
number_format($rst[4],0,'.',','),
number_format($rst[8],0,'.',','),
number_format($rst[6],0,'.',','),
number_format($rst[7],0,'.',','),
$starts,$ends,$rst[0],$starts,$ends,$rst[0],$reg4,$starts,$ends,$rst[0]);
}


printf("<tr style='border-bottom:1px solid silver;font-weight:bold;'>
<td bgcolor='CED9D3'>Total</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'>%s%%</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'>%s%%</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='right'bgcolor='CED9D3'>%s</td>
<td align='center'><a href='rpmrdagenttgsummary.php?starts=%s&ends=%s&zones=%s'' target='_blank'>Agent</a></td>
<td align='center'><a href='rpmrangenew.php?starts=%s&ends=%s&zones=%s' target='_blank'>Range</a></td>
<td align='center'><a href='rpmtgttitemwisetgsummary.php?starts=%s&ends=%s&zones=%s' target='_blank'>items</a></td>
</tr></table>",
number_format($tton,2,'.',','),
number_format($rdton,2,'.',','),
number_format($ordton,2,'.',','),
number_format($pendton,2,'.',','),
number_format($prton,2,'.',','),
number_format($tgv,0,'.',','),
number_format($odv,0,'.',','),
number_format($penval,0,'.',','),
number_format($acv,0,'.',','),
number_format($acv/$tgv*100,1,'.',','),
number_format($tgv-$acv,0,'.',','),
number_format($rdv,0,'.',','),
number_format($rdv/$tgv*100,1,'.',','),
number_format($dbrd,0,'.',','),
number_format($dirtot,0,'.',','),
number_format($pcl,0,'.',','),
number_format($wdays,0,',',','),
$starts,$ends,$reg3,$starts,$ends,$reg4,$starts,$ends,$reg4);


printf("</div>");

}
}
?>

</div>
</body>
</html>