<?
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("../opendb.php");
include("../test.php");
$savecount=$_REQUEST['savecount'];
$tdamt=$_REQUEST['tdamt'];
$tcamt=$_REQUEST['tcamt'];
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];
$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;
	}
}
$acper=mysqli_query($db,"select acfrom,acto from acperiod");
$acfinrow=mysqli_fetch_array($acper);
$fromdate=$acfinrow[0];
$todate=$acfinrow[1];
if($dt >= $fromdate && $dt <= $todate) 
{
	
}
else
{
	print "<script> alert('Invalid Date selection'); 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;
$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]=addslashes($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++;
}


	$m=0;
	$cr=$_REQUEST['cr'];
	foreach($cr as $a)
	{
 		$cr[$m]=$a; 
 		$m++;
	}
	$m=0;
	$cr_amt=$_REQUEST['cr_amt'];
	foreach($cr_amt as $a)
	{
 		$cramt[$m]=$a; 
 		$m++;
	}
	$m=0;
	$cr_narr=$_REQUEST['cr_narr'];
	foreach($cr_narr as $a)
	{
 		$crnarr[$m]=addslashes($a); 
 		$m++;
	}
	$m=0;
	$cragain=$_REQUEST['cragain'];
	foreach($cragain as $a)
	{
 		$cr_again[$m]=$a;
		$m++;
	}
	$m=0;
	$cragval=$_REQUEST['cragval'];
	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);

    $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','".addslashes(htmlspecialchars($drnarr[$j]))."','$brcode','JV','$dr_again[$j]','$dr_agval[$j]','0')";
		//echo $sql;
		//echo "<br>";
		mysqli_query($db,$sql)or die(mysqli_error());
		$sqldn="insert into psnindia_psndbdn.journal2 values('$dt','$jvnodn','Dr','$dr[$j]','$dramt[$j]','0','".addslashes(htmlspecialchars($drnarr[$j]))."','$brcode','JV','$dr_again[$j]','$dr_agval[$j]','0')";
		mysqli_query($db,$sqldn)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]','0')";
		//echo $sql;
		//echo "<br>";
		mysqli_query($db,$sql) or die(mysqli_error());
		$sqldn1="insert into psnindia_psndbdn.journal2 values('$dt','$jvnodn','Cr','$cr[$n]','0','$cramt[$n]','".addslashes(htmlspecialchars($crnarr[$n]))."','$brcode','JV','$cr_again[$n]','$cr_agval[$n]','0')";
		mysqli_query($db,$sqldn1) or die(mysqli_error());
		}
		$ip_details="$jvno|JV|Add";
		log_entry($brcode,$user,$ip_details,"Journal Entry - $jvno",$db);
		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>";
	
		}
	}
}
?>
