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

include_once("opendb.php");
include_once("test.php");
$m=$_REQUEST['m'];
$d=$_REQUEST['d'];
$y=$_REQUEST['y'];
$tcamt=$_REQUEST['tcamt'];
$tdamt=$_REQUEST['tdamt'];
$savecount=$_REQUEST['savecount'];
date_default_timezone_set("Asia/Kolkata");
$logdt=date("Y-m-d");
$logtime=date("H:i:s");
if($savecount==0)
{
$us=mysqli_query($db,"select level from bruser where username='$user' and brcode='$brcode'")or die(mysqli_error($db));
$r=mysqli_fetch_row($us);
$user_level=$r[0];
$dt=$y.'-'.$m.'-'.$d;
$acpd=mysqli_query($db,"select * from acperiod") or die ('err'.mysqli_error($db));
     $acrow=mysqli_fetch_row($acpd);

      $acf= $acrow[0];
      $act= $acrow[1];
      
      if($dt<$acf || $dt>$act)
             {
                 print "<script> alert('Invalid Date, Entry Possible for Current Financial Year only'); </script>";
                 return;
             }
             
if($user_level<=4)
{
    if(strtolower($user)!='kishore' && strtolower($user)!='harshitha')
    {
	$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]>10 || $dtrow[0]<0)
	{
 		print "<script> alert('Invalid Date, Entry possible for last 7 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;
$dr=$_REQUEST['dr'];
$i=0;
foreach($dr as $a)
{
 	$dr[$i]=$a; 
 	$i++;
}
$dr_amt=$_REQUEST['dr_amt'];

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

$i=0;
foreach($dr_amt as $a)
{
 	$dramt[$i]=$a; 
 	$i++;
}

	$cr=$_REQUEST['cr'];	
	$m=0;
	foreach($cr as $a)
	{
 		$cr[$m]=$a; 
 		$m++;
	}
	$cr_amt=$_REQUEST['cr_amt'];
	$m=0;
	foreach($cr_amt as $a)
	{
 		$cramt[$m]=$a; 
 		$m++;
	}
	$cr_narr=$_REQUEST['cr_narr'];
	$m=0;
	foreach($cr_narr as $a)
	{
 		$crnarr[$m]=addslashes($a); 
 		$m++;
	}
	$cragain=$_REQUEST['cragain'];
	$m=0;
	foreach($cragain as $a)
	{
 		$cr_again[$m]=$a;
		$m++;
	}
	$cragval=$_REQUEST['cragval'];
	$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' and led not in('T D S RECEIVABLE','TDS A.Y 2023-24','TDS A.Y 2024-25','TDS A.Y 2024-25','TDS A.Y 2025-26')");
 		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) or TDS entry not allowed');</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'");
		

		$row=mysqli_fetch_row($res);
		if($row[0]<=0)
		$jvno=1;
		else
		$jvno=($row[0]+1);




if($savecount==0)
{

		for($j=0;$j<$i;$j++)
		{
		    
		$sql="insert into journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no,prftcntr,docdt) values('$dt','$jvno','Dr','$dr[$j]','$dramt[$j]','0','".addslashes(htmlspecialchars($drnarr[$j]))."','$brcode','JV','$dr_again[$j]','$dr_agval[$j]','$jvno','$logtime $user','$logdt')";
		//echo $sql;
		//echo "<br>";
		mysqli_query($db,$sql)or die(mysqli_error($db));
		
		}
		//***********************
	
		for($n=0;$n<$m;$n++)
		{
 		$sql="insert into journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no,prftcntr,docdt) values('$dt','$jvno','Cr','$cr[$n]','0','$cramt[$n]','".addslashes(htmlspecialchars($crnarr[$n]))."','$brcode','JV','$cr_again[$n]','$cr_agval[$n]','$jvno','$logtime  $user','$logdt')";
		//echo $sql;
		//echo "<br>";
		mysqli_query($db,$sql) or die(mysqli_error($db));
	
		}
		$ip_address = "$jvno|JV|Add";
		log_entry($brcode,$user,$ip_address,"Journal Entry - $jvno",$db);
}
		if($brcode=='PSN236')
		{
		//print "<script> window.open('../payrec/prnvoucher.php?jvno=$jvno'); </script>";
		}
		$savecount=1;
		print "<script>alert('Data Saved');</script>";
		print "<script> document.getElementById('savecount').value=1;document.location='jour.php';</script>";
	
		}
	}
}
?>
