<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
    print "<script> alert('Session timed out'); parent.document.location='ind.php'; </script>";
    return;
}
include("../opendb.php");
include("../test.php");
$jvno=$_REQUEST['jvno'];
$dt=$_REQUEST['dt'];
$df=$_REQUEST['df'];
$dto=$_REQUEST['dto'];

if(($user=='mathew')||($user=='sreejith')||($user=='shameem')||($user=='minosh')||($user=='thomas')||($user=='vibin')||($user=='latheef' && $brcode == 'PSN638') ||($brcode=='PSN841') )
{
?>
<style>
body,td{font-family:'arial narrow';letter-spacing:1;}
th{background-color:c1d799}
.mnu{margin-left:5}
body{BACKGROUND-COLOR:fcfff1}
</style>
<script type="text/javascript">
var tq=0,tq2=0;
var cur; //=1;
var vv;
var drs=[];
function setval(a)
{
 vv=a.name;
}
function insRow()
{ 
nar=''
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="<select name=dr[] onClick=setval(this)>";

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>"
z1.innerHTML="<input type=text id='dd"+cur+"' name=dr_narr[] size=30 value="+nar+">"

ag.innerHTML="<select name='dragain[]'><option>OFNO</option><option>DA</option><option> ON Acc </option> <option>CHEQ</option></select>";
agval.innerHTML="<input type=text name='dragval[]'>";
//alert(cur);
cur++; 

}
function delRow(x1)
{ 
 var num=x1.name;
 if(cur>=1)
  {
    var x=document.getElementById('myTable').deleteRow(cur-1)
  //  var x=document.getElementById('myTable').deleteRow(cur)
    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;
}

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

//-------*888888888888888888888888*********************8888
var cur2=0;
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+1)
var value="<select name=cr[] onClick=setval(this)>";
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=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[]'><option>OFNO</option><option>DA</option><option> ON Acc </option> <option>CHEQ</option></select>";
cragval1.innerHTML="<input type=text name='cragval[]'>";

cur2++;
}
function delRow2(x1)
{
 var num=x1.name;
 if(cur2>0)
 {
   var x=document.getElementById('myTable2').deleteRow(cur2-1)
   cur2--;
 }
}

function valid()
{
    calccr();
    calcdr();
    if(document.f1.tdamt.value==document.f1.tcamt.value)
        return true;
    else
        {alert('Dr, Cr Amount mis-match'); return false;}
}
</script>
<font color=red> Journal No: <?print $jvno?></font>
<form name=f1 action='updjour.php' method='post' onSubmit="return valid()">
<input type=hidden name='jvno' value='<?print $jvno?>'>
<input type=hidden name='dt' value='<?print $dt?>'>
<input type=hidden name='df' value='<?print $df?>'>
<input type=hidden name='dto' value='<?print $dto?>'>

Debit <input type="button" onclick="insRow()" value=" + ">
<input type='button' onclick='delRow(this)' value=' - '>
<input type='button' onclick='calcdr()' value=' CALCULATE DEBIT SUM  '>
<br>&nbsp;
<table id="myTable" border="1">
<?
$req=mysqli_query($db,"select * from ledger where brcode='$brcode' and grp!='Cash in hand'  and brcode='$brcode' order by led");
$i=0;
while($rowq=mysqli_fetch_row($req))
{
 print "\n<script>drs[$i]='$rowq[0]' </script>";
 $i++;
}

$re2=mysqli_query($db,"select * from journal2 where jvno='$jvno' and type='Dr' and brcode='$brcode'") or die(mysqli_error());
print "<script> cur=".mysqli_num_rows($re2)."; </script>";
 print "<tr><td>Date </td><td colspan=5><input type=text name='dt' value='".implode('-', array_reverse(explode('-', $dt)))."'></td></tr>";
 print "<th>Type <th>Particulars <th> Amount <th>Narr<th>Ref <th>No";
while($row=mysqli_fetch_array($re2))
{
 print "<tr><td><select name='type[]'><option>Dr</option><option>Cr</option></select>";
 print "<td><select name='dr[]'><option>$row[partic]</option>";
$re=mysqli_query($db,"select * from ledger where brcode='$brcode' and grp!='Cash in hand'  and brcode='$brcode' order by led");
$i=0;
while($row1=mysqli_fetch_array($re))
{
  print "\n<option>$row1[led]</option>";
}
 print "</select></td><td><input type=text name='dr_amt[]' value='$row[dramt]' size=12></td>";
 print "<td><input type=text name='dr_narr[]' value='$row[narr]' size=18></td>";
 print "<td><select name='dragain[]'><option>$row[against]</option><option>OFNO</option><option>DA</option><option> ON Acc </option> <option>CHEQ</option></select></td>";  
 print "<td><input type=text name='dragval[]' value='$row[no]' size=10></td>";
}
print "</tr></table>";
?>
<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">
<?
$re2=mysqli_query($db,"select * from journal2 where jvno='$jvno' and type='Cr' and brcode='$brcode'") or die(mysqli_error());
while($row=mysqli_fetch_array($re2))
{
 print "<tr><td><select name='type[]'><option>Cr</option><option>Dr</option></select>";
 print "<td><select name='cr[]'><option>$row[partic]</option>";
$re=mysqli_query($db,"select * from ledger where brcode='$brcode' and grp!='Cash in hand'  order by led");
$i=0;
while($row1=mysqli_fetch_array($re))
{
  print "\n<option>$row1[led]</option>";
}
 print "</select></td>";
 print "\n<td><input type=text name='cr_amt[]' value='$row[cramt]' size=12></td>";
 print " <td><input type=text name='cr_narr[]' value='$row[narr]' size=18></td>";

 print "\n<td><select name='cragain[]'><option>$row[against]</option><option>OFNO</option><option>DA</option><option> ON Acc </option> <option>CHEQ</option></select></td>";  
 print "\n<td><input type=text name='cragval[]' value='$row[no]' size=10></td>";
}
print "</tr></table>";
?>
<p style={background-color:silver}>
<input type=submit value='UPDATE'>
</P>
</form>
<?
}
?>
