<?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 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=2;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">Market return - reason wise ranges summary</div>
<div class="homes"><a href="prdlogings.php" style='color:#A9BCF5;'>Sign out</a></div>

</body>

</html>




<?php

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

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

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


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

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

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


$rpsty="regions=".$regions."&agents=".urlencode($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();

reportdets("Reason wise ranges");


printf("</div><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]."' ";}
}


printf("<form method='post' action='mrreasonrangesummarys.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='tdt'><br><table id='dtd' style='font-size:13'><th border='white' bgcolor='silver' colspan='10'><b>Return Qty and value</th>");

printf("<tr bgcolor='grey' style='color:#E6E6E6;'><td width=200 align='center'  style='border:1px solid white' >Range</td>");
printf("<td width=80 align='center'  style='border:1px solid white' >Returns</td><td width=80 align='center'  style='border:1px solid white' >
Qty (Pcs)</td><td width=80 align='center'  style='border:1px solid white' >Weight</td><td width=100 align='center' style='border:1px solid white' >Value</td>");
printf("<td width=80 align='center'  style='border:1px solid white' >%%</td><td width=80 align='center' style='border:1px solid white'>Reasons</td>
<td width=80 align='center' style='border:1px solid white'>Regions</td><td width=80 align='center' style='border:1px solid white'>Agents</td><td width=80 align='center' style='border:1px solid white'>Items</td></tr>");



$flt=$seflt." and dates>='".$_REQUEST['starts']."' and dates<='".$_REQUEST['ends']."' ";
if(strtolower($zones)!='all'){
$flt=$flt." and zones='".$zones."' ";}
if(strtolower($regions)!='all'){
$flt=$flt." and regions='".$regions."' ";}
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($items)!='all'){
$flt=$flt." and item_names='".$items."' ";}
if(strtolower($agenttypes)!='all'){
$flt=$flt." and agent_types='".$agenttypes."' ";}
if(strtolower($mrtypes)!='all'){
$flt=$flt." and mr_types='".$mrtypes."' ";}
if(strtolower($mrreasons)!='all'){
$flt=$flt." and reasons='".$mrreasons."' ";}
if(strtolower($mractions)!='all'){
$flt=$flt." and actions='".$mractions."' ";}


$sqlst="select mr_types,count(mr_numbers),sum(pcs_qty),sum(pcs_qty*packet_weights/1000),sum(pcs_qty*pcs_price) as vl from market_returns ".$flt." group by mr_types order by item_ranges ";

$tsvt=0;

$ttn=0;
$ttq=0;
$ttw=0;
$ttv=0;
 
$rqry=mysqli_query($conn,$sqlst);
while($rrst=mysqli_fetch_row($rqry)){
$tsvt=$tsvt+$rrst[4];}

$rqry=mysqli_query($conn,$sqlst);
while($rrst=mysqli_fetch_row($rqry)){

$ttn=$ttn+$rrst[1];
$ttq=$ttq+$rrst[2];
$ttw=$ttw+$rrst[3];
$ttv=$ttv+$rrst[4];


$hrfzones="<a href='mrzonewisesummarys.php?rpsty=agentsitems&starts=".$starts."
&ends=".$ends."&zones=".$zones."&regions=".$regions."&agents=".$agents."&territorys=".$territorys."&ranges=".$ranges."&subranges=".$subranges."&items=".$items."&mrtypes=".$rrst[0]."&reasons=".$reasons."&actions=".$actions."' target='_blank'>Zones</a>";

$hrfregions="<a href='mrregionwisesummarys.php?rpsty=agentsitems&starts=".$starts."
&ends=".$ends."&zones=".$zones."&regions=".$regions."&agents=".$agents."&territorys=".$territorys."&ranges=".$ranges."&subranges=".$subranges."&items=".$items."&mrtypes=".$rrst[0]."&reasons=".$reasons."&actions=".$actions."' target='_blank'>Regions</a>";

$hrfagents="<a href='mragentwisesummarys.php?rpsty=agentsitems&starts=".$starts."
&ends=".$ends."&zones=".$zones."&regions=".$regions."&agents=".$agents."&territorys=".$territorys."&ranges=".$ranges."&subranges=".$subranges."&items=".$items."&mrtypes=".$rrst[0]."&reasons=".$reasons."&actions=".$actions."' target='_blank'>Agents</a>";

$hrfitems="<a href='mritemsummarys.php?rpsty=agentsitems&starts=".$starts."
&ends=".$ends."&zones=".$zones."&regions=".$regions."&agents=".$agents."&territorys=".$territorys."&ranges=".$ranges."&subranges=".$subranges."&items=".$items."&mrtypes=".$rrst[0]."&reasons=".$reasons."&actions=".$actions."' target='_blank'>Items</a>";


printf("<tr class='cls' style='font-weight:bold;background:#D8D8D8;'><td>%s</td><td align='right' style='padding-right:0.5%%;'>%s</td><td align='right' style='padding-right:0.5%%;'>%s</td>
<td align='right' style='padding-right:0.5%%;'>%s</td><td align='right' style='padding-right:0.5%%;'>%s</td><td align='right' style='padding-right:1%%;'>%s%%</td>
<td align='center'>%s</td><td align='center'>%s</td><td align='center'>%s</td><td align='center'>%s</td></tr>",
$rrst[0],number_format($rrst[1],0,'.',','),number_format($rrst[2],0,'.',','),number_format($rrst[3],2,'.',','),number_format($rrst[4],0,'.',','),
number_format($rrst[4]*100/$tsvt,2,'.',','),$hrfzones,$hrfregions,$hrfagents,$hrfitems);
	
$sqlst="select item_ranges,count(pcs_qty),sum(pcs_qty),sum(pcs_qty*packet_weights/1000),sum(pcs_qty*pcs_price) as vl from market_returns ".$flt." and mr_types='".$rrst[0].
"' group by item_ranges order by vl DESC";

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

while($rst=mysqli_fetch_row($qry)){
$svtot=$svtot+$rst[4];}

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


$hrfzones="<a href='mrzonewisesummarys.php?rpsty=agentsitems&starts=".$starts."
&ends=".$ends."&zones=".$zones."&regions=".$regions."&agents=".$agents."&territorys=".$territorys."&ranges=".$rst[0]."&subranges=".$subranges."&items=".$items."&mrtypes=".$rrst[0]."&reasons=".$reasons."&actions=".$actions."' target='_blank'>Zones</a>";

$hrfregions="<a href='mrregionwisesummarys.php?rpsty=agentsitems&starts=".$starts."
&ends=".$ends."&zones=".$zones."&regions=".$regions."&agents=".$agents."&territorys=".$territorys."&ranges=".$rst[0]."&subranges=".$subranges."&items=".$items."&mrtypes=".$rrst[0]."&reasons=".$reasons."&actions=".$actions."' target='_blank'>Regions</a>";

$hrfagents="<a href='mragentwisesummarys.php?rpsty=agentsitems&starts=".$starts."
&ends=".$ends."&zones=".$zones."&regions=".$regions."&agents=".$agents."&territorys=".$territorys."&ranges=".$rst[0]."&subranges=".$subranges."&items=".$items."&mrtypes=".$rrst[0]."&reasons=".$reasons."&actions=".$actions."' target='_blank'>Agents</a>";

$hrfitems="<a href='mritemsummarys.php?rpsty=agentsitems&starts=".$starts."
&ends=".$ends."&zones=".$zones."&regions=".$regions."&agents=".$agents."&territorys=".$territorys."&ranges=".$rst[0]."&subranges=".$subranges."&items=".$items."&mrtypes=".$rrst[0]."&reasons=".$reasons."&actions=".$actions."' target='_blank'>Items</a>";

printf("<tr class='cls'><td>%s</td><td align='right' style='padding-right:0.5%%;'>%s</td><td align='right' style='padding-right:0.5%%;'>%s</td><td align='right' style='padding-right:0.5%%;'>%s</td><td align='right' style='padding-right:1%%;'>%s</td><td align='right' style='padding-right:1%%;'>%s%%</td><td align='center'>%s</td><td align='center'>%s</td><td align='center'>%s</td><td align='center'>%s</td></tr>",$rst[0],number_format($rst[1],0,'.',','),number_format($rst[2],0,'.',','),
number_format($rst[3],2,'.',','),number_format($rst[4],0,'.',','),number_format($rst[4]*100/$svtot,2,'.',','),$hrfzones,$hrfregions,$hrfagents,$hrfitems);
}

printf("<tr><td style='color:transparent;'>-</td></tr>");
}

$hrfzones="<a href='mrzonewisesummarys.php?rpsty=agentsitems&starts=".$starts."
&ends=".$ends."&zones=".$zones."&regions=".$regions."&agents=".$agents."&territorys=".$territorys."&ranges=".$ranges."&subranges=".$subranges."&items=".$items."&mrtypes=".$mrtypes."&reasons=".$reasons."&actions=".$actions."' target='_blank'>Zones</a>";

$hrfregions="<a href='mrregionwisesummarys.php?rpsty=agentsitems&starts=".$starts."
&ends=".$ends."&zones=".$zones."&regions=".$regions."&agents=".$agents."&territorys=".$territorys."&ranges=".$ranges."&subranges=".$subranges."&items=".$items."&mrtypes=".$mrtypes."&reasons=".$reasons."&actions=".$actions."' target='_blank'>Regions</a>";

$hrfagents="<a href='mragentwisesummarys.php?rpsty=agentsitems&starts=".$starts."
&ends=".$ends."&zones=".$zones."&regions=".$regions."&agents=".$agents."&territorys=".$territorys."&ranges=".$ranges."&subranges=".$subranges."&items=".$items."&mrtypes=".$mrtypes."&reasons=".$reasons."&actions=".$actions."' target='_blank'>Agents</a>";

$hrfitems="<a href='mritemsummarys.php?rpsty=agentsitems&starts=".$starts."
&ends=".$ends."&zones=".$zones."&regions=".$regions."&agents=".$agents."&territorys=".$territorys."&ranges=".$ranges."&subranges=".$subranges."&items=".$items."&mrtypes=".$mrtypes."&reasons=".$reasons."&actions=".$actions."' target='_blank'>Reasons</a>";

printf("<tr class='cls' style='font-weight:bold;'><td>Total</td><td align='right' style='padding-right:0.5%%;'>%s</td><td align='right' style='padding-right:0.5%%;'>%s</td>
<td align='right' style='padding-right:0.5%%;'>%s</td><td align='right' style='padding-right:1%%;'>%s</td><td align='right' style='padding-right:1%%;'>%s%%</td><td align='center'>%s</td><td align='center'>%s</td><td align='center'>%s</td><td align='center'>%s</td></tr>",number_format($ttn,0,'.',','),
number_format($ttq,0,'.',','),number_format($ttw,0,'.',','),number_format($ttv,0,'.',','),
number_format(100,2,'.',','),$hrfzones,$hrfregions,$hrfagents,$hrfitems);


?>