<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='inde.php'; </script>";
 return;
}

include_once("opendb.php");

$res=mysqli_query($db,"select max(jvno) from journal2 where brcode='$brcode'");
 $row=mysqli_fetch_row($res);
 if($row[0]<=0)
  $jvno=1;
 else
  $jvno=($row[0]+1);

$res=mysqli_query($db,"select max(dbno) from dbnote where brcode='$brcode'");
 $row=mysqli_fetch_row($res);
 if($row[0]<=0)
  $dbno=1;
 else
  $dbno=($row[0]+1);

$d=$_REQUEST['d'];
$m=$_REQUEST['m'];
$y=$_REQUEST['y'];
$dt=$y.'-'.$m.'-'.$d;
$i=0;
$dr=$_REQUEST['dr'];
$cr=$_REQUEST['cr'];
if(count($dr)<=0)
{
 print "<script> alert('Invalid Entry - Debit'); history.back(1); </script>";
 return;
}
if( count($cr)<=0)
{
 print "<script> alert('Invalid Entry - Credit'); history.back(1); </script>";
 return;
}

//***********************
$i=0;
foreach($dr as $a)
{
 $dr[$i]=$a; 
 $i++;
}

$i=0;
$dr_amt=$_REQUEST['dr_amt'];
foreach($dr_amt as $a)
{
 $dramt[$i]=$a; 
 $i++;
}
$i=0;
$dr_narr=$_REQUEST['dr_narr'];
foreach($dr_narr as $a)
{
 $drnarr[$i]=$a; 
 $i++;
}
$i=0;
$ag=$_REQUEST['ag'];
foreach($ag as $a)
{
 $again[$i]=$a;$i++;
}
$i=0;
$dragain=$_REQUEST['dragain'];
foreach($dragain as $a)
{
 $dr_again[$i]=$a;$i++;
}
$i=0;
$dragval=$_REQUEST['dragval'];
foreach($dragval as $a)
{
 $dr_agval[$i]=$a;$i++;
}


for($j=0;$j<$i;$j++)
{
 $sql="insert into journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$dt','$jvno','Dr','$dr[$j]','$dramt[$j]','0','$drnarr[$j] .(DB No.$dbno)','$brcode','DB Note','$dr_again[$j]','$dr_agval[$j]','$dbno')";
 mysqli_query($db,$sql)or die(mysqli_error($db));

 $sql="insert into dbnote(date,dbno,type,partic,dramt,cramt,narr,brcode,no,against,dbto) values('$dt','$dbno','Dr','$dr[$j]','$dramt[$j]','0','$drnarr[$j]' ,'$brcode','$jvno','$dr_again[$j]','$dr_agval[$j]')";
 mysqli_query($db,$sql)or die(mysqli_error($db));
}

//***********************
$i=0;
foreach($cr as $a)
{
 $cr[$i]=$a; 
 $i++;
}

$i=0;
foreach($cr_amt as $a)
{
 $cramt[$i]=$a; 
 $i++;
}
$i=0;
foreach($cr_narr as $a)
{
 $crnarr[$i]=$a; 
 $i++;
}

$i=0;
foreach($cragain as $a)
{
 $cr_again[$i]=$a;$i++;
}
$i=0;
foreach($cragval as $a)
{
 $cr_agval[$i]=$a;$i++;
}

for($j=0;$j<$i;$j++)
{
 $sql="insert into journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$dt','$jvno','Cr','$cr[$j]','0','$cramt[$j]','$crnarr[$j] .(DB No.$dbno)','$brcode','DB Note','$cr_again[$j]','$cr_agval[$j]','$dbno')";
mysqli_query($db,$sql) or die(mysqli_error($db));

 $sql="insert into dbnote(date,dbno,type,partic,dramt,cramt,narr,brcode,no,against,dbto) values('$dt','$dbno','Cr','$cr[$j]','0','$cramt[$j]','$crnarr[$j]','$brcode','$jvno','$cr_again[$j]','$cr_agval[$j]')";
mysqli_query($db,$sql) or die(mysqli_error($db));

}
print "<font color=green>Data has been posted</font><br>";
include("dbnote.php");
?>
