<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='index.php'; </script>";
 return;
}
     include_once("opendb.php");
     include_once("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($db));
     $r=mysqli_fetch_row($us);
     $user_level=$r[0];
     $savecount=$_REQUEST['savecount'];
     if($savecount==0)
     {
         $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($db));
             $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;
             }
         }

        $i=0;

//***********************
       $dr_tot=0;
       $cr_tot=0;

       $i=0;
       $z=0;
       $cr=$_REQUEST['cr'];
       foreach($cr as $a)
       {
         $cr[$i]=$a; 
         $i++;
         if($a=='')
         $z++;
       }
       
/*
      if(count(array_unique($cr))<count($cr))
      {
        echo "Name has duplicates";
        exit;
      }
*/  
      $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;
      $dragain=$_REQUEST['dragain'];
      foreach($dragain as $a)
      {
        $dragain[$i]=$a;$i++;
      }
      $i=0;
      $prftcntdr=$_REQUEST['prftcntdr'];
      foreach($prftcntdr as $a)
      {
        $prftcntdr[$i]=$a;$i++;
      }
      
      $i=0;
      $cr_amt=$_REQUEST['cr_amt'];
      foreach($cr_amt as $a)
      {
        $cr_amt[$i]=$a;
        $cr_tot+=$a;
        $i++;
      }
      
     
      $docdtvl=$_REQUEST['docdtvl'];
     
     $docno=$_REQUEST['docno'];


      $jtype=$_REQUEST[jtype];
      $invno=$_REQUEST[invno];

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

      if($z>0){
          echo "Error!! The A/c Head Must Not Be Blank";
         exit;
       } 
       if($jtype=='select')
       {
          echo "Error!! Please select Transaction Type";
          exit;
       }

       $flg=0;
       $flg2=0;
       $flg3=0;
       $flg4=0;
       for($j=0;$j<$i;$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('myTable').rows[$k].cells[0].style.color='red';</script>";
            $flg++;
          }  
          if($cr[$j]=='')
          {
            print "<script>document.getElementById('myTable').rows[$k].cells[0].style.color='red';</script>";
            $flg++;
          }
         
          $jrqry=mysqli_query($db,"select tr_no from journal2 where brcode='$brcode' and j_type='$jtype' and tr_no='$invno'");
          if(mysqli_num_rows($jrqry)<1)
          {
	    $invno=$invno; 
          }
          else
          {
	    $flg3++;
          }

          $qr=mysqli_query($db,"select * from transactmast where brcode='$brcode' and trnsid='$jtype'");
          $qurow=mysqli_fetch_array($qr);
          $trnrqr=$qurow[3];
          if($trnrqr=='1')
          {
            $res=mysqli_query($db,"select drcr from ledger where led='$cr[$j]' and brcode='$brcode'");
            $ledrw=mysqli_fetch_array($res);
            $ldgtp=$ledrw[0];
            if(($ldgtp=='Dr')&&($cr_amt[$j]>0))
            {
               $flg2++;
            }
            if(($ldgtp=='Cr')&&($dr_amt[$j]>0))
            {
               $flg2++;
            }

            $sql="select col3,col4,col5,col6,col7,col8,col9,col10 from reportmast where brcode='$brcode' and trnstype='$jtype'";
//echo $sql;
            $res=mysqli_query($db,$sql);
            $rw=mysqli_fetch_array($res);

            $re=mysqli_query($db,"select * from ledger where led='$cr[$j]' and brcode='$brcode' and grp IN('$rw[0]','$rw[1]','$rw[2]','$rw[3]','$rw[4]','$rw[5]','$rw[6]','$rw[7]')") or die("ERR6:".mysqli_error($db));

            if(mysqli_num_rows($re)<=0)
            {
           
                $flg4++;
            } 
         }

/*
        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>";
          
        }
*/
     }

     if($flg>0)
     {
       echo "<small>Check the  Dr of Red marked row(s)</br></small>";
     }
     elseif($flg2>0)
     {
       echo "<small>Wrong Debit/Credit amount </small>";
     }
     elseif($flg3>0)
     {
       echo "<small>Invoice No. Already exists</small>";
     }
     elseif($flg4>0)
     {
       echo "<small>Invalid ledger</small>";
     }
     else
     {

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


    $flg1=0;
    $flg3=0;
    
/*
    $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);
*/
    $tim=date("d-m-Y*H:i:s");
    $jvno=$brcode.'*'.$user.'*'.$tim;

    for($j=0;$j<$i;$j++)
    {
       if($docdtvl=='')
       {
       $docdtvl='0000-00-00';
       }
       $sql="insert into journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no,prftcntr,docdt) values('$dt','$jvno','Dr','$cr[$j]','$dr_amt[$j]','$cr_amt[$j]','".addslashes(htmlspecialchars($dr_narr[$j]))."','$brcode','$jtype','$dragain[$j]','$docno','$invno','$prftcntdr[$j]','$docdtvl')";
      mysqli_query($db,$sql)or die(mysqli_error($db));
   }


   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='journew_17032017.php?jtype=$jtype';</script>";
   
       //print "<script> window.open('prnvoucher.php?jvno=$jvno'); </script>";
    }

//include("jour.php");
  }

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