<?
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");
$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'];
$chq_no = $_REQUEST['chq_no'];
$bpno = $_REQUEST['bpno'];
$payee = $_REQUEST['payee'];
$dt=$y.'-'.$m.'-'.$d;
if($user_level<=4)
{
	$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;
	}
}
$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;
$i=0;
//***********************
$i=0;
$dr=$_REQUEST['dr'];
foreach($dr as $a)
{
 	$dr[$i]=$a; 
 	$i++;
}

$i=0;
$mtot_dr = 0;
$dr_amt=$_REQUEST['dr_amt'];
foreach($dr_amt as $a)
{
 	$dramt[$i]=$a; 
 	$mtot_dr += $dramt[$i];
 	if ($dramt[$i]<=0)
	{
 		print "<script> alert('Invalid Entry - Debit Amount is empty'); history.back(1); </script>";
 		return;
	}

 	$i++;
}



$i=0;
$dr_narr=$_REQUEST['dr_narr'];
foreach($dr_narr as $a)
{
 	$drnarr[$i]=$a; 
 	$i++;
}
$i=0;
$ag=$_REQUEST['ag'];
foreach($ag as $a)
{
 	$again[$i]=$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++;
}
$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;



$rsdn=mysqli_query($db,"select max(bpno) from psnindia_psndbdn.bnkpay where brcode='$brcode'");
$cr_rowdn=mysqli_fetch_row($rsdn);
if($cr_rowdn[0]<=0)
{
   $crnodn=1;
}
 else  
 {
   $crnodn=$cr_rowdn[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);
 }






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

//***********************
$ii=0;
$mtot_cr = 0;
$cr_amt=$_REQUEST['cr_amt'];
foreach($cr_amt as $a)
{
 	$cramt[$ii]=$a; 
 	$mtot_cr += $cramt[$ii];
	if ($cramt[$ii]<=0)
	{
 		print "<script> alert('Invalid Entry - Credit Amount is empty'); history.back(1); </script>";
 		return;
	}

 	$ii++;
}
$mbal = round($mtot_dr,2)-round($mtot_cr,2);
print $mtot_dr-$mtot_cr;
print $mbal."aaaaaaa";
if($mbal!=0)
{
	print "<script> alert('Debit & Credit not matching ($mtot_dr <> $mtot_cr)!!!'); history.back(1); </script>";
	return;
}
for($j=0;$j<$i;$j++)
{
 	$sql="insert into journal2 values('$dt','$jvno','Dr','$dr[$j]','$dramt[$j]','0','$drnarr[$j].(BP:$bpno)','$brcode','BP','$dr_again[$j]','$dr_agval[$j]','$bpno')";
	mysqli_query($db,$sql)or die(mysqli_error());
	$sql="insert into bnkpay values('$dt','$bpno','Dr','$dr[$j]','$dramt[$j]','0','$drnarr[$j].(JV:$jvno)','$brcode','$jvno','$dr_again[$j]','$dr_agval[$j]','0000-00-00')";
	mysqli_query($db,$sql)or die(mysqli_error());
	
	$sqldn="insert into psnindia_psndbdn.journal2 values('$dt','$jvnodn','Dr','$dr[$j]','$dramt[$j]','0','$drnarr[$j].(BP:$crnodn)','$brcode','BP','$dr_again[$j]','$dr_agval[$j]','$crnodn')";
	mysqli_query($db,$sqldn)or die(mysqli_error());
	$sqldn1="insert into psnindia_psndbdn.bnkpay values('$dt','$crnodn','Dr','$dr[$j]','$dramt[$j]','0','$drnarr[$j].(JV:$jvnodn)','$brcode','$jvnodn','$dr_again[$j]','$dr_agval[$j]','0000-00-00')";
	mysqli_query($db,$sqldn1)or die(mysqli_error());
}
$re=mysqli_query($db,"select * from chq_bnk_for where brcode = '$brcode' and bank = '$cr[0]'") or die(mysqli_error());
$row=mysqli_fetch_row($re);
//$mformat = '../paypdf/'.$row[1].'?jvno='.$jvno;
//print $mformat;
//print "<script> window.open('$mformat'); </script>";
$ii=0;
foreach($cr as $a)
{
 	$cr[$i]=$a; 
 	$ii++;
}

//***********************
$ii=0;
$mtot_cr = 0;
foreach($cr_amt as $a)
{
 	$cramt[$i]=$a; 
 	$mtot_cr += $cramt[$i];
	if ($cramt[$i]<=0)
	{
 		print "<script> alert('Invalid Entry - Credit Amount is empty'); history.back(1); </script>";
 		return;
	}

 	$ii++;
}


$i=0;
$cr_narr=$_REQUEST['cr_narr'];
foreach($cr_narr as $a)
{
 	$crnarr[$i]=$a; 
 	$i++;
}
$i=0;
$cragain=$_REQUEST['cragain'];
foreach($cragain as $a)
{
 	$cr_again[$i]=$a;$i++;
}
$i=0;
$cragval=$_REQUEST['cragval'];
foreach($cragval as $a)
{
 	$cr_agval[$i]=$a;$i++;
}
date_default_timezone_set("Asia/Kolkata");	
$dttime = date ('y-m-d H:i:s'); 
$payee = addslashes($payee);
//$sql1="insert into chq_det values('$chq_no','$payee','$dttime','$cramt[0]','$brcode','$bpno','$jvno','$cr[0]','BP','Chq. Issued','0','$dt')";
//mysqli_query($db,$sql1)or die(mysqli_error());

for($j=0;$j<$i;$j++)
{
 	$sql="insert into journal2 values('$dt','$jvno','Cr','$cr[$j]','0','$cramt[$j]','$crnarr[$j].(BP:$bpno)','$brcode','BP','$cr_again[$j]','$cr_agval[$j]','$bpno')";
	mysqli_query($db,$sql) or die(mysqli_error());
	$sql="insert into bnkpay values('$dt','$bpno','Cr','$cr[$j]','0','$cramt[$j]','$crnarr[$j].(JV:$jvno)','$brcode','$jvno','$cr_again[$j]','$cr_agval[$j]','0000-00-00')";
	mysqli_query($db,$sql) or die(mysqli_error());
	
	
	
	$sqldn2="insert into psnindia_psndbdn.journal2 values('$dt','$jvnodn','Cr','$cr[$j]','0','$cramt[$j]','$crnarr[$j].(BP:$crnodn)','$brcode','BP','$cr_again[$j]','$cr_agval[$j]','$crnodn')";
	mysqli_query($db,$sqldn2) or die(mysqli_error());
	$sqldn3="insert into psnindia_psndbdn.bnkpay values('$dt','$crnodn','Cr','$cr[$j]','0','$cramt[$j]','$crnarr[$j].(JV:$jvnodn)','$brcode','$jvnodn','$cr_again[$j]','$cr_agval[$j]','0000-00-00')";
	mysqli_query($db,$sqldn3) or die(mysqli_error());
	
	
	
}

$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 "<script> alert('Debit, Credit mismatch of $rrrow[0], pls edit this entry again')</script>";
 	print "<font color=red>Data has been posted with error.  Edit again</font><br>";
}
else
	print "<font color=green>Data has been posted</font><br>";
$ip_details="$jvno|BP|Add";
log_entry($brcode,$user,$ip_details,"Bank Payment JVNo: $jvno, BPNo: $bpno",$db);
include("bpay_1.php");
?>