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

include("../../opendb.php");
include("../../test.php");
$us=mysqli_query($db,"select level from bruser where username='$user'")or die(mysqli_error($db));
$r=mysqli_fetch_row($us);
$user_level=$r[0];

$dt=$y.'-'.$m.'-'.$d;
if($user_level<=4)
{
$sq="select to_days(curdate())-to_days('$dt')";
$dtre=mysqli_query($db,$sq)or die(mysqli_error($db));
$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;
}
}

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

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;
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]=addslashes($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++;
}

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


for($j=0;$j<$i;$j++)
{
 $sql="insert into journal2 values('$dt','$jvno','Dr','$dr[$j]','$dramt[$j]','0','".addslashes(htmlspecialchars($drnarr[$j]))."','$brcode','JV','$dr_again[$j]','$dr_agval[$j]','$jvno')";
 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]=addslashes($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]','".addslashes(htmlspecialchars($crnarr[$j]))."','$brcode','JV','$cr_again[$j]','$cr_agval[$j]','$jvno')";
mysqli_query($db,$sql) or die(mysqli_error($db));
}
log_entry($brcode,$user,$REMOTE_ADDR,"Journal Entry - $jvno");
$rr=mysqli_query($db,"select sum(dramt)-sum(cramt) from journal2 where brcode='$brcode' and jvno='$jvno'");
$rrrow=mysqli_fetch_row($rr);
if($rrrow[0]!=0)
{
 print "<script> alert('Debit, Credit mismatch of $rrrow[0], pls delete this entry')</script>";
 print "<font color=red>Data has been posted with error</font><br>";
}
else
print "<font color=green>Data has been posted</font><br>";
log_entry($brcode,$user,$REMOTE_ADDR,"Journal JVNo: $jvno, jvNo: $jvno");
print "<script> window.open('prnvoucher.php?jvno=$jvno'); </script>";

include("jour.php");
?>
