<?php
require "racs.php";
require "fgstyles.php";
session_start();
if(!isset($_SESSION['titles']) || !isset($_SESSION['positions'])|| !isset($_SESSION['usernames'])){
header("location:index.php");}
?>

<html>
<title>Diana|Donation Location</title>
<head>
<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" type="text/css" href="vtxstyles.css">

<script>
function scopys(pr)
{
var dts=document.getElementById("dt");
var sels=window.getSelection();
sels.removeAllRanges();
var ranges=document.createRange();
ranges.selectNodeContents(dts);
sels.addRange(ranges);
document.execCommand("copy");
alert("Copied - successfull");
sels.removeAllRanges();
}


function srgs(pr)
{
var sv=document.getElementById('srctxt').value.toLowerCase();
var dt=document.getElementById('dt');
var rn=dt.rows.length;
for(var cnt=1;cnt<rn-1;cnt++){
tvl=dt.rows[cnt].cells[0].innerText.toLowerCase();
rvl=dt.rows[cnt].cells[1].innerText.toLowerCase();
svl=dt.rows[cnt].cells[2].innerText.toLowerCase();

if(sv.length>1 && (tvl.indexOf(sv)>=0 || rvl.indexOf(sv)>=0 || svl.indexOf(sv)>=0)){
dt.rows[cnt].style.display='table-row';}
else{
dt.rows[cnt].style.display='none';}
if(sv.length<=1){
dt.rows[cnt].style.display='table-row';}	
}

}
</script>
</head>
<body style='font-family:calibri;font-size:small;'>
<?php

$flt=racs(1);
if($flt=="access failed"){
printf("<div style='font-color:red;'>>Access failed\nContanct Sales and Marketing division</div>");
}
else{

$conn=mysqli_connect("localhost","primarysales","S5L7lF01EQZvQrGK","primarysales_diana");
//mysql_select_db("primarysales_diana",$conn);

if(isset($_REQUEST['starts'])){
echo "starts ok";	
$starts=$_REQUEST['starts'];}
else{
$starts=date('Y-m-01');}

if(isset($_REQUEST['ends'])){
$ends=$_REQUEST['ends'];}
else{
$ends=date('Y-m-t');}

if(isset($_REQUEST['zones'])){
$zones=$_REQUEST['zones'];}
else{
$zones='all';}

if(isset($_REQUEST['regions'])){
$regions=$_REQUEST['regions'];}
else{
$regions='all';}

if(isset($_REQUEST['agents'])){
$agents=$_REQUEST['agents'];}
else{
$agents='all';}

if(isset($_REQUEST['terrys'])){
$terrys=$_REQUEST['terrys'];}
else{
$terrys='all';}

if(isset($subregions)){
$subregions=$_REQUEST['subregions'];}
else{
$subregions='all';}

if(isset($_REQUEST['agenttypes'])){
$agenttypes=$_REQUEST['agenttypes'];}
else{
$agenttypes='all';}

if(isset($_REQUEST['items'])){
$items=$_REQUEST['items'];}
else{
$items='all';}

if(isset($_REQUEST['ranges'])){
$ranges=$_REQUEST['ranges'];}
else{
$ranges='all';}

if(isset($_REQUEST['subranges'])){
$subranges=$_REQUEST['subranges'];}
else{
$subranges='all';}

if(isset($_REQUEST['stores'])){
$stores=$_REQUEST['stores'];}
else{
$stores='all';}

if(isset($_REQUEST['storelocate'])){
$storelocate=$_REQUEST['storelocate'];}
else{
$storelocate='all';}

$flt=racs(1);

if(strtolower($zones)!='all'){
$flt=$flt." and zones='".$zones."' ";}
if(strtolower($regions)!='all'){
$flt=$flt." and regions='".$regions."' ";}
if(strtolower($subregions)!='all'){
$flt=$flt." and sub_regions='".$subregions."' ";}
if(strtolower($agents)!='all'){
$flt=$flt." and agent_names='".$agents."' ";}
if(strtolower($agenttypes)!='all'){
$flt=$flt." and agent_types='".$agenttypes."' ";}
if(strtolower($terrys)!='all'){
$flt=$flt." and territory_names='".$terrys."' ";}
if(strtolower($items)!='all'){
$flt=$flt." and item_names='".$items."' ";}
if(strtolower($ranges)!='all'){
$flt=$flt." and item_ranges='".$ranges."' ";}
if(strtolower($subranges)!='all'){
$flt=$flt." and sub_ranges='".$subranges."' ";}
if(strtolower($storelocate)!='all'){
$flt=$flt." and stores_names='".$storelocate."' ";}


stps(1);
printf("<div style='position:absolute;left:1%%;top:5%%;width:15%%;'>");

//stks(1);//prshows('pr','zone','gobs','sales','Periodic sales',$zones,$regions,$agents,$agenttypes,$terrys,$ranges,$subranges,$items);
stksnew(1);
printf("</div>");


printf("<div style='position:absolute;left:18%%;top:5%%;width:65%%;'>");

$dvnotes=$_REQUEST['dvnumbers'];
$dvdates=$_REQUEST['dvdates'];
$stklot=$_REQUEST['storelocate'];


$sqlst="select distinct dates,note_number,ref_dates,ref_numbers,agent_names,stores_names,driver_names,vehicle_names,note_types from st_notes ".$flt." and dates='".$dvdates."' and note_number='".$dvnotes."' and stores_names='".$stklot."'";

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

printf("<p style='font-size:14;font-weight:bold;color:grey;'>%s</p>",$rst[8]);

printf("<table id='dt' style='width:100%%;font-size:13;color:grey;border-collapse:collapse;color:grey;color:#006699;'>");
printf("<tr><td>Date</td><td>%s</td><td>Number</td><td>%s</td><td>Warehouse</td><td>%s</td></tr>",$rst[0],$rst[1],$rst[5]);
printf("<tr><td>Invoice</td><td>%s</td><td>Number</td><td>%s</td><td>Agent</td><td>%s</td></tr>",$rst[2],$rst[3],$rst[4]);
printf("<tr><td>Vehicle</td><td>%s</td><td>Driver</td><td>%s</td></tr>",$rst[7],$rst[6]);
printf("</table>");

printf("<br>");

printf("<table style='width:100%%;color:grey;font-size:13;border-collapse:collapse;color:#006699;'>");
printf("<tr style='text-align:right;background:silver;font-weight:bold;'><td align='center'>Item</td><td>Rate</td><td>Invoice qty</td></td>
<td>Refer qty</td><td>dv qty</td><td>Balance qty</td></tr>");

$sqlst="select item_names,box_price,ref_box_qty,balance_box_qty,box_qty from st_notes where dates='".$dvdates."' and note_number='".$dvnotes."'and stores_names='".$stklot."'";
$qry=mysqli_query($conn,$sqlst);

while($rst=mysqli_fetch_row($qry)){
printf("<tr style='border-bottom:1px solid silver;'><td>%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></tr>",$rst[0],number_format($rst[1],2,'.',',')
,number_format($rst[2],2,'.',','),number_format($rst[3],2,'.',','),number_format($rst[4],2,'.',','),number_format($rst[3]-$rst[4],2,'.',','));
}

printf("</table>");

printf("</div>");

}

?>

</div>
</body>
</html>