<?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 showsrw(pr)
{
var dt=document.getElementById("dtd");
var cn=dt.rows.length;

for(cnt=2;cnt<cn;cnt++){
dt.rows[cnt].style.borderBottom="none";
dt.rows[cnt].cells[0].style.color="black";
dt.rows[cnt].cells[1].style.color="black";
dt.rows[cnt].cells[2].style.color="black";
dt.rows[cnt].style.display="none";}

var nd=document.getElementsByName(pr);
for(cnt=0;cnt<nd.length;cnt++){
nd[cnt].style.borderBottom="1px solid grey";
nd[cnt].style.display="table-row";
if(nd[cnt].className=='cls'){
nd[cnt].style.fontSize="small";}
}

if(pr=='boths'){
for(cnt=3;cnt<cn;cnt++){
if(dt.rows[cnt].className=='cls'){
dt.rows[cnt].style.borderBottom="1px solid grey";
dt.rows[cnt].cells[0].style.color="transparent";
dt.rows[cnt].cells[1].style.color="transparent";
dt.rows[cnt].cells[2].style.color="transparent";
if(dt.rows[cnt].className=='cls'){
dt.rows[cnt].style.fontSize="smaller";}
}
dt.rows[cnt].style.display="table-row";}}

if(pr=='dtvs'){
dt.rows[2].style.display="table-row";}

}



function showsbn(pr)
{
var dt=document.getElementById("dtd");
var cn=dt.rows.length;

if(dt.rows[2].style.display!="none"){
for(cnt=3;cnt<dt.rows[2].cells.length;cnt++){
if(dt.rows[2].cells[cnt].innerHTML==pr){
dt.rows[2].cells[cnt].style.display="table-cell";}
else{
dt.rows[2].cells[cnt].style.display="none";}
}}

var nd=document.getElementsByClassName("tcnm");
for(cnt=0;cnt<nd.length;cnt++){
nd[cnt].style.display="none";}

var nd=document.getElementsByName(pr);
for(cnt=0;cnt<nd.length;cnt++){
nd[cnt].style.display="inline-block";}


if(pr=='alls'){
var nd=document.getElementsByClassName("tcnm");
for(cnt=0;cnt<nd.length;cnt++){
nd[cnt].style.display="inline-block";}}

}




function fdtfinds(pr)
{

var vl=document.getElementById("dtfinds").value;
var dt=document.getElementById("dtd");
var cn=dt.rows.length;
var vls=vl.toLowerCase();

for(var cnt=3;cnt<cn;cnt++){
	
var tmp=dt.rows[cnt].cells[0].innerHTML;
var tmpa=tmp.toLowerCase();
var tmp=dt.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){
dt.rows[cnt].style.display="table-row";}
else{
dt.rows[cnt].style.display="none";}

if(vls.length==0){
dt.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">Monthly sales value</div>
<div class="homes"><a href="prdlogings.php" style='color:#A9BCF5;'>Sign out</a></div>
</body>

</html>




<?php

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

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

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

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

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

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

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

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

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

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

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

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

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


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

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


mrshowshps();

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>Monthly sales</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['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'>");


$conn=mysqli_connect("localhost","primarysales","S5L7lF01EQZvQrGK","primarysales_diana");
$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]."' ";}
}

#echo $seflt;


printf("<form method='post' action='mrtmmonths.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 style='color:#045FB4' class='frk'><input type='radio' name='chsh' checked onclick=\"showsrw('dtbr')\">Lines</div>");
printf("<div style='color:#045FB4'  class='frm'><input type='radio' name='chsh'  onclick=\"showsrw('dtvs')\">Values</div>");
printf("<div  style='color:#045FB4' class='frn'><input type='radio' name='chsh'  onclick=\"showsrw('boths')\">Both</div>");


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


$sqlst="select max(vl) from (select MONTHNAME(dates) as dts,sum(pcs_qty*pcs_price)as vl from market_returns ".$flt."group by MONTHNAME(dates))as mdv ";
$qry=mysqli_query($conn,$sqlst);
$rst=mysqli_fetch_row($qry);
$maxvl=$rst[0];


$arr=array();

$fsqlst="select mdates as fdt ";
$tmps=",(0+";

$sqlst="select distinct item_ranges from market_returns ".$flt;
$qry=mysqli_query($conn,$sqlst);
$cnt=0;
while($rst=mysqli_fetch_row($qry)){
$arr[$cnt]=$rst[0];
$fsqlst=$fsqlst.",".$rst[0].".vls";
#$tmps=$tmps."+".$rst[0].".vls";
$tmps=$tmps."+COALESCE(".$rst[0].".vls,0)";
$cnt++;}

$fsqlst=$fsqlst.$tmps.") ";

$fsqlst=$fsqlst." from (select distinct MONTHNAME(dates) as mdates from market_returns ".$flt." order by dates) as sdts ";
$rsqlst="";

for($cnt=0;$cnt<count($arr);$cnt++){
$rsqlst=$rsqlst." left join 
(select MONTHNAME(dates) as dtst, item_ranges,sum(pcs_qty*pcs_price) as vls from market_returns ".$flt."  and item_ranges='".$arr[$cnt]."' group by MONTHNAME(dates)) as ".$arr[$cnt]." on ".$arr[$cnt].".dtst=mdates ";
}

$sqlst=$fsqlst.$rsqlst." ";
#echo $sqlst;

#printf("<div class='tdt'>");
printf("<br>");


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

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


$clarr=array("#B2C248","#5C9C52","#9F8FA9","#357EC7","#F78181");
printf("<BR>");
$dates=0;

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

printf("<table style='font-size:11;width:100%%;'><td width=50%%></td>");
#printf("<tr><td align='left' width='15%%;'><input type='radio' name='showbrs' onclick=\"showsrw('dtbr')\" checked>Show lines</td>");
#printf("<td align='left' width='15%%;'><input type='radio' name='showbrs' onclick=\"showsrw('dtvs')\">Show values</td>");
#printf("<td align='left' width='15%%;'><input type='radio' name='showbrs' onclick=\"showsrw('boths')\">Show all</td>");

for($cnt=0;$cnt<count($arr);$cnt++){
printf("<td width='8%%' style='color:%s;' align='center' onclick=\"showsbn('%s')\"><b>%s</b></td>",$clarr[$cnt],$arr[$cnt],$arr[$cnt]);}
printf("<td width='8%%' style='grey;' align='center' onclick=\"showsbn('alls')\"><b>Full range</b></td><td></tr></tr></table><BR>");


printf("<table id='dtd' style='font-size:13;width:100%%;'><th colspan='%s' bgcolor='grey' style='color:#D8D8D8;'>Monthly sales value (in Millions)</th>",count($arr)+3);
printf("<tr bgcolor='silver'><td style='width:15%%;border:1px solid white;' align='center'>Month</td><td style='width:8%%;border:1px solid white;' align='center'>Month sale</td>
<td style='width:10%%;border:1px solid white;' align='center'>Total sale</td><td colspan='%s' style='border:1px solid white;' width='40%%' align='right' bgcolor='silver'>%sMn.</td></tr>",count($arr),number_format($maxvl/1000000,0,'.',','));

printf("<tr style='display:none;'><td></td><td></td><td></td>");
for($cnt=0;$cnt<count($arr);$cnt++){
printf("<td style='color:%s;' align='center'>%s</td>",$clarr[$cnt],$arr[$cnt]);}
printf("</tr>");


$tvl=0;
$ttl=0;

while($rst=mysqli_fetch_row($qry)){
$ttl=$ttl+$rst[count($arr)+1];}

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

#$ykarr=str_split($rst[0],4);
#$yrwk=$ykarr[0]." - ".$ykarr[1];

$tmps="";
$tvl=$tvl+$rst[count($arr)+1];

$pcn=$rst[count($arr)+1]*100/$ttl;

printf("<tr style='border-bottom:1px solid grey;' name='dtbr'><td align='center'>%s</td><td align='right'>%s (%s%%)</td><td align='right'>%s (%s%%)</td><td colspan='%s'><div>", $rst[0], number_format(round($rst[count($arr)+1]/1000000,PHP_ROUND_HALF_DOWN),2,'.',',') ,number_format($pcn,0,'.',','),number_format($tvl/1000000,2,'.',','),number_format($tvl*100/$ttl,0,'.',','), count($arr));

for($cnt=0;$cnt<count($arr);$cnt++){
$pcn=round($rst[$cnt+1]*100/$maxvl,PHP_ROUND_HALF_DOWN);
$tmps=$tmps.$arr[$cnt].":".number_format($rst[$cnt+1],0,'.',',')." ";
printf("<div class='tcnm' name='%s' style='color:grey;width:%s%%;display:inline-block;background:%s;border-radius:1px;'>-</div>",$arr[$cnt],$pcn,$clarr[$cnt]);
}

$pcn=$rst[count($arr)+1]*100/$ttl;
printf("</div></td></tr><tr name='dtvs' class='cls' style='font-size:10;color:grey;display:none;'><td align='center'>%s</td><td align='right'>%s (%s%%)</td><td align='right'>%s (%s%%)</td>",$rst[0], number_format(round($rst[count($arr)+1],PHP_ROUND_HALF_DOWN),0,'.',','),number_format($pcn,0,'.',','), number_format($tvl,0,'.',','), number_format($tvl*100/$ttl,0,'.',','));

for($cnt=0;$cnt<count($arr);$cnt++){
printf("<td class='tcnm' align='right' name='%s' style='color:%s;'>%s (%s%%)</td>",$arr[$cnt],$clarr[$cnt],number_format($rst[$cnt+1],0,'.',','),number_format($rst[$cnt+1]*100/$rst[count($arr)+1],0,'.',','));}
printf("</tr>");
}

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



?>
