<?
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");
$jvno=$_REQUEST['jvno'];
$savecount=$_REQUEST['savecount'];
$pswd=$_REQUEST['pswd'];
$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'];
$rr=mysqli_query($db,"select acpass2 from branch");
$pu=mysqli_fetch_array($rr);

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

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


$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' and j_type='$j_row[j_type]'")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(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) 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]')";
mysqli_query($db,$sql) or die(mysqli_error($db));
  if($j_row[j_type]=='CR')
  {
    mysqli_query($db,"insert into cshrec(date,crno,type,partic,dramt,cramt,narr,brcode,j_type,against,no) 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(date,brno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,brec_date) 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(date,bpno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,brec_date) 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));
  }

}
echo $sql;
echo "<br>";
//******************************************************************************

for($j=0;$j<$drcnt;$j++)
{
 $sql="insert into journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) 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]')";

 mysqli_query($db,$sql)or die(mysqli_error($db));
  if($j_row[j_type]=='CP')
  {
    mysqli_query($db,"insert into cshpay(date,cpno,type,partic,dramt,cramt,narr,brcode,j_type,against,no) 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(date,brno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,brec_date) 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(date,bpno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,brec_date) 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));
  }

}
echo $sql;
echo "<br>";
exit;
$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>";
 exit;
}
else
{
print "<font color=green>Data has been posted</font><br>";

//log_entry($brcode,$user,$REMOTE_ADDR,"Edit $j_row[j_type] Journal - $jvno");
print "<font color=green>Data has been posted to log</font><br>";
echo "<script> document.getElementById('savecount').value=1;document.location='db1.php';</script>";
//print "<script>history.back(1);</script>"
}
}
else
{
echo "Data already saved";
  echo "<script> document.location='db1.php';</script>";
}
?>
