<?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 dta=document.getElementById("dtda");
var dtb=document.getElementById("dtdb");
var dtc=document.getElementById("dtdc");
var cn=dta.rows.length;
var vls=vl.toLowerCase();

for(var cnt=2;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+1].style.display="table-row";
dtc.rows[cnt].style.display="table-row";}
else{
dta.rows[cnt].style.display="none";
dtb.rows[cnt+1].style.display="none";
dtc.rows[cnt].style.display="none";}

if(vls.length==0){
dta.rows[cnt].style.display="table-row";
dtb.rows[cnt+1].style.display="table-row";
dtc.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">Target zone 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();
$arvl=array();
$tvt=0;

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>Trend zone achievement</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>");

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

$conn=mysqli_connect("localhost","primarysales","S5L7lF01EQZvQrGK","primarysales_dianaold");
$seflt=" where agent_names is not null and ( ";

$sesqlst="select * from acs where user_names='".$_SESSION['usernames']."'";
$seqry=mysqli_query($conn,$sesqlst);

$cnt=mysqli_num_rows($seqry);

while($serst=mysqli_fetch_row($seqry)){
$cnt--;

$seflt=$seflt." ( agent_names is not null ";

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


$seflt=$seflt.") ";

if($cnt>0){
$seflt=$seflt." or ";}
}

$seflt=$seflt.") ";

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

showshps();

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

printf("<form method='post' action='prsalestypes.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("<BR>");

$rsqlst="";
$sqlst="";
$tsqlst="";

$rsqlst="select distinct invoice_types from rc_invoices";
$ct=0;

$sqlst="select mregions ";

$rqry=mysqli_query($conn,$rsqlst);
$cn=mysqli_query($conn,$rsqlst);

printf("<br><br><table style='font-size:13'><th border='white' bgcolor='silver' colspan='%s'><b>Primary sales by invoice types</th>",($cn*2)+1);

$rqry=mysqli_query($conn,$rsqlst);
printf("<tr bgcolor='grey' style='color:#E6E6E6;'><td width=100 align='center'  style='border:1px solid white' >Region</td>");

while($rrst=mysqli_fetch_row($rqry)){
printf("<td align='center' colspan='2'>%s</td>",$rrst[0]);
}
print("</tr><tr class='cls'><td></td>");

$rqry=mysqli_query($conn,$rsqlst);
while($rrst=mysqli_fetch_row($rqry)){
printf("<td align='center' width=100>Qty</td><td align='center' width=100>Value</td>");
}
print("</tr>");

$rqry=mysqli_query($conn,$rsqlst);
while($rrst=mysqli_fetch_row($rqry)){
$ct++;
$sqlst=$sqlst.",tv".$ct.".qtys,tv".$ct.".vls ";
$tsqlst=$tsqlst." left join (select agent_names as rgs, sum(box_qty) as qtys,sum(box_qty*box_price) as vls from rc_invoices ".$flt.
" and invoice_types='".$rrst[0]."'  group by agent_names) as tv".$ct." on tv".$ct.".rgs=mregions ";
}

$sqlst=$sqlst." from (select distinct agent_names as mregions from rc_invoices ".$flt." ) as sss ";


$sqlst=$sqlst.$tsqlst;

#echo $sqlst;

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

while($rst=mysqli_fetch_row($qry)){
printf("<tr class='cls'><td>%s</td>",$rst[0]);

for($cnt=1;$cnt<$cn;$cnt++){
printf("<td align='right' style='padding-right:2%%;'>%s</td>",number_format($rst[$cnt],2,'.',','));}
printf("</tr>");
}


printf("</table>");

printf("<BR><BR>");

$rsqlst="";
$sqlst="";
$tsqlst="";


///////////////////////////////////////
$rsqlst="select distinct invoice_types from rc_invoices";
$ct=0;

$sqlst="select mregions,mitems ";

$rqry=mysqli_query($conn,$rsqlst);
$cn=mysqli_num_fields($rqry);

printf("<br><br><table style='font-size:13'><th border='white' bgcolor='silver' colspan='%s'><b>Primary sales by invoice types</th>",($cn*2)+2);

$rqry=mysqli_query($conn,$rsqlst);
printf("<tr bgcolor='grey' style='color:#E6E6E6;'><td width=100 align='center'  style='border:1px solid white' >Zone</td>");
printf("<td width=80 align='center'  style='border:1px solid white' >Item</td>");

while($rrst=mysqli_fetch_row($rqry)){
printf("<td align='center' colspan='2'>%s</td>",$rrst[0]);
}
print("</tr><tr class='cls'><td></td><td></td>");

$rqry=mysqli_query($conn,$rsqlst);
while($rrst=mysqli_fetch_row($rqry)){
printf("<td align='center' width=100>Qty</td><td align='center' width=100>Value</td>");
}
print("</tr>");

$rqry=mysqli_query($conn,$rsqlst);
while($rrst=mysqli_fetch_row($rqry)){
$ct++;
$sqlst=$sqlst.",tv".$ct.".qtys,tv".$ct.".vls ";
$tsqlst=$tsqlst." left join (select zones as rgs,item_names  as items,sum(box_qty) as qtys,sum(box_qty*box_price) as vls from rc_invoices ".$flt.
" and invoice_types='".$rrst[0]."'  group by zones,item_names) as tv".$ct." on tv".$ct.".items=mitems and tv".$ct.".rgs=mregions ";
}

$sqlst=$sqlst." from (select distinct zones as mregions,item_names as mitems from rc_invoices ".$flt." ) as sss ";


$sqlst=$sqlst.$tsqlst;

#echo $sqlst;

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

while($rst=mysqli_fetch_row($qry)){
printf("<tr class='cls'><td>%s</td><td>%s</td>",$rst[0],$rst[1]);

for($cnt=2;$cnt<$cn;$cnt++){
printf("<td align='right' style='padding-right:2%%;'>%s</td>",number_format($rst[$cnt],2,'.',','));}
printf("</tr>");
}


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

?>