<?
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= "";
       $tmp = $_FILES['ff']['tmp_name'];
       move_uploaded_file($tmp, $filepath . $imagename) or die(' err:could not copy file');
        //print "File copied :".$fl;
   }
     $filepathval=$filepath.$imagename;
     $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==4)
    {
     
        //print_r($spline);
        
        
       $no_col=sizeof($spline);
      for($x=0;$x<=$no_col;$x++)
      {
    
          
        if($spline[$x]=='Invoice No')
          $invno_no1 = $x;
          //print $invno_no1;
      }
    }
    if($j>3)
    {
 $invno1 = trim($spline[$invno_no1]);

//print $invno1;
    
    if($invno1!=$invno_prv)
    {
        
        
      $query="select pord_no from sparetransout where pord_no='$invno1'";
        //print $query;
        $rs=mysqli_query($db,$query);
        if(mysqli_num_rows($rs)>0)
        {
            $invno_arr[$ctr++]=$invno1;
        }
    }
        $invno_prv=$invno1;
        
    }$j++;
  }
  
  //mysqli_query($db,"delete from sparetransout_test");

  //-------------------------------------------------------------
  //$filepathval=$filepath.$imagename;
  $fp=fopen($filepath.$imagename,"r") or die("Cant open file");
  $invpr='';
  $i=1;
  while(!feof($fp))
  {
    $line=fgets($fp,1024);
    //print $line.$i++."<br>";
    
    $spline=explode("\t",$line);
    //print_r($spline);
    //echo $i;
    //echo "<br>";
    if($i==4)
    {
        //print "hi1";
    // print_r($spline);
       $no_col=sizeof($spline);
      
      for($x=0;$x<=$no_col;$x++)
      {
           if($spline[$x]=='STO Invoice Date')
         $invdate_no = $x;
          
        if($spline[$x]=='Invoice No')
          $ptype_no = $x;
        if($spline[$x]=='Material')
          $itemno_no = $x;
        if($spline[$x]=='')
          $itemdesc_no = $x;
        if($spline[$x]=='Receiving Dealer')
          $grno_no = $x;
        if($spline[$x]=='Supplying Dealer')
          $itemtype_no = $x;  
       

        if($spline[$x]=='pname')
          $partname_no = $x;

      }
    }
    if($i==3)
    {
        //print_r($spline);
        //print "hi2";
       $no_col=sizeof($spline);
    
      for($x=0;$x<=$no_col+1;$x++)
      {
            if($spline[$x]=='Invoice Qty')
          $qty_no = $x;
            if(trim($spline[$x])=='Net Order Value')
          $total_no = $x;
          
             if(trim($spline[$x])=='Tax Amount')
          $tax_no = $x;
          
      }
    }
    if($i>2)
    {
        
       // print "hi3";
       
          $qty = trim($spline[$qty_no]);
          $tot = trim($spline[$total_no]);
          
          $qty = trim($qty,'"');
          $qty = str_replace(',', '', $qty);
          //$tot = trim($tot,'"');
          print $tot."==";
          $tot = str_replace(',', '', $tot);
          $tot = str_replace('"', '', $tot);
          
          if($tot=='Net Order Value' || $tot=='INR')
            $tot=0;
          if(strlen($tot)<=0)
          {
              $tot=0;
          }
          if($qty<='')
          {
              $qty=0;
          }
          //echo $tot;
          //echo "<br>";
          //echo $qty;
          //echo "<br>";
          //print_r($spline);
        //exit;
        //$tot=0;
        
        //print $spline[$ptype_no]."== ($tot>0)&&($qty>0)[".$spline[$total_no]."= $total_no ][$qty_no]<br>";
        
        if(($tot>0)&&($qty>0))
        {
            //echo "round($tot / $qty,5)";
            $tot = round($tot / $qty,5);
        }
        else
        {
            $tot=0; 
            $qty=0;
        }
          $tax = trim($spline[$tax_no]);
          $tax = trim($tax,'"');
          $tax = str_replace(',', '', $tax);
          
    }   
    if($i>3)
    {
        //print_r($spline);
        //print "hi4";
          $ptype = trim($spline[$ptype_no]);
          $invdate = trim($spline[$invdate_no]);
          $invdate = str_replace('.', '-', $invdate);
          $invdate = implode('-', array_reverse(explode('-', $invdate)));
          $itemno = trim($spline[$itemno_no]);
          $itemdesc = trim($spline[$itemdesc_no]);
          //$itemdesc =mysqli_real_escape_string($db,$itemdesc);
          $itemdesc =mysqli_real_escape_string($db,$itemdesc);
          $grno = trim($spline[$grno_no]);
          $grno = str_replace(',', '', $grno);
          $itemtype = trim($spline[$itemtype_no]);
          $pname = trim($spline[$partname_no]);
    }


      $k=$i-1; 
          
      if($i>4 && $ptype!='')
      {
          //print "hi5";
               if($invdate_no<='' || $itemno_no<='' || $qty_no<='' || $total_no<='' || $grno_no<='')
               //if( $itemno_no<='' || $qty_no<='' || $total_no<='' || $grno_no<='')
                {
                   print $invdate."<br>1";
                    print $itemno."<br>2";
                     print $qty."<br>3";
                      print $tot."<br>4";
                       print $grno."<br>5";
                print "<script>alert('You $invdate_no have $itemno_no Selected $qty_no ab $total_no Wrong File...!!! $grno_no');</script>";
                
                                return;
                }
                
               
           if(array_search($ptype,$invno_arr)>0)
           {
               print "<font color='red'>Already Inserted.....!!!--".$ptype."<br></font>";
           }
           else
           {
//print "okk";

        //print "select b.brcode from branch b where b.brman='$grno'";
        $qry=mysqli_query($db,"select b.brcode from branch b where b.brman='$grno'");
        $qry1=mysqli_fetch_row($qry);
         $item=$qry1[0];
         $tobr=$qry1[0];
         //print "select brcode from branch where brman='$itemtype'";
        $qry1=mysqli_query($db,"select brcode from branch where brman='$itemtype'");
        $qry12=mysqli_fetch_row($qry1);
        $frbrcode=$qry12[0];
        
        $aprice=$qty*$tot;
        $gtot=$aprice+$tax;
        //print $qty;
        //print $tot;
         //print $aprice;
        //print $gtot;
        if($tax>0)
        {
        $taxp=$tax/$aprice*100;
        $taxp=round($taxp);
        }
        else
        {
          $taxp=0;  
        }
       
        //print $taxp;
        
 print  $sql="insert into sparetransout (tr_no,trdate,entrydate,partcode,partname,pord_no,qty,to_branch,brcode,amount,saprefno,grdate,taxamt,aprice,taxp,gtot,trfrtype) values('$ptype','$invdate','$invdate','$itemno','".addslashes($pname)."','$ptype','$qty','$tobr','$frbrcode','$tot','$ptype','0000-00-00','$tax','$aprice','$taxp','$gtot','normal')";
        print $i.". ".$sql."<br>";
        
        print "<font color='blue'>Successfuly Inserted.....!!!--".$invno."<br></font>";
        mysqli_query($db,$sql) or die('Insert Err:'.mysqli_error($db));
        
           }
      }
    
    $i++; 
  }
  print "<script> alert('Successfully Uploaded....!!!');</script>";
    
}
?>
<link rel="stylesheet" href="style.css" type="text/css" />
<style>
body,td{font-family:'calibri';letter-spacing:1;}
tr{background-color:rgb(95, 198, 547);}
.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 Transfer OUT</h1>
<tr><td>File <input type=file name='ff'><input type=submit value='Upload' name='upd'></td></tr>
<tr><td>(Convert it to TAB TEXT Format and upload)
 </td></tr>
</table>
</td></tr>
</table>
</form>