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

?>
<!doctype html>
<html lang="en">

<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="">Customer</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='inputField' onchange='showitem(this.value)'></td>");
printf("<tr class='cls'><td>S-Calls</td><td><input type='text' id='scall' onkeydown=\"scallskey(event)\"></td></tr>");
printf("<tr class='cls'><td>M-Calls</td><td><input type='text' id='mcall' onkeydown=\"mcallskey(event)\"></td></tr>");
printf("<tr class='cls'><td>P-Calls</td><td><input type='text' id='pcall' onkeydown=\"pcallskey(event)\" required ></td></tr>");

printf("<tr class='cls'><td>Route</td><td><input type='text' id='slroute' onkeydown=\"slrouteskey(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>Bill summary</font></td></tr></table>");
printf("<table style='font-size:13;width:100%%;'   id='dstots'>");
printf("<tr><td>Gross sales</td><td align='right'>%s</td></tr>",number_format(0,2,'.',','));
printf("<tr><td>Bill Discount</td><td align='right'>%s</td></tr>",number_format(0,2,'.',','));
printf("<tr><td>Sales Rtns</td><td align='right'>%s</td></tr>",number_format(0,2,'.',','));
printf("<tr><td>M-Rtns</td><td align='right'>%s</td></tr>",number_format(0,2,'.',','));
printf("<tr style='border-bottom:1px solid #f00;'><td>Net sales</td><td align='right'; font color='red'>%s</td></tr>",number_format(0,2,'.',','));

printf("<tr><td>Rtn-Dis</td><td style='color:#FF5733'; align='right'>%s</td></tr>",number_format(0,2,'.',','));

printf("<tr><td>Mrtn-Dis</td><td style='color:purple'; align='right'>%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>Discount</td><td><input type='text' id='wsdiscs' name='wsdiscs' placeholder='Rs.' onkeydown=\"bdiskey(event)\" onkeyup=\"wsdiscky(event)\" onblur=\"gfcs(this)\"></td></tr>");

printf("<tr class='cls'><td>Srtn Dis:</td><td><input type='text' id='srtndis' name='srtndis' placeholder='Rs.' onkeydown=\"srtskey(event)\" onkeyup=\"srtndiskey(event)\" onblur=\"gfcs(this)\"></td></tr>");

printf("<tr class='cls'><td>Mrtn Dis:</td><td><input type='text' id='mrtndis' name='mrtndis' placeholder='Rs.' onkeydown=\"mrtskey(event)\" onkeyup=\"mrtndiskey(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.' 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.' onkeydown=\"credittdn(event)\" onkeyup=\"creditskey(event)\" onblur=\"gfcs(this)\"></td></tr>");

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

printf("<tr class='cls'><td>*******</td><td><input type='text' id='cheqcollecttt' name='cheqcollectttt' placeholder='*******' disabled onkeydown=\"cqnumrtdn(event)\" onkeyup=\"cheqnumsrkey(event)\" onblur=\"gfcs(this)\"></td></tr>");

printf("<tr class='cls'><td>Cheq Collection</td><td><input type='text' id='cheqcollect' name='cheqcollect' placeholder='Rs.' onkeydown=\"cqnumtdn(event)\" onkeyup=\"cheqnumskey(event)\" onblur=\"gfcs(this)\"></td></tr>");

printf("<tr class='cls'><td>Cash Collection:</td><td><input type='text' id='cashcollect' name='cashcollect' placeholder='Rs.' onkeydown=\"chqdttdn(event)\" onkeyup=\"cheqdtskey(event)\" onblur=\"gfcs(this)\"></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("</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='11' 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=10%% 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='ACTIVE' 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=40%% align='center'  style='border:1px solid white' >Item</td>
<td width=5%% align='center'  style='border:1px solid white' >Price</td>
<td width=5%% align='center'  style='border:1px solid white' >Qty</td>
<td width=2%% align='center' style='border:1px solid white' >Free</td>
<td width=1%% align='center' style='border:1px solid white' >Srtn</td>
<td width=1%% align='center' style='border:1px solid white' >Mrtn</td>
<td width=15%% align='center' style='border:1px solid white' >Gros Sale</td>
<td width=15%% align='center' style='border:1px solid white' >SrtnVal</td>
<td width=15%% align='center' style='border:1px solid white' >MrtnVal</td>
");

$sqlst="select item_ranges,sub_ranges,item_name_mrt,whole_sale_price/rd_uom from itemsmrd where item_status='ACTIVE' 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=5  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=2 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='center'><input type='text' size=2  style='text-align:right;font-size:10;' id='srtns%s' tabindex='0' onkeydown=\"srntdn(event)\" onkeyup=\"stsrtns(event)\"onfocus=\"onfcs(this.parentNode.parentNode.rowIndex)\" onblur=\"outfcs(this)\"></td>

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

<td align='right'></td>
<td align='right'></td>
<td align='right';font color='blue'></td>
</tr>",


$rst[0],ucwords($rst[2]),number_format($rst[3],2,'.',','),$cnt,$cnt,$cnt,$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==39){
pr.preventDefault();
document.getElementById("srtns"+(rw)).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();}
}

}


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 tvll=parseFloat(document.getElementById("dstots").rows[2].cells[1].innerHTML);
var tvlsd=parseFloat(document.getElementById("dstots").rows[5].cells[1].innerHTML);
var tvlsdn=parseFloat(document.getElementById("dstots").rows[3].cells[1].innerHTML);
var tvldnd=parseFloat(document.getElementById("dstots").rows[6].cells[1].innerHTML);

var vl=parseFloat(vl);
document.getElementById("dstots").rows[1].cells[1].innerHTML=(vl).toFixed(2);
document.getElementById("dstots").rows[4].cells[1].innerHTML=((tvl-vl)-(tvll-tvlsd)-(tvlsdn-tvldnd)).toFixed(2);
}

}

function srtndiskey(pr)
{
vlv=pr.target.value;
if(pr.keyCode==13||pr.keyCode==40){
if(isNaN(vlv)){
alert("Check the value");
pr.target.focus();}
}
if(pr.keyCode!=13 && pr.keyCode!=40 && pr.keyCode!=38){
var tvll=parseFloat(document.getElementById("dstots").rows[0].cells[1].innerHTML);
var tvlls=parseFloat(document.getElementById("dstots").rows[1].cells[1].innerHTML);
      var vlv=parseFloat(vlv);
document.getElementById("dstots").rows[5].cells[1].innerHTML=(vlv).toFixed(2);

var tvdf=parseFloat(document.getElementById("dstots").rows[6].cells[1].innerHTML);
var tvd=parseFloat(document.getElementById("dstots").rows[2].cells[1].innerHTML);
var tvds=parseFloat(document.getElementById("dstots").rows[3].cells[1].innerHTML);
document.getElementById("dstots").rows[4].cells[1].innerHTML=((tvll-tvlls)-(tvd-vlv)-(tvds-tvdf)).toFixed(2);
}
}

function mrtndiskey(pr)
{
mrvl=pr.target.value;
if(pr.keyCode==13||pr.keyCode==40){
if(isNaN(mrvl)){
alert("Check the value");
pr.target.focus();}
}
if(pr.keyCode!=13 && pr.keyCode!=40 && pr.keyCode!=38){
var mrvlv=parseFloat(document.getElementById("dstots").rows[0].cells[1].innerHTML);
var mrvl=parseFloat(mrvl);
document.getElementById("dstots").rows[6].cells[1].innerHTML=(mrvl).toFixed(2);

var tvdd=parseFloat(document.getElementById("dstots").rows[1].cells[1].innerHTML);

var tvdx=parseFloat(document.getElementById("dstots").rows[2].cells[1].innerHTML);

var tvdxr=parseFloat(document.getElementById("dstots").rows[5].cells[1].innerHTML);
var tvdxx=parseFloat(document.getElementById("dstots").rows[3].cells[1].innerHTML);
document.getElementById("dstots").rows[4].cells[1].innerHTML=((mrvlv-tvdd)-(tvdx-tvdxr)-(tvdxx-mrvl)).toFixed(2);
}
}

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[4].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);
document.getElementById("agd").rows[7].cells[1].innerHTML=(cshd-vl-cramt-cqamt);
}
}

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[4].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);
document.getElementById("agd").rows[7].cells[1].innerHTML=(cshdg-crdt-cashamt-cqamtt);
}
}

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[4].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);
document.getElementById("agd").rows[7].cells[1].innerHTML=(cshdd-vl-cashamtt-cqamtty);
}
}




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("wsdiscs").focus();}
}

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

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

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

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

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

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

function slrouteskey(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("pcall").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();}
if(keys==38){
document.getElementById("mrtndis").focus();    
}
}

function credittdn(pr)
{
var keys=pr.which || pr.window.event.keyCode;
if(keys==40){
document.getElementById("cheq").focus();
//document.getElementById("cheqnum").disabled = false;    
}
if(keys==13){
document.getElementById("cheq").focus();
//document.getElementById("cheqnum").disabled = false;    
}
if(keys==38){
document.getElementById("cash").focus();
//document.getElementById("cheqnum").disabled = true;    
}
}

function cqtdn(pr)
{
var keys=pr.which || pr.window.event.keyCode;
if(keys==40){
document.getElementById("cheqcollect").focus();
//document.getElementById("cheqnum").disabled = false;    
}
if(keys==13){
document.getElementById("cheqcollect").focus();
//document.getElementById("cheqnum").disabled = false;    
}
if(keys==38){
document.getElementById("credits").focus();
//document.getElementById("cheqnum").disabled = true;
}
}

function cqnumtdn(pr)
{
var keys=pr.which || pr.window.event.keyCode;
if(keys==40){
//document.getElementById("bank").disabled = false;    
document.getElementById("cashcollect").focus();
//document.getElementById("cheqnum").disabled = true;

}
if(keys==13){
//document.getElementById("bank").disabled = false;
document.getElementById("cashcollect").focus();
//document.getElementById("cheqnum").disabled = true;
}
if(keys==38){
document.getElementById("cheq").focus();
//document.getElementById("cheqnum").disabled = true;
}
}

function bnktdn(pr)
{
var keys=pr.which || pr.window.event.keyCode;
if(keys==40){
document.getElementById("cheqnum").disabled = true; 
document.getElementById("bank").disabled = true;
document.getElementById("inputField").disabled = false;
document.getElementById("inputField").focus();
}
if(keys==13){
document.getElementById("cheqnum").disabled = true; 
document.getElementById("bank").disabled = true;
document.getElementById("inputField").disabled = false;
document.getElementById("inputField").focus();
}
if(keys==38){
document.getElementById("cheqnum").disabled = false;    
document.getElementById("cheqnum").focus();
document.getElementById("bank").disabled = true;
}
}

function chqdttdn(pr)
{
var keys=pr.which || pr.window.event.keyCode;
if(keys==38){
document.getElementById("cheqcollect").focus();
}
if(keys==13){
document.getElementById("uploads").focus();
}
}


function srntdn(pr)
{
var keys=pr.which || pr.window.event.keyCode;
var rwrtn=pr.target.parentNode.parentNode.rowIndex;
var nrwrtn=rwrtn+1;
var prwrtn=rwrtn-1;
if(keys==40){
document.getElementById("srtns"+nrwrtn).focus();}
if(keys==38){
document.getElementById("srtns"+prwrtn).focus();}
if(keys==37){
document.getElementById("frees"+rwrtn).focus();}
if(keys==39){
pr.preventDefault();
document.getElementById("mrtns"+(rwrtn)).focus();}
}

function mrtndn(pr)
{
var keys=pr.which || pr.window.event.keyCode;
var rwrtnn=pr.target.parentNode.parentNode.rowIndex;
var nrwrtnn=rwrtnn+1;
var prwrtnn=rwrtnn-1;
if(keys==40){
document.getElementById("mrtns"+nrwrtnn).focus();}
if(keys==38){
document.getElementById("mrtns"+prwrtnn).focus();}
if(keys==37){
document.getElementById("srtns"+(rwrtnn)).focus();}
if(keys==27){
pr.preventDefault();
document.getElementById("wsdiscs").focus();}
}






function stqtys(pr)
{
var x = pr.which || pr.event.keyCode;
if (x == 33 || 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[7].innerHTML=rvl.toFixed(2);
for(cnt=2;cnt<dt.rows.length;cnt++){
var tmpp=parseFloat(dt.rows[cnt].cells[7].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);
var slrtns=parseFloat(document.getElementById("dstots").rows[2].cells[1].innerHTML);
var slmrtn=parseFloat(document.getElementById("dstots").rows[3].cells[1].innerHTML);
var slmrtnd=parseFloat(document.getElementById("dstots").rows[6].cells[1].innerHTML);
var slrtnd=parseFloat(document.getElementById("dstots").rows[5].cells[1].innerHTML);

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

}
}
}

function stsrtns(pr)
{
var x = pr.which || pr.event.keyCode;
if (x == 33 || x == 34) {
document.getElementById("dtfinds").focus();
document.getElementById("dtfinds").value = "";
}
var x = pr.which || pr.event.keyCode;
var keys=pr.which || pr.window.event.keyCode;
var rww=pr.target.parentNode.parentNode.rowIndex;
var dt=document.getElementById("dtd");
var tt=document.getElementById("dstots");
var totss=0;
var tmpp=0;
var discs=0;
var nrww=rww+1;
var prww=rww-1;
pr.target.style.backgroundColor="white";
var vlsrtn=pr.target.value;
if(isNaN(vlsrtn) && vlsrtn!=''){
pr.target.style.backgroundColor="red";}

else{
var rvlm=(vlsrtn*dt.rows[rww].cells[2].innerHTML);
if(rvlm>0){
dt.rows[rww].cells[8].innerHTML=rvlm.toFixed(2);
for(cnt=2;cnt<dt.rows.length;cnt++){
var tmppo=parseFloat(dt.rows[cnt].cells[8].innerHTML);
if(!isNaN(tmppo)){
totss=totss+tmppo;
}}
tt.rows[2].cells[1].innerHTML=totss.toFixed(2);
var gts=tt.rows[2].cells[1].innerHTML=totss.toFixed(2);

var slgros=parseFloat(document.getElementById("dstots").rows[0].cells[1].innerHTML);

var sldis=parseFloat(document.getElementById("dstots").rows[1].cells[1].innerHTML);
var slmrtn=parseFloat(document.getElementById("dstots").rows[3].cells[1].innerHTML);
var slmrtnd=parseFloat(document.getElementById("dstots").rows[6].cells[1].innerHTML);
var slrtnd=parseFloat(document.getElementById("dstots").rows[5].cells[1].innerHTML);

tt.rows[4].cells[1].innerHTML=((slgros-sldis)-(gts-slrtnd)-(slmrtn-slmrtnd)).toFixed(2);
}
else{
dt.rows[rww].cells[8].innerHTML="";}
if(keys==13){
document.getElementById("srtns"+nrww).focus();
}
}
}

function mrtns(pr)
{
var x = pr.which || pr.event.keyCode;
if (x == 33 || x == 34) {
document.getElementById("dtfinds").focus();
document.getElementById("dtfinds").value = "";
}
var x = pr.which || pr.event.keyCode;
var keys=pr.which || pr.window.event.keyCode;
var rwwx=pr.target.parentNode.parentNode.rowIndex;
var dt=document.getElementById("dtd");
var tt=document.getElementById("dstots");
var tots=0;
var totss=0;
var tmpp=0;
var discs=0;
var nrwwt=rwwx+1;
var prwwt=rwwx-1;
pr.target.style.backgroundColor="white";
var vlsrtn=pr.target.value;
if(isNaN(vlsrtn) && vlsrtn!=''){
pr.target.style.backgroundColor="red";}

else{
var rvlm=(vlsrtn*dt.rows[rwwx].cells[2].innerHTML);
if(rvlm>0){
dt.rows[rwwx].cells[9].innerHTML=rvlm.toFixed(2);
for(cnt=2;cnt<dt.rows.length;cnt++){
var tmppo=parseFloat(dt.rows[cnt].cells[9].innerHTML);
if(!isNaN(tmppo)){
tots=tots+tmppo;
}}
tt.rows[3].cells[1].innerHTML=tots.toFixed(2);
var gtss=tt.rows[3].cells[1].innerHTML=tots.toFixed(2);

var slgs=parseFloat(document.getElementById("dstots").rows[0].cells[1].innerHTML);

var sld=parseFloat(document.getElementById("dstots").rows[1].cells[1].innerHTML);
var slmr=parseFloat(document.getElementById("dstots").rows[2].cells[1].innerHTML);
var slmrd=parseFloat(document.getElementById("dstots").rows[6].cells[1].innerHTML);
var slrd=parseFloat(document.getElementById("dstots").rows[5].cells[1].innerHTML);

tt.rows[4].cells[1].innerHTML=((slgs-sld)-(gtss-slmrd)-(slmr-slrd)).toFixed(2);
}
else{
dt.rows[rwwx].cells[9].innerHTML="";}
if(keys==13){
document.getElementById("mrtns"+nrwwt).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("inputField");
if(dts.value==""){
swal('Remind!', 'Date Field required', 'warning');
return false;
}


var dtts=document.getElementById("scall").value;
if (dtts == ""){
swal('Remind!', 'Sheduled Call Field required', 'warning');
return false;
}

var mcalls=document.getElementById("mcall").value;
if (mcalls == ""){
swal('Remind!', 'Call Made Field required', 'warning');
return false;
}

var pcalls=document.getElementById("pcall").value;
if (pcalls == ""){
swal('Remind!', 'Productive Call Field required', 'warning');
return false;
}


var invtotck=parseFloat(document.getElementById("dstots").rows[4].cells[1].innerHTML);
var cashtot=parseFloat(document.getElementById("agd").rows[8].cells[1].innerHTML);
var credittot=parseFloat(document.getElementById("agd").rows[9].cells[1].innerHTML);
var chqtot=parseFloat(document.getElementById("agd").rows[10].cells[1].innerHTML);
if (invtotck!=(cashtot+credittot+chqtot) && invtotck>0){
swal('Remind!', 'Please Update Payment Method', 'warning');
return false;
}

else{
swal('Database ++!', '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;
var srtns=0;
var mrtns=0;

str="actp=rduploads&rddates="+document.getElementById("inputField").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[4].cells[1].innerHTML;
str=str+"&invsrtn="+tt.rows[2].cells[1].innerHTML;
str=str+"&invmrtn="+tt.rows[3].cells[1].innerHTML;
str=str+"&srtndis="+tt.rows[5].cells[1].innerHTML;
str=str+"&mrtndis="+tt.rows[6].cells[1].innerHTML;
str=str+"&cash="+document.getElementById("cash").value;
str=str+"&credit="+document.getElementById("credits").value;
str=str+"&cheq="+document.getElementById("cheq").value;
str=str+"&cheqcollect="+document.getElementById("cheqcollect").value;
str=str+"&cashcollect="+document.getElementById("cashcollect").value;
str=str+"&shedulecl="+document.getElementById("scall").value;
str=str+"&madecl="+document.getElementById("mcall").value;
str=str+"&prodcal="+document.getElementById("pcall").value;
str=str+"&slroute="+document.getElementById("slroute").value;

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

if(qtys!=0 || frees!=0 || srtns!=0 || mrtns!=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;}

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

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

str=str+"&qtys"+cn+"="+qtys;
str=str+"&frees"+cn+"="+frees;
str=str+"&srtns"+cn+"="+srtns;
str=str+"&mrtns"+cn+"="+mrtns;
cn++;
}
}
str=str+"&rws="+cn;
dt.style.display="none";
tt.style.display="none";

<?php
$conn=mysqli_connect("localhost","primarysales","S5L7lF01EQZvQrGK","primarysales_dbm");
$sqlst="select max(invoice_number) from invoice_counter";
$qry=mysqli_query($conn,$sqlst);
$rstm=mysqli_fetch_row($qry);
if(mysqli_num_rows($qry)==0){
$invm=date('y')."0001";}
else{
if(intval($rstm[0],10)==0){
$invm=date('y')."0001";}
else{
$invm=intval($rstm[0],10)+1;}
}
?>

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="<br><a href='invoice.php?>'class='btn btn-success'>ADD INVOICE</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='invoice.php?'class='btn btn-success'>ADD INVOICE</a><br><br><a href='salestype.php?'class='btn btn-info'>VIEW INVOICE</a><br>";

}

}}}
httpr.open("POST","rdsalesuploadsm.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 Customer',
ajax: {
url: 'getcustomer.php',
dataType: 'json',
delay: 150,
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();
}
}

</script>



