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

if($dr==$cr)
{
 print "<script> alert('Insufficient/Invalid Enry(by,to)'); history.back(1); </script>";
 return;
}

if(strlen($amt)<=0||!is_numeric($amt)||$amt<=0)
{
 print "<script> alert('Insufficient/Invalid Amount'); history.back(1); </script>";
 return;
}
if(!checkdate($m,$d,$y))
{
 print "<script>alert('Invalid Date'); history.back(1);</script>";
 return;
}


include("opendb.php");
$dt=$y.'-'.$m.'-'.$d;

$dtcheck1=mysqli_query($db,'select acfrom,acto from acperiod') or die(mysqli_error());
$dtrow=mysqli_fetch_array($dtcheck1);
 $sssq="select to_days('$dt')-to_days('$dtrow[0]'),to_days('$dtrow[1]')-to_days('$dt')";

$dtcheck2=mysqli_query($db,$sssq) or die(mysqli_error());
 $dtrow2=mysqli_fetch_row($dtcheck2) or die(mysqli_error());

 if($dtrow2[0]<0 || $dtrow2[1]<0)
 {
   print "<script>alert('Invalid date, Check a/c Period'); history.back(1);</script>";
   return;
 }


$nr=$narr;
$sql="insert into journal values('$dt','$dr','$cr','$amt','$narr', '$brcode','$user')";
mysqli_query($db,$sql) or die(mysqli_error());
$sql="select b.led,b.grp,a.nature from acgroups a,ledger b where b.brcode='$brcode' and b.led='".$dr."' and a.gname=b.grp";
$result=mysqli_query($db,$sql) or die(mysqli_error());
$row=mysqli_fetch_row($result);
//print "ledger=".$row[0]." group=".$row[1]." nature=".$row[2];


$code=substr($dr,0,3).substr(time(),7,3);
$code=str_replace(' ','-',$code);

$sq="insert into actrans (code,grp,nature,dt,ledger,payment,mode,dr,cr,narr,amt,brcode,usercode) values('$code','$row[1]','$row[2]','$dt', '$dr','$amt','jour','$dr','$cr','$nr', '$amt', '$brcode','$user')";
mysqli_query($db,$sq) or die(mysqli_error());
print "<font color=green>Data has been saved</font>";
include("journal.php");

?>
