<?
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");
date_default_timezone_set("Asia/Kolkata");
$i=0;
$cr=$_REQUEST['cr'];
foreach($cr as $a)
{
 $cr[$i]=$a; 
 $i++;
}
$i=0;$totcr=0;
$cr_amt=$_REQUEST['cr_amt'];
foreach($cr_amt as $a)
{
 $cramt[$i]=$a; 
 $totcr+=$a;
 $i++;
}

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

$crcnt=$i;

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

$i=0;$totdr=0;
$dr_amt=$_REQUEST['dr_amt'];
foreach($dr_amt as $a)
{
 $dramt[$i]=$a; 
 $totdr+=$a;
 $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++;
}
$drcnt=$i;

$dt=$_REQUEST['dt'];
$jvno=$_REQUEST['jvno'];

$totdr=trim($totdr);
$totcr=trim($totcr);
if($totdr!=$totcr)
{
 print "<script> alert('Dr Cr Amount mismatch'); history.back(1); </script>";
 return;
}
 $upddatetime=date('Y-m-d H:i:s');

$re=mysqli_query($db,"select j_type,no,tr_no from journal2 where jvno='$jvno' and brcode='$brcode'")or die(mysqli_error());
$j_row=mysqli_fetch_array($re);
$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);
}

$drr=mysqli_query($db,"select brec_date,brno from bnkrec where j_type='$jvno' and brcode='$brcode'");
//echo "select brec_date,brno from bnkrec where j_type='$jvno' and brcode='$brcode'";
      
      $bankacnumrow=mysqli_num_rows($drr);
      //echo $bankacnumrow;
      if($bankacnumrow>0)
      {
          $rec_row=mysqli_fetch_row($drr) or die(mysqli_error());
            $bankrecno=$rec_row[1];
      }
      else
      {
          $bnkres=mysqli_query($db,"select max(brno) from bnkrec where brcode='$brcode'");
          $bnkrow=mysqli_fetch_row($bnkres);
		if($bnkrow[0]<=0)
		{
		$bankrecno=1;
		}
		else
		{
		    $bankrecno=($bnkrow[0]+1);
		}

      }
      
//echo $bankrecno;
//exit;

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

  if($j_row['j_type']=='CR')
  {
    mysqli_query($db,"delete from cshrec where j_type='$jvno' and brcode='$brcode'")or die(mysqli_error());
  }
  if($j_row['j_type']=='CP')
  {
    mysqli_query($db,"delete from cshpay where j_type='$jvno' and brcode='$brcode'")or die(mysqli_error());
  }
  if($j_row['j_type']=='BR')
  {
        $drr=mysqli_query($db,"select brec_date from bnkrec where j_type='$jvno'  and brcode='$brcode'");
        $drnumbr=mysqli_num_rows($drr);
        if($drnumbr>0)
        {
            $rec_row=mysqli_fetch_row($drr) or die(mysqli_error());
            $sql="delete from bnkrec where j_type='$jvno' and brcode='$brcode'"; 
            mysqli_query($db,$sql)or die(mysqli_error());
        }
  }
  if($j_row['j_type']=='BP')
  {
     $drr=mysqli_query($db,"select brec_date from bnkpay where j_type='$jvno'  and brcode='$brcode'");
     $drnum=mysqli_num_rows($drr);
     if($drnum>0)
     {
        $rec_row=mysqli_fetch_row($drr) or die(mysqli_error());
        $sql="delete from bnkpay where j_type='$jvno' and brcode='$brcode'"; 
        mysqli_query($db,$sql)or die(mysqli_error());
     }
    
  }


$dt=parseDate($dt);


for($j=0;$j<$crcnt;$j++)
{
 $sql="insert into journal2 values('$dt','$jvno','Cr','$cr[$j]','0','$cramt[$j]','".addslashes(htmlspecialchars($crnarr[$j]))."','$brcode','$j_row[j_type]','$cr_again[$j]','$cr_agval[$j]','$j_row[tr_no]')";
echo $sql;
mysqli_query($db,$sql) or die(mysqli_error());

  if($j_row['j_type']=='CR')
  {
    mysqli_query($db,"insert into cshrec values('$dt','$j_row[tr_no]','Cr','$cr[$j]','0','$cramt[$j]', '".addslashes(htmlspecialchars($crnarr[$j]))."','$brcode','$jvno','$cr_again[$j]','$cr_agval[$j]')") or die(mysqli_error());
  }
  if($j_row['j_type']=='BR')
  {
    $sql="insert into bnkrec values('$dt','$bankrecno','Cr','$cr[$j]','0','$cramt[$j]', '".addslashes(htmlspecialchars($crnarr[$j]))."','$brcode','$jvno','$cr_again[$j]','$cr_agval[$j]','$dt')";
    mysqli_query($db,$sql) or die('bnkrec err:'.mysqli_error());
  }
  if($j_row['j_type']=='BP')
  {
    $sql="insert into bnkpay values('$dt','$j_row[tr_no]','Cr','$cr[$j]','0','$cramt[$j]', '".addslashes(htmlspecialchars($crnarr[$j]))."','$brcode','$jvno','$cr_again[$j]','$cr_agval[$j]','$dt')";
echo $sql;
    mysqli_query($db,$sql) or die('bnkpay err:'.mysqli_error());
  }
echo $sql;
}
echo $drcnt;
//******************************************************************************

for($j=0;$j<$drcnt;$j++)
{
 $sql="insert into journal2 values('$dt','$jvno','Dr','$dr[$j]','$dramt[$j]','0','".addslashes(htmlspecialchars($drnarr[$j]))."','$brcode','$j_row[j_type]','$dr_again[$j]','$dr_agval[$j]','$j_row[tr_no]')";
echo $sql;
 mysqli_query($db,$sql)or die(mysqli_error());
  if($j_row['j_type']=='CP')
  {
    mysqli_query($db,"insert into cshpay values('$dt','$j_row[tr_no]','Dr','$dr[$j]','0','$dramt[$j]', '".addslashes(htmlspecialchars($drnarr[$j]))."','$brcode','$jvno','$dr_again[$j]','$dr_agval[$j]')") or die(mysqli_error());
  }
  if($j_row['j_type']=='BR')
  {
      echo "insert into bnkrec values('$dt','$bankrecno','Dr','$dr[$j]','$dramt[$j]','0', '".addslashes(htmlspecialchars($drnarr[$j]))."','$brcode','$jvno','$dr_again[$j]','$dr_agval[$j]','$rec_row[0]')";
    mysqli_query($db,"insert into bnkrec values('$dt','$j_row[tr_no]','Dr','$dr[$j]','$dramt[$j]','0', '".addslashes(htmlspecialchars($drnarr[$j]))."','$brcode','$jvno','$dr_again[$j]','$dr_agval[$j]','$dt')") or die('bnkrec err2:'.mysqli_error());
  }
  if($j_row['j_type']=='BP')
  {
    mysqli_query($db,"insert into bnkpay values('$dt','$j_row[tr_no]','Dr','$dr[$j]','$dramt[$j]','0', '".addslashes(htmlspecialchars($drnarr[$j]))."','$brcode','$jvno','$dr_again[$j]','$dr_agval[$j]','$dt')") or die('bnkpay err2:'.mysqli_error());
  }

}
$ip_details="$jvno|".$j_row['j_type']."|Edit";
log_entry($brcode,$user,$ip_details,"Edit $j_row[j_type] Journal - $jvno",$db);
print "<font color=green>Data has been posted</font><br><script>document.location='../db2.php?df=$df&dto=$dto'</script>";
?>
