<?
session_start();
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../ind.php'; </script>";
 return;
}
include("../opendb.php");
include("../test.php");

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

$dbal=round($totdr,2);
$cbal=round($totcr,2);
print $dbal;
print $cbal;



if($cbal!=$dbal)
{
 print "<script> alert('Dr Cr Amount mismatch $cbal,$dbal'); history.back(1); </script>";
 return;
}

 $sqins="insert into journal2deljour(select * from journal2 where jvno='$jvno' and brcode='$brcode' )";
mysqli_query($db,$sqins)or die(mysqli_error($db));
$re=mysqli_query($db,"select j_type,no,tr_no from journal2 where jvno='$jvno' and brcode='$brcode'")or die(mysqli_error($db));
$j_row=mysqli_fetch_array($re);

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

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


$dt=parseDate($dt);


for($j=0;$j<$crcnt;$j++)
{
 $sql="insert into journal2 values('$dt','$jvno','Cr','$cr[$j]','0','$cramt[$j]','$crnarr[$j]','$brcode','$j_row[j_type]','$cr_again[$j]','$cr_agval[$j]','$j_row[tr_no]')";
mysqli_query($db,$sql) or die(mysqli_error($db));
  if($j_row['j_type']=='CR')
  {
    mysqli_query($db,"insert into cshrec values('$dt','$j_row[tr_no]','Cr','$cr[$j]','0','$cramt[$j]', '$crnarr[$j]','$brcode','$jvno','$cr_again[$j]','$cr_agval[$j]')") or die(mysqli_error($db));
  }
  if($j_row['j_type']=='BR')
  {
    $sql="insert into bnkrec values('$dt','$j_row[tr_no]','Cr','$cr[$j]','0','$cramt[$j]', '$crnarr[$j]','$brcode','$jvno','$cr_again[$j]','$cr_agval[$j]','$rec_row[0]')";

    mysqli_query($db,$sql) or die('bnkrec err:'.mysqli_error($db));
  }
  if($j_row['j_type']=='BP')
  {
    $sql="insert into bnkpay values('$dt','$j_row[tr_no]','Cr','$cr[$j]','0','$cramt[$j]', '$crnarr[$j]','$brcode','$jvno','$cr_again[$j]','$cr_agval[$j]','$rec_row[0]')";

    mysqli_query($db,$sql) or die('bnkpay err:'.mysqli_error($db));
  }

}

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

for($j=0;$j<$drcnt;$j++)
{
 $sql="insert into journal2 values('$dt','$jvno','Dr','$dr[$j]','$dramt[$j]','0','$drnarr[$j]','$brcode','$j_row[j_type]','$dr_again[$j]','$dr_agval[$j]','$j_row[tr_no]')";

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

}

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