<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
 	print "<script> alert('Session timed out'); parent.document.location='../../index.php'; </script>";
 	return;
}
include("../../opendb.php");
$rs=mysqli_query($db,"select max(bpno) from bnkpay where brcode='$brcode'");
$cr_row=mysqli_fetch_row($rs);
if($cr_row[0]<=0)
   	$bpno=1;
else   
   	$bpno=$cr_row[0]+1;
?>
<link type="text/css" href="../../css/form.css" rel="stylesheet" />
<link type="text/css" href="../../jquery/jquery-ui.min.css" rel="stylesheet" />
<script type="text/javascript" src="../../js/jquery.js"></script>
<script type="text/javascript" src="../../jquery/jquery-1.7.min.js"></script>
<script type="text/javascript" src="../../jquery/jquery-ui.min.js"></script>
<script type="text/javascript" src="../../jquery/jquery.jqEasyCharCounter.min.js"></script>

<style>
body,td{font-family:'arial narrow';letter-spacing:1;}
th{background-color:c1d799}
.mnu{margin-left:5}
body{BACKGROUND-COLOR:fcfff1}
</style>
<html>
<head>
<title>ZF</title>
<script type="text/javascript" src="../../scripts/jquery-latest.js"></script>
<link type="text/css" href="../../includes/style1.css" rel="stylesheet" />
<link type="text/css" href="../../css/form.css" rel="stylesheet" />
<script type="text/javascript">
$(function() {
          if ($.browser.msie && $.browser.version.substr(0,1)<7)
          {
			$('li').has('ul').mouseover(function(){
				$(this).children('ul').show();
				}).mouseout(function(){
				$(this).children('ul').hide();
				})
          }
        });        
    </script>

<script type="text/javascript">

$(function(){
$(".dr").live("keydown.autocomplete",function(){
$dr = $(this);
$row = $dr.parent().parent();
$(this).autocomplete({
		source: function(request, response ) {

		$.ajax({
			url: "../../payrec/searchdbtor.php",
			dataType: "json",
			data: "key="+$dr.val(),
			success: function( data ) {
				response( $.map( data, function( item ) {


					return {
						label: '<span class="la-name">'+item.dr+' </span><span class="la-code">'+  item.drcbal + '</span>'    ,
						value: item.dr,
						name:item.drcbal,
						tax:item.tax,

						stock:item.stock,
						rate:item.rate,
						reduct:item.reduct,


					}
				}));
			}
		});
	}

,
	minLength: 2,
	select: function( event, ui ) {
	$("#payee").val(ui.item.value);

        	$row.find('.cbal').val(ui.item.name);
	$row.find('.stok').val(ui.item.stock);
	$row.find('.unitprice').val(ui.item.rate);

	$row.find('.tax').val(ui.item.tax);
	$row.find('.itmvl').val(ui.item.reduct);
	$row.find('.cr_amt').focus();
	
	 }

	}).data("ui-autocomplete")._renderItem = function (ul, item) {
         return $("<li></li>")
             .data("item.autocomplete", item)
             .append( item.label )
             .appendTo(ul);
     };


});

});
</script>

<script type="text/javascript">
var tq=0,tq2=0;
var cur=1;
var vv;
var drs=[];
var crs=[];
var chk=[];
function setval(a)
{
 	vv=a.name;
	var ele1='payee';
	document.f1.payee.value = a;
}

function setval1(a)
{
   	var x= document.getElementById(a).selectedIndex ; 
	var pp=chk[2];
	var x1=parseFloat(chk[x])+1;
   	document.f1.chq_no.value=x1;
	//var ele1='chq_no';
<?
	/*$mbank = $cr[0];
	$re=mysqli_query($db,"select chq_no from chq_det where brcode='$brcode' and bank = '$mbank' order by chq_dt desc limit 1") or die(mysqli_error());
	$row12=mysqli_fetch_row($re);
	$mm="select chq_no from chq_det where brcode='$brcode' and bank = 'SBI' order by chq_dt desc limit 1";*/
?>
//	document.f1.chq_no.value = '<? print $row12[0]+1?>';	
}

function insRow()
{
	var x=document.getElementById('myTable').insertRow(cur)
	if(cur>1)
		 var nar=document.getElementById('dd1').value;
	var a=x.insertCell(0)
	var y=x.insertCell(1)
	var z=x.insertCell(2)	
	var z1=x.insertCell(3)	
	var ag=x.insertCell(4)
	var agval=x.insertCell(5)
	a.innerHTML=cur
	var value="<input type=text name=dr[] id='dr"+cur+"' class=dr style='width: 350px;'>";
	//for(k=0;k<drs.length;k++)
		///value=value+"<option>"+drs[k]+"</option>";
	//value=value+"</select>";
	y.innerHTML=value
	z.innerHTML="<input type=text name=dr_amt[] id=dr"+cur+" size=10 style={text-align:right}>"
	z1.innerHTML="<input type=text id='dd"+cur+"' name=dr_narr[] size=30 value="+nar+">"
	ag.innerHTML="<select name='dragain[]' onChange=getda(this,cur,'dr')><option>CHEQ</option> <option>DA</option></select>";
	agval.innerHTML="<input type=text id='dragval"+cur+"'name='dragval[]'>";
	//alert(cur);
	cur++; 
}
function delRow(x1)
{
 	var num=x1.name;
 	if(cur>1)
  	{
    		var x=document.getElementById('myTable').deleteRow(cur-1)
		cur--;
	}
}

//************************************
function calcdr()
{
	var tq=0;
	for(i=1;i<cur;i++)
	{
		 var ele='dr'+i; //alert(ele);
		 if(document.getElementById(ele).value>0)
		 {
			amount=document.getElementById(ele).value;
			tq+=parseFloat(amount);
		  }
	 }
	document.f1.tdamt.value=tq.toFixed(2);
}
function calccr()
{
	var tq2=0;
	for(i=1;i<cur2;i++)
	{
		 var ele2='cr'+i; //alert(ele);
		 if(document.getElementById(ele2).value>0)
		  {
			   amount=document.getElementById(ele2).value;
			   tq2+=parseFloat(amount);
		  }
	 }
	document.f1.tcamt.value=tq2.toFixed(2);
}

//-------*888888888888888888888888*********************8888
var cur2=1;
var cr="cr";
function insRow2()
{
	var x=document.getElementById('myTable2').insertRow(cur2)
	if(cur2>1)
		 var nar=document.getElementById('d2d1').value;
	var a=x.insertCell(0)
	var y=x.insertCell(1)
	var z=x.insertCell(2)	
	var z1=x.insertCell(3)	
	var crag=x.insertCell(4)
	var cragval1=x.insertCell(5)
	a.innerHTML=cur2;
	var value="<select name=cr[] id='bnk"+cur2+"' onChange=setval1(this.id)><option value='0' >select</option>";
	for(k=0;k<crs.length;k++)
		value=value+"<option>"+crs[k]+"</option>";
	value=value+"</select>";
	y.innerHTML=value
	var nar=document.f1.dd1.value;
	z.innerHTML="<input type=text name=cr_amt[] id=cr"+cur2+" size=10>"
	z1.innerHTML="<input type=text id='d2d"+cur2+"' name=cr_narr[] size=30 value='"+nar+"'>"
	crag.innerHTML="<select name='cragain[]' onChange=getda(this,cur2,cr)><option>CHEQ</option> <option>DA</option></select>";
	cragval1.innerHTML="<input type=text id='cragval"+cur2+"' name='cragval[]' value='"+document.f1.dragval1.value+"'>";
	cur2++;
}
function delRow2(x1)
{
 	var num=x1.name;
 	if(cur2>1)
 	{
   		var x=document.getElementById('myTable2').deleteRow(cur2-1)
	  	cur2--;
 	}
}
function valid()
{
 	calccr();
 	calcdr();
	var d1=parseFoloat(document.f1.d.value);
	var m1=parseFoloat(document.f1.m.value);
 	var y1=parseFoloat(document.f1.y.value);
 	if(d1<=0||m1<=0||y1<=0)
 	{
  		alert('Invalid Date');
  		return false;
 	}
 	if(document.f1.tdamt.value==document.f1.tcamt.value)
   		return true;
 	else
   		{alert('Dr, Cr Amount mis-match'); return false;}
}


function getda(x,y,drcr)
{
	if(x.selectedIndex==1)
 		window.open('getda.php?no='+y+'&drcr='+drcr);
}
</script>
</head>
<body background="../../back_ground.jpg">
<?php include('logo1.php'); ?>
<?php include('top.php'); ?>
<br>
<table class="container"><tr><td>
<font style="font-family: 'Trebuchet MS',Verdana,Helvetica,sans-serif;">  &nbsp;&nbsp;Accounts >> Bank Payment</font>
<center><h3>Bank Payment Number:<?print $bpno?></h3></center>
<form name=f1 action='svbnk_pay_1.php' method='post' onSubmit="return valid()">
Debit <input type="button" onclick="insRow()" value=" + ">
<input type='button' onclick='delRow(this)' value=' - '>
<input type='button' onclick='calcdr()' value=' CALCULATE DEBIT SUM  '>
<p align=center style={background-color:silver}>

Date:dd <input type=text name='d' size='2' value='<?print date("d",time());?>'>
mm <input type=text name='m' size='2' value='<?print date("m",time());?>'>
yy <input type=text name='y' size='4' value='<?print date("Y",time());?>'>
</p>
<table id="myTable" border="1" class='report'>
<th>No.<th>Dr <th>Amt <th>Narr<th>Against <th>Cheq. No.
<tr>
<!--
<td><input type=text name=dr[] onClick=setval()></td>
<td><input type=text name=dr_amt[]></td>
-->
</tr>
</table><br>
Total Debit Amount <input type=text readonly name='tdamt'>
<hr>

Credit <input type="button" onclick="insRow2()" value=" + ">
<input type='button' onclick='delRow2(this)' value=' - '>
<input type='button' onclick='calccr()' value=' CALCULATE CREDIT SUM  '>

<br>&nbsp;

<table id="myTable2" border="1" class='report'>
<th>No.<th>Cr <th>Amt <th>Narr<th>Against <th>Value
</table>
Cheque No. <input type=text name='chq_no'>
Payee <input type=text name='payee' id='payee' size='80'>
<br>
Total Credit Amount <input type=text readonly name='tcamt'>
<br>
<Hr><input type=submit value=' SAVE BANK PAYMENT '>
</form>
</div>
</body>
<?
/*
$re=mysqli_query($db,"select * from ledger where brcode='$brcode' order by led") or die(mysqli_error());
$i=0;

while($row=mysqli_fetch_row($re))
{
 	print "\n<script>drs[$i]='$row[0]' </script>";
	$i++;
}
*/
$re=mysqli_query($db,"select * from ledger where (grp='Bank accounts' or grp='Bank O/d') and brcode='$brcode' order by led") or die(mysqli_error());
$i=0;
$j=1;
while($row=mysqli_fetch_row($re))
{
	$re1=mysqli_query($db,"select chq_no from chq_det where brcode='$brcode' and bank = '$row[0]' order by chq_dt desc limit 1") or die(mysqli_error());
	//echo "select chq_no from chq_det where brcode='$brcode' and bank = '$row[0]' order by chq_dt desc limit 1";
	$row12=mysqli_fetch_row($re1);
 	print "\n<script>crs[$i]='$row[0]' </script>";
       print "\n<script>chk[$j]='$row12[0]' </script>";
 	$i++;
       $j++;
}
?>
</html>
