<?
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("../test.php");
include("../opendb.php");
$us=mysqli_query($db,"select level from bruser where username='$user'")or die(mysqli_error());
$r=mysqli_fetch_row($us);
$user_level=$r[0];
$y=$_REQUEST['y'];
$m=$_REQUEST['m'];
$d=$_REQUEST['d'];
$dt=$y.'-'.$m.'-'.$d;
if($user_level<=3)
{
$sq="select to_days(curdate())-to_days('$dt')";
$dtre=mysqli_query($db,$sq)or die(mysqli_error());
$dtrow=mysqli_fetch_row($dtre);

if($dtrow[0]>2 || $dtrow[0]<0)
{
 print "<script> alert('Invalid Date, Entry possible for last 2 days only'); history.back(1); </script>";
 return;
}
}
$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);



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

foreach($dr as $a)
{
 $drs[$i]=$a; 
 $i++;
}


for($x=0;$x<$i;$x++)
{
  for($y=0;$y<$x;$y++)
     { 
         if($drs[$x]==$drs[$y])
           print "<script>alert('Duplicate entry - $drs[$x]'); history.back(1) </script>";
     }
}


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


for($x=0;$x<$i;$x++)
{
  for($y=0;$y<$x;$y++)
     { 
         if($crs[$x]==$crs[$y])
           print "<script>alert('Duplicate entry - $crs[$x]'); history.back(1) </script>";
     }
}


for($x=0;$x<$i;$x++)
{
  for($y=0;$y<$i;$y++)
     { 
        if($drs[$x]==$crs[$y])
          print "<script> alert('Invalid Entry - $drs[$x]'); history.back(1) </script>";
     }
}

foreach($dr_amt as $a)
{ 
 if($a<=0)
 {
           print "<script> alert('Invalid Amount - $a'); history.back(1) </script>";
 }
}

foreach($cr_amt as $a)
{ 
 if($a<=0 )
 {
           print "<script> alert('Invalid Amount - $a'); history.back(1) </script>";
 }
}
*/
//***********************
$i=0;
$dr=$_REQUEST['dr'];
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;
$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++;
}
echo $i;
$rs=mysqli_query($db,"select max(brno) from bnkrec where brcode='$brcode'");
$cr_row=mysqli_fetch_row($rs);
 if($cr_row[0]<=0)
   $brno=1;
 else   
   $brno=$cr_row[0]+1;
   
   
   
$rsdn=mysqli_query($db,"select max(brno) from psnindia_psndbdn.bnkrec where brcode='$brcode'");
$cr_rowdn=mysqli_fetch_row($rsdn);
if($cr_rowdn[0]<=0)
{
   $crnodn=1;
}
 else  
 {
   $crnodn=$cr_rowdn[0]+1;
 }
$resdn=mysqli_query($db,"select max(jvno) from psnindia_psndbdn.journal2 where brcode='$brcode'");
 $rowdn=mysqli_fetch_row($resdn);
 if($rowdn[0]<=0)
 {
    $jvnodn=1;
 }
 else
 {
    $jvnodn=($rowdn[0]+1);
 }   
   
   
   
   
   
   
   
   
   

for($j=0;$j<$i;$j++)
{
 $sql="insert into journal2 values('$dt','$jvno','Dr','$dr[$j]','$dramt[$j]','0','$drnarr[$j].(BR:$brno)','$brcode','BR','$dr_again[$j]','$dr_agval[$j]','$brno')";
 mysqli_query($db,$sql)or die(mysqli_error());

 $sql="insert into bnkrec values('$dt','$brno','Dr','$dr[$j]','$dramt[$j]','0','$drnarr[$j].(JV:$jvno)','$brcode','$jvno','$dr_again[$j]','$dr_agval[$j]','0000-00-00')";
 mysqli_query($db,$sql)or die(mysqli_error());


$sqldn="insert into psnindia_psndbdn.journal2 values('$dt','$jvnodn','Dr','$dr[$j]','$dramt[$j]','0','$drnarr[$j].(BR:$crnodn)','$brcode','BR','$dr_again[$j]','$dr_agval[$j]','$crnodn')";
 mysqli_query($db,$sqldn)or die(mysqli_error());

 $sqldn1="insert into psnindia_psndbdn.bnkrec values('$dt','$crnodn','Dr','$dr[$j]','$dramt[$j]','0','$drnarr[$j].(JV:$jvnodn)','$brcode','$jvnodn','$dr_again[$j]','$dr_agval[$j]','0000-00-00')";
 mysqli_query($db,$sqldn1)or die(mysqli_error());
}

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

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

$i=0;
$cragain=$_REQUEST['cragain'];
foreach($cragain as $a)
{
 $cr_again[$i]=$a;$i++;
}
$i=0;
$cragval=$_REQUEST['cragval'];
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].(BR:$brno)','$brcode','BR','$cr_again[$j]','$cr_agval[$j]','$brno')";
mysqli_query($db,$sql) or die(mysqli_error());

 $sql="insert into bnkrec values('$dt','$brno','Cr','$cr[$j]','0','$cramt[$j]','$crnarr[$j].(JV:$jvno)','$brcode','$jvno','$cr_again[$j]','$cr_agval[$j]','0000-00-00')";
mysqli_query($db,$sql) or die(mysqli_error());



$sqldn2="insert into psnindia_psndbdn.journal2 values('$dt','$jvnodn','Cr','$cr[$j]','0','$cramt[$j]','$crnarr[$j].(BR:$crnodn)','$brcode','BR','$cr_again[$j]','$cr_agval[$j]','$crnodn')";
mysqli_query($db,$sqldn2) or die(mysqli_error());

 $sqldn3="insert into psnindia_psndbdn.bnkrec values('$dt','$crnodn','Cr','$cr[$j]','0','$cramt[$j]','$crnarr[$j].(JV:$jvnodn)','$brcode','$jvnodn','$cr_again[$j]','$cr_agval[$j]','0000-00-00')";
mysqli_query($db,$sqldn3) or die(mysqli_error());





}
$ip_details = "$jvno|BR|Add";
log_entry($brcode,$user,$ip_details,"Bank Receipt JVNo: $jvno, CR No:$brno",$db);
print "<script> window.open('prnbnkrec.php?no=$brno')' </script>";
print "<font color=green>Data has been posted</font><br>";
include("brec.php");
?>
