<?
session_start();
include_once("test.php");
include_once('opendb.php');
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
//$br=$_SESSION['branch'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='inde.php'; </script>";
 return;
}
$y=$_REQUEST['y'];
$m=$_REQUEST['m'];
$d=$_REQUEST['d'];

if($savecount==0)
{
$us=mysqli_query($db,"select level from bruser where username='$user'")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)
{
$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 10 days only'); history.back(1); </script>";
 return;
}
}

$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);


$dt=$y.'-'.$m.'-'.$d;

//***********************/
 $dr_tot=0;
 $cr_tot=0;

$i=0;
$d=0;
$dr=$_REQUEST['dr'];
foreach($dr as $a)
{
 $dr[$i]=$a; 
 $i++;

 if($a=='')
      $d++;
}


if(count(array_unique($dr))<count($dr))
{
    echo " Name has duplicates";
    exit;
} 


$i=0;
$dr_amt=$_REQUEST['dr_amt'];
foreach($dr_amt as $a)
{
 $dr_amt[$i]=$a; 
 $dr_tot+=$a;
 $i++;
}
$i=0;
$dr_narr=$_REQUEST['dr_narr'];
foreach($dr_narr as $a)
{
 $dr_narr[$i]=$a; 
 $i++;
}

$i=0;
$dragain=$_REQUEST['dragain'];
foreach($dragain as $a)
{
 $dragain[$i]=$a;$i++;
}
$i=0;
$dragval=$_REQUEST['dragval'];
foreach($dragval as $a)
{
 $dragval[$i]=$a;
 $i++;
}

$c=0;
$p=0;
$cr=$_REQUEST['cr'];
foreach($cr as $a)
{
 $cr[$p]=$a; 
 $p++;

 if($a=='')
      $c++;
}


if(count(array_unique($cr))<count($cr))
{
    echo " Name has duplicates";
    exit;
} 


$p=0;
$cr_amt=$_REQUEST['cr_amt'];
foreach($cr_amt as $a)
{
 $cr_amt[$p]=$a; 
 $cr_tot+=$a;
 $p++;
}
$p=0;
$cr_narr=$_REQUEST['cr_narr'];
foreach($cr_narr as $a)
{
 $cr_narr[$p]=$a; 
 $p++;
}

$p=0;
$cragain=$_REQUEST['cragain'];
foreach($cragain as $a)
{
 $cragain[$p]=$a;
 $p++;
}
$p=0;
$cragval=$_REQUEST['cragval'];
foreach($cragval as $a)
{
 $cragval[$p]=$a;
  $p++;
}

$brnos=$_REQUEST['brnos'];

$dr_tot=number_format($dr_tot,2,".","");
$cr_tot=number_format($cr_tot,2,".","");
 if($dr_tot!= $cr_tot){
     echo "drtt ". $dr_tot;
     echo "crtt ". $cr_tot;
    echo "Error!!  The Total Amount From Both Sides Are Not Matching";
    exit;
 } 

 if(($d>0)||($c>0)){
    echo "Error!!  The A/c Head From Both Sides Must Not Be Blank";
    exit;
 } 


$flg=0;
$flg2=0;
date_default_timezone_set("Asia/Kolkata");
$logdt=date("Y-m-d");
$logtime=date("H:i:s");

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')||($dr_narr[$j]==''))
    {
        print "<script>document.getElementById('myTable').rows[$k].cells[0].style.color='red';</script>";
 
        $flg2++;
    }
}

if($flg>0)
 {
    echo "<small>Check the  Dr of Red marked row(s)</br></small>";
    exit;
 }

 elseif($flg2>0)
 {
    echo "<small>Enter Amount/Narration of Red marked row(s)</small>";
    exit;
 }
else
{

	
    $flg1=0;
	$flg3=0;
    for($j=0;$j<$p;$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')||($cr_narr[$j]==''))
		{
    		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
    {

        $rs=mysqli_query($db,"select max(bpno) from bnkpay where brcode='$brcode'");
	    $cr_row=mysqli_fetch_row($rs);

 		if($cr_row[0]<=0)
   			$bpno=1;
 		else   
   			$bpno=$cr_row[0]+1;

		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]','$dr_amt[$j]','0','$dr_narr[$j].(BP:$bpno)','$brcode','BP','CHEQ RET','$dragval[$j]','$bpno','$logtime $user','$logdt')";
            mysqli_query($db,$sql)or die(mysqli_error($db));
 		    $sql="insert into bnkpay(date,bpno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,brec_date) values('$dt','$bpno','Dr','$dr[$j]','$dr_amt[$j]','0','$dr_narr[$j].(JV:$jvno)','$brcode','$jvno','CHEQ RET','$dragval[$j]','0000-00-00')";
            mysqli_query($db,$sql)or die(mysqli_error($db));

	    }
             //$re_bnk=mysqli_query($db,"select led from ledger where brcode='$brcode' and grp = 'Bank Accounts' limit 1");
            //$row11=mysqli_fetch_array($re_bnk);
            //$cr=$row11[0];
        for($j=0;$j<$p;$j++)
		{
 		    $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[$j]','0','$cr_amt[$j]','$cr_narr[$j].(BP:$bpno)','$brcode','BP','CHEQ RET','$cragval[$j]','$bpno','$logtime $user','$logdt')";
		    mysqli_query($db,$sql) or die(mysqli_error($db));

 		    $sql="insert into bnkpay(date,bpno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,brec_date) values('$dt','$bpno','Cr','$cr[$j]','0','$cr_amt[$j]','$cr_narr[$j].(JV:$jvno)','$brcode','$jvno','CHEQ RET','$cragval[$j]','0000-00-00')";
		    mysqli_query($db,$sql) or die(mysqli_error($db));

		}
		$jvqry="update journal2 set against='CHEQ RET' where jvno='$brnos' and brcode='$brcode' and j_type='BR'";
		mysqli_query($db,$jvqry) or die(mysqli_error($db));
	            /*
 			 $sql="insert into journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$dt','$jvno','Cr','$cr','0','$cr_tot','$cr_narr[$j].(BP:$bpno)','$brcode','BP','$cragain[$j]','$cragval[$j]','$bpno')";
			       mysqli_query($db,$sql) or die(mysqli_error($db));

 			 $sql="insert into bnkpay(date,bpno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,brec_date) values('$dt','$bpno','Cr','$cr','0','$cr_tot','$cr_narr[$j].(JV:$jvno)','$brcode','$jvno','$cragain[$j]','$cragval[$j]','0000-00-00')";
				mysqli_query($db,$sql) or die(mysqli_error($db));

		    */
            $imgflnm=$_REQUEST['imgflnm'];
			$imagename='thumb/'.$imgflnm;			
			$extension = strtolower(pathinfo($imagename,PATHINFO_EXTENSION));
			$filnm=$jvno.$brcode . '.' . $extension;
			$newimgname='thumb/'.$filnm;
			if (file_exists($imagename)) {
			rename($imagename,$newimgname);
			}
	   $rr=mysqli_query($db,"select sum(dramt)-sum(cramt) from journal2 where brcode='$brcode' and jvno='$jvno'");
			$rrrow=mysqli_fetch_row($rr);

	   if($rrrow[0]!=0)
		{
 			print "Debit, Credit mismatch of $rrrow[0].<br>Data couldn't save, pls edit this entry again.";
			// print "<font color=red>Data has been posted with error.  Edit this entry</font><br>";
 			exit;
	       }
	else
		//print "<script> window.open('prnvoucher.php?jvno=$jvno'); </script>";
	       print "<font color=green>Data has been posted</font><br>";

	 	 echo "<script> document.getElementById('savecount').value=1;document.location='bpay_chqbnce.php';</script>";
	 	 $ip_address = "$jvno|BP|Add";
		 log_entry($brcode,$user,$ip_address,"Bank Payment JVNo: $jvno, BPNo: $bpno",$db);
		//include("bpay.php");
   }
 }
}
elseif($savecount==1)
{
echo "Data already saved";
  echo "<script> document.location='bpay_chqbnce.php';</script>";
}
?>
