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

<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<script>
 $(function() {
               $("#startdate").datepicker({ dateFormat: "yy-mm-dd" }).val()
               $("#enddate").datepicker({ dateFormat: "yy-mm-dd" }).val()
       });

function fdtfinds(pr)
{

var vl=document.getElementById("dtfinds").value;
var dt=document.getElementById("dtd");
var cn=dt.rows.length;
var vls=vl.toLowerCase();

var vcalls=0;
var gvs=0;
var dvs=0;
var nvs=0;

for(var cnt=2;cnt<cn-1;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 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){
vcalls=vcalls+parseFloat(dt.rows[cnt].cells[4].innerText.replace(",",""));
gvs=gvs+parseFloat(dt.rows[cnt].cells[5].innerText.replace(",",""));
dvs=dvs+parseFloat(dt.rows[cnt].cells[6].innerText.replace(",",""));
nvs=nvs+parseFloat(dt.rows[cnt].cells[7].innerText.replace(",",""));
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";}
}


dt.rows[cn-1].cells[4].innerText=vcalls.toFixed(2);
dt.rows[cn-1].cells[5].innerText=gvs.toFixed(2);
dt.rows[cn-1].cells[6].innerText=dvs.toFixed(2);
dt.rows[cn-1].cells[7].innerText=nvs.toFixed(2);

}


</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">RD sales log book</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' style='width:80%%;'>");


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

$tcnt=mysqli_num_rows($seqry);
$cnt=$tcnt;

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[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($cnt>1){
$seflt=$seflt." ) or ";}
if($cnt==0){
$seflt=$seflt.") ";}
}

$seflt=$seflt.") ";
#echo $seflt;

printf("<form method='post' action='dsrcheck.php?%s'>",$rpsty);
printf("<div class='fra'>From</div><div class='frb'>
<input type='text' size=10 class='starts'  name='starts' value='%s' id='startdate'></div><div class='frc'>To</div><div class='frd'><input type='text class='ends' size=10 name='ends' value='%s' id='enddate'></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>Sales value Rs.</th>");
printf("<tr bgcolor='grey'  style='color:#E6E6E6;'><td width=75 align='center' style='border:1px solid white' >Dates</td><td width=120 align='center' style='border:1px solid white'>Agent</td><td width=120 align='center'   style='border:1px solid white'>Territory
</td><td width=150 align='center' style='border:1px solid white'>Route</td>
<td style='border:1px solid white' width=80 align='center'>Product. calls</td><td width=80 style='border:1px solid white' align='center'>Gross value</td>
<td width=80 style='border:1px solid white' align='center'>Discount</td><td width=80 style='border:1px solid white' align='center'>Free-Issue</td><td width=80 style='border:1px solid white' align='center' >Net value</td><td width=60 style='border:1px solid white' align='center'>Open</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."' ";}

#echo $flt;
#echo $sqlst;

$sqlst="select distinct invoice_number,agent_names,territory_names,dates,routes,sum(pcalls) as pcl,sum(pcs_qty*pcs_price) as gvl,sum(rtdiscount_values+wsdiscount_values) as disvl,sum(pcs_qty*pcs_price)-avg(discount_values)-sum(free_qtys*pcs_price),sum(free_qtys*pcs_price) as frvll from rd_invoices ".$flt." group by agent_names,dates,invoice_number order by dates DESC";
$qry=mysqli_query($conn,$sqlst);

#echo $sqlst;
$pcl=0;
$gvll=0;
$disvl=0;
$netvl=0;
$frvl=0;

while($rst=mysqli_fetch_row($qry)){
$pcl=$pcl+$rst[5];
$gvll=$gvll+$rst[6];
$disvl=$disvl+$rst[7];
$frvl=$frvl+$rst[9];
$netvl=$netvl+$rst[6]-$rst[7]-$rst[9];

printf("<tr class='cls' bgcolor='white' onmouseover=\"this.style.backgroundColor='grey';\" onmouseout=\"this.style.backgroundColor='white';\"><td align='center'>%s</td>
<td>%s</td><td>%s</td><td align='left'>%s</td><td align='center'>%s</td><td align='right'>%s</td><td align='right'>%s</td><td align='right'>%s</td><td align='right'>%s</td>
<td align='center'><a href='invcheck.php?dates=%s&agents=%s&territorys=%s&invoices=%s' target='_blank'>open</td></tr>",$rst[3],$rst[1],$rst[2],$rst[4],
number_format($rst[5],0,'.',','),number_format($rst[6],2,'.',','),number_format($rst[7],2,'.',','),number_format($rst[9],2,'.',','),number_format($rst[8],2,'.',','),$rst[3],$rst[1],$rst[2],$rst[0]);
}
printf("<tr style='font-weight:bold;'><td></td><td>Region-Total</td><td></td><td></td><td align='center'>%s</td><td align='right'>%s</td><td align='right'>%s</td><td align='right'>%s</td><td align='right'>%s</td></tr>",
number_format($pcl,0,'.',','),number_format($gvll,2,'.',','),number_format($disvl,2,'.',','),number_format($frvl,2,'.',','),number_format($netvl,2,'.',','));
printf("<tr style='font-weight:bold;'><td></td><td>Agent-Total</td><td></td><td></td><td align='center'>%s</td><td align='right'>%s</td><td align='right'>%s</td><td align='right'>%s</td><td align='right'>%s</td></tr>",
number_format(0,0,'.',','),number_format(0,2,'.',','),number_format(0,2,'.',','),number_format(0,2,'.',','),number_format(0,2,'.',','));
?>