<?php

require "showshp.php";

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


?>


<html>


<head>

<link rel="stylesheet" type="text/css" href="rdsty.css">
<link rel="stylesheet" type="text/css" media="all" href="jsDatePick_ltr.min.css" />
<link rel="stylesheet" type="text/css" media="all" href="jsDatePick_ltr.css" />
<script type="text/javascript" src="jsDatePick.min.1.3.js"></script>
<script src="datesps.js"></script>

<script>

var arragents=new Array();

function inits()
{
var httpr= new XMLHttpRequest();

httpr.onreadystatechange=function(){
if(httpr.readyState==4 && httpr.status==200){
var vl=httpr.responseText;
var tmpa=vl.split("###");
for(cn=0;cn<tmpa.length;cn++){
arragents[cn]=tmpa[cn];}
}}

var sprftypes="<?php echo $_REQUEST['sprftypes']?>";
httpr.open("POST","pragentsrqst.php?actp=actions&param=0&sprftypes="+sprftypes,true);
httpr.send();
}



function fdtfinds(pr)
{

var keys=pr.keyCode;
//var keys=window.event.keyCode;

var schtxt=document.getElementById("schtxt").value;
var schagents=document.getElementById("schagents");
var sz=0;


if(keys==40){
document.getElementById("schagents").selectedIndex=0;
document.getElementById("schagents").focus();}


if(keys==8 && document.getElementById("schtxt").length==0){
pr.preventDefault();}

if(keys==13){
pr.preventDefault();}

if(keys==27 || keys==13){
schagents.style.display="none";}
else{

schagents.length=0;

var tmpa=schtxt.toLowerCase();

for(var cnt=0;cnt<arragents.length;cnt++){
var tmpb=arragents[cnt].toLowerCase();
var rst=tmpb.indexOf(tmpa);
if(rst>=0){
sz++;
var options=new Option(arragents[cnt],arragents[cnt]);
schagents.add(options,null);}
}

if(sz>0){
schagents.style.display="block";}

if(sz<20){
schagents.size=sz;}
else{
schagents.size=20;}
}


}



function selagents(pr)
{
var schagents=document.getElementById("schagents");
var schtxt=document.getElementById("schtxt");

var keys=window.event.keyCode;

if(keys==8){
pr.preventDefault();
schtxt.focus();
schagents.style.display="none";}

if(keys==27){
schtxt.focus();
schagents.style.display="none";}

if(keys==38 && schagents.size==0){
schagents.style.display="none";
schtxt.focus();}

if(keys==13){
pr.preventDefault();
var vl=schagents.options[schagents.selectedIndex].text;
schtxt.value=vl;
schagents.size=0;
schagents.style.display="none";
schtxt.focus();}

}


function foutsagents()
{
document.getElementById("schagents").style.display="none";
document.getElementById("schtxt").focus();
}


function agclicks()
{
var schagents=document.getElementById("schagents");
var schtxt=document.getElementById("schtxt");

var vl=schagents.options[schagents.selectedIndex].text;
schtxt.value=vl;
schagents.size=0;
schagents.style.display="none";
schtxt.focus();
}



function gensprf()
{
var schtxt=document.getElementById("schtxt").value;
var sts=0;

for(var cnt=0;cnt<arragents.length;cnt++){
if(schtxt==arragents[cnt]){
sts=1;}
}


if(sts==1 && schtxt.length>1){
var sprftypes="<?php echo $_REQUEST['sprftypes']?>";
var prhrf=encodeURI("http://www.diana.lk/prd/prprofs.php?actp=pragprf&params=")+encodeURIComponent(schtxt)+"&sprftypes="+sprftypes;
window.location.href=prhrf;}
else{
alert("Check again and try - "+ schtxt);
sts=0;}


}

</script>

</head>



<body style="font-family:calibri;font-size:small" onload="inits()">
<div class="hdn"></div>

<?php
printf("<div class='ses'>%s (%s)</div>",$_SESSION['titles'],$_SESSION['positions']);

printf("<div class='tops' align='center'>%s Profile</div>",ucwords($_REQUEST['sprftypes']));

?>
<div class="homes"><a href="index.php" style='color:#A9BCF5;'>Sign out</a></div>

</body>

</html>




<?php

if(isset($_REQUEST['sprftypes']) && isset($_REQUEST['params'])){

$sprftypes=$_REQUEST['sprftypes'];
$params=$_REQUEST['params'];

if(strtolower($params)=='all'){
$params='all';}


$grt=0;
$grq=0;
$arr=array();
$arvl=array();
$tvt=0;

$starts=$_REQUEST['starts'];
$ends=$_REQUEST['ends'];
$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;}


showsrds();

$conn=mysqli_connect("localhost","primarysales","S5L7lF01EQZvQrGK","primarysales_dianaold");
$months=date('m');
$years=date('Y');

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

$mindates=$years."-".$months."-01";
$dates=date_create($mindates);
$mindates=date_format($dates,'Y-M-d');
$maxdates=date('Y-M-d');


$sqlst="select zone,region,rsm_name,executive_name,agent_name,agent_type,territory_name,rep_name from agentsmrd where agent_name='".$_REQUEST['params']."' ";
$qry=mysqli_query($conn,$sqlst);
$rst=mysqli_fetch_row($qry);


printf("<div class='dts'><table style='font-size:14'><tr style='border-bottom:1px solid silver;'><td width=200><b><font color=#0066cc>Report details</font></td></tr</table>");
printf("<table><table style='font-size:13'>");
printf("<tr style='padding:5%%;'><td>Title</td><td>: Agent profile</td></tr>");
printf("<tr style='padding:5%%;'><td>From</td><td>: %s</td></tr>",$mindates);
printf("<tr style='padding:5%%;'><td>To</td><td>: %s</td></tr>",$maxdates);

printf("<tr style='padding:5%%;'><td>Zone</td><td>: %s</td></tr>",$rst[0]);
printf("<tr style='padding:5%%;'><td>Region</td><td>: %s</td></tr>",$rst[1]);
printf("<tr style='padding:5%%;'><td>RSM</td><td>: %s</td></tr>",$rst[2]);
printf("<tr style='padding:5%%;'><td>Executive</td><td>: %s</td></tr>",$rst[3]);
printf("<tr style='padding:5%%;'><td>Type</td><td>: %s</td></tr>",$rst[5]);
printf("<tr style='padding:5%%;'><td>Territory</td><td>: %s</td></tr>",$rst[6]);
printf("<tr style='padding:5%%;'><td>Rep</td><td>: %s</td></tr>",$rst[7]);
printf("<tr style='padding:5%%;'><td>Agent</td><td>: %s</td></tr>",$rst[4]);
printf("</table></div>");


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

$sprftypes=$_REQUEST['sprftypes'];

if($sprftypes=='zones'){
$sqparams="zones";
$zones=$params;}
if($sprftypes=='regions'){
$sqparams="regions";
$regions=$params;}
if($sprftypes=='agents'){
$sqparams="agent_names";
$agents=$params;}
if($sprftypes=='territorys'){
$sqparams="territory_names";
$territorys=$params;}


printf("<div class='srg'>");
printf("<table style='position:absolute;top:10%%;left:2%%;font-size:14;color:black;'><tr><td width=45%%>%s : %s</td><td width=10%% align='right' style='padding-right:1%%;'>Search</td><td width=25%%><input type='text' style='font-size:12;width:100%%;border:1px solid silver;border-radius:2px;' size=30 id='schtxt' onkeyup=\"fdtfinds(event)\"></td><td align='center' width=10%% onclick=\"gensprf()\"><div style='width:80%%;height:80%%;border:1px solid grey;border-radius:2px;'  onclick=\"gensprf()\" onmouseover=\"this.style.backgroundColor='silver';this.style.color='#E6E6E6';this.style.borderColor='grey';\" onmouseout=\"this.style.backgroundColor='#E6E6E6';this.style.color='black';this.style.borderColor='grey';\"> <b>Go</b</div></td></tr></table>",ucwords($sprftypes),$params);

printf("<select id='schagents' style='position:relative;top:80%%;left:66%%;color:grey;z-index:1;' hidden onkeydown=\"selagents(event)\" onfocusout=\"foutsagents()\" ondblclick=\"agclicks()\"><option style='font-size:12;'>ones</option></select></div>");

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

$sqlst="select ".$sqparams." from rd_invoices where ".$sqparams."='".$params."' ";
#echo $sqlst;
$qry=mysqli_query($conn,$sqlst);
if(mysqli_num_rows($qry)>0 || $params=='all'){


$seflt=" where agent_names is not null ";

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

while($serst=mysqli_fetch_row($seqry)){
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]."' ";}
}

#echo $seflt;



#printf("<div style='color:#045FB4' class='frk'><input type='radio' name='chsh' checked onclick=\"document.getElementById('tdta').style.display='block';document.getElementById('tdtb').style.display='block';\">Values</div>");
#printf("<div style='color:#045FB4'  class='frm'><input type='radio' name='chsh'  onclick=\"document.getElementById('tdta').style.display='none';document.getElementById('tdtb').style.display='block';\"> %%</div>");
#printf("<div  style='color:#045FB4' class='frn'><input type='radio' name='chsh'  onclick=\"document.getElementById('tdta').style.display='block';document.getElementById('tdtb').style.display='block';\">Both</div>");

$pmonths=date("'m'");


#$flt=$seflt." and dates>='".$_REQUEST['starts']."' and dates<='".$_REQUEST['ends']."' ";
$flt=$seflt;
if(strtolower($regions)!='all'){
$flt=$flt." and regions='".$regions."' ";}
if(strtolower($zones)!='all'){
$flt=$flt." and zones='".$zones."' ";}
if(strtolower($agents)!='all'){
$flt=$flt." and agent_names='".$agents."' ";}
if(strtolower($territorys)!='all'){
$flt=$flt." and territory_names='".$territorys."' ";}
if(strtolower($ranges)!='all'){
$flt=$flt." and item_ranges='".$ranges."' ";}
if(strtolower($subranges)!='all'){
$flt=$flt." and sub_ranges='".$subranges."' ";}
if(strtolower($executive_names)!='all'){
$flt=$flt." and executive_names='".$executive_names."' ";}


$sqlst="select acts,bgts from (select sum(box_qty*box_price)as acts from rd_invoices ".$flt." and dates>='".date('Y-m-01')."' and dates<='".date('Y-m-t')."') as acs,(select sum(box_qty*box_price) as bgts from rc_targets ".$flt." and dates>='".date('Y-m-01')."' and dates<='".date('Y-m-t')."' ) as bjs ";

$qry=mysqli_query($conn,$sqlst);
$rst=mysqli_fetch_row($qry);

if($rst[1]==0){
$ach="Target not set";}
if($rst[1]!=0){
$ach=number_format($rst[0]*100/$rst[1],0,'.',',')." %<br>Achieved";}

print("<div class='tdt' style='display:block;font-size:14;' id='tdta'>");
printf("<br>");
printf("<table id='dtd' style='font-size:13;width:80%%;border-left:1px solid silver;'><tr class='cls' style='background:silver;font-weight:bold;color:#424242;border:0px;'><td width=25%%>%s summary (Past 6 months)</td>
<td align='right' width=10%% style='padding-right:2%%;'>Value</td><td align='right' width=10%% style='padding-right:2%%;'>Kg</td><td align='center' width=45%% rowspan='13'style='color:#0066CC;font-size:24;background:white;border-left:1px solid silver;'><b>%s</b></td></tr>",ucwords($sprftypes),$ach);


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

if(date('m')==1){
$maxdates=(date('Y')-1)."-12-31";}
else{
$maxdates=date('Y')."-".(date('m')-1)."-".cal_days_in_month(CAL_GREGORIAN,((date('m')-1)),date('Y'));}


if((date('m')-5)<1){
$mindates=(date('Y')-1)."-".(12-(date('m')-6))."-01";}
else{
$mindates=date('Y')."-".(date('m')-6)."-01";}

$sqlst="select sum(box_qty*box_price),sum(box_qty*packet_weights*packets_per_box/10000) from rd_invoices ".$flt." and dates>='".$mindates."' and dates<='".$maxdates."'";
#echo $sqlst;
$qry=mysqli_query($conn,$sqlst);
$rst=mysqli_fetch_row($qry);
printf("<tr class='cls'><td>Total sales</td><td align='right'>%s</td><td align='right'>%s</td></tr>",number_format($rst[0],0,'.',','),number_format($rst[1],0,'.',','));
$tsales=$rst[0];
$tkgs=$rst[1];


$sqlst="select avg(vls),avg(kgs) from (select month(dates) as months,sum(box_qty*box_price) as vls,sum(box_qty*packet_weights*packets_per_box/10000) as kgs from rd_invoices ".$flt." and   dates>='".$mindates."' and dates<='".
$maxdates."' group by month(dates)) as sst where vls<>0";
$qry=mysqli_query($conn,$sqlst);
$rst=mysqli_fetch_row($qry);
printf("<tr class='cls'><td>Average monthly sale</td><td align='right'>%s</td><td align='right'>%s</td></tr>",number_format($rst[0],0,'.',','),number_format($rst[1],0,'.',','));

$sqlst="select min(vls),min(kgs) from (select month(dates) as months,sum(box_qty*box_price) as vls,sum(box_qty*packet_weights*packets_per_box/10000) as kgs from rd_invoices ".$flt." and  dates>='".$mindates."' and dates<='".
$maxdates."' group by month(dates)) as sst where vls<>0";
$qry=mysqli_query($conn,$sqlst);
$rst=mysqli_fetch_row($qry);
printf("<tr class='cls'><td>Minimum monthly sale</td><td align='right'>%s</td><td align='right'>%s</td></tr>",number_format($rst[0],0,'.',','),number_format($rst[1],0,'.',','));
#echo $sqlst;

$sqlst="select max(vls),max(kgs) from (select  month(dates) as months,sum(box_qty*box_price) as vls,sum(box_qty*packet_weights*packets_per_box/10000) as kgs from rd_invoices ".$flt." and dates>='".$mindates."' and dates<='".
$maxdates."' group by month(dates)) as sst";
$qry=mysqli_query($conn,$sqlst);
$rst=mysqli_fetch_row($qry);
printf("<tr class='cls'><td>Maxmimum monthly sale</td><td align='right'>%s</td><td align='right'>%s</td></tr>",number_format($rst[0],0,'.',','),number_format($rst[1],0,'.',','));

$sqlst="select std(vls),std(kgs) from (select  month(dates) as months,sum(box_qty*box_price) as vls,sum(box_qty*packet_weights*packets_per_box/10000) as kgs from rd_invoices ".$flt." and dates>='".$mindates."' and dates<='".
$maxdates."' group by month(dates)	) as sst";
$qry=mysqli_query($conn,$sqlst);
$rst=mysqli_fetch_row($qry);
printf("<tr class='cls'><td>Sales monthly spread</td><td align='right'>(%s%%)</td><td align='right'>(%s%%)</td></tr>",
number_format($rst[0]*100/$tsales,0,'.',','),number_format($rst[1]*100/$tkgs,0,'.',','));

$sqlst="select sum(box_qty*box_price*margin_rates),sum(box_qty*box_price*margin_rates)*100/sum(box_qty*box_price) from rd_invoices ".$flt." and dates>='".$mindates."' and dates<='".
$maxdates."'";
$qry=mysqli_query($conn,$sqlst);
$rst=mysqli_fetch_row($qry);
printf("<tr class='cls'><td colspan='2'>Sales margin %%</td><td align='right'> (%s%%)</td></tr>",number_format($rst[1],2,'.',','));

$sqlst="select count(items) from (select  distinct item_names as items from rd_invoices ".$flt." and dates>='".$mindates."' and dates<='".
$maxdates."') as sst";
$qry=mysqli_query($conn,$sqlst);
$rst=mysqli_fetch_row($qry);
printf("<tr class='cls'><td colspan='2'>Number of items sold</td><td align='right'>%s</td></tr>",number_format($rst[0],0,'.',','));

$sqlst="select count(invs) from (select  distinct invoice_number as invs from rd_invoices ".$flt." and dates>='".$mindates."' and dates<='".
$maxdates."') as sst";
$qry=mysqli_query($conn,$sqlst);
$rst=mysqli_fetch_row($qry);
printf("<tr class='cls'><td colspan='2'>Number of invoices</td><td align='right'>%s</td></tr>",number_format($rst[0],0,'.',','));


$hrfitems="<a href='rdhsitemsummary.php?rpsty=agentsitems&dtd=dtdaa&starts=".$mindates."
&ends=".$maxdates."&zones=".$zones."&regions=".$regions."&agents=".urlencode($agents)."&territorys=".$territorys."'target='_blank'>Item summary</a>";

$hrfinvoices="<a href='rdagentsinvoices.php?rpsty=agentsitems&dtd=dtdaa&starts=".$mindates."
&ends=".$maxdates."&zones=".$zones."&regions=".$regions."&agents=".urlencode($agents)."&territorys=".$territorys."'target='_blank'>Invoice summary</a>";

$hrfregions="<a href='rdhsregionsummary.php?rpsty=agentsitems&dtd=dtdaa&starts=".$mindates."
&ends=".$maxdates."&zones=".$zones."&regions=".$regions."&agents=".urlencode($agents)."&territorys=".$territorys."'target='_blank'>Region summary</a>";

$hrfagents="<a href='rdhsagentsummary.php?rpsty=agentsitems&dtd=dtdaa&starts=".$mindates."
&ends=".$maxdates."&zones=".$zones."&regions=".$regions."&agents=".urlencode($agents)."&territorys=".$territorys."'target='_blank'>Agent summary</a>";

if($sprftypes=='zones'){
printf("<tr class='cls'><td colspan='3'>Goto - %s</td></td>",$hrfregions);
printf("<tr class='cls'><td colspan='3'>Goto - %s</td></td>",$hrfagents);}

if($sprftypes=='regions'){
printf("<tr class='cls'><td colspan='3'>Goto - %s</td></td>",$hrfagents);}

printf("<tr class='cls'><td colspan='3'>Goto - %s</td></td>",$hrfitems);
printf("<tr class='cls'><td colspan='3'>Goto - %s</td></td>",$hrfinvoices);
printf("</table>");

printf("<BR>");


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

$arr=array();

$sqlst="select invts";

for($cnt=0;$cnt<6;$cnt++){
$arr[$cnt]=0;
$sqlst=$sqlst.",".$months."vls";
$months--;
if($months==0){
$years--;}
}



$sqlst=$sqlst." from (select distinct invoice_types as invts from rd_invoices) as invs";

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

for($cnt=0;$cnt<6;$cnt++){
$sqlst=$sqlst." left join (select invoice_types as ".$months."invs, sum(box_qty*box_price) as ".$months."vls from rd_invoices ".$flt." and dates>='".$years."-".$months."-01' and dates<='".$years."-".$months."-".cal_days_in_month(CAL_GREGORIAN,$months,$years)."' group by invoice_types) as ".$months.$years."st on ".$months."invs=invts ";
$months--;
if($months==0){
$months=12;	
$years--;}
}

#echo $sqlst;



printf("<BR>");


print("<div style='display:block;font-size:14;' id='tdta'>");
printf("<br>");
printf("<table id='dtd' style='font-size:13;width:100%%;border:1px solid silver;'><th style='background:grey;color:silver;' colspan='13' align='center'><b>Sales trend - by invoice type</th>");

printf("<tr class='cls' style='background:silver;'><td align='center'>Invoice type</td>");
$months=date('m');
$years=date('Y');

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

$dates=date_create($years."-".$months);
printf("<td colspan='2' align='center'>%s</td>",date_format($dates,'M-Y'));
$months--;
if($months==0){
$months=12;	
$years--;}
}

printf("</tr>");


$arr=array();
$qry=mysqli_query($conn,$sqlst);
while($rst=mysqli_fetch_row($qry)){
for($cnt=0;$cnt<6;$cnt++){
$arr[$cnt]=$arr[$cnt]+$rst[$cnt+1];}
}

$qry=mysqli_query($conn,$sqlst);
while($rst=mysqli_fetch_row($qry)){
printf("<tr class='cls'><td>%s</td>",$rst[0]);
for($cnt=0;$cnt<6;$cnt++){
if($arr[$cnt]==0){
$pcn=0;}
else{
$pcn=$rst[$cnt+1]*100/$arr[$cnt];}

printf("<td align='right'>%s</td><td align='right'>%s%%</td>",number_format($rst[$cnt+1],0,'.',','), number_format($pcn,0,'.',','));}
printf("</tr>");
}


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

printf("</table>");

printf("<BR>");


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

$arr=array();

$sqlst="select invts";

for($cnt=0;$cnt<6;$cnt++){
$arr[$cnt]=0;
$sqlst=$sqlst.",".$months."vls";
$months--;
if($months==0){
$years--;}
}



$sqlst=$sqlst." from (select distinct item_ranges as invts from rd_invoices) as invs";

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

for($cnt=0;$cnt<6;$cnt++){
$sqlst=$sqlst." left join (select item_ranges as ".$months."invs, sum(box_qty*box_price) as ".$months."vls from rd_invoices ".$flt." and dates>='".$years."-".$months."-01' and dates<='".
$years."-".$months."-".cal_days_in_month(CAL_GREGORIAN,$months,$years)."' group by item_ranges) as ".$months.$years."st on ".$months."invs=invts ";
$months--;
if($months==0){
$months=12;	
$years--;}
}

#echo $sqlst;


printf("<BR>");

print("<div style='display:block;font-size:14;' id='tdta'>");
printf("<br>");
printf("<table id='dtd' style='font-size:13;width:100%%;border:1px solid silver;'><th style='background:grey;color:silver;' colspan='13' align='center'><b>Sales trend - by range</th>");

printf("<tr class='cls' style='background:silver;'><td align='center'>Item range</td>");
$months=date('m');
$years=date('Y');

for($cnt=0;$cnt<6;$cnt++){
$dates=date_create($years."-".$months);
printf("<td colspan='2' align='center'>%s</td>",date_format($dates,'M-Y'));
$months--;
if($months==0){
$months=12;	
$years--;}
}

printf("</tr>");
$arr=array();
$qry=mysqli_query($conn,$sqlst);
while($rst=mysqli_fetch_row($qry)){
for($cnt=0;$cnt<6;$cnt++){
$arr[$cnt]=$arr[$cnt]+$rst[$cnt+1];}
}

$qry=mysqli_query($conn,$sqlst);
while($rst=mysqli_fetch_row($qry)){
printf("<tr class='cls'><td>%s</td>",$rst[0]);
for($cnt=0;$cnt<6;$cnt++){
if($arr[$cnt]==0){
$pcn=0;}
else{
$pcn=$rst[$cnt+1]*100/$arr[$cnt];}

printf("<td align='right'>%s</td><td align='right'>%s%%</td>",number_format($rst[$cnt+1],0,'.',','), number_format($pcn,0,'.',','));}
printf("</tr>");
}

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

printf("</table>");

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


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

$arr=array();

$sqlst="select invts";

for($cnt=0;$cnt<6;$cnt++){
$arr[$cnt]=0;
$sqlst=$sqlst.",".$months."vls";
$months--;
if($months==0){
$years--;}
}



$sqlst=$sqlst." from (select distinct item_ranges as invts from rd_invoices) as invs";

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

for($cnt=0;$cnt<6;$cnt++){
$sqlst=$sqlst." left join (select item_ranges as ".$months."invs, sum(box_qty*packet_weights*packets_per_box/1000) as ".$months."vls from rd_invoices ".$flt." and dates>='".$years."-".$months."-01' and dates<='".$years."-".$months."-".cal_days_in_month(CAL_GREGORIAN,$months,$years)."' group by item_ranges) as ".$months.$years."st on ".$months."invs=invts ";
$months--;
if($months==0){
$months=12;	
$years--;}
}

#echo $sqlst;


printf("<BR>");

print("<div style='display:block;font-size:14;' id='tdta'>");
printf("<br>");
printf("<table id='dtd' style='font-size:13;width:100%%;border:1px solid silver;'><th style='background:grey;color:silver;' colspan='13' align='center'><b>Sales trend - by tonnage</th>");

printf("<tr class='cls' style='background:silver;'><td align='center'>Item range</td>");
$months=date('m');
$years=date('Y');

for($cnt=0;$cnt<6;$cnt++){
$dates=date_create($years."-".$months);
printf("<td colspan='2' align='center'>%s</td>",date_format($dates,'M-Y'));
$months--;
if($months==0){
$months=12;	
$years--;}
}

printf("</tr>");
$arr=array();
$qry=mysqli_query($conn,$sqlst);
while($rst=mysqli_fetch_row($qry)){
for($cnt=0;$cnt<6;$cnt++){
$arr[$cnt]=$arr[$cnt]+$rst[$cnt+1];}
}

$qry=mysqli_query($conn,$sqlst);
while($rst=mysqli_fetch_row($qry)){
printf("<tr class='cls'><td>%s</td>",$rst[0]);
for($cnt=0;$cnt<6;$cnt++){
if($arr[$cnt]==0){
$pcn=0;}
else{
$pcn=$rst[$cnt+1]*100/$arr[$cnt];}

printf("<td align='right'>%s</td><td align='right'>%s%%</td>",number_format($rst[$cnt+1],0,'.',','), number_format($pcn,0,'.',','));}
printf("</tr>");
}

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

printf("</table>");

printf("<BR>");


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

$arr=array();

$sqlst="select invts";

for($cnt=0;$cnt<6;$cnt++){
$arr[$cnt]=0;
$arr[$cnt*2]=0;
$sqlst=$sqlst.",".$months."tgs".",".$months."vls,".$months."vls*100/".$months."tgs";
$months--;
if($months==0){
$years--;}
}



$sqlst=$sqlst." from (select distinct item_ranges as invts from rd_invoices) as invs";

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

for($cnt=0;$cnt<6;$cnt++){
$sqlst=$sqlst." left join (select item_ranges as ".$months."invsg, sum(box_qty*box_price) as ".$months."tgs from rc_targets ".$flt." and dates>='".$years."-".$months."-01' and dates<='".$years."-".$months."-".cal_days_in_month(CAL_GREGORIAN,$months,$years)."' group by item_ranges) as ".$months.$years."tg on ".$months."invsg=invts  ";

$sqlst=$sqlst." left join (select item_ranges as ".$months."invs, sum(box_qty*box_price) as ".$months."vls from rd_invoices ".$flt." and dates>='".$years."-".$months."-01' and dates<='".$years."-".$months."-".cal_days_in_month(CAL_GREGORIAN,$months,$years)."' group by item_ranges) as ".$months.$years."st on ".$months."invs=invts  ";

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

#echo $sqlst;


printf("<BR>");

print("<div style='display:block;font-size:14;' id='tdta'>");
printf("<br>");
printf("<table id='dtd' style='font-size:13;width:100%%;border:1px solid silver;'><th  style='background:grey;color:silver;' colspan='19' align='center'><b>Target achievement trend - sales value</th>");

printf("<tr class='cls' style='background:silver;'><td align='center'>Item range</td>");
$months=date('m');
$years=date('Y');

for($cnt=0;$cnt<6;$cnt++){
$dates=date_create($years."-".$months."-01");
printf("<td colspan='3' align='center'>%s</td>",date_format($dates,'M-Y'));
$months--;
if($months==0){
$months=12;	
$years--;}
}

$sts=0;

$qry=mysqli_query($conn,$sqlst);
while($rst=mysqli_fetch_row($qry)){
printf("<tr class='cls'><td>%s</td>",$rst[0]);
for($cnt=0;$cnt<18;$cnt++){
$sts++;
if($sts==3){
$sts=0;
printf("<td align='right'>%s%%</td>",number_format($rst[$cnt+1],0,'.',','));}
else{
printf("<td align='right'>%s</td>",number_format($rst[$cnt+1],0,'.',','));}
$arr[$cnt]=$arr[$cnt]+$rst[$cnt+1];}
printf("</tr>");
}

$sts=0;

printf("<tr class='cls' style='font-weight:bold;'><td align='center'>Total</td>");
for($cnt=0;$cnt<18;$cnt++){

if($sts==0 && $arr[$cnt]==0){
$pcn=100;}
if($sts==0 && $arr[$cnt]>0){
$pcn=$arr[$cnt+1]*100/$arr[$cnt];}

$sts++;
if($sts<3){
printf("<td align='right'>%s</td>",number_format($arr[$cnt],0,'.',','));}
else{
$sts=0;	
printf("<td align='right'>%s%%</td>",number_format($pcn,0,'.',','));}
	
}


printf("</table>");

printf("<BR>");


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

$arr=array();

$sqlst="select invts";

for($cnt=0;$cnt<6;$cnt++){
$arr[$cnt]=0;
$arr[$cnt*2]=0;
$sqlst=$sqlst.",".$months."tgs".",".$months."vls,".$months."vls*100/".$months."tgs";
$months--;
if($months==0){
$years--;}
}



$sqlst=$sqlst." from (select distinct item_ranges as invts from rd_invoices) as invs";

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

for($cnt=0;$cnt<6;$cnt++){
$sqlst=$sqlst." left join (select item_ranges as ".$months."invsg, sum(box_qty*packets_per_box*packet_weights/1000) as ".$months."tgs from rc_targets ".$flt." and dates>='".$years."-".$months."-01' and dates<='".$years."-".$months."-".cal_days_in_month(CAL_GREGORIAN,$months,$years)."' group by item_ranges) as ".$months.$years."tg on ".$months."invsg=invts  ";

$sqlst=$sqlst." left join (select item_ranges as ".$months."invs, sum(box_qty*packets_per_box*packet_weights/1000) as ".$months."vls from rd_invoices ".$flt." and dates>='".$years."-".$months."-01' and dates<='".$years."-".$months."-".cal_days_in_month(CAL_GREGORIAN,$months,$years)."' group by item_ranges) as ".$months.$years."st on ".$months."invs=invts  ";

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

#echo $sqlst;


printf("<BR>");

print("<div style='display:block;font-size:14;' id='tdta'>");
printf("<br>");
printf("<table id='dtd' style='font-size:13;width:100%%;border:1px solid silver;'><th  style='background:grey;color:silver;' colspan='19' align='center'><b>Target achievement trend - tonnage</th>");

printf("<tr class='cls' style='background:silver;'><td align='center'>Item range</td>");
$months=date('m');
$years=date('Y');

for($cnt=0;$cnt<6;$cnt++){
$dates=date_create($years."-".$months."-01");
printf("<td colspan='3' align='center'>%s</td>",date_format($dates,'M-Y'));
$months--;
if($months==0){
$months=12;	
$years--;}
}

$sts=0;

$qry=mysqli_query($conn,$sqlst);
while($rst=mysqli_fetch_row($qry)){
printf("<tr class='cls'><td>%s</td>",$rst[0]);
for($cnt=0;$cnt<18;$cnt++){
$sts++;
if($sts==3){
$sts=0;
printf("<td align='right'>%s%%</td>",number_format($rst[$cnt+1],0,'.',','));}
else{
printf("<td align='right'>%s</td>",number_format($rst[$cnt+1],0,'.',','));}
$arr[$cnt]=$arr[$cnt]+$rst[$cnt+1];}
printf("</tr>");
}

$sts=0;

printf("<tr class='cls' style='font-weight:bold;'><td align='center'>Total</td>");
for($cnt=0;$cnt<18;$cnt++){

if($sts==0 && $arr[$cnt]==0){
$pcn=100;}
if($sts==0 && $arr[$cnt]>0){
$pcn=$arr[$cnt+1]*100/$arr[$cnt];}

$sts++;
if($sts<3){
printf("<td align='right'>%s</td>",number_format($arr[$cnt],0,'.',','));}
else{
$sts=0;	
printf("<td align='right'>%s%%</td>",number_format($pcn,0,'.',','));}
	
}

printf("</table>");

printf("<BR>");


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

$arr=array();

$sqlst="select invts";

for($cnt=0;$cnt<6;$cnt++){
$arr[$cnt]=0;
$arr[$cnt*2]=0;
$sqlst=$sqlst.",".$months."tgs".",".$months."vls,".$months."vls*100/".$months."tgs";
$months--;
if($months==0){
$years--;}
}



$sqlst=$sqlst." from (select distinct item_ranges as invts from rd_invoices) as invs";

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

for($cnt=0;$cnt<6;$cnt++){
$sqlst=$sqlst." left join (select item_ranges as ".$months."invsg, sum(box_qty*margin_rates) as ".$months."tgs from rc_targets ".$flt." and dates>='".$years."-".$months."-01' and dates<='".$years."-".$months."-".cal_days_in_month(CAL_GREGORIAN,$months,$years)."' group by item_ranges) as ".$months.$years."tg on ".$months."invsg=invts  ";

$sqlst=$sqlst." left join (select item_ranges as ".$months."invs, sum(box_qty*margin_rates) as ".$months."vls from rd_invoices ".$flt." and dates>='".$years."-".$months."-01' and dates<='".$years."-".$months."-".cal_days_in_month(CAL_GREGORIAN,$months,$years)."' group by item_ranges) as ".$months.$years."st on ".$months."invs=invts  ";

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

#echo $sqlst;


printf("<BR>");

print("<div style='display:block;font-size:14;' id='tdta'>");
printf("<br>");
printf("<table id='dtd' style='font-size:13;width:100%%;border:1px solid silver;'><th  style='background:grey;color:silver;' colspan='19' align='center'><b>Target achievement trend - margin</th>");

printf("<tr class='cls' style='background:silver;'><td align='center'>Item range</td>");
$months=date('m');
$years=date('Y');

for($cnt=0;$cnt<6;$cnt++){
$dates=date_create($years."-".$months."-01");
printf("<td colspan='3' align='center'>%s</td>",date_format($dates,'M-Y'));
$months--;
if($months==0){
$months=12;	
$years--;}
}

$sts=0;

$qry=mysqli_query($conn,$sqlst);
while($rst=mysqli_fetch_row($qry)){
printf("<tr class='cls'><td>%s</td>",$rst[0]);
for($cnt=0;$cnt<18;$cnt++){
$sts++;
if($sts==3){
$sts=0;
printf("<td align='right'>%s%%</td>",number_format($rst[$cnt+1],0,'.',','));}
else{
printf("<td align='right'>%s</td>",number_format($rst[$cnt+1],0,'.',','));}
$arr[$cnt]=$arr[$cnt]+$rst[$cnt+1];}
printf("</tr>");
}

$sts=0;

printf("<tr class='cls' style='font-weight:bold;'><td align='center'>Total</td>");
for($cnt=0;$cnt<18;$cnt++){

if($sts==0 && $arr[$cnt]==0){
$pcn=100;}
if($sts==0 && $arr[$cnt]>0){
$pcn=$arr[$cnt+1]*100/$arr[$cnt];}

$sts++;
if($sts<3){
printf("<td align='right'>%s</td>",number_format($arr[$cnt],0,'.',','));}
else{
$sts=0;	
printf("<td align='right'>%s%%</td>",number_format($pcn,0,'.',','));}
	
}


printf("</table>");

printf("<BR>");


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

$sqlst="select invts";

for($cnt=0;$cnt<6;$cnt++){
$arr[$cnt]=0;
$sqlst=$sqlst.",".$months."vls";
$months--;
if($months==0){
$years--;}
}


$sqlst=$sqlst." from (select distinct item_ranges as invts from rd_invoices) as invs";

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

for($cnt=0;$cnt<6;$cnt++){
$sqlst=$sqlst." left join (select item_ranges as ".$months."invs, sum(box_qty*box_price*margin_rates)/sum(box_qty*box_price) as ".$months."vls from rd_invoices ".$flt." and  dates>='".$years."-".$months."-01' and dates<='".$years."-".$months."-".cal_days_in_month(CAL_GREGORIAN,$months,$years)."' group by item_ranges) as ".$months.$years."st on ".$months."invs=invts ";
$months--;
if($months==0){
$months=12;	
$years--;}
}

#echo $sqlst;


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

printf("<BR>");

print("<div style='display:block;font-size:14;' id='tdta'>");
printf("<br>");
printf("<table id='dtd' style='font-size:13;width:80%%;border:1px solid silver;'><th  style='background:grey;color:silver;' colspan='7' align='center'><b>Margin %% trend</th>");

printf("<tr class='cls' style='background:silver;'><td align='center'>Item range</td>");
$months=date('m');
$years=date('Y');


for($cnt=0;$cnt<6;$cnt++){
$dates=date_create($years."-".$months."-01");
printf("<td align='center'>%s</td>",date_format($dates,'M-Y'));
$arr[$cnt]=0;
$months--;
if($months==0){
$years--;}
}


$qry=mysqli_query($conn,$sqlst);
while($rst=mysqli_fetch_row($qry)){
printf("<tr class='cls'><td>%s</td>",$rst[0]);
for($cnt=0;$cnt<6;$cnt++){
printf("<td align='right' style='padding-right:5%%;'>%s</td>",number_format($rst[$cnt+1]*100,2,'.',','));
$arr[$cnt]=$arr[$cnt]+$rst[$cnt+1];}
printf("</tr>");
}




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

$sqlst="select * from (select 0 as invts) as invs left join  ";

for($cnt=0;$cnt<6;$cnt++){
$sqlst=$sqlst." (select 0 as ".$months."invs, sum(box_qty*box_price*margin_rates)*100/sum(box_qty*box_price) as ".$months."vls from rd_invoices ".$flt." and dates>='".$years."-".$months."-01' and dates<='".$years."-".$months."-".cal_days_in_month(CAL_GREGORIAN,$months,$years)."' group by MONTH(dates)) as ".$months.$years."st on ".$months."invs=invts ";
$months--;
if($months==0){
$months=12;	
$years--;}
if($cnt<5){
$sqlst=$sqlst." left join ";}
}

#echo $sqlst;

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

$qry=mysqli_query($conn,$sqlst);
$rst=mysqli_fetch_row($qry);

for($cnt=0;$cnt<6;$cnt++){
printf("<td align='right' style='padding-right:5%%;'>%s</td>",number_format($rst[($cnt*2)+2],2,'.',','));
}



printf("</table>");


printf("<BR>");


print("<div style='display:block;font-size:14;' id='tdta'>");
printf("<br>");
printf("<table id='dtd' style='font-size:13;width:100%%;border:1px solid silver;'><th  style='background:grey;color:silver;' colspan='2' align='center'><b>Item movement ranking</th>");

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

if(date('m')==1){
$maxdates=(date('Y')-1)."-12-31";}
else{
$maxdates=date('Y-m-').cal_days_in_month(CAL_GREGORIAN,((date('m')-1)),date('Y'));}

 

#$sqlst="set @mx:=0;set @mn:=0; select * from (select @mx:=@mx+1 as mxcnt,mxitems,mxqtys,mxvls from (select item_names as mxitems,sum(box_qty) as mxqtys,sum(box_qty*box_price)mxvls from rd_invoices ".$flt." and dates>='".$mindates."' and dates<='".$maxdates."' group by item_names  order by mxvls DESC limit 10) as mxs) as mxm left join (select @mn:=@mn+1 as mncnt,mnitems,mnqtys,mnvls from (select item_names as #mnitems,sum(box_qty) as mnqtys,sum(box_qty*box_price) as mnvls from rd_invoices ".$flt." and dates>='".$mindates."' and dates<='".$maxdates."' group by item_names order by mnvls ASC limit 10) as mns) as mnm on #mxcnt=mncnt limit 10";

$sqlst="select item_names as mxitems,sum(box_qty) as mxqtys,sum(box_qty*box_price),sum(box_qty*packets_per_box*packet_weights/1000)  from rd_invoices ".$flt." and  dates>='".$mindates."' and dates<='".$maxdates."' group by item_names  order by mxqtys DESC limit 10";
$qry=mysqli_query($conn,$sqlst);

printf("<tr><td><table style='font-size:13;width:100%%;'><tr class='cls'><td>Highest moved items</td></tr><tr class='cls' style='background:silver;'><td>Item</td><td align='center'>Qty</td><td align='center'>Value</td><td align='center'>Kg</td></tr>");
while($rst=mysqli_fetch_row($qry)){
printf("<tr class='cls'><td>%s</td><td align='right'>%s</td><td align='right'>%s</td><td align='right'>%s</td></tr>",$rst[0],number_format($rst[1],0,'.',','),number_format($rst[2],0,'.',','),number_format($rst[3],0,'.',','));}
printf("</table></td>");

$sqlst="select item_names as mxitems,sum(box_qty) as mxqtys,sum(box_qty*box_price),sum(box_qty*packets_per_box*packet_weights/1000) from rd_invoices ".$flt." and dates>='".$mindates."' and dates<='".$maxdates."' group by item_names  order by mxqtys ASC limit 10";

echo"<div style='color:transparent;'>".$sqlst."</div>";
#echo "Top highiest and lowest moved items";

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

printf("<td><table style='font-size:13;width:100%%;'><tr class='cls' stu;e><td>Lowest moved items</td></tr><tr class='cls' style='background:silver;'><td>Item</td><td align='center'>Qty</td><td align='center'>Value</td><td align='center'>Kg</td></tr>");

while($rst=mysqli_fetch_row($qry)){
printf("<tr class='cls'><td>%s</td><td align='right'>%s</td><td align='right'>%s</td><td align='right'>%s</td></tr>",$rst[0],number_format($rst[1],0,'.',','),number_format($rst[2],0,'.',','),number_format($rst[3],0,'.',','));}

printf("</table></td>");
printf("</td></tr></table>");


printf("<BR>");
$months=date('m');
$years=date('Y');

$arr=array();

$sqlst="select invts";

for($cnt=0;$cnt<6;$cnt++){
$arr[$cnt]=0;
$sqlst=$sqlst.",".$months."vls";
$months--;
if($months==0){
$years--;}
}



$sqlst=$sqlst." from (select distinct sub_ranges as invts from rd_invoices order by item_ranges) as invs";

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

for($cnt=0;$cnt<6;$cnt++){
$sqlst=$sqlst." left join (select sub_ranges as ".$months."invs, sum(box_qty*box_price) as ".$months."vls from rd_invoices ".$flt." and dates>='".$years."-".$months."-01' and dates<='".
$years."-".$months."-".cal_days_in_month(CAL_GREGORIAN,$months,$years)."' group by sub_ranges) as ".$months.$years."st on ".$months."invs=invts ";
$months--;
if($months==0){
$months=12;	
$years--;}
}

#echo $sqlst;


printf("<BR>");

print("<div style='display:block;font-size:14;' id='tdta'>");
printf("<br>");
printf("<table id='dtd' style='font-size:13;width:100%%;border:1px solid silver;'><th  style='background:grey;color:silver;' colspan='13' align='center'><b>Sub range sales trend - value </th>");

printf("<tr class='cls' style='background:silver;'><td align='center'>Item range</td>");
$months=date('m');
$years=date('Y');

for($cnt=0;$cnt<6;$cnt++){
$dates=date_create($years."-".$months);
printf("<td colspan='2' align='center'>%s</td>",date_format($dates,'M-Y'));
$months--;
if($months==0){
$months=12;	
$years--;}
}

$arr=array();
$qry=mysqli_query($conn,$sqlst);
while($rst=mysqli_fetch_row($qry)){
for($cnt=0;$cnt<6;$cnt++){
$arr[$cnt]=$arr[$cnt]+$rst[$cnt+1];}
}

$qry=mysqli_query($conn,$sqlst);
while($rst=mysqli_fetch_row($qry)){
printf("<tr class='cls'><td>%s</td>",$rst[0]);
for($cnt=0;$cnt<6;$cnt++){
if($arr[$cnt]==0){
$pcn=0;}
else{
$pcn=$rst[$cnt+1]*100/$arr[$cnt];}

printf("<td align='right'>%s</td><td align='right'>%s%%</td>",number_format($rst[$cnt+1],0,'.',','), number_format($pcn,0,'.',','));}
printf("</tr>");
}

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

printf("</table>");

printf("<BR>");

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


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

$arr=array();

$sqlst="select invts";

for($cnt=0;$cnt<6;$cnt++){
$arr[$cnt]=0;
$sqlst=$sqlst.",".$months."vls";
$months--;
if($months==0){
$years--;}
}



$sqlst=$sqlst." from (select distinct sub_ranges as invts from rd_invoices order by item_ranges) as invs";

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

for($cnt=0;$cnt<6;$cnt++){
$sqlst=$sqlst." left join (select sub_ranges as ".$months."invs, sum(box_qty*packet_weights*packets_per_box/1000) as ".$months."vls from rd_invoices ".$flt." and dates>='".$years."-".$months."-01' and dates<='".$years."-".$months."-".cal_days_in_month(CAL_GREGORIAN,$months,$years)."' group by sub_ranges) as ".$months.$years."st on ".$months."invs=invts ";
$months--;
if($months==0){
$months=12;	
$years--;}
}

#echo $sqlst;


printf("<BR>");

print("<div style='display:block;font-size:14;' id='tdta'>");
printf("<br>");
printf("<table id='dtd' style='font-size:13;width:100%%;border:1px solid silver;'><th  style='background:grey;color:silver;' colspan='13' align='center'><b>Sub range trend - tonnonage (Kg)</th>");

printf("<tr class='cls' style='background:silver;'><td align='center'>Invoice type</td>");
$months=date('m');
$years=date('Y');

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

$dates=date_create($years."-".$months);
printf("<td colspan='2' align='center'>%s</td>",date_format($dates,'M-Y'));
$months--;
if($months==0){
$months=12;	
$years--;}
}

printf("</tr>");

$arr=array();
$qry=mysqli_query($conn,$sqlst);
while($rst=mysqli_fetch_row($qry)){
for($cnt=0;$cnt<6;$cnt++){
$arr[$cnt]=$arr[$cnt]+$rst[$cnt+1];}
}

$qry=mysqli_query($conn,$sqlst);
while($rst=mysqli_fetch_row($qry)){
printf("<tr class='cls'><td>%s</td>",$rst[0]);
for($cnt=0;$cnt<6;$cnt++){
if($arr[$cnt]==0){
$pcn=0;}
else{
$pcn=$rst[$cnt+1]*100/$arr[$cnt];}

printf("<td align='right'>%s</td><td align='right'>%s%%</td>",number_format($rst[$cnt+1],0,'.',','), number_format($pcn,0,'.',','));}
printf("</tr>");
}

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

printf("</table>");

printf("<BR>");

printf("</div>");

}
}

?>

