<?php

require "showshp.php";

session_start();
if(!isset($_SESSION['titles']) || !isset($_SESSION['positions'])|| !isset($_SESSION['usernames'])){
header("location:index.php");
}
// Turn off all error reporting
error_reporting(0);

// Report simple running errors
//error_reporting(E_ERROR | E_WARNING | E_PARSE);

// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
//error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

// Report all errors except E_NOTICE
// This is the default value set in php.ini
//error_reporting(E_ALL ^ E_NOTICE);

// Report all PHP errors (see changelog)
//error_reporting(E_ALL);

// Report all PHP errors
//error_reporting(-1);

// Same as error_reporting(E_ALL);
//ini_set('error_reporting', E_ALL);
?>

<html>

<head>

<link rel="stylesheet" type="text/css" href="rdsty.css">
<script type="text/javascript" src="jsDatePick.min.1.3.js"></script>
<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 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=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">Item range sales summary</div>
<div class="homes"><a href="index.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";}

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

showsrds();

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]."' ";}
if(strtolower($serst[8])!='all'){
$seflt=$seflt." and executive_names='".$serst[8]."' ";}
if(strtolower($serst[9])!='all'){
$seflt=$seflt." and chanell='".$serst[9]."' ";}
if(strtolower($serst[10])!='all'){
$seflt=$seflt." and rsm_names='".$serst[10]."' ";}
}


printf("<form method='post' action='rdrangesummarys.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='12'><b>Sales value Rs.</th>");

printf("<tr bgcolor='grey' style='color:#E6E6E6;'><td width=220 align='center'  style='border:1px solid white' >Range</td>");
printf("<td width=100 align='center' style='border:1px solid white' >Target</td><td width=100 align='center'  style='border:1px solid white' >Gross-Rd</td><td width=100 align='center'  style='border:1px solid white' >%%</td>");
printf("<td width=100 align='center'  style='border:1px solid white' >Retail-Dis</td>");
printf("<td width=100 align='center'  style='border:1px solid white' >Wh/Sale-Dis</td>");
printf("<td width=100 align='center'  style='border:1px solid white' >Free-Issue</td>");
printf("<td width=100 align='center'  style='border:1px solid white' >Market-Rtn</td>");
printf("<td width=100 align='center'  style='border:1px solid white' >Net-Rd</td>");
printf("<td width=100 align='center'  style='border:1px solid white' >sub ranges</td>");
printf("<td width=100 align='center'  style='border:1px solid white' >Item list</td>");

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


$sqlst="select mranges,vls,rtdis,wsdis,frvll,tgwt,mrwt from (select  distinct item_ranges as mranges from rd_invoices) as avs 

left outer join(select item_ranges,sum(pcs_qty*pcs_price) as vls,sum(rtdiscount_values) as rtdis,sum(wsdiscount_values) as wsdis,sum(free_qtys*pcs_price) as frvll from rd_invoices ".$flt." group by item_ranges order by vls DESC) as acs on acs.item_ranges=mranges 
left outer join(select item_ranges,sum(box_qty*box_price) as tgwt from rc_targets ".$flt." group by item_ranges) as tgs on tgs.item_ranges=mranges
left outer join(select item_ranges,sum(((pcs_qty*pcs_price)+(box_qty*box_price))*(100-discount_rates)/100) as mrwt from market_returns ".$flt." group by item_ranges) as mrm on mrm.item_ranges=mranges 
";


#echo $sqlst;

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

$qtot=0;
$vtot=0;
$rtd=0;
$wsd=0;
$ntrd=0;
$frtot=0;
$mrtot=0;
$rtrate=0;
$wsrate=0;
$mxv=0;
$mvm=0;

while($rst=mysqli_fetch_row($qry)){
$qtot=$qtot+$rst[5];
$vtot=$vtot+$rst[1];
$rtd=$rtd+$rst[2];
$wsd=$wsd+$rst[3];
$frtot=$frtot+$rst[4];
$ntrd=$ntrd+$rst[1]-$rst[2]-$rst[3]-$rst[4]-$rst[6];
$mrtot=$mrtot+$rst[6];
}
$qry=mysqli_query($conn,$sqlst);
while($rst=mysqli_fetch_row($qry)){
$mxv=$rtd;
$mvm=$wsd;
$rtrate=$mxv/$vtot;
$wsrate=$mvm/$vtot;
printf("<tr class='cls' bgcolor='white' onmouseover=\"this.style.backgroundColor='silver';\" onmouseout=\"this.style.backgroundColor='white';\"><td width=220>%s</td><td style='text-align:right;padding-right:3%%;'>%s</td><td align='right'>%s</td><td align='right'>%s%%</td><td align='right'>%s</td><td align='right'>%s</td><td align='right'>%s</td><td align='right'>%s</td><td align='right'>%s</td>",ucwords($rst[0]),number_format($rst[5],2,'.',','),number_format($rst[1],2,'.',','),number_format($rst[1]*100/$rst[5],2,'.',','),number_format($rst[1]*$wsrate,2,'.',','),number_format($rst[1]*$rtrate,2,'.',','),number_format($rst[4],2,'.',','),number_format($rst[6],2,'.',','),number_format($rst[1]-$rst[2]-$rst[3]-$rst[4],2,'.',','));
printf("<td  width=80 style='font-size:13' align='center'><a href='rdsubrangesummarys.php?starts=%s&ends=%s&regions=%s&agents=%s&territorys=all&ranges=%s' target='_blank'>sub ranges</td><td  width=80 style='font-size:13' align='center'><a href='rditemsummarys.php?starts=%s&ends=%s&regions=%s&agents=%s&territorys=all&ranges=%s' target='_blank'>item list</a></td></tr>",$starts,$ends,$regions,$agents,$rst[0],$starts,$ends,$regions,$agents,$rst[0]);
}
printf("<tr bgcolor='white' onmouseover=\"this.style.backgroundColor='silver';\" onmouseout=\"this.style.backgroundColor='white';\"><td width=150><b>Total</td><td  style='text-align:right;padding-right:3%%;'><b>%s</td><td align='right'><b>%s</td><td align='right'><b>100%%</td><td align='right'><b>%s</td><td align='right'><b>%s</td><td align='right'><b>%s</td><td align='right'><b>%s</td><td align='center'><b>%s</td>",number_format($qtot,2,'.',','),number_format($vtot,2,'.',','),number_format($wsd,2,'.',','),number_format($rtd,2,'.',','),number_format($frtot,2,'.',','),number_format($mrtot,2,'.',','),number_format($ntrd,2,'.',','));



?>
