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

include("../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);


$dt=$y.'-'.$m.'-'.$d;
$i=0;
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;
foreach($dr_amt as $a)
{
 $dramt[$i]=$a; 
 $i++;
}
$i=0;
foreach($dr_narr as $a)
{
 $drnarr[$i]=$a; 
 $i++;
}
$i=0;
foreach($ag as $a)
{
 $again[$i]=$a;$i++;
}
$i=0;
foreach($dragain as $a)
{
 $dr_again[$i]=$a;$i++;
}
$i=0;
foreach($dragval as $a)
{
 $dr_agval[$i]=$a;$i++;
}


for($j=0;$j<$i;$j++)
{
 $sql="insert into journal2 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());

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

//***********************
$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 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());

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

}
print "<font color=green>Data has been posted</font><br>";
print "<script> window.open('prndbnote.php?dbno=$dbno'); </script>";
include("dbnote.php");
?>
