<?php
require "racs.php";
require "vtshowsBBB.php";
session_start();
if(!isset($_SESSION['titles']) || !isset($_SESSION['positions'])|| !isset($_SESSION['usernames'])){
header("location:vtlogings.php");}
?>
<html>
<title>Sales program</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();
if(sv.length>1 && (tvl.indexOf(sv)>=0 || rvl.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';}	
}
}


function rsalls(pr)
{
var dt=document.getElementById("dt");
var nt=dt.rows.length;
for(var cnt=2;cnt<nt;cnt++){
document.getElementById(cnt+'_qtys').value='';}
}



function rgsels(pr)
{
var dt=document.getElementById("dt");
var nt=dt.rows.length;
var rgs=document.getElementById('sranges').options[document.getElementById('sranges').selectedIndex].value;

if(rgs=='All'){
for(var cnt=2;cnt<nt;cnt++){
dt.rows[cnt].style.display='table-row';}
}

else{
for(var cnt=2;cnt<nt;cnt++){
if(dt.rows[cnt].cells[0].innerText!=rgs){
dt.rows[cnt].style.display='none';}
else{
dt.rows[cnt].style.display='table-row';}
}}

}



function vdt(pr)
{
var dt=document.getElementById("dt");
var nt=dt.rows.length;
var vstates=0;

if(document.getElementById('dvfroms').selectedIndex==0){
vstates++;
document.getElementById('dvfroms').style.backgroundColor='pink';}

if(document.getElementById('inputField').value==''){
vstates++;
document.getElementById('inputField').style.backgroundColor='pink';}

if(document.getElementById('refernumbers').selectedIndex==0){
vstates++;
document.getElementById('refernumbers').style.backgroundColor='pink';}

if(document.getElementById('dvstores').selectedIndex==0){
vstates++;
document.getElementById('dvstores').style.backgroundColor='pink';}

if(document.getElementById('dvdrivers').selectedIndex==0){
vstates++;
document.getElementById('dvdrivers').style.backgroundColor='pink';}

if(document.getElementById('dvvehicles').selectedIndex==0){
vstates++;
document.getElementById('dvvehicles').style.backgroundColor='pink';}



if(pr.target.value=="Check-in"){
	
for(var cnt=2;cnt<nt;cnt++){
var evs=document.getElementById(cnt+'_qtys');

if(evs.value=='') {
dt.rows[cnt].style.display="none";}
else{
if(isNaN(evs.value)){
vstates=1;
evs.style.backgroundColor="pink";}	
if(parseInt(evs.value,10)==0){
dt.rows[cnt].style.display="none";}
}
}

if(vstates==0){
document.getElementById('vdsends').style.backgroundColor='#3498DB';
document.getElementById('vdsends').style.color='white';
document.getElementById('vdsends').disabled=false;}

pr.target.value="Go back";}

else{
for(var cnt=2;cnt<nt;cnt++){
dt.rows[cnt].style.display="table-row";
document.getElementById(cnt+'_qtys').style.backgroundColor='white';}

pr.target.value="Check-in";
document.getElementById('inputField').style.backgroundColor='white';
document.getElementById('refernumbers').style.backgroundColor='white';
document.getElementById('dvstores').style.backgroundColor='white';
document.getElementById('vdsends').disabled=true;
document.getElementById('vdsends').style.backgroundColor='silver';
document.getElementById('vdsends').style.color='grey';
document.getElementById('vdsends').style.backgroundColor='silver';
document.getElementById('vdsends').style.color='grey';
document.getElementById('vdsends').disabled=true;
}

}	


function vsds(pr)
{
var dt=document.getElementById('dt');
var nt=dt.rows.length;
var httpr=new XMLHttpRequest();
var cn=0;

var str="rqstgrns.php?actp=grns&ddates="+document.getElementById('inputField').value+"&refernumbers="+document.getElementById('refernumbers').value+
"&dvstores="+document.getElementById('dvstores').options[document.getElementById('dvstores').selectedIndex].text+
"&dvfroms="+document.getElementById('dvfroms').options[document.getElementById('dvfroms').selectedIndex].text+
"&dvvehicles="+document.getElementById('dvvehicles').options[document.getElementById('dvvehicles').selectedIndex].text+
"&dvdrivers="+document.getElementById('dvdrivers').options[document.getElementById('dvdrivers').selectedIndex].text;


for(var cnt=2;cnt<nt;cnt++){
if(dt.rows[cnt].style.display!='none' && document.getElementById(cnt+'_qtys').value!=''){
str=str+"&items"+cn+"="+encodeURIComponent(dt.rows[cnt].cells[1].innerHTML);	
//str=str+"&"+cn+"_items="+encodeURIComponent(dt.rows[cnt].cells[1].innerText);
str=str+"&"+cn+"_rates="+dt.rows[cnt].cells[2].innerText;
str=str+"&"+cn+"_packetsperbox="+dt.rows[cnt].cells[3].innerText;
str=str+"&"+cn+"_qtys="+document.getElementById(cnt+'_qtys').value;
cn++;
}}

str=str+"&rcnt="+cn;

dt.style.display="none";
document.getElementById("rds").style.display="none";
var dv=document.getElementById('mnf');
dv.innerText="Uploading...";
dv.style.fontSize=14;

httpr.onreadystatechange=function(){
if(httpr.readyState==4 && httpr.status==200){
var tmp=httpr.responseText.split("***");
if(tmp[0]=='success'){
dv.innerHTML="<br>Upload successful<br><br>Delivery note - <a href='vtprstdvshows.php?dvdates="+tmp[3]+"&dvnumbers="+tmp[4]+"'>"+tmp[4]+"(Open)</a>";}
else{	
dv.innerText="Upload unsuccessful";}
}}
 
httpr.open('GET',str,true)
httpr.send();

}
 	



function dvs(pr)
{
var keys=pr.keyCode;
var tr=pr.target.parentNode.parentNode;
var vl=pr.target.value;


if(vl!=''){
if(isNaN(vl)){
vl=vl;
}
else{
var tmp=parseInt(vl,10);
//if(tmp==0){
//tr.cells[5].innerText='OK';}
}	
}
if((keys==13||keys==40) && tr.rowIndex<tr.parentNode.rows.length){
document.getElementById((tr.rowIndex+1)+'_qtys').focus();}	
if(keys==38 && tr.rowIndex>2){
document.getElementById((tr.rowIndex-1)+'_qtys').focus();}	

}
	

	
function onfcs(pr)
{
pr.parentNode.parentNode.style.backgroundColor='silver';
pr.parentNode.parentNode.style.color='#2471A3';
pr.parentNode.parentNode.style.fontWeight='bold';

}
function onbrs(pr)
{
pr.parentNode.parentNode.style.backgroundColor='white';;
pr.parentNode.parentNode.style.color='grey';
pr.parentNode.parentNode.style.fontWeight='normal';
}
</script>



</head>
<body style='font-family:calibri;font-size:small;'>
<div style='width:95%;height:100%;'>



<?php
$flt=stacs(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");
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';}

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


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

printf("<div class='mnf' id='mnf'>");
$invoices=$_REQUEST['dinvoicenumbers'];
$dates=$_REQUEST['ddates'];

printf("<table  id='dt' name='dt' style='position:absolute;top:3%%;width:65%%;font-size:13;color:grey;border-collapse:collapse;border:1px solid silver;'>");
printf("<tr><td colspan='3' style='font-size:14;font-weight:bold;'>Finished Goods GRN</td><td align='center'>
<td align='center'><input type='button' value='Reset All' style='font-size:12;;border:1px solid silver;color:grey;' onclick=\"rsalls(2)\"></td></tr>");

printf("<tr style='background:silver;border:1px solid silver;text-align:right;border-bottom:1px solid silver;'><td align='left'>
<select id='sranges' style='font-size:12;font-family:calibri;border:1px solid grey;color:#006699;background:silver;' onchange=\"rgsels(1)\"><option>All</option>");

$sqlst="select distinct item_ranges from itemsmrd ";
$qry=mysqli_query($conn,$sqlst);

while($rst=mysqli_fetch_row($qry)){
printf("<option>%s</option>",$rst[0]);}
	
printf("</select></td><td align='center'>Item</td><td align='center'>Rate</td><td>Packets per box</td><td align='center'>Qty</td></tr>");


$sqlst="select distinct item_ranges,item_name,whole_sale_price,packets_per_box from itemsmrd where item_status='Active' order by item_ranges,sub_ranges ";
$qry=mysqli_query($conn,$sqlst);

$cnt=2;

while($rst=mysqli_fetch_row($qry)){
printf("<tr style='border-bottom:1px solid silver;'><td>%s</td><td>%s</td><td align='right'>%s</td><td align='right'>%s</td>
<td align='center'><input type='text' id='%s_qtys' size=5 style='font-family:calibri;font-size:12;color:grey;text-align:right;' onkeyup=\"dvs(event)\"
onchange=\"dvs(event)\" onfocus=\"onfcs(this)\" onblur=\"onbrs(this)\"></td></tr>",
$rst[0],$rst[1],number_format($rst[2],2,'.',','),number_format($rst[3],2,'.',','),$cnt);
$cnt++;
}


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

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

printf("<div id='rds' style='position:absolute;top:8%%;left:73%%;border:1px solid silver;border-radius:2px;width:22%%;font-size:13;'>");

printf("<table style='width:100%%;font-size:13;color:grey;'>");
printf("<tr style='background:silver;'><td colspan='2' style='font-size:14;'><b>FG GRN summary</b></td></tr>");

printf("<tr><td>Date</td><td><input type='text' id='inputField' size=10 style='font-family:calibri;font-size:12;border:1px solid silver;'></td></tr>");
printf("<tr><td>Reference</td><td><input type='text' id='refernumbers' size=10 style='font-family:calibri;font-size:12;border:1px solid silver;'></td></tr>");

printf("<tr><td>From</td><td><select id='dvfroms' style='font-family:calibri;font-size:12;width:80%%;'><option></option>");
$sqlst="select distinct stores from warehouse ";
$qry=mysqli_query($conn,$sqlst);

while($rst=mysqli_fetch_row($qry)){
printf("<option>%s</option>",$rst[0]);}
printf("</select></td></tr>");

printf("<tr><td>Warehouse</td><td><select id='dvstores' style='font-family:calibri;font-size:12;width:80%%;'><option></option>");
$sqlst="select distinct stores from warehouse ";
$qry=mysqli_query($conn,$sqlst);
while($rst=mysqli_fetch_row($qry)){
printf("<option>%s</option>",$rst[0]);}
printf("</select></td></tr>");


printf("<tr><td>Vehicle</td><td><select id='dvvehicles' style='font-family:calibri;font-size:12;'><option></option><option>Other</option>");
$sqlst="select distinct vehicle_number from delivery_vehicles";
$qry=mysqli_query($conn,$sqlst);
while($rst=mysqli_fetch_row($qry)){
printf("<option>%s</option>",$rst[0]);}
printf("<option>None</option></select></td></tr>");
	
printf("<tr><td>Driver</td><td><select id='dvdrivers' style='font-family:calibri;font-size:13;'><option></option><option>Other</option>");
$sqlst="select distinct driver_names from vehicle_drivers";
$qry=mysqli_query($conn,$sqlst);

while($rst=mysqli_fetch_row($qry)){
printf("<option>%s</option>",$rst[0]);}
printf("<option>None</option></select></td></tr>");

printf("<tr><td align='center' colspan='2'><br><input type='button' id='vdchecks' value='Check-in' 
style='font-family:verdana;width:90%%;height:80%%;border:1px solid silver;border-radius:1px;font-size:13;color:grey;background:silver;font-weight:bold;'  onclick=\"vdt(event)\"></td></tr>");

printf("<tr><td align='center' colspan='2'><br><input type='button' id='vdsends'  value='Send' disabled 
style='font-family:verdana;width:90%%;height:80%%;border:1px solid silver;border-radius:1px;font-size:13;color:grey;background:silver;font-weight:bold;' onclick=\"vsds(1)\"><br></td></tr>");
printf("<tr><td>-</td></tr></table></div>");
}
?>
</div>
</body>
</html>