<?
session_start();
include_once("test.php");
include_once('opendb.php');
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
//$br=$_SESSION['branch'];
$led=$_REQUEST['led'];
$grp=$_REQUEST['grp'];
$op=$_REQUEST['op'];
$drcr=$_REQUEST['drcr'];
$allbr=$_REQUEST['allbr'];

if(strlen($user)<=0)
{
  	print "<script> alert('Invalid User'); document.location='inde.php' </script>";
  	return;
}
$led=trim($led);
if(strlen($led)<=0 || strlen($grp)<=0)
{
 	print "<script>alert('Insufficient Entry'); history.back(1);</script>";
 	return;
}
//if(ereg("[&']",$led))
//{
 	//print "<script> alert('Please avoid & and \''); history.back(1);</script>";
 	//return;
//}
$code=substr($led,0,3).substr(time(),7,3);
$code=str_replace(' ','-',$code);
$led=str_replace('  ',' ',$led);


if($allbr=='y')
{
	$brares=mysqli_query($db,"select brcode from branch");
	$kk=1;
	while($brledrow=mysqli_fetch_array($brares))
	{
		$re=mysqli_query($db,"select * from ledger where led='$led' and brcode='$brledrow[brcode]'");
		if(mysqli_num_rows($re)<=0)
 		{
			$code=substr($led,0,3).substr(time(),7,3).$kk++;
			$code=str_replace(' ','-',$code);
			$led=str_replace('  ',' ',$led);
			$led = strtoupper($led);
		   	$sql="insert into ledger(led,grp,op,drcr,code,brcode) values('$led','$grp','$op','$drcr','$code','$brledrow[brcode]')";
		   	mysqli_query($db,$sql) or die(mysqli_error($db));
		   	// log added on 5th oct 2017
		   	
		   	log_entry($brcode,$user,$REMOTE_ADDR,"ledger creation:$led",$db);
		 }
	}
}// end if
else
{
	$re11=mysqli_query($db,"select * from ledger where led='$led' and brcode='$brcode'");
	if(mysqli_num_rows($re11)>0)
	{
		 print "<script>alert('Duplicate Entry'); history.back(1); </script>";
		 return;
	}
	else
	{
	//$led = strtoupper($led);
	$sql="insert into ledger(led,grp,op,drcr,code,brcode) values('$led','$grp','$op','$drcr','$code','$brcode')";
	
	mysqli_query($db,$sql) or die(mysqli_error($db));
	log_entry($brcode,$user,$REMOTE_ADDR,"ledger creation:$led ",$db);
	}
} // end else
print "Ledger has been created";
include("ledge.php");
?>
