<?
session_start();

$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
include("test.php");
include("opendb.php");
if(strlen($user)<=0)
{
  print "<script> alert('Invalid User'); document.location='../ind.php' </script>";
  return;
}
$us=mysqli_query($db,"select level from bruser where username='$user'")or die(mysqli_error($db));
$r=mysqli_fetch_row($us);
$user_level=$r[0];
 print "User Level:".$user_level;
?>
<head>
<script>
 function sh()
 {
  //window.open('prnledger.php');
 }
</script>
</head>
<style>
 td{font-family:verdana;font-size:14;color:black;}
 th{font-family:verdana;font-size:14;color:white;background-color:#5b5858}
 body{background-color:ivory}
</style>
</center>
<body>
<table border=0 width=100%>
<tr><td>
<font face=verdana size=3 color='dark brown'></b></font>
</td></tr>
<tr><td colspan=2 bgcolor=green></td></tr>
<tr><td colspan=2>
<table border=0 cellspacing=1 cellpadding=1 bgcolor=black width=>

<?
$cnt=mysqli_query($db,"select distinct(brcode) from journal2_june7 where brcode='$brcode' ")or die(mysqli_error($db));
while($rowbr=mysqli_fetch_array($cnt))
{
  
 $sq="select distinct(jvno) from journal2_june7 where brcode='$rowbr[0]' and j_type in('CR','CP','BR','BP','JV') order by date";

$result=mysqli_query($db,$sq)or die(mysqli_error($db));
//$jvno=1;

 $sqjv="select max(jvno) from journal2 where brcode='$rowbr[0]'  ";

$resultjv=mysqli_query($db,$sqjv)or die(mysqli_error($db));
if(mysqli_num_rows($resultjv)>0)
{
  $jvnor=mysqli_fetch_array($resultjv)  ;
  $jvno=$jvnor[0]+1;
}
else
{
$jvno=1;
}
while($rowjv=mysqli_fetch_array($result))
{
$sql="update journal2_june7 set jvno='$jvno' where brcode='$rowbr[0]' and jvno='$rowjv[0]'";

mysqli_query($db,$sql) or die(mysqli_error($db));
print "$sql<br>";
//$sqlins="insert into journal2(select * from journal2_june7 where brcode='$rowbr[0]' and jvno='$jvno' and j_type in('CR','CP','BR','BP','JV') )";
$sqlins="select * from journal2_june7 where brcode='$rowbr[0]' and jvno='$jvno' and j_type in('CR','CP','BR','BP','JV')";

$tranjv=mysqli_query($db,$sqlins) or die(mysqli_error($db));
while($jvrow=mysqli_fetch_array($tranjv))
{
   print "ok<br>";
   
  //$sqljvins="insert into journal2 values('$jvrow[0]','$jvrow[1]','$jvrow[2]','$jvrow[3]','$jvrow[4]','$jvrow[5]','$jvrow[6]','$jvrow[7]','$jvrow[8]','$jvrow[9]','$jvrow[10]','$jvrow[11]','$jvrow[12]','$jvrow[13]')" ; 
 $sqljvins="insert into journal2 values('$jvrow[date]','$jvrow[jvno]','$jvrow[type]','".addslashes($jvrow[partic])."','$jvrow[dramt]','$jvrow[cramt]','".addslashes($jvrow[narr])."','$jvrow[brcode]','$jvrow[j_type]','$jvrow[against]','$jvrow[no]','$jvrow[tr_no]','$jvrow[prftcntr]','$jvrow[docdt]')" ; 

mysqli_query($db,$sqljvins) or die(mysqli_error($db));
print "$sqljvins<br>";

if($jvrow[j_type]=='CR' && $jvrow[type]=='Cr')
 {
     $sqlcrins="insert into cshrec values('$jvrow[date]','$jvrow[tr_no]','$jvrow[type]','".addslashes($jvrow[partic])."','$jvrow[dramt]','$jvrow[cramt]','".addslashes($jvrow[narr])."','$jvrow[brcode]','$jvrow[jvno]','$jvrow[against]','$jvrow[tr_no]','0','$jvrow[docdt]')";
     mysqli_query($db,$sqlcrins) or die(mysqli_error($db));
     
 }
 if($jvrow[j_type]=='CP' && $jvrow[type]=='Dr')
 {
     $sqlcrins="insert into cshpay values('$jvrow[date]','$jvrow[tr_no]','$jvrow[type]','".addslashes($jvrow[partic])."','$jvrow[dramt]','$jvrow[cramt]','".addslashes($jvrow[narr])."','$jvrow[brcode]','$jvrow[jvno]','$jvrow[against]','$jvrow[tr_no]','0','$jvrow[docdt]')";
     mysqli_query($db,$sqlcrins) or die(mysqli_error($db));

 }
 if($jvrow[j_type]=='BR')
 {
     $sqlcrins="insert into bnkrec values('$jvrow[date]','$jvrow[tr_no]','$jvrow[type]','".addslashes($jvrow[partic])."','$jvrow[dramt]','$jvrow[cramt]','".addslashes($jvrow[narr])."','$jvrow[brcode]','$jvrow[jvno]','$jvrow[against]','$jvrow[tr_no]','$jvrow[docdt]','0','$jvrow[docdt]')";
    mysqli_query($db,$sqlcrins) or die(mysqli_error($db));
 
     
 }
 if($jvrow[j_type]=='BP')
 {
     $sqlcrins="insert into bnkpay values('$jvrow[date]','$jvrow[tr_no]','$jvrow[type]','".addslashes($jvrow[partic])."','$jvrow[dramt]','$jvrow[cramt]','".addslashes($jvrow[narr])."','$jvrow[brcode]','$jvrow[jvno]','$jvrow[against]','$jvrow[tr_no]','$jvrow[docdt]','0','$jvrow[docdt]')";
 
     mysqli_query($db,$sqlcrins) or die(mysqli_error($db));

 }
 
print "$sqlcrins<br>";
}

$sqlupd="update journal2 set prftcntr='fi' where brcode='$rowbr[0]' and jvno='$jvno'";

mysqli_query($db,$sqlupd) or die(mysqli_error($db));
print "$sqlupd<br>";
$jvno++;
}
}
print "<script> alert('Successfully Uploaded....!!!');</script>";

?>
</table>
