<?
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");
if($savecount==0)
{
$us=mysqli_query($db,"select level from bruser where username='$user'")or die(mysqli_error());
$r=mysqli_fetch_row($us);
$user_level=$r[0];
$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;
	}
}
if($tdamt!=$tcamt)
{
	print "<script>alert('Error......!,Cr, Dr Entry Missmatch12212'); history.back(1); </script>";
 	return;
}

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


	$m=0;
	foreach($cr as $a)
	{
 		$cr[$m]=$a; 
 		$m++;
	}
	$m=0;
	foreach($cr_amt as $a)
	{
 		$cramt[$m]=$a; 
 		$m++;
	}
	$m=0;
	foreach($cr_narr as $a)
	{
 		$crnarr[$m]=addslashes($a); 
 		$m++;
	}
	$m=0;
	foreach($cragain as $a)
	{
 		$cr_again[$m]=$a;
		$m++;
	}
	$m=0;
	foreach($cragval as $a)
	{
 		$cr_agval[$m]=$a;
		$m++;
	}

	$flg=0;
	$flg2=0;
	for($j=0;$j<$i;$j++)
	{
 		$k=$j+1;
 		$qry=mysqli_query($db,"select * from ledger where led='$dr[$j]' and brcode='$brcode'");
 		if(mysqli_num_rows($qry)<=0)
 		{

  		print "<script>document.getElementById('myTable').rows[$k].cells[0].style.color='red';</script>";
  		$flg++;
 		}
  
   		if($dr[$j]=='')
    		{
    		print "<script>document.getElementById('myTable').rows[$k].cells[0].style.color='red';</script>";
 		$flg++;
    		}
        	if(($dr_amt[$j]=='')||($dr_amt[$j]=='0'))
		{  
    		print "<script>document.getElementById('myTable').rows[$k].cells[0].style.color='red';</script>";
		//echo $dr_narr[$j]."<br>";
		$flg2++;
    		}
	}

	if($flg>0)
 		{
  		echo "<script>alert('Check The Ledger Name of Red marked row(s)');</script>";
 		}
 	elseif($flg2>0)
 		{
  		echo "<small>Enter Amount/Narration of Red marked row(s)</small>";
 		}
	else
		{

		$flg1=0;
		$flg3=0;



		for($j=0;$j<$m;$j++)
		{
 			$k=$j+1;
 			$qry=mysqli_query($db,"select * from ledger where led='$cr[$j]' and brcode='$brcode'");
 			if(mysqli_num_rows($qry)<=0)
 			{

  			print "<script>document.getElementById('myTable2').rows[$k].cells[0].style.color='red';</script>";
  			$flg1++;
 			}
  
   			if($cr[$j]=='')

    			{
    			print "<script>document.getElementById('myTable2').rows[$k].cells[0].style.color='red';</script>";
 			$flg1++;
    			}

     			if(($cr_amt[$j]=='')||($cr_amt[$j]=='0'))

    			{
    			print "<script>document.getElementById('myTable2').rows[$k].cells[0].style.color='red';</script>";
    			$flg3++;
    			}
		}


		if($flg1>0)
 		{
  		echo "<small>Check the  Cr of Red marked row(s)</br></small>";
 		}
		elseif($flg3>0)
 		{
  		echo "<small>Enter Amount/Narration of Red marked row(s)</small>";
 		}
		else
		{



		$res=mysqli_query($db,"select max(jvno) from journal2 where brcode='$brcode'");
		$sql="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]','')";
		//echo $sql;
		//echo "<br>";
		mysqli_query($db,$sql)or die(mysqli_error());
		}
		//***********************
	
		for($n=0;$n<$m;$n++)
		{
 		$sql="insert into journal2 values('$dt','$jvno','Cr','$cr[$n]','0','$cramt[$n]','".addslashes(htmlspecialchars($crnarr[$n]))."','$brcode','JV','$cr_again[$n]','$cr_agval[$n]','')";
		//echo $sql;
		//echo "<br>";
		mysqli_query($db,$sql) or die(mysqli_error());
		}
		log_entry($brcode,$user,$REMOTE_ADDR,"Journal Entry - $jvno");
		if($brcode=='PSN236')
		{
		print "<script> window.open('../payrec/prnvoucher.php?jvno=$jvno'); </script>";
		}
		print "<script>alert('Data Saved');</script>";
		print "<script> document.getElementById('savecount').value=1;document.location='jour.php';</script>";
	
		}
	}
}
?>
