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

include("../../opendb.php");
include("../../test.php");
$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];
date_default_timezone_set("Asia/Kolkata");
$jvno=$_REQUEST['jvno'];
$savecount=$_REQUEST['savecount'];
$cr=$_REQUEST['cr'];
$cr_amt=$_REQUEST['cr_amt'];
$cr_narr=$_REQUEST['cr_narr'];
$cragain=$_REQUEST['cragain'];
$cragval=$_REQUEST['cragval'];
$dr=$_REQUEST['dr'];
$dr_amt=$_REQUEST['dr_amt'];
$dr_narr=$_REQUEST['dr_narr'];
$ag=$_REQUEST['ag'];
$dragain=$_REQUEST['dragain'];
$dragval=$_REQUEST['dragval'];
$dt=$_REQUEST['dt'];
$docdt=$_REQUEST['docdt'];

$dt= implode('-', array_reverse(explode('-', $dt)));
if($savecount==0)
{

settype($totdr,'float');
settype($totcr,'float');
settype($a,'float');

$totdr=0;
$totcr=0;
$i=0;
foreach($cr as $a)
{
 $cr[$i]=$a; 
 $i++;
}
$i=0;$totcr=0;
foreach($cr_amt as $a)
{
 $cramt[$i]=$a; 
 $totcr+=$a;
 $i++;
}

$i=0;
foreach($cr_narr as $a)
{
 $crnarr[$i]=$a; 
 $i++;
}

$i=0;
foreach($cragain as $a)
{
 $cr_again[$i]=$a;$i++;
}
$i=0;
foreach($cragval as $a)
{
 $cr_agval[$i]=$a;

 $i++;

}
 $crcnt=$i;


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

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

$i=0;
foreach($dr_narr as $a)
{
 $drnarr[$i]=$a; 
 $i++;
}
$i=0;
foreach($ag as $a)
{
 $again[$i]=$a;$i++;
}
$i=0;
foreach($dragain as $a)
{
 $dr_again[$i]=$a;$i++;
}
$i=0;
foreach($dragval as $a)
{
 $dr_agval[$i]=$a;$i++;
 
}

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

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


$drcnt=$i;


$totdr+=0;
$totcr+=0;

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

$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]>7 || $dtrow[0]<0)
{
 print "Invalid Date, Entry possible for last 7 days only";
  return;
}
}
            
$re=mysqli_query($db,"select j_type,no,tr_no,docdt from journal2 where jvno='$jvno' and brcode='$brcode'")or die(mysqli_error($db));
$j_row=mysqli_fetch_array($re);
$docdt='0000-00-00';
$upddatetime=date('Y-m-d H:i:s');
$jvqry=mysqli_query($db,"select * from journal2 where jvno='$jvno' and brcode='$brcode'")or die('Led Post Err1'.mysqli_error($db));
while($jvqryrw=mysqli_fetch_array($jvqry))
{
    $jvsql="insert into journal2_log(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no,user,entrytime,action) values('$jvqryrw[date]','$jvqryrw[jvno]','$jvqryrw[type]','$jvqryrw[partic]','$jvqryrw[dramt]','$jvqryrw[cramt]','$jvqryrw[narr]','$brcode','$jvqryrw[j_type]','$jvqryrw[against]','$jvqryrw[no]','$jvqryrw[tr_no]','$user','$upddatetime','Edit $j_row[j_type] Journal - $jvno')";
    echo $jvsql;
    mysqli_query($db,$jvsql);
}


mysqli_query($db,"delete from journal2 where jvno='$jvno' and brcode='$brcode' and j_type='$j_row[0]'")or die(mysqli_error($db));



for($j=0;$j<$crcnt;$j++)
{
 $sql="insert into journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no,docdt) values('$dt','$jvno','Cr','$cr[$j]','0','$cramt[$j]','".addslashes(htmlspecialchars($crnarr[$j]))."','$brcode','$j_row[j_type]','$cr_again[$j]','$cr_agval[$j]','$logtime $user','$logdt')";
print $sql;
mysqli_query($db,$sql) or die(mysqli_error($db));
  

}

//******************************************************************************

for($j=0;$j<$drcnt;$j++)
{
 $sql="insert into journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no,docdt) values('$dt','$jvno','Dr','$dr[$j]','$dramt[$j]','0','".addslashes(htmlspecialchars($drnarr[$j]))."','$brcode','$j_row[j_type]','$dr_again[$j]','$dr_agval[$j]','$logtime $user','$logdt')";

 mysqli_query($db,$sql)or die(mysqli_error($db));


}

$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 again</font><br>";
print "<script>history.back(1);</script>";
}
else
print "<font color=green>Data has been posted</font><br>";

print "<font color=green>Data has been posted to log</font><br>";
log_entry($brcode,$user,"$jvno|Edit $j_row[j_type] Journal|Edit","Edit $j_row[j_type] Journal - $jvno",$db);

echo "<script> document.getElementById('savecount').value=1;document.location='../db1.php';</script>";
print "<script>history.back(1);</script>";
}
elseif($savecount==1)
{
echo "Data already saved";
  echo "<script> document.location='../db1.php';</script>";
}
?>
