<?php

require "showshp.php";

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


?>


<html>

<head>

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


<script>

function mnh(mn,bn)
{

}


function mch(mn)
{
if(mn.style.background!='silver'){
mn.style.background='silver';}
}


function mnc(mn)
{
if(mn.style.background!='silver'){
mn.style.background='white';}

}



function gsh(sgn)
{

var cle=document.getElementsByClassName("tdtc");
for(cn=0;cn<cle.length;cn++){
cle[cn].style.display="none";}

document.getElementById(sgn).style.display="block";

}


function fdtfinds(pr)
{

var vl=document.getElementById("dtfinds").value;
var dta=document.getElementById("dtda");
var dtb=document.getElementById("dtdb");

var cn=dta.rows.length;
var vls=vl.toLowerCase();

for(var cnt=3;cnt<cn;cnt++){
	
var tmp=dta.rows[cnt].cells[0].innerHTML;
var tmpa=tmp.toLowerCase();
var tmp=dta.rows[cnt].cells[1].innerHTML;
var tmpb=tmp.toLowerCase();

var sta=tmpa.indexOf(vls);
var stb=tmpb.indexOf(vls);

if((sta>=0 || stb>=0 ) && vls.length>0){
dta.rows[cnt].style.display="table-row";
dtb.rows[cnt].style.display="table-row";}
else{
dta.rows[cnt].style.display="none";
dtb.rows[cnt].style.display="none";}

if(vls.length==0){
dta.rows[cnt].style.display="table-row";
dtb.rows[cnt].style.display="table-row";}
}

	
}


</script>

</head>

<body style="font-family:calibri;font-size:small" onload="mnh('amnc','agentsb')">
<div class="hdn"></div>

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

<div class="tops" align="center">Region sales summary</div>
<div class="homes"><a href="prdlogings.php" style='color:#A9BCF5;'>Sign out</a></div>

</body>

</html>






<?php

$starts=$_REQUEST['starts'];
$ends=$_REQUEST['ends'];

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

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

if(isset($_REQUEST['territorys'])){
$territorys=$_REQUEST['territorys'];}
else{
$territorys="All";}

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

if(isset($_REQUEST['rsms'])){
$rsms=$_REQUEST['rsms'];}
else{
$rsms="All";}

if(isset($_REQUEST['executives'])){
$executives=$_REQUEST['executives'];}
else{
$executives="All";}

if(isset($_REQUEST['reps'])){
$reps=$_REQUEST['reps'];}
else{
$reps="All";}

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

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

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

$rpsty="regions=".$regions."&agents=".$agents."&territorys=".$territorys."&ranges=".$ranges."&zones=".$zones."&rsms=".$rsms."&executives=".$executives."&reps=".$reps
."&items=".$items."&subranges=".$subranges; 


if($starts==0 || $starts==''){
$starts=$years."-".$months."-01";}
if($ends==0 || $ends==''){
$ends=$years."-".$months."-".$days;}

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

$seflt=" where agent_names is not null ";

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


$sqlst="select max(user_dates) from rc_invoices ";
$qry=mysqli_query($conn,$sqlst);
$rst=mysqli_fetch_row($qry);

#printf("<div class='updst'>Last updated: %s</div>",$rst[0]);

showshps();


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

if(isset($_REQUEST['zones'])){
$zones=$_REQUEST['zones'];
printf("<tr><td>Zone:</td><td>%s</td></tr>",$zones);}
else{
$zones="All";}

if(isset($_REQUEST['regions'])){
$regions=$_REQUEST['regions'];
printf("<tr><td>Region:</td><td>%s</td></tr>",$regions);}
else{
$regions="All";}

if(isset($_REQUEST['territorys'])){
$territorys=$_REQUEST['territorys'];
printf("<tr><td>Territory:</td><td>%s</td></tr>",$territorys);}
else{
$territorys="All";}

if(isset($_REQUEST['agents'])){
$agents=$_REQUEST['agents'];
printf("<tr><td>Agent:</td><td>%s</td></tr>",$agents);}
else{
$agents="All";}

if(isset($_REQUEST['agents'])){
$agents=$_REQUEST['agents'];
printf("<tr><td>Agent:</td><td>%s</td></tr>",$agents);}
else{
$agents="All";}

if(isset($_REQUEST['agentttypes'])){
$agenttypes=$_REQUEST['agenttypes'];
printf("<tr><td>Agent type:</td><td>%s</td></tr>",$agenttype);}
else{
$agenttypes="All";}

if(isset($_REQUEST['rsms'])){
$rsms=$_REQUEST['rsms'];
printf("<tr><td>RSM:</td><td>%s</td></tr>",$rsms);}
else{
$rsms="All";}

if(isset($_REQUEST['executives'])){
$executives=$_REQUEST['executives'];
printf("<tr><td>Executive:</td><td>%s</td></tr>",$executives);}
else{
$executives="All";}

if(isset($_REQUEST['reps'])){
$reps=$_REQUEST['reps'];
printf("<tr><td>Rep:</td><td>%s</td></tr>",$reps);}
else{
$reps="All";}

if(isset($_REQUEST['ranges'])){
$ranges=$_REQUEST['ranges'];
printf("<tr><td>Range:</td><td>%s</td></tr>",$ranges);}
else{
$ranges="All";}

if(isset($_REQUEST['subranges'])){
$subranges=$_REQUEST['subranges'];
printf("<tr><td>Sub-range:</td><td>%s</td></tr>",$subranges);}
else{
$subranges="All";}

if(isset($_REQUEST['items'])){
$items=$_REQUEST['items'];
printf("<tr><td>Item:</td><td>%s</td></tr>",$items);}
else{
$items="All";}

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



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

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


printf("<form method='post' action='prregionsummarys.php?%s'>",$rpsty);
printf("<div class='fra'>From</div><div class='frb'>
<input type='text' size=10 class='starts'  name='starts' value='%s' id='inputField'></div><div class='frc'>To</div><div class='frd'><input type='text class='ends' size=10 name='ends' value='%s' id='inputField2'></div>
<div class='fre'><input type='submit' value='go' style='width:70'></div><div class='frf'>Search</div><div class='frg'>
<input type='text' size=20 id='dtfinds' autocomplete='off' onkeydown=\"fdtfinds(event)\"></div></form>",$starts,$ends);


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


$grt=0;
$grq=0;
$arr=array();
$arv=array();
$tvt=0;
$tsv=0;



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


$tsqlst="select distinct item_ranges from itemsmrd";
$tqry=mysqli_query($conn,$tsqlst);
$rns=mysqli_num_rows($qry);

$tqry=mysqli_query($conn,$tsqlst);
$cnt=0;
while($trst=mysqli_fetch_row($tqry)){
$arr[$cnt]=$trst[0];
$cnt++;}


$asqlst="";
for($cnt=0;$cnt<count($arr);$cnt++){
$asqlst=$asqlst." left join (select sub_regions,agent_types,sum((pcs_qty*pcs_price)+(box_qty*box_price)) as vls from rc_invoices ".$flt." and item_ranges='".$arr[$cnt]."' group by sub_regions,agent_types) as ".$arr[$cnt]." on ".$arr[$cnt].".sub_regions=mregions and ".$arr[$cnt].".agent_types=mtypes ";}

$rsqlst="select mregions as region,mtypes as agenttypes";

for($cnt=0;$cnt<count($arr);$cnt++){
$rsqlst=$rsqlst.",".$arr[$cnt].".vls as ".$arr[$cnt];
$arv[$cnt]=0;}

$rsqlst=$rsqlst." from (select distinct sub_region mregions,agent_type as mtypes from agentsmrd ";
if(strtolower($zones)!='all'){
$rsqlst=$rsqlst." where zone='".$zones."' ";}
$rsqlst=$rsqlst.") as rgs ";

$sqlst=$rsqlst.$asqlst." order by mregions,mtypes";

#echo $sqlst;

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

$cn=mysqli_num_fields($qry);

while($rst=mysqli_fetch_row($qry)){
for($rnt=2;$rnt<$cn;$rnt++){
$arv[$rnt-2]=$arv[$rnt-2]+$rst[$rnt];
$tvt=$tvt+$rst[$rnt];}
}

printf("<div class='tdt' style='font-size:14'>");


printf("<div class='tdtc' id='tdtc' style='display:blockl;display:none;'><div class='cbr'>");

$carr=array("#58ACFA","#FA5882","#04B486","#BCA9F5","#FAAC58","#0174DF","red","blue","yellow");

printf("<div style='position:absolute;top:%s;left:720;height:20;width:58;background:white;border:solid thin grey;color:black;font-size:10;border-radius:2px;text-align:center;'><a href='javascript:gsh(\"tdtc\")'>All</div>",30);
for($cnt=0;$cnt<$cn-2;$cnt++){
printf("<div style='position:absolute;top:%s;left:720;height:20;width:60;background:%s;color:black;font-size:10;border-radius:2px;text-align:center;'><a href='javascript:gsh(\"tdtc%s%s\")'>%s</div>",($cnt*30)+60,$carr[$cnt],$arr[$cnt],$cnt,$arr[$cnt]);
}

while($trst=mysqli_fetch_row($tqry)){
$arr[$cnt]=$trst[0];
$cnt++;}

#$qry=mysqli_query($conn,$sqlst);
mysqli_data_seek($qry,0);
$cn=mysqli_num_fields($qry);

$clr=0;
$psb=0;
$max=0;
$tmax=0;
$tsv=0;
$tvt=0;

while($rst=mysqli_fetch_row($qry)){
$tsv=0;
for($cnt=0;$cnt<$cn;$cnt++){
if($cnt>0 && $max<=$rst[$cnt]){
$max=$rst[$cnt];}
$tvt=$tvt+$rst[$cnt];
$tsv=$tsv+$rst[$cnt];
if($tmax<$tsv){
$tmax=$tsv;}
}
}


for($cnt=0;$cnt<11;$cnt++){
printf("<ha style='position:absolute;font-size:10;left:-50;top:%s'>%s</ha>",295-(295/10*$cnt),number_format($tmax/10*$cnt,0,'.',','));}

#$qry=mysqli_query($conn,$sqlst);
mysqli_data_seek($qry,0);
while($rst=mysqli_fetch_row($qry)){
$tsv=0;
$ht=0;
$pst=300;
$psb=$psb+35;

for($rnt=0;$rnt<$cn;$rnt++){
$tsv=$tsv+$rst[$rnt];}

if($tsv>0){
printf("<div style='position:absolute;font-size:10;color:black;transform:rotate(270deg);transform-origin:0%%0%%;top:295;left:%s;height:300;z-index:1'><b>%s - %s</b></div>",$psb+2,$rst[0],$rst[1]);

for($rnt=2;$rnt<$cn;$rnt++){
$ht=$rst[$rnt]*300/$tmax;
$pst=$pst-$ht;
printf("<div class='chd' style='top:%s;left:%s;height:%s;background:%s;overflow:hidden;z-index:-1;'></div>",$pst,$psb,$ht,$carr[$rnt-2]);
}
}

}

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

#printf("</br>");


#//////////////////////////////////////////////////////////////////////////////////////////////////


for($dcn=0;$dcn<$cn-2;$dcn++){

printf("<div class='tdtc' id='tdtc%s%s' style='display:none'><div class='cbr'>",$arr[$dcn],$dcn);

$carr=array("#58ACFA","#FA5882","#04B486","#BCA9F5","#FAAC58","#0174DF","red","blue","yellow");

printf("<div style='position:absolute;top:%s;left:720;height:20;width:58;background:white;border:solid thin grey;color:black;font-size:10;border-radius:2px;text-align:center;'><a href='javascript:gsh(\"tdtc\")'>All</div>",30);
for($cnt=0;$cnt<$cn-2;$cnt++){
printf("<div style='position:absolute;top:%s;left:720;height:20;width:60;background:%s;color:black;font-size:10;border-radius:2px;text-align:center;'><a href='javascript:gsh(\"tdtc%s%s\")'>%s</div>",($cnt*30)+60,$carr[$cnt],$arr[$cnt],$cnt,$arr[$cnt]);
}

$rgsqlst="select sub_regions,agent_types,sum((pcs_qty*pcs_price)+(box_qty*box_price)) from rc_invoices ".$flt." and item_ranges='".$arr[$dcn]."' group by sub_regions,agent_types order by sub_regions,agent_types";
$rgqry=mysqli_query($conn,$rgsqlst);


$clr=0;
$psb=0;
$tmax=0;
$tsv=0;
	
while($rst=mysqli_fetch_row($rgqry)){
$tsv=$tsv+$rst[2];
if($tmax<$rst[2]){
$tmax=$rst[2];}
}

for($cnt=0;$cnt<11;$cnt++){
printf("<div style='position:absolute;font-size:10;left:-50;top:%s'>%s</div>",295-(295/10*$cnt),number_format($tmax/10*$cnt,0,'.',','));}

$rgqry=mysqli_query($conn,$rgsqlst);
#mysqli_data_seek($rgqry,0);
while($rst=mysqli_fetch_row($rgqry)){
$ht=0;
$pst=300;
$psb=$psb+35;


if($tsv>0){
printf("<div style='position:absolute;font-size:10;color:black;transform:rotate(270deg);transform-origin:0%%0%%;top:295;left:%s;height:30;'><b>%s - %s</b></div>",$psb+2,$rst[0],$rst[1]);

$ht=$rst[2]*300/$tmax;
$pst=$pst-$ht;
printf("<div class='chd' style='top:%s;left:%s;height:%s;background:%s;overflow:hidden;z-index:-1;'></div>",$pst,$psb,$ht,$carr[$dcn]);
}

}

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



}



#//////////////////////////////////////////////////////////////////////////////////////////////////

printf("</br>");

printf("<div class='tdta' id='tdta' style='display:block'><br><table id='dtda' style='font-size:13'><th bgcolor='silver' colspan='%s'>Sales value Rs.</th><tr style='color:#E6E6E6' bgcolor='grey'><td width=150 align='center' style='boprer:1px solid white' >Region</td><td width=150 align='center' style='boprer:1px solid white' >Type</td>",$rns+10);
$tqry=mysqli_query($conn,$tsqlst);
while($trst=mysqli_fetch_row($tqry)){
printf("<td align='center' width=100 style='boprer:1px solid white' >%s</td>",$trst[0]);
}

printf("<td width=100 align='center' style='boprer:1px solid white' >Total</td><td align='center' width=100 style='boprer:1px solid white' >Agent list</td><td align='center' width=100 style='boprer:1px solid white' >Territory list</td></tr>");

#$qry=mysqli_query($conn,$sqlst);
mysqli_data_seek($qry,0);
while($rst=mysqli_fetch_row($qry)){
$tsv=0;
for($rnt=2;$rnt<$cn;$rnt++){
$tsv=$tsv+$rst[$rnt];}

if($tsv!=0){
printf("<tr class='cls' bgcolor='white' onmouseover=\"this.style.backgroundColor='silver';\" onmouseout=\"this.style.backgroundColor='white';\"><td>%s</a></td><td>%s</td>",$rst[0],$rst[1]);
for($rnt=2;$rnt<$cn;$rnt++){
printf("<td align='right'>%s</td>",number_format($rst[$rnt],0,'.',','));}
printf("<td align='right'><b>%s</td><td style='font-size:13'  align='center'><a href='pragentsummarys.php?rpsty=agentsummarys&starts=%s&ends=%s&regions=%s&agents=All&agenttypes=%s' target='_blank'>agency list</td><td style='font-size:13' align='center'><a href='pritemsummarys.php?rpsty=agentsummarys&starts=%s&ends=%s&regions=%s&territorys=All&agenttypes=%s' target='_blank'>Item list</td></tr>",number_format($tsv,0,'.',','),$starts,$ends,$rst[0],$rst[1],$starts,$ends,$rst[0],$rst[1]);
}
}

printf("<tr><td><b>Total</td><td></td>");
for($rnt=0;$rnt<$cn-2;$rnt++){
printf("<td align='right'><b>%s</td>",number_format($arv[$rnt],0,'.',','));}

printf("<td align='right'><b>%s</b></td>",number_format($tvt,0,'.',','));
printf("<td align='center'><a href='pragentsinvoices.php?starts=%s&ends=%s&regions=%s&agents=All&territorys=all'>Agency list </a></td>",$starts,$ends,$rst[0]);
printf("<td align='center'><a href='pragentsitems.php?starts=%s&ends=%s&regions=%s&agents=All&territorys=all'>Item list</a></td></tr></table></div>",$starts,$ends,$rst[0]);
printf("<br>");

printf("<div class='tdta' id='tdta' style='display:block'><br><table id='dtdb' style='font-size:13'><th bgcolor='silver' colspan='%s'>Sales %%</th><tr style='color:#E6E6E6' bgcolor='grey'><td width=150 align='center' style='boprer:1px solid white' >Region</td><td width=150 align='center' style='boprer:1px solid white' >Type</td>",$rns+10);
$tqry=mysqli_query($conn,$tsqlst);
while($trst=mysqli_fetch_row($tqry)){
printf("<td align='center' width=100 style='boprer:1px solid white' >%s</td>",$trst[0]);
}

printf("<td width=100 align='center' style='boprer:1px solid white' >Total</td></tr>");

#$qry=mysqli_query($conn,$sqlst);
mysqli_data_seek($qry,0);
$cn=mysqli_num_fields($qry);

#$qry=mysqli_query($conn,$sqlst);
mysqli_data_seek($qry,0);
while($rst=mysqli_fetch_row($qry)){
$tsv=0;
$dcm=0;

for($rnt=1;$rnt<$cn;$rnt++){
$tsv=$tsv+$rst[$rnt];}

if($tsv!=0){
printf("<tr class='cls' bgcolor='white' onmouseover=\"this.style.backgroundColor='silver';\" onmouseout=\"this.style.backgroundColor='white';\"><td>%s</a></td><td>%s</td>",$rst[0],$rst[1]);
for($rnt=2;$rnt<$cn;$rnt++){
if(($rst[$rnt]/$tsv*100)<1 && ($rst[$rnt]/$tsv*100)!=0){
$dcm=1;}
else{
$dcm=0;}
printf("<td align='center'>%s%%</td>",number_format($rst[$rnt]/$tsv*100,$dcm,'.',','));}
printf("<td align='center'><b>%s%%</td></tr>",number_format($tsv/$tvt*100,0,'.',','));
}
}

printf("<tr><td><b>Total</td><td></td>");
for($rnt=0;$rnt<$cn-2;$rnt++){
if(($arv[$rnt]/$tvt*100)<1 && ($arv[$rnt]/$tvt*100)!=0){
$dcm=1;}
else{
$dcm=0;}

printf("<td align='center'><b>%s%%</td>",number_format($arv[$rnt]/$tvt*100,$dcm,'.',','));}

printf("<td align='center'><b>%s%%</b></td></tr></table></div></div>",number_format($tvt/$tvt*100,0,'.',','));




?>