<?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>


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++){
if(dt.rows[cnt].className=='cls'){

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 tmp=dt.rows[cnt].cells[2].innerHTML;
var tmpc=tmp.toLowerCase();

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

if((sta>=0 || stb>=0 || stc>=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">Agency items 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=".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;}


showshps();


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




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

#echo $seflt;

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

printf("<tr bgcolor='grey' style='color:#E6E6E6;'><td width=200 align='center'  style='border:1px solid white' >Agent</td><td width=120  style='border:1px solid white'  align='center'>Range</td><td width=250 align='center'  style='border:1px solid white' >Item</td><td width=80  style='border:1px solid white'  align='center'>Qty</td><td width=100  style='border:1px solid white'  align='center'>Value</td><td width=90  style='border:1px solid white' align='center'>Invoice list</td></tr>");

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

$arr=array();
$cnt=0;
$rnqtotals=0;
$rnvtotals=0;
$grqtotals=0;
$grvtotals=0;

$sqlst="select distinct item_ranges from itemsmrd";
$aqry=mysqli_query($conn,$sqlst);
While($arst=mysqli_fetch_row($aqry)){
$arr[$cnt]=$arst[0];
$cnt++;}


for($cnt=0;$cnt<count($arr);$cnt++){
$sqlst="select agent_names,item_ranges,item_names,sum(box_qty),sum(box_qty*box_price) as vls from rc_invoices ".$flt." and item_ranges='".$arr[$cnt]."' group by agent_names,item_ranges,item_names order by agent_names,item_ranges,vls DESC ";
$qry=mysqli_query($conn,$sqlst);

#echo $sqlst;

while($rst=mysqli_fetch_row($qry)){
printf("<tr class='cls' bgcolor='white' onmouseover=\"this.style.backgroundColor='silver';\" onmouseout=\"this.style.backgroundColor='white';\"><td>%s</td><td>%s</td><td>%s</td><td align='right'>%s<td align='right'>%s</td>",$rst[0],$rst[1],$rst[2],number_format($rst[3],0,'.',','),number_format($rst[4],2,'.',','));
printf("<td width=80  style='font-size:13' align='center'><a href='pragentsitemsinvoices.php?starts=%s&ends=%s&regions=%s&agents=%s&items=%s&territorys=%s' target='_blank'>invoice list</a></td></tr>",$starts,$ends,$regions,urlencode($rst[0]),$rst[2],$territorys);
$rnqtotals=$rnqtotals+$rst[3];
$rnvtotals=$rnvtotals+$rst[4];
}
printf("<tr class='totals' bgcolor='white' onmouseover=\"this.style.backgroundColor='orange';\" onmouseout=\"this.style.backgroundColor='white';\"><td></td><td><b>%s</td><td><b>Total</td><td align='right'><b>%s<td align='right'><b>%s</td></tr><tr height=14>",$arr[$cnt],number_format($rnqtotals,0,'.',','),number_format($rnvtotals,2,'.',','));
$grqtotals=$grqtotals+$rnqtotals;
$grvtotals=$grvtotals+$rnvtotals;
$rnqtotals=0;
$rnvtotals=0;
}

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