<?php
require "racs.php";
require "vtshowsBBB.php";
session_start();
if(!isset($_SESSION['titles']) || !isset($_SESSION['positions'])|| !isset($_SESSION['usernames'])){
header("location:index.php");}
?>
<html>
<title>RpM 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 salls(pr)
{
var dt=document.getElementById("dt");
var nt=dt.rows.length;
for(var cnt=2;cnt<nt;cnt++){
document.getElementById(cnt+'_qtys').value=dt.rows[cnt].cells[3].innerText.replace(/,/g,"");
dt.rows[cnt].cells[6].innerText='OK';}
}
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='';
dt.rows[cnt].cells[6].innerText=dt.rows[cnt].cells[3].innerText;}
}
function vdt(pr)
{
var dt=document.getElementById("dt");
var nt=dt.rows.length;
var vstates=0;
if(document.getElementById('inputField').value==''){
vstates++;
document.getElementById('inputField').style.backgroundColor='pink';}

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

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

if(document.getElementById('dvdrivers').selectedIndex==0){
vstates++;
document.getElementById('dvdrivers').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";}

var tvl=dt.rows[cnt].cells[3].innerText.replace(/,/g,"");	
if(parseInt(evs.value.replace(/,/g,""),10)>parseInt(tvl,10)){
vstates=1;	
evs.style.background="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('dvstores').style.backgroundColor='white';
document.getElementById('dvvehicles').style.backgroundColor='white';
document.getElementById('dvdrivers').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="rqstdvnotes.php?actp=dvnotes&ddates="+document.getElementById('inputField').value+"&invnumbers="+document.getElementById('invnumbers').innerText+
"&invdates="+document.getElementById('invdates').innerText+"&invagents="+encodeURIComponent(document.getElementById('invagents').innerText)+
"&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++){
//frees= document.getElementById("frees"+cnt).value; 
    
if(dt.rows[cnt].style.display!='none' && document.getElementById(cnt+'_qtys').value!=''){
    
//if(dt.rows[cnt].style.display!='none' && document.getElementById(cnt+'_qtys').value!='' || frees!=0){
    
str=str+"&items"+cn+"="+encodeURIComponent(dt.rows[cnt].cells[0].innerText);	
//str=str+"&"+cn+"_items="+encodeURIComponent(dt.rows[cnt].cells[0].innerText);
str=str+"&"+cn+"_rates="+dt.rows[cnt].cells[1].innerText;
str=str+"&"+cn+"_refqtys="+dt.rows[cnt].cells[2].innerText.replace(/,/g,"");
str=str+"&"+cn+"_qbalances="+dt.rows[cnt].cells[3].innerText.replace(/,/g,"");
str=str+"&"+cn+"_qtys="+document.getElementById(cnt+'_qtys').value.replace(/,/g,"");
//if(frees==""){
//frees=0;}
//str=str+"&frees"+cn+"="+frees;
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)){
tr.cells[6].innerText="Error";
}
else{
var tmp=parseInt(tr.cells[3].innerText.replace(/,/g,""),10)-parseInt(vl,10);
tr.cells[6].innerText=tmp;
if(tmp==0){
tr.cells[6].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=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");

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

#$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($stores)!='all'){
$flt=$flt." and stores_names='".$stores."' ";}

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

$conn=mysqli_connect("localhost","primarysales","S5L7lF01EQZvQrGK","primarysales_diana");
$sqlst="select dlvup from acs where user_names='".$_SESSION['usernames']."' ";
$qry=mysqli_query($conn,$sqlst);

$rst=mysqli_fetch_row($qry);
if($rst[0]==0){
printf("<div style='position:absolute;top:100;left:200;font-size:16;'>Access Denied >>>>>> Contact Admin</div>");}
else{

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;'>");
printf("<tr style='border:1px solid silver;'><td colspan='5' style='font-size:14;font-weight:bold;'>Delivery note</td>
<td align='center'>
<input type='button' value='Set All' style='font-size:12;color:grey;border:1px solid silver;' onclick=\"salls(1)\"></td>
<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;'><td align='center'>Item</td>
<td align='center'>Rate</td>
<td>Invoice qty</td>
<td>Balance Qty</td>
<td>Deliver qty</td>
<td>Free Pcs</td>
<td>Pending qty</td>
</tr>");

$sqlst="select distinct item_names,box_price,box_qty as qtys,(dv_box_qty+undv_box_qty) as dvs,item_ranges from rc_invoices ".$flt." and
dates='".$dates."' and invoice_number='".$invoices."' ";
 
$cnt=1;
$qry=mysqli_query($conn,$sqlst);
 while($rst=mysqli_fetch_row($qry)){
$cnt++;
if($rst[2]==$rst[3]){
$dsf="disabled";}
else{
$dsf="";}

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

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;'>");

$sqlst="select dates,invoice_number,agent_names from rc_invoices ".$flt." and dates='".$dates."' and invoice_number='".$invoices."' ";
$qry=mysqli_query($conn,$sqlst);
$rst=mysqli_fetch_row($qry);

printf("<table style='width:100%%;font-size:13;color:grey;'>");
printf("<tr style='background:silver;'><td colspan='2' style='font-size:14;'><b>Delivery note summary</b></td></tr>");
printf("<tr><td>Date </td><td id='invdates'>%s</td></tr>",$rst[0]);
printf("<tr><td>Invoice</td><td id='invnumbers'>%s</td></tr>",$rst[1]);
printf("<tr><td>Customer</td><td id='invagents'>%s</td></tr>",$rst[2]);
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>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;width:80%%;'><option></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("</select></td></tr>");
	
printf("<tr><td>Driver</td><td><select id='dvdrivers' style='font-family:calibri;font-size:12;width:80%%;'><option></option>");
$sqlst="select distinct driver_short_names	from vehicle_drivers";
$qry=mysqli_query($conn,$sqlst);

while($rst=mysqli_fetch_row($qry)){
printf("<option>%s</option>",$rst[0]);}
printf("</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>