<?
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");
$m=$_REQUEST['m'];
$d=$_REQUEST['d'];
$y=$_REQUEST['y'];
$us=mysqli_query($db,"select level from bruser where username='$user'")or die(mysqli_error());
$r=mysqli_fetch_row($us);
$user_level=$r[0];
if($savecount==0)
{
echo "hi";
$dt=$y.'-'.$m.'-'.$d;
if($user_level<=4)
{
$sq="select to_days(curdate())-to_days('$dt')";
$dtre=mysqli_query($db,$sq)or die(mysqli_error());
$dtrow=mysqli_fetch_row($dtre);

if($dtrow[0]>2 || $dtrow[0]<0)
{
 print "<script> alert('Invalid Date, Entry possible for last 2 days only'); history.back(1); </script>";
 return;
}
}

$dt=$y.'-'.$m.'-'.$d;
$i=0;
/*if(count($dr)<=0)
{
 print "<script> alert('Invalid Entry - Debit'); history.back(1); </script>";
 return;
}
if( count($cr)<=0)
{
 print "<script> alert('Invalid Entry - Credit'); history.back(1); </script>";
 return;
}
$dr=$_REQUEST['dr'];
foreach($dr as $a)
{
 $drs[$i]=$a; 
 $i++;
}


for($x=0;$x<$i;$x++)
{
  for($y=0;$y<$x;$y++)
     { 
         if($drs[$x]==$drs[$y])
           print "<script>alert('Duplicate entry - $drs[$x]'); history.back(1) </script>";
     }
}


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


for($x=0;$x<$i;$x++)
{
  for($y=0;$y<$x;$y++)
     { 
         if($crs[$x]==$crs[$y])
           print "<script>alert('Duplicate entry - $crs[$x]'); history.back(1) </script>";
     }
}


for($x=0;$x<$i;$x++)
{
  for($y=0;$y<$i;$y++)
     { 
        if($drs[$x]==$crs[$y])
          print "<script> alert('Invalid Entry - $drs[$x]'); history.back(1) </script>";
     }
}
$dr_amt=$_REQUEST['dr_amt'];
foreach($dr_amt as $a)
{ 
 if($a<=0)
 {
           print "<script> alert('Invalid Amount - $a'); history.back(1) </script>";
 }
}
$cr_amt=$_REQUEST['cr_amt'];
foreach($cr_amt as $a)
{ 
 if($a<=0 )
 {
           print "<script> alert('Invalid Amount - $a'); history.back(1) </script>";
 }
}
*/
//***********************
 $dr_tot=0;
 $cr_tot=0;

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

$i=0;
$dr_amt=$_REQUEST['dr_amt'];
foreach($dr_amt as $a)
{
 $dr_amt[$i]=$a; 
 $dr_tot+=$a;
 $i++;
}
$i=0;
$dr_narr=$_REQUEST['dr_narr'];
foreach($dr_narr as $a)
{
 $dr_narr[$i]=addslashes($a); 
 $i++;
}
$i=0;
$ag=$_REQUEST['ag'];
foreach($ag as $a)
{
 $again[$i]=$a;$i++;
}
$i=0;
$dragain=$_REQUEST['dragain'];
foreach($dragain as $a)
{
 $dragain[$i]=$a;$i++;
}
$i=0;
$dragval=$_REQUEST['dragval'];
foreach($dragval as $a)
{
 $dragval[$i]=$a;$i++;
}




$p=0;
$c=0;
$cr=$_REQUEST['cr'];	
foreach($cr as $a)
{
 $cr[$p]=$a; 
 $p++;
 if($a=='')
      $c++;

}


$p=0;
$cr_amt=$_REQUEST['cr_amt'];
foreach($cr_amt as $a)
{
 $cr_amt[$p]=$a; 
 $p++;
 $cr_tot+=$a;
}


$p=0;
$cr_narr=$_REQUEST['cr_narr'];
foreach($cr_narr as $a)
{
 $cr_narr[$p]=addslashes($a); 
 $p++;
}

$p=0;
$cragain=$_REQUEST['cragain'];
foreach($cragain as $a)
{
 $cragain[$p]=$a;$p++;
}
$p=0;
$cragval=$_REQUEST['cragval'];
foreach($cragval as $a)
{
 $cragval[$p]=$a;$p++;
}

if($tdamt!=$tcamt){
    echo "Error!!  The Total Amount From Both Sides Are Not Matching";
    exit;
 } 

 if(($d>0)||($c>0)){
    echo "Error!!  The A/c Head From Both Sides Must Not Be Blank";
    exit;
 } 


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

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

    {
    print "<script>document.getElementById('myTable').rows[$k].cells[0].style.color='green';</script>";
 $flg++;
    }
        if(($dr_amt[$j]=='')||($dr_amt[$j]=='0')||($dr_narr[$j]=='')){

  
    print "<script>document.getElementById('myTable').rows[$k].cells[0].style.color='red';</script>";
echo $dr_narr[$j]."<br>";
$flg2++;
    }
}

if($flg>0)
 {
  echo "<small>Check the  Dr of Red marked row(s)</br></small>";
 }
 elseif($flg2>0)
 {
  echo "<small>Enter Amount/Narration of Red marked row(s)</small>";
 }
else
{

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


$flg1=0;
$flg3=0;

for($j=0;$j<$p;$j++)
{
 $k=$j+1;
 $qry=mysqli_query($db,"select * from ledger where led='$cr[$j]' and brcode='$brcode'");
 if(mysqli_num_rows($qry)<=0)
 {

  print "<script>document.getElementById('myTable2').rows[$k].cells[0].style.color='blue';</script>";
  $flg1++;
 }
  
   if($cr[$j]=='')

    {
    print "<script>document.getElementById('myTable2').rows[$k].cells[0].style.color='green';</script>";
 $flg1++;
    }

     if(($cr_amt[$j]=='')||($cr_amt[$j]=='0')||($cr_narr[$j]==''))

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

if($flg1>0)
 {
  echo "<small>Check the  Cr of Red marked row(s)</br></small>";
 }
elseif($flg3>0)
 {
  echo "<small>Enter Amount/Narration of Red marked row(s)</small>";
 }
else
{



$res=mysqli_query($db,"select max(jvno) from journal2 where brcode='$brcode'");
 $row=mysqli_fetch_row($res);
 if($row[0]<=0)
  $jvno=1;
 else
  $jvno=($row[0]+1);


for($j=0;$j<$i;$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]','$dr_amt[$j]','0','".addslashes(htmlspecialchars($dr_narr[$j]))."','$brcode','JV','$dragain[$j]','$dragval[$j]','')";
 mysqli_query($db,$sql)or die(mysqli_error());
}

for($j=0;$j<$p;$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','$cr_amt[$j]','".addslashes(htmlspecialchars($cr_narr[$j]))."','$brcode','JV','$cragain[$j]','$cragval[$j]','')";
mysqli_query($db,$sql) or die(mysqli_error());
}
log_entry($brcode,$user,$REMOTE_ADDR,"Journal Entry - $jvno");
$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 this entry</font><br>";
 exit;
}
else
print "<font color=green>Data has been posted</font><br>";
echo "<script> document.getElementById('savecount').value=1;document.location='jour.php';</script>";
log_entry($brcode,$user,$REMOTE_ADDR,"Journal JVNo: $jvno, jvNo: $jvno");
print "<script> window.open('prnvoucher.php?jvno=$jvno'); </script>";

//include("jour.php");
}
}
}
elseif($savecount==1)
{
echo "Data already saved";
  echo "<script> document.location='jour.php';</script>";
}
?>
