<?
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("includes/top.php");
include("opendb.php");
if(isset($_POST['upd']))
{
    $ff=$_REQUEST['ff'];
    
  if($ff !="none")
  {
       $imagename=$_FILES['ff']['name'];
       
       $filepath= "util/";
       $tmp = $_FILES['ff']['tmp_name'];
       move_uploaded_file($tmp, $filepath . $imagename) or die(' err:could not copy file');
        //print "File copied :".$ff;
   }
  
     $fp=fopen($filepath.$imagename,"r") or die("Cant open file");
  $ctr=1;
  $j=1;
  $invno_arr[]='';
  $invno_prv='';
    while(!feof($fp))
  {
     $line=fgets($fp,1024);
     $spline=explode("\t",$line);
    if($j==1)
    {
      $no_col=sizeof($spline);
      for($x=0;$x<=$no_col;$x++)
      { 
           if($spline[$x]=='Tran Date')
            $invno_no1 = $x;
      }
    }
    if($j>1)
    {
		$invno1 = trim($spline[$invno_no1]);
        $invno_prv=$invno1;
        $invdate = trim($spline[1]);
    }$j++;

    if(strlen($invdate)==10)
    {
     //   $invdate = implode('-', array_reverse(explode('-', $invdate)));
       // $invre=mysqli_query($db,"select id from psnindia_gen4psnauto_psn_psndb.upentry where brcode='PSN236' and date = '$invdate' and type = 'FB' ");
        //$row=mysqli_fetch_row($invre);
        //if(strlen($row[0])>0)
        //{
            //print "<script> alert('Data already Uploaded ....!!!');document.location='uploadtrans.php';</script>";
        //}
    }       
          
  }
  
  
  
  //-------------------------------------------------------------
  $fp=fopen($filepath.$imagename,"r") or die("Cant open file");
  
  $i=1;
  while(!feof($fp))
  {
    $line=fgets($fp,1024);
    $spline=explode("\t",$line);
     
    if($i==11)
    {
      $no_col=sizeof($spline);
      
      for($x=0;$x<=$no_col;$x++)
      {
         if($spline[$x]=='Tran Date')
         {
            $trdate_no = $x;
         }
         if($spline[$x]=='Particulars')
         {
            $partic_no = $x;
         }
         //print_r($partic_no);
         if($spline[$x]=='Value Date')
         {
            $valudt_no = $x;
         }
        if($spline[$x]=='Tran Type')
        {
            $trtyp_no = $x;
        }
        if($spline[$x]=='Cheque Details')
        {
            $chqdtls_no = $x;
        }
        if($spline[$x]=='Withdrawal')
        {
            $withdrw_no = $x;
        }
         if($spline[$x]=='Deposit')
         {
          $deposit_no = $x;
         }
         if(trim($spline[$x])=='Balance Amount')
         {
            $balance_no = $x;
         }
               }
    }
    if($i>11)
    {
        echo $i;
        $invdate = trim($spline[$trdate_no]);
        $invdate = implode('-', array_reverse(explode('-', $invdate)));
        $partic = trim($spline[$partic_no]);
        $valudt=trim($spline[$valudt_no]);
        $trtyp = trim($spline[$trtyp_no]);
        $chqdtls = trim($spline[$chqdtls_no]);
        $withdrw=trim($spline[$withdrw_no]);
        $deposit=trim($spline[$deposit_no]);
        $balance=trim($spline[$balance_no]);
        $deposit = str_replace(',', '', $deposit);
        $deposit = str_replace('"', '', $deposit);
        $withdrw = str_replace(',', '', $withdrw);
        $withdrw = str_replace('"', '', $withdrw);
        if($withdrw<='') $withdrw=0;
        if($deposit<='') $deposit=0;
        $k=$i-1;
        $deposit = trim($deposit,'"');
        $deposit = str_replace(',', '', $deposit);
        $balance = trim($balance,'"');
        $balance = str_replace(',', '', $balance); 
        if($balance<='')
        {
            $balance=0;
        }
        if(strlen($invdate)>0)
        {
            $invre=mysqli_query($db,"select id from psnindia_gen4psnauto_psn_psndb.upentry where brcode='PSN236' and date = '$invdate' and type = 'FB' and partic = '$partic' and dramt ='$withdrw' and cramt = '$deposit' and no = '$balance' ");
            $row=mysqli_fetch_row($invre);
            if(strlen($row[0])>0)
            {
                print "<tr><td>Already inserted date = $invdate, partic = '$partic', dramt ='$withdrw', cramt = '$deposit', no = '$balance' <br>";
            }
            else
            {
                $sql="insert into upentry (date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$invdate','0','FB','$partic','$withdrw','$deposit','test','$brcode','$trtyp','0','$balance',0)";
                mysqli_query($db,$sql) or die('Insert Err:'.mysqli_error($db));
                echo $sql;
                
                echo "<br>";
            }
        }
    }
    $i++;
  }
  
  //print "<script> alert('Successfully Uploaded....!!!');document.location='uploadtrans.php';</script>";
  print "<tr><td>Successfully uploaded.........!</td></tr>";
}
?>
<link rel="stylesheet" href="style.css" type="text/css" />
<style>
body,td{font-family:'calibri';letter-spacing:1;}
tr{background-color:rgb(95, 197, 187);}
.mnu{margin-left:5}
</style>
<center>
<br><br><br><br><br><br>
<form action='' method='post' ENCTYPE="multipart/form-data">
<table border=1 width=60%>
<tr><td>
<table width=100%>
<th colspan=2><h1>Upload Bank Statement(FB)</h1>
<tr><td>File <input type=file name='ff'><input type=submit value='Upload' name='upd'></td></tr>
<tr><td>
 </td></tr>
</table>
</td></tr>
</table>
</form>