<?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=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">Items 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=".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;}


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='rditemsummarys.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);



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

$asqlst=" left join (select item_names as tgagent_names,sum(box_qty)as tgqtys,sum((pcs_qty*pcs_price)+(box_qty*box_price)) as tgvls from rc_targets ".$flt." group by item_names) as targets on tgagent_names=magent_names ";

$asqlst=$asqlst." left join (select item_names as acagent_names,sum(pcs_qty/packets_per_box) as acqtys,sum((pcs_qty*pcs_price)+(box_qty*box_price)) as acvls,sum(discount_values) as acmargins,sum(free_qtys*pcs_price) as frvlu
from rd_invoices ".$flt." group by item_names) as acts on acagent_names=magent_names";

$asqlst=$asqlst." left join (select item_names as mragent_names,sum(((pcs_qty*pcs_price)+(box_qty*box_price))*(100-discount_rates)/100) as mrvls from market_returns ".$flt." group by item_names) as mrt on mragent_names=magent_names ";


$rsqlst="select magent_names as region ";

$rsqlst=$rsqlst.",tgqtys,tgvls,acqtys,acvls,acmargins,frvlu,mrvls,mranges,sranges ";

$rsqlst=$rsqlst." from (select distinct item_name as magent_names,item_ranges as mranges,sub_ranges as sranges from itemsmrd ";
$rsqlst=$rsqlst." order by item_ranges) as rgs ";


$sqlst=$rsqlst.$asqlst." order by mranges,magent_names,sranges ";
#echo $sqlst;

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

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

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

print("<div style='display:block;font-size:14;' id='tdta'>");
printf("<table id='dtd' style='font-size:13;width:100%%;'><th bgcolor='silver' colspan='12' align='center'><b>Target item achievement (in Millions)</th>
<td style='color:#2ECCFA' bgcolor='silver'>%s</td><tr  style='color:#E6E6E6' bgcolor='grey'>",$exchrf);

printf("<tr class='cls'><td margin=15%% align='center'>Item</td><td margin=15%% align='center' style='background:silver;'>Targt-Qty</td><td margin=15%% align='center' style='background:silver;'>Rd-Qty</td>
<td margin=15%% align='center' style='background:silver;'>Ach%%</td><td margin=15%% align='center' style='background:silver;'>Qty-vari</td><td margin=15%% align='center'>Target value</td><td margin=15%% align='center'>RD-value</td>
<td margin=15%% align='center'>Value-vari</td><td margin=15%% align='center'>Discount</td><td margin=15%% align='center'>Free-Issue</td>
<td margin=15%% align='center'>Market-Rtn</td><td margin=15%% align='center'>Net-RD</td></tr>");

$rdtot=0;
$rdrt=0;
$rdd=0;
$nett=0;
$drt=0;
$dst=0;

while($rst=mysqli_fetch_row($qry)){
$rdtot=$rdtot+$rst[4];
$dst=$dst+$rst[5];
}


$qry=mysqli_query($conn,$sqlst);
while($rst=mysqli_fetch_row($qry)){
if(($rst[1]!=0 || $rst[3])!=0){
if($rst[1]==0){
$pcn=100;}
if($rst[3]==0){
$pcn=0;}
if($rst[3]>0 && $rst[1]>0){
$pcn=$rst[3]*100/$rst[1];
}
$drt=$dst/$rdtot;

printf("<tr class='cls'><td margin=15%%>%s</td><td align='right' margin=15%% style='background:silver'>%s</td><td align='right' margin=15%% style='background:silver;'>%s</td><td align='center' margin=15%% style='background:silver;'>%s%%</td><td align='right' style='background:silver;'>%s</td>
<td align='right' margin=15%%>%s</td><td align='right' margin=15%%>%s</td><td align='right' margin=15%%>%s</td><td align='right' margin=15%%>%s</td><td align='right' margin=15%%>%s</td>
<td align='right' margin=15%%>%s</td><td align='right' margin=15%%>%s</td></tr>",
$rst[0],number_format($rst[1],0,'.',','),number_format($rst[3],0,'.',','),number_format($pcn,0,'.',','),number_format($rst[1]-$rst[3],0,'.',','),number_format($rst[2],0,'.',','),
number_format($rst[4],0,'.',','),number_format($rst[2]-$rst[4],0,'.',','),number_format($rst[4]*$drt,2,'.',','),number_format($rst[6],0,'.',','),number_format($rst[7],0,'.',','),number_format($rst[4]-$rst[5]-$rst[6]-$rst[7],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  style='text-align:right;padding-right:3%%;'><b>%s</td>",number_format($rdtot,2,'.',','),number_format($dst,2,'.',','));

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




?>
