<?php
session_start();
if(!isset($_SESSION['titles']) || !isset($_SESSION['positions'])|| !isset($_SESSION['usernames'])){
header("location:index.php");
}
$inv=$_REQUEST['invoice'];

?>
<!doctype html>
<!--<html manifest="myapp.appcache">-->
<html lang="en">
<?php include_once('include.php');?>


<body style="font-family:calibri;font-size:small" ></body>

<div class="content-wrapper">
<section class="content">
<div class="box box-default">

<div class="form-group" id="updl"></div>

<div class="box-body">
<div class="col-md-3">
<div class="form-group" id="custtname">
<label for="">Distributor</label>
<select class="form-control input-sm select2" type="text" id="custname" name="custname" onchange="showUser(this.value)" style="width: 100%;">
</select>
</div>

<?php
printf("<table id='agdetails' style='font-size:10;border:1px solid silver;width:100%%;background:#F2F2F2;'>");
printf("<tr class='cls'><td colspan='2' style='background:silver;'><b>Bill details</b></td></tr>");
printf("<tr class='cls'><td>Date</td><td><input type='text' id='datepicker' onchange='showitem(this.value)'></td>");

//printf("<tr class='cls'><td>Invoice-No</td><td><input type='text' id='billnum' value='' onkeyup=\"blnmskey(event)\" required ></td></tr>");

printf("</table>");


printf("<BR>");

printf("<table style='font-size:13;' id='dsms'><tr style='border-bottom:1px solid silver;'><td width=100><b><font color=grey>summary</font></td></tr></table>");
printf("<table style='font-size:13;width:100%%;'   id='dstots'>");
printf("<tr><td>Gross Purchase</td><td align='right'>%s</td></tr>",number_format(0,2,'.',','));
printf("<tr><td>Invoice Discount</td><td align='right'>%s</td></tr>",number_format(0,2,'.',','));

printf("<tr style='border-bottom:1px solid #f00;'><td>Net Purchase</td><td align='right'; font color='red'>%s</td></tr>",number_format(0,2,'.',','));

printf("</table>");

printf("<BR>");

printf("<table id='agdtls' style='font-size:10;border:1px solid silver;width:100%%;background:#F2F2F2;'>");

printf("<tr class='cls'><td colspan='2' style='background:silver;'><b>Discounts</b></td></tr>");

printf("<tr class='cls'><td>Inv- Discount</td><td><input type='text' id='wsdiscs' name='wsdiscs' placeholder='Enter Invoice margin 6' required onkeyup=\"wsdiscky(event)\" onblur=\"gfcs(this)\"></td></tr>");

printf("</table>");

printf("<BR>");

printf("<table id='agd' style='font-size:10;border:1px solid silver;width:100%%;background:#F2F2F2;'>");

//printf("<tr class='cls'><td colspan='2' style='background:silver;'><b>Payment</b></td></tr>");

//printf("<tr class='cls'><td>Cash</td><td><input type='text' id='cash' name='cashh' placeholder='Rs.' disabled onkeydown=\"cashtdn(event)\" onkeyup=\"cashky(event)\" onblur=\"gfcs(this)\"></td></tr>");

//printf("<tr class='cls'><td>Credit:</td><td><input type='text' id='credits' name='creditss' placeholder='Rs.' disabled onkeydown=\"credittdn(event)\" onkeyup=\"creditskey(event)\" onblur=\"gfcs(this)\"></td></tr>");

//printf("<tr class='cls'><td>Claim:</td><td><input type='text' id='claiminc' name='claiminc' placeholder='Rs.' disabled onkeydown=\"claimtdn(event)\" onkeyup=\"claimkey(event)\" onblur=\"gfcs(this)\"></td></tr>");

//printf("<tr class='cls'><td>Claim Date:</td><td><input type='text' id='inputField' name='inputFields' disabled name='claimdate'></td></tr>");


//printf("<tr class='cls'><td>Cheque:</td><td><input type='text' id='cheq' name='cheques' placeholder='Rs.' disabled onkeydown=\"cqtdn(event)\" onkeyup=\"cheqskey(event)\" onblur=\"gfcs(this)\"></td></tr>");

//printf("<tr class='cls'><td>Credit Period</td><td><input type='text' id='dudate' name='dudate' placeholder='Credit Days' disabled></td></tr>");

//printf("<tr class='cls'><td>Due Amount</td><td style='color:purple'; align='center'>%s</td></tr>",number_format(0,2,'.',','));

//printf("<tr class='cls' hidden><td hidden>Cash Amount</td><td style='color:purple'; align='center'>%s</td></tr>",number_format(0,2,'.',','));

//printf("<tr class='cls' hidden><td hidden>Credit Amount</td><td style='color:purple'; align='center'>%s</td></tr>",number_format(0,2,'.',','));

//printf("<tr class='cls' hidden><td hidden>Cheque Amount</td><td style='color:purple'; align='center'>%s</td></tr>",number_format(0,2,'.',','));

//printf("<tr class='cls' hidden><td hidden>Claim Amount</td><td style='color:purple'; align='center'>%s</td></tr>",number_format(0,2,'.',','));

printf("</table>");

printf("<tr></tr><tr><td colspan='2' align='center'><br>
<input type='button' value='Upload now' id='uploads' style='width:95%%;color:#0066cc;background:white;border-radius:3px;font-weight:bold;border:1px solid #0066cc;height:100%%;'onmouseover=\"this.style.backgroundColor='#0066cc';this.style.color='white';\" onmouseout=\"this.style.backgroundColor='white';this.style.color='#0066cc';\" onclick=\"sendups(1)\"></td></tr>");


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

<div class="col-md-9">
<?php
$conn=mysqli_connect("localhost","primarysales","S5L7lF01EQZvQrGK","primarysales_diana");

printf("<style='width:100%%;'><br><table id='dtd' style='font-size:10;border:1px solid silver;'><td border='white' bgcolor='silver' colspan='6' style='border:1px solid white;text-align:left;'><input type='text' placeholder='Item Search' id='dtfinds' onchange=\"fdtfinds(event)\" onkeyup=\"fdtfinds(event)\" onclick=\"fdtfinds(event)\"></td>");

printf("<tr bgcolor='grey' style='color:#E6E6E6;font-weight:bold;font-size:10;'><td width=15%% align='center'  style='border:1px solid white' >");

printf("<select id='sranges' tabindex='2' onchange=\"sranges(event)\" style='border:1px solid grey;background:silver;'><option>All</option>");

$sqlst="select distinct item_ranges from itemsmrd where item_status='ORDER' order by item_ranges";
$qry=mysqli_query($conn,$sqlst);
while($rst=mysqli_fetch_row($qry)){
printf("<option>%s</option>",$rst[0]);}
printf("</select></td>");

printf("<td width=50%% align='center'  style='border:1px solid white' >Item</td>
<td width=8%% align='center'  style='border:1px solid white' >Price</td>
<td width=8%% align='center'  style='border:1px solid white' >Value</td>
<td width=4%% align='center' style='border:1px solid white' >Free</td>
<td width=15%% align='center' style='border:1px solid white' >Order Value</td>
");

$sqlst="select item_ranges,sub_ranges,item_name_mrt,whole_sale_price from itemsmrd where item_status='ORDER' order by item_ranges,sub_ranges,packet_weight,item_name";

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

while($rst=mysqli_fetch_row($qry)){
    $price=$rst[3];
$cnt++;

printf("<tr class='cls' style='background:#F2F2F2;'>
<td>%s</td>
<td>%s</td>
<td>%s</td>

<td align='center'><input type='text' size=8  style='text-align:right;font-size:10;' id='sales%s' tabindex='0' onkeydown=\"trdns(event)\" onkeyup=\"stqtys(event)\"onfocus=\"onfcs(this.parentNode.parentNode.rowIndex)\" onblur=\"outfcs(this)\"></td>

<td align='center'><input type='text' size=4 id='frees%s' style='text-align:right;font-size:10;' onkeydown=\"stfrs(event)\"onfocus=\"onfcs(this.parentNode.parentNode.rowIndex)\" onblur=\"outfcs(this)\"></td>

<td align='right'></td>

</tr>",

$rst[0],ucwords($rst[2]),$rst[3],$cnt,$cnt,$cnt,$cnt);
}
?>
</div>
</div>
</div>
</section>
</div>


<script>
//Date picker
    $('#datepicker').datepicker({
      autoclose: true
    })

function stfrs(pr)
{
var keys=pr.which || pr.keyCode;
var dt=document.getElementById("dtd");
var rw=pr.target.parentNode.parentNode.rowIndex;
pr.target.style.backgroundColor="white";

if(keys==40){
pr.preventDefault();
document.getElementById("frees"+(rw+1)).focus();}

if(keys==37){
pr.preventDefault();
document.getElementById("sales"+(rw)).focus();}


if(keys==38){
pr.preventDefault();
document.getElementById("frees"+(rw-1)).focus();}

if(keys==13){
var vl=document.getElementById("frees"+rw).value;
if(isNaN(vl) && vl!=''){
pr.target.style.backgroundColor="red";}
else{
document.getElementById("frees"+(rw+1)).focus();}
}

if(keys==27){
pr.preventDefault();
document.getElementById("billnum").focus();}
}

function blnmskey(pr)
{
var keys=pr.which || pr.window.event.keyCode;
if(keys==40){
document.getElementById("wsdiscs").focus();}
if(keys==13){
document.getElementById("wsdiscs").focus();}
if(keys==38){
document.getElementById("datepicker").focus();    
}
}


function onfcs(pr)
{
document.getElementById("dtd").rows[pr].style.backgroundColor="grey";
document.getElementById("dtd").rows[pr].style.fontWeight="bold";
document.getElementById("dtd").rows[pr].style.color="black";
}


function outfcs(pr)
{
gfcs(pr);
rw=pr.parentNode.parentNode.rowIndex;
document.getElementById("dtd").rows[rw].style.backgroundColor="#F2F2F2";
document.getElementById("dtd").rows[rw].style.fontWeight="normal";
document.getElementById("dtd").rows[rw].style.color="black";
}



function wsdiscky(pr)
{
vl=pr.target.value;
if(pr.keyCode==13 || pr.keyCode==40){
if(isNaN(vl)){
alert("Check the value");
pr.target.focus();}
}
if(pr.keyCode!=13 && pr.keyCode!=40 && pr.keyCode!=38){
var tvl=parseFloat(document.getElementById("dstots").rows[0].cells[1].innerHTML);
var vl=parseFloat(vl);
document.getElementById("dstots").rows[1].cells[1].innerHTML=(vl).toFixed(2);
document.getElementById("dstots").rows[2].cells[1].innerHTML=(tvl-(tvl/100*vl)).toFixed(2);
}
if(pr.keyCode==13){
document.getElementById("cash").focus();} 

if(pr.keyCode==40){
document.getElementById("cash").focus();} 

if(pr.keyCode==38){
document.getElementById("billnum").focus();} 
}

function cashky(pr)
{
vl=pr.target.value;
if(pr.keyCode==13 || pr.keyCode==40){
if(isNaN(vl)){
vl=0;
pr.target.focus();}
}
if(pr.keyCode!=13 && pr.keyCode!=40 && pr.keyCode!=38){
var vl=parseFloat(vl);
document.getElementById("agd").rows[8].cells[1].innerHTML=(vl).toFixed(2);
var cshd=parseFloat(document.getElementById("dstots").rows[2].cells[1].innerHTML);
var cramt=parseFloat(document.getElementById("agd").rows[9].cells[1].innerHTML);
var cqamt=parseFloat(document.getElementById("agd").rows[10].cells[1].innerHTML);
var clamtv=parseFloat(document.getElementById("agd").rows[11].cells[1].innerHTML);
document.getElementById("agd").rows[7].cells[1].innerHTML=(cshd-vl-cramt-cqamt-clamtv);
}
}

function creditskey(pr)
{
crdt=pr.target.value;
if(pr.keyCode==13||pr.keyCode==40){
if(isNaN(crdt)){
alert("Check the value");
pr.target.focus();}
}
if(pr.keyCode!=13 && pr.keyCode!=40 && pr.keyCode!=38){
var crdt=parseFloat(crdt);
document.getElementById("agd").rows[9].cells[1].innerHTML=(crdt).toFixed(2);
var cshdg=parseFloat(document.getElementById("dstots").rows[2].cells[1].innerHTML);
var cashamt=parseFloat(document.getElementById("agd").rows[8].cells[1].innerHTML);
var cqamtt=parseFloat(document.getElementById("agd").rows[10].cells[1].innerHTML);
var clamt=parseFloat(document.getElementById("agd").rows[11].cells[1].innerHTML);
document.getElementById("agd").rows[7].cells[1].innerHTML=(cshdg-crdt-cashamt-cqamtt-clamt);
}
}

function claimkey(pr)
{
cla=pr.target.value;
if(pr.keyCode==13||pr.keyCode==40){
if(isNaN(cla)){
alert("Check the value");
pr.target.focus();}
}
if(pr.keyCode!=13 && pr.keyCode!=40 && pr.keyCode!=38){
var cla=parseFloat(cla);
document.getElementById("agd").rows[11].cells[1].innerHTML=(cla).toFixed(2);
var csst=parseFloat(document.getElementById("dstots").rows[2].cells[1].innerHTML);
var cashamtt=parseFloat(document.getElementById("agd").rows[8].cells[1].innerHTML);
var crdamtt=parseFloat(document.getElementById("agd").rows[9].cells[1].innerHTML);
var cqamtt=parseFloat(document.getElementById("agd").rows[10].cells[1].innerHTML);
document.getElementById("agd").rows[7].cells[1].innerHTML=(csst-cla-cashamtt-cqamtt-crdamtt);
}
}


function cheqskey(pr)
{
vl=pr.target.value;
if(pr.keyCode==13||pr.keyCode==40){
if(isNaN(vl)){
alert("Check the value");
pr.target.focus();}
}
if(pr.keyCode!=13 && pr.keyCode!=40 && pr.keyCode!=38){
var vl=parseFloat(vl);
document.getElementById("agd").rows[10].cells[1].innerHTML=(vl).toFixed(2);
var cshdd=parseFloat(document.getElementById("dstots").rows[2].cells[1].innerHTML);
var cashamtt=parseFloat(document.getElementById("agd").rows[8].cells[1].innerHTML);
var cqamtty=parseFloat(document.getElementById("agd").rows[9].cells[1].innerHTML);
var clamts=parseFloat(document.getElementById("agd").rows[11].cells[1].innerHTML);
document.getElementById("agd").rows[7].cells[1].innerHTML=(cshdd-vl-cashamtt-cqamtty-clamts);
}
}

function gfcs(pr)
{
vl=pr.value;
if(isNaN(vl)&&vl!=''){
pr.target.style.backgroundColor="red";}
}


function fdtfinds(pr)
{
var vl=document.getElementById("dtfinds").value;
var fvl=document.getElementById("sranges").options[document.getElementById("sranges").selectedIndex].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 && (fvl==dt.rows[cnt].cells[0].innerText || fvl=='All')){
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";}
}
}


function trdns(pr)
{
var keys=pr.which || pr.window.event.keyCode;
var rw=pr.target.parentNode.parentNode.rowIndex;
var nrw=rw+1;
var prw=rw-1;
if(keys==40){
document.getElementById("sales"+nrw).focus();}
if(keys==38){
document.getElementById("sales"+prw).focus();}
if(keys==39){
pr.preventDefault();
document.getElementById("frees"+(rw)).focus();}
if(keys==27){
pr.preventDefault();
document.getElementById("billnum").focus();}
}


function cashtdn(pr)
{
var keys=pr.which || pr.window.event.keyCode;
if(keys==40){
document.getElementById("credits").focus();}
if(keys==13){
document.getElementById("credits").focus();}
}

function credittdn(pr)
{
var keys=pr.which || pr.window.event.keyCode;
if(keys==40){
document.getElementById("claiminc").focus();
}
if(keys==13){
document.getElementById("claiminc").focus();
}
if(keys==38){
document.getElementById("cash").focus();
}
}

function claimtdn(pr)
{
var keys=pr.which || pr.window.event.keyCode;
if(keys==40){
document.getElementById("cheq").focus();
}
if(keys==13){
document.getElementById("cheq").focus();
}
if(keys==38){
document.getElementById("credits").focus();
}
}

function cqtdn(pr)
{
var keys=pr.which || pr.window.event.keyCode;
if(keys==40){
document.getElementById("dudate").focus();
}
if(keys==13){
document.getElementById("dudate").focus();
}
if(keys==38){
document.getElementById("inputField").focus();
}
}


function stqtys(pr)
{
var x = pr.which || pr.event.keyCode;
if (x == 8 || x == 34) {
document.getElementById("dtfinds").focus();
document.getElementById("dtfinds").value = "";
}
var keys=pr.which || pr.window.event.keyCode;
var rw=pr.target.parentNode.parentNode.rowIndex;
var dt=document.getElementById("dtd");
var tt=document.getElementById("dstots");
var tots=0;
var discs=0;
var nrw=rw+1;
var prw=rw-1;

pr.target.style.backgroundColor="white";
var vl=pr.target.value;
if(isNaN(vl) && vl!=''){
pr.target.style.backgroundColor="red";}
else{
var rvl=(vl*dt.rows[rw].cells[2].innerHTML);
if(rvl>0){
dt.rows[rw].cells[5].innerHTML=rvl.toFixed(2);
for(cnt=2;cnt<dt.rows.length;cnt++){
var tmpp=parseFloat(dt.rows[cnt].cells[5].innerHTML);
if(!isNaN(tmpp)){
tots=tots+tmpp;
}}
tt.rows[0].cells[1].innerHTML=tots.toFixed(2);

var gts=tt.rows[0].cells[1].innerHTML=tots.toFixed(2);
var sldis=parseFloat(document.getElementById("dstots").rows[1].cells[1].innerHTML);

tt.rows[2].cells[1].innerHTML=((gts-sldis)).toFixed(2);
}
else{
dt.rows[rw].cells[5].innerHTML="";}
if(keys==13){
document.getElementById("sales"+nrw).focus();

}
}
}


function sendups(pr)
{
var x = document.getElementById("custname").value;
if (x == "") {
swal('Remind!', 'Customer Name is required', 'warning');
return false;
}
var dts=document.getElementById("datepicker");
if(dts.value==""){
swal('Remind!', 'Date Field required', 'warning');
return false;
}
var y = document.getElementById("wsdiscs").value;
if (y == "") {
swal('Remind!', 'Dioscount is required', 'warning');
return false;
}
else{
swal('DB+++!', 'Invoice Updated', 'success');
var dt=document.getElementById("dtd");
var tt=document.getElementById("dstots");
//var tt=document.getElementById("agd");
var str=0;
var cn=0;
var ln=dt.rows.length;
var qtys=0;
var frees=0;

str="actp=rduploads&rddates="+document.getElementById("datepicker").value;
str=str+"&rdagents="+document.getElementById("custname").value;
str=str+"&wsdcvalues="+tt.rows[1].cells[1].innerHTML;
str=str+"&bilgross="+tt.rows[0].cells[1].innerHTML;
str=str+"&bilnetvl="+tt.rows[2].cells[1].innerHTML;

for(var cnt=2;cnt<ln;cnt++){
qtys=document.getElementById("sales"+cnt).value;
frees= document.getElementById("frees"+cnt).value;

if(qtys!=0 || frees!=0 && dt.rows[cnt].style.display!='none'){
str=str+"&prices"+cn+"="+dt.rows[cnt].cells[2].innerHTML;
str=str+"&items"+cn+"="+encodeURIComponent(dt.rows[cnt].cells[1].innerHTML);

if(qtys==""){
qtys=0;}

if(frees==""){
frees=0;}

str=str+"&qtys"+cn+"="+qtys;
str=str+"&frees"+cn+"="+frees;
cn++;
}

}

str=str+"&rws="+cn;

dt.style.display="none";
tt.style.display="none";

document.getElementById("dsms").innerHTML="";
document.getElementById("custtname").innerHTML="";
document.getElementById("agdetails").innerHTML="";
document.getElementById("agdtls").innerHTML="";
document.getElementById("agd").innerHTML="";
document.getElementById("updl").style.fontSize=16;
document.getElementById("updl").innerHTML="<a href='addrangeorder.php?>'class='btn btn-success'>ADD ORDER</a>";


var httpr= new XMLHttpRequest();
httpr.onreadystatechange=function(){
if(httpr.readyState==4 && httpr.status==200){
var tmps=httpr.responseText.split("####");
if(tmps[0]=="errors"){
document.getElementById("updl").style.fontSize=16;
alert("Upload not successful<br><br>Check the data");}
if(tmps[0]=="oks"){
document.getElementById("updl").style.fontSize=16;
document.getElementById("updl").innerHTML="<a href='addrangeorder.php?'class='btn btn-success'>ADD ORDER</a><br><br><a href='agentorderapvl.php?'class='btn btn-info'>VIEW ORDER INV</a><br>";

}

}}}
httpr.open("POST","purchaseuploads.php",true);
httpr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
httpr.send(str);
document.getElementById("uploads").style.visibility='hidden';
//document.write(str);
}


function sranges(pr)
{
var keys=pr.keyCodes;
var dt=document.getElementById("dtd");
var ln=dt.rows.length;
if(keys==8){
pr.preventDefault();}
var vl=pr.target.options[pr.target.selectedIndex].value;
if(vl=='All'){
for(var cnt=2;cnt<ln;cnt++){
dt.rows[cnt].style.display="table-row";}
}
if(vl!='All'){
for(var cnt=2;cnt<ln;cnt++){
dt.rows[cnt].style.display="none";
if(dt.rows[cnt].cells[0].innerText==vl){
dt.rows[cnt].style.display="table-row";}
}
}
}

$(function () {
//Initialize Select2 Elements
$('.select2').select2({
placeholder: 'Select an Agent',
ajax: {
url: 'getsupplier.php',
dataType: 'json',
delay: 10,
processResults: function (data) {
return {
results: data
};
},
cache: true
}
})
})

function showUser(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else { 
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("shcust").value = this.responseText;
document.getElementById("datepicker").focus();
}
};
xmlhttp.open("GET","showcustomer.php?q="+str,true);
xmlhttp.send();
}
}

function showitem(str) {
document.getElementById("sales2").focus();
}


</script>



