<?php

require "racs.php";
require "vtshowsBBB.php";

session_start();

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

?>

<html>
<title>Sales program</title>

<head>
<link rel="stylesheet" type="text/css" href="vtxstyles.css">

<script>

function rgshows(pr,pv)
{
var dv=document.getElementById("dvshw");
dv.style.display="block";
dv.innerHTML=pv;
dv.style.top=pr.clientY;
dv.style.left='74%';

}


function rghides(pr)
{
document.getElementById("dvshw").style.display='none';
}

</script>


</head>

<body style='font-family:calibri;font-size:small;'>
<div style='postion:absolute;left:0;top:0;width:100%%;'>

<?php


$flt=racs(1);

if($flt=="access failed"){
printf("<div style='font-color:red;'>>Access failed\nContanct Sales and Marketing division</div>");
}
else{

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


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

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

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

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

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

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

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

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

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

#$flt=racs(1);

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


$conn=mysql_connect("localhost","primarysales","S5L7lF01EQZvQrGK");
mysql_select_db("primarysales_dianaold",$conn);
stps(1);

printf("<div class='sds'>");
prshows('pr','dashbs','gobs','sales','Tonnage trend',$zones,$regions,$agents,$agenttypes,$terrys,$ranges,$subranges,$items);

Printf("<br><br>");
printf("<a href='/prd/prdashbs.php'>Switch to classic mode</a>");
printf("</div>");



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

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



if($months>=4){
$fstarts=date('Y-04-01');
$pstarts=($years-1)."-04-01";
$fyears=$years;
$pyears=$years-2;
$pends=$years."-".$months."-".cal_days_in_month(CAL_GREGORIAN,$months,$years);}
else{
$fstarts=($years-1)."-04-01";
$pstarts=($years-2)."-04-01";
$fyears=$years-1;
$pyears=$years-1;
$pends=($years-1)."-".$months."-".cal_days_in_month(CAL_GREGORIAN,$months,$fstarts);}
	
$starts=date('Y-m-01');
$ends=date('Y-m-t');

$sqlst="select max(dates) from rc_invoices where dates>='".$starts."' and dates<='".$ends."' ";
$qry=mysql_query($sqlst,$conn);
if(mysql_num_rows($qry)==0){
$tdates=date('Y-m-d');}
else{
$rst=mysql_fetch_row($qry);
$tdates=$rst[0];}


$sqlst="select invs,tv,mv from (select distinct invoice_types as invs from rc_invoices where dates>='".$starts."' and dates<='".$ends."') as tps left join 
 (select invoice_types,sum(box_qty*box_price) as tv from rc_invoices where dates='".$tdates."' group by invoice_types) as tvl on invs=tvl.invoice_types left join 
(select invoice_types,sum(box_qty*box_price) as mv from rc_invoices where dates>='".$starts."' and dates<='".$ends."' group by invoice_types) as mvs on invs=mvs.invoice_types ";

printf("<table style='width:38%%;color:grey;font-size:13;border:0px solid silver;border-collapse:collapse;'><tr><td width=50%%>
<tr style='font-size:13;'><td colspan='3' style='font-size:18;color:grey;border-bottom:1px solid silver;'>Day summary</td></tr>
<tr style='background:silver;border:1px solid silver;font-size:14;color:white;'><td>Sale type</td>
<td align='right'>%s</td><td align='right'>%s</td></tr>",$tdates,date('Y-M')); 

$tdv=0;
$mnv=0;

$qry=mysql_query($sqlst,$conn);
while($rst=mysql_fetch_row($qry)){
printf("<tr class='bls'><td>%s</td><td align='right'>%s<td align='right'>%s</td></tr>",
$rst[0],number_format($rst[1],0,'.',','),number_format($rst[2],0,'.',','));
$tdv=$tdv+$rst[1];
$mnv=$mnv+$rst[2];
}

printf("<tr class='bls' style='font-weight:bold;font-size:13;'><td>Total</td><td align='right'>%s<td align='right'>%s</td></tr></table>",number_format($tdv,0,'.',','),number_format($mnv,0,'.',','));



printf("<BR><table style='width:97%%;'><tr style='vertical-align:top;'>");

$sqlst="select tgv,acv from (select sum(box_qty*box_price) as tgv from rc_targets where dates>='".$starts."' and dates<='".$ends."') as tgs,
(select sum(box_qty*box_price) as acv from rc_invoices where dates>='".$starts."' and dates<='".$ends."') as acs ";
$qry=mysql_query($sqlst,$conn);
$rst=mysql_fetch_row($qry);

printf("<td style='border:1px solid #006699;width:16%%;'><table style='width:90%%;'><tr><td class='dls' colspan='2'>%s-Achievement</td></tr>",date('Y-M'));
if($rst[0]==0){
printf("<tr><td  class='dls' colspan='2' style='font-size:20;border-bottom:1px solid #006699;'>Not set</td></tr>");}
else{
printf("<tr><td  class='dls' colspan='2' style='font-size:20;border-bottom:1px solid #006699;'>%s%%</td></tr>",number_format($rst[1]*100/$rst[0],0,'.',','));}
printf("<tr><td  class='dls' >Target</td><td class='dls' align='right'>%s</td></tr><tr><td  class='dls'>Actual</td><td class='dls' align='right'>%s</td></tr>
<tr><td  class='dls'>Variance</td><td class='dls' align='right'>%s</td></tr></table></td>",
number_format($rst[0],0,'.',','),number_format($rst[1],0,'.',','),number_format($rst[1]-$rst[0],0,'.',','));

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

$sqlst="select tgv,acv from (select sum(box_qty*box_price) as tgv from rc_targets where dates>='".$fstarts."' and dates<='".$ends."') as tgs,
(select sum(box_qty*box_price) as acv from rc_invoices where dates>='".$fstarts."' and dates<='".$ends."') as acs ";

$qry=mysql_query($sqlst,$conn);
$rst=mysql_fetch_row($qry);

printf("<td style='border:1px solid #006699;width:16%%;'><table style='width:60%%;'><tr><td class='dls' colspan='2'>FY %s - Acheivement</td></tr>",$fyears);
if($rst[0]==0){
printf("<tr><td  class='dls' colspan='2' style='font-size:20;border-bottom:1px solid #006699;'>Not set</td></tr>");}
else{
printf("<tr><td  class='dls' colspan='2' style='font-size:20;border-bottom:1px solid #006699;'>%s%%</td></tr>",number_format($rst[1]*100/$rst[0],0,'.',','));}
printf("<tr><td  class='dls' >Target</td><td class='dls' align='right'>%s</td></tr><tr><td  class='dls'>Actual</td><td class='dls' align='right'>%s</td></tr>
<tr><td  class='dls'>Variance</td><td class='dls' align='right'>%s</td><tr><td colspan='2' style='font-size:12;color:silver;'>Budget include only from Oct 2015</td></tr></table></td>",
number_format($rst[0],0,'.',','),number_format($rst[1],0,'.',','),number_format($rst[1]-$rst[0],0,'.',','));

////////////////////////////////
$sqlst="select tgv,acv from (select sum(box_qty*box_price) as tgv from rc_invoices where dates>='".$starts."' and dates<='".$ends."') as tgs,
(select sum(box_qty*box_price) as acv from rc_invoices where dates>='".($years-1)."-".$months."-01' and dates<='".
($years-1)."-".$months."-".cal_days_in_month(CAL_GREGORIAN,$months,($years-1))."') as acs ";


$qry=mysql_query($sqlst,$conn);
$rst=mysql_fetch_row($qry);

printf("<td style='border:1px solid #006699;width:18%%;'><table style='width:90%%;'><tr><td class='dls' colspan='2'>%s-Month growth</td></tr>",date('Y-M'));
if($rst[1]==0){
printf("<tr><td  class='dls' colspan='2' style='font-size:20;border-bottom:1px solid #006699;'>Not set</td></tr>");}
else{
printf("<tr><td  class='dls' colspan='2' style='font-size:20;border-bottom:1px solid #006699;'>%s%%</td></tr>",number_format($rst[0]*100/$rst[1],0,'.',','));}
printf("<tr><td  class='dls' >%s</td><td class='dls' align='right'>%s</td></tr><tr><td  class='dls'>%s</td><td class='dls' align='right'>%s</td></tr>
<tr><td  class='dls'>Variance</td><td class='dls' align='right'>%s</td></tr></table></td>",$years,
number_format($rst[0],0,'.',','),($years-1),number_format($rst[1],0,'.',','),number_format($rst[0]-$rst[1],0,'.',','));


////////////////////////////////
$sqlst="select tgv,acv from (select sum(box_qty*box_price) as tgv from rc_invoices where dates>='".$fstarts."' and dates<='".$ends."') as tgs,
(select sum(box_qty*box_price) as acv from rc_invoices where dates>='".$pstarts."' and dates<='".$pends."') as acs ";


$qry=mysql_query($sqlst,$conn);
$rst=mysql_fetch_row($qry);

printf("<td style='border:1px solid #006699;width:16%%;'><table style='width:90%%;'><tr><td class='dls' colspan='2'>FY %s - growth</td></tr>",$fyears);
if($rst[1]==0){
printf("<tr><td  class='dls' colspan='2' style='font-size:20;border-bottom:1px solid #006699;'>Not set</td></tr>");}
else{
printf("<tr><td  class='dls' colspan='2' style='font-size:20;border-bottom:1px solid #006699;'>%s%%</td></tr>",number_format($rst[0]*100/$rst[1],0,'.',','));}
printf("<tr><td  class='dls' >FY %s</td><td class='dls' align='right'>%s</td></tr><tr><td  class='dls'>FY %s</td><td class='dls' align='right'>%s</td></tr>
<tr><td  class='dls'>Variance</td><td class='dls' align='right'>%s</td></tr></table></td>",$fyears,
number_format($rst[0],0,'.',','),$fyears-1,number_format($rst[1],0,'.',','),number_format($rst[0]-$rst[1],0,'.',','));


///////////////////////////////
$sqlst="select tgv,acv,tmg,amg from (select sum(box_qty*box_price) as tgv,sum(box_qty*box_price*margin_rates) as tmg from rc_targets where dates>='".$starts."' and dates<='".$ends."') as tgs,
(select sum(box_qty*box_price) as acv,sum(box_qty*box_price*margin_rates) as amg from rc_invoices where dates>='".$starts."' and dates<='".$ends."') as acs ";
$qry=mysql_query($sqlst,$conn);
$rst=mysql_fetch_row($qry);

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

printf("<td style='border:1px solid #006699;width:12%%;'><table style='width:95%%;font-size:12;color:#006699;'><tr>
<td class='dls' colspan='2'>Running margin</td></tr>
<tr><td  class='dls' colspan='2' style='font-size:20;border-bottom:1px solid #006699;'>%s%%</td></tr>
<tr><td colspan='2'>Target margin %s%% (%s)</td></tr></table></td>",number_format($amg,0,'.',','),number_format($tmg,0,'.',','),date('M-Y'));

$sqlst="select item_ranges,sum(box_qty*box_price) from rc_invoices where dates>='".$starts."' and dates<='".$ends."' group by item_ranges";
$qry=mysql_query($sqlst,$conn);

$tv=0;

while($rst=mysql_fetch_row($qry)){
$tv=$tv+$rst[1];}

if($tv==0){
$tv=1;}#patching

printf("<td style='border:1px solid #006699;width:20%%;'><table style='width:100%%;font-size:12;color:#006699;'><tr>
<td class='dls' colspan='2'>Sales mix</td></tr>
<tr><td  class='dls' colspan='2' style='font-size:20;border-bottom:1px solid #006699;'>%s</td></tr>",date('M-Y'));
$qry=mysql_query($sqlst,$conn);
while($rst=mysql_fetch_row($qry)){
printf("<tr><td>%s</td><td align='right'>%s</td><td>(%s%%)</td></tr>",$rst[0],number_format($rst[1],0,'.',','),number_format($rst[1]*100/$tv,0,'.',','));
}
printf("<tr><td>Total</td><td align='right'>%s</td><td>(%s%%)</td></tr></table></td></table>",number_format($tv,0,'.',','),number_format($tv*100/$tv,0,'.',','));


printf("<BR><div id='trzns' style='width:97%%;font-size:12;display:none;'>");
$years=date('Y');
$months=date('m');

$sqlst="select mranges,0 as rods";
$tsqlst="(select distinct zone as mranges from agentsmrd) as mgs ";

for($cnt=0;$cnt<5;$cnt++){
$sqlst=$sqlst.",COALESCE(".$cnt."tgs.vls,0) as ".$years.$months."tgs ";	

$tsqlst=$tsqlst." left join (select zones as trgs,sum((box_qty*box_price)+(pcs_qty*pcs_price)) as vls from rc_targets where 
dates>='".$years."-".$months."-01' and dates<='".$years."-".$months."-".cal_days_in_month(CAL_GREGORIAN,$months,$years)."' group by zones) as ".$cnt.
"tgs  on mranges=".$cnt."tgs.trgs ";

if($months==1){
$months=12;
$years--;}
else{
$months--;}	
}	

$sqlst=$sqlst." from ".$tsqlst;

$sqlst=$sqlst." union all ";

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

$sqlst=$sqlst." select mranges,1";
$tsqlst="(select distinct zone as mranges from agentsmrd) as mgs ";
$tch="<tr style='color:grey;'><td style='width:15%%;'>Zone</td><td></td>";


for($cnt=0;$cnt<5;$cnt++){
$sqlst=$sqlst.",COALESCE(".$cnt."acs.vls,0) as ".$years.$months."act";	

$tsqlst=$tsqlst." left join (select zones as acrgs,sum((box_qty*box_price)+(pcs_qty*pcs_price)) as vls from rc_invoices where 
dates>='".$years."-".$months."-01' and dates<='".$years."-".$months."-".cal_days_in_month(CAL_GREGORIAN,$months,$years)."' group by zones) as ".$cnt.
"acs on mranges=".$cnt."acs.acrgs ";

$tch=$tch."<td colspan='2' style='width:15%%;text-align:right;'>".date_format(date_create($years."-".$months."-01"),'Y-M')." </td>";

if($months==1){
$months=12;
$years--;}
else{
$months--;}	
}	

$sqlst=$sqlst." from ".$tsqlst." order by mranges,rods ";
#echo $sqlst;

$qry=mysql_query($sqlst,$conn);
$fcn=mysql_num_fields($qry);

$arr=array();

printf("<table style='font-size:12;border:0px solid grey;border-collapse:collapse;width:95%%;color:grey;'>
<tr><td colspan='10' style='font-size:18;color:grey;font-weight:normal;text-align:left;border-bottom:1px solid grey;'>Zone wise sales</td>
<td colspan='2' align='right' style='border-bottom:1px solid #006699;' 
onclick=\"document.getElementById('trzns').style.display='none';document.getElementById('trngs').style.display='block';\">
Go to Ranges</td></tr><tr>%s</tr>",$tch);

$sts=0;
while($rst=mysql_fetch_row($qry)){
if($sts==0){
printf("<tr><td rowspan='2' style='width:15%%;border-bottom:1px solid grey;'>%s</td>",$rst[0]);

for($cnt=0;$cnt<$fcn-2;$cnt++){
$arr[$cnt]=$rst[$cnt+2];}
}


if($sts==1){
printf("<td style='width:5%%;'>Target</td>");	
for($cnt=0;$cnt<$fcn-2;$cnt++){
if($arr[$cnt]==0){
$pcn=100;}
else{
$pcn=$rst[$cnt+2]*100/$arr[$cnt];}

printf("<td align='right' style='width:10%%;'>%s</td><td style='width:5%%;text-align:center;background:#D4D4D4;color:#006BC3;border:2px solid white;' 
rowspan='2'>%s%%</td>",
number_format($arr[$cnt],0,'.',','),number_format($pcn,0,'.',','));}

printf("</tr><tr style='border-bottom:1px solid grey;'><td style='width:5%%;'>Actual</td>");
for($cnt=0;$cnt<$fcn-2;$cnt++){
printf("<td align='right'>%s</td>",number_format($rst[$cnt+2],0,'.',','));}
}

if($sts==1){
$sts=0;}
else{
$sts++;}

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



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


printf("<div id='trngs' style='width:97%%;'>");
$years=date('Y');
$months=date('m');

$sqlst="select mranges,0 as rods";
$tsqlst="(select distinct item_ranges as mranges from itemsmrd where item_status='Active') as mgs ";


for($cnt=0;$cnt<5;$cnt++){
$sqlst=$sqlst.",COALESCE(".$cnt."tgs.vls,0) as ".$years.$months."tgs ";	

$tsqlst=$tsqlst." left join (select item_ranges as trgs,sum((box_qty*box_price)+(pcs_qty*pcs_price)) as vls from rc_targets where 
dates>='".$years."-".$months."-01' and dates<='".$years."-".$months."-".cal_days_in_month(CAL_GREGORIAN,$months,$years)."' group by item_ranges) as ".$cnt.
"tgs  on mranges=".$cnt."tgs.trgs ";

if($months==1){
$months=12;
$years--;}
else{
$months--;}	
}	

$sqlst=$sqlst." from ".$tsqlst;

$sqlst=$sqlst." union all ";

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

$sqlst=$sqlst." select mranges,1";
$tsqlst="(select distinct item_ranges as mranges from itemsmrd where item_status='Active') as mgs ";
$tch="<tr style='color:grey;'><td style='width:15%%;'>Range</td><td></td>";


for($cnt=0;$cnt<5;$cnt++){
$sqlst=$sqlst.",COALESCE(".$cnt."acs.vls,0) as ".$years.$months."act";	

$tsqlst=$tsqlst." left join (select item_ranges as acrgs,sum((box_qty*box_price)+(pcs_qty*pcs_price)) as vls from rc_invoices where 
dates>='".$years."-".$months."-01' and dates<='".$years."-".$months."-".cal_days_in_month(CAL_GREGORIAN,$months,$years)."' group by item_ranges) as ".$cnt.
"acs on mranges=".$cnt."acs.acrgs ";

$tch=$tch."<td colspan='2' style='width:15%%;text-align:right;'>".date_format(date_create($years."-".$months."-01"),'Y-M')." </td>";

if($months==1){
$months=12;
$years--;}
else{
$months--;}	
}	

$sqlst=$sqlst." from ".$tsqlst." order by mranges,rods ";
#echo $sqlst;

$qry=mysql_query($sqlst,$conn);
$fcn=mysql_num_fields($qry);

$arr=array();

printf("<table style='font-size:12;border:0px solid grey;border-collapse:collapse;width:75%%;color:grey;'>
<tr><td colspan='10' style='font-size:18;color:grey;font-weight:normal;text-align:left;border-bottom:1px solid grey;'>Range wise sales</td>
<td colspan='2' align='right' style='border-bottom:1px solid #006699;' 
onclick=\"document.getElementById('trngs').style.display='none';document.getElementById('trzns').style.display='block';\">
Go to Zones</td></tr><tr>%s</tr>",$tch);

$sts=0;
while($rst=mysql_fetch_row($qry)){
if($sts==0){
printf("<tr><td rowspan='2' style='width:15%%;border-bottom:1px solid grey;'>%s</td>",$rst[0]);

for($cnt=0;$cnt<$fcn-2;$cnt++){
$arr[$cnt]=$rst[$cnt+2];}
}


if($sts==1){
printf("<td style='width:5%%;'>Target</td>");	
for($cnt=0;$cnt<$fcn-2;$cnt++){
if($arr[$cnt]==0){
$pcn=100;}
else{
$pcn=$rst[$cnt+2]*100/$arr[$cnt];}

printf("<td align='right' style='width:10%%;'>%s</td><td style='width:5%%;text-align:center;background:#D4D4D4;color:#006BC3;border:2px solid white;' 
rowspan='2'>%s%%</td>",
number_format($arr[$cnt],0,'.',','),number_format($pcn,0,'.',','));}

printf("</tr><tr style='border-bottom:1px solid grey;'><td style='width:5%%;'>Actual</td>");
for($cnt=0;$cnt<$fcn-2;$cnt++){
printf("<td align='right'>%s</td>",number_format($rst[$cnt+2],0,'.',','));}
}

if($sts==1){
$sts=0;}
else{
$sts++;}

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

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

#printf("<BR><table style='font-size:13;'>");
#printf("<tr><td>%s - Invoice list</td></tr>",date('Y-m-d'));
#printf("<tr><td>%s - Item list</td></tr>",date('Y-m-d'));
#printf("<tr><td>%s - Agents list</td></tr>",date('Y-m-d'));
#printf("</tr></table>");

$sprt="?actp=rtp&zones=all&regions=all&agents=all&agenttypes=all&terrys=all&items=all&ranges=all&subranges=all&starts=".
$tdates."&ends=".$tdates."' target='_blank'";


printf("<BR><table style='width:85%%;height:9%%;font-size:13;color:silver;'>
<tr><td style='background:none;text-align:left;color:#006699;'>%s</td>
<td style='border-left:1px solid silver;background:none;text-align:center'><a href='vtpragentinvoices.php?%s>Goto invoices</a></td>
<td style='border-left:1px solid silver;background:none;text-align:center'><a href='vtpragentsales.php?%s>Goto Agents</a></td>
<td style='border-left:1px solid silver;background:none;text-align:center'><a href='vtprrtitemwise.php?%s>Goto Items</a></td>
</tr></table>",$tdates,$sprt,$sprt,$sprt);

printf("<BR><br><br><br><br><br><br><br><br><br><br><br><br><br><table style='font-size:13;border:0px solid silver;border-collapse:collapse;width:100%%;color:grey;'>
<th colspan='2' style='font-size:18;color:grey;font-weight:normal;text-align:left;border-bottom:1px solid silver;'>Monthly sales</th>
<tr style='background:silver;font-size:14;color:white;text-align:center;border:1px solid silver;'><td>Month</td>
<td>FY %s</td><td>FY %s</td></tr>",$fyears,$fyears-1);

$sqlst="select mns,nrgs,nvl,pmn,prgs,pvl from (select distinct MONTHNAME(dates) as mns,item_ranges as trgs from rc_invoices order by MONTH(dates)) as mnt 
left join (select MONTHNAME(dates) as nmn,item_ranges as nrgs,
sum((box_qty*box_price)+(pcs_qty*pcs_price)) as nvl from rc_invoices ".$flt." and dates>='".$fstarts.
"' and dates<='".$ends."' group by MONTHNAME(dates),item_ranges) as nms on mns=nmn and trgs=nrgs left 
join (select MONTHNAME(dates) as pmn,item_ranges as prgs,sum((box_qty*box_price)+(pcs_qty*pcs_price)) as pvl from rc_invoices ".$flt." and dates>='".$pstarts.
"' and dates<='".$pends."' group by MONTHNAME(dates),item_ranges) as pms on  mns=pmn and prgs=trgs";


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

$tv=0;
$pv=0;
$pdt=0;
$cnt=0;
$tch="";
$pch="";

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

if($cnt==0){
$tch=$rst[0]."<br>";}

if($rst[0]==$pdt || $cnt==0){
$tv=$tv+$rst[2];
$pv=$pv+$rst[5];
	
$tch=$tch.$rst[1]." - ".number_format($rst[2],2,'.',',')."<br>";
$pch=$pch.$rst[4]." - ".number_format($rst[5],2,'.',',')."<br>";}

if($rst[0]!=$pdt && $cnt!=0){
printf("<tr style='border:1px solid silver;'><td>%s</td>
<td align='right' onmouseover=\"rgshows(event,'%s')\"  onmouseout=\"rghides(event)\">%s</td>
<td align='right' onmouseover=\"rgshows(event,'%s')\"  onmouseout=\"rghides(event)\">%s</td>
</tr>",$pdt,$tch,number_format($tv,0,'.',','),$pch,number_format($pv,0,'.',','));	

$tch=$rst[0]."<br>";
$pch=$rst[0]."<br>";
$tv=$rst[2];
$pv=$rst[5];
$tch=$tch.$rst[1]." - ".number_format($rst[2],2,'.',',')."<br>";
$pch=$pch.$rst[4]." - ".number_format($rst[5],2,'.',',')."<br>";}

$pdt=$rst[0];	
$cnt++;
}

printf("<tr style='border:1px solid silver;'><td>%s</td>
<td align='right' onmouseover=\"rgshows(event,'%s')\"  onmouseout=\"rghides(event)\">%s</td></tr>",$pdt,$tch,number_format($tv,0,'.',','));	
$tch=$rst[0]."<br>";
$tv=$rst[2];
$tch=$tch.$rst[1]." - ".number_format($rst[2],2,'.',',')."<br>";

printf("</table>");


printf("</div></div>");
printf("<div class='shws' style='display:none;' id='dvshw'>-</div>");

}

?>

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




