<?
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;
   }
     $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]=='Purchase Order No.')
            $invno_no1 = $x;
      }
    }
    if($j>1)
    {
		 $invno1 = trim($spline[$invno_no1]);
    
    if($invno1!=$invno_prv)
    {
        $query="select tr_no from sparetransin_test where tr_no='$invno1' and brcode='$brcode'";
        $rs=mysqli_query($db,$query);
        if(mysqli_num_rows($rs)>0)
        {
            $invno_arr[$ctr++]=$invno1;
        }
    }
        $invno_prv=$invno1;
        
    }$j++;
  }
  
  

  //-------------------------------------------------------------
  $fp=fopen($filepath.$imagename,"r") or die("Cant open file");
  $invpr='';
  $i=1;
  while(!feof($fp))
  {
     $line=fgets($fp,1024);
     $spline=explode("\t",$line);
              if($i==1)
    {
      $no_col=sizeof($spline);
      for($x=0;$x<=$no_col;$x++)
      {
        if($spline[$x]=='Purchase Order Type')
          $ptype_no = $x;
        if($spline[$x]=='Purchase Date')
          $invdate_no = $x;
        if($spline[$x]=='Material')
          $itemno_no = $x;
        if($spline[$x]=='Material Descirption')
          $itemdesc_no = $x;
        if($spline[$x]=='Quantity')
          $qty_no = $x;
        if($spline[$x]=='Amount')
          $total_no = $x;
        if($spline[$x]=='Purchase Order No.')
          $grno_no = $x;
        if($spline[$x]=='Material Type')
          $itemtype_no = $x;
        if($spline[$x]=='Supplying Plant')
          $supplnt_no = $x;
        if($spline[$x]=='Purchasing Document')
          $purdoc_no = $x;
        if($spline[$x]=='Purchasing Doc. Type')
          $purdoctype_no = $x;
      }
    }
    if($i>1)
    {
          $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_escape_string($itemdesc);
          $qty = trim($spline[$qty_no]);
          $tot = trim($spline[$total_no]);
          $grno = trim($spline[$grno_no]);
          $qty = trim($qty,'"');
          $qty = str_replace(',', '', $qty);
          $tot = trim($tot,'"');
          $tot = str_replace(',', '', $tot);
          $grno = str_replace(',', '', $grno);
          $itemtype = trim($spline[$itemtype_no]);
          $supplnt = trim($spline[$supplnt_no]);
          $purdoc = trim($spline[$purdoc_no]);
          $purdctp = trim($spline[$purdoctype_no]); 
          
          
    if($supplnt>0 && $purdctp=='ZSTO')
    {
        $qry=mysqli_query($db,"select brcode from branch where brman='$supplnt'");
        $qry1=mysqli_fetch_row($qry);
        mysqli_query($db,"update sparetransin_test set from_branch='$qry1[0]' where brcode='$brcode' and tr_no='$purdoc'");
    }
    if($invdate_no<='' || $itemno_no<='' || $itemdesc_no<='' || $qty_no<='' || $total_no<='' || $grno_no<='' )
    {
        //print "<script>alert('You Are Selected a Wrong File...!!!');</script>";
        //return;
    }
    
      $k=$i-1;
      
      if($ptype=='ZSTO')
      {
          if(array_search($grno,$invno_arr)>0)
           {
               print "<font color='red'>Already Inserted.....!!!--".$grno."<br></font>";
           }
           else
           {
                            if($itemtype=='ZLUB')
                                $mitemtype='Lubricant';
                            else
                                $mitemtype='Spares';

        //$query=mysqli_query($db,"select brcode from branch where brman='$tobr'");
        //$row1=mysqli_fetch_array($query);

        $sql="insert into sparetransin_test (tr_no,trdate,entrydate,partcode,partname,qty,brcode,amount,saprefno) values('$grno','$invdate','$invdate','$itemno','$itemdesc','$qty','$brcode','$tot','$grno')";
        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....!!!');document.location='upld_transin.php';</script>";

}
?>
<link rel="stylesheet" href="style.css" type="text/css" />
<style>
body,td{font-family:'calibri';letter-spacing:1;}
tr{background-color:rgb(95, 197, 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 Transwer In</h1>
<tr><td>File <input type=file name='ff'><input type=submit value='Upload' name='upd'></td></tr>
<tr><td>Always download the spare purchases from SAP with below columns in ANY ORDER (Dealer Invoice no,Invoice date,Material,Material Desc.,<br>Delivered Qty,List price Per Unit,Category Discount Value,Total taxable,Handling charges,VAT Rate,VAT Amount,Grand Total,Material Type)<br>(Convert it to TAB TEXT Format and upload)
 </td></tr>
</table>
</td></tr>
</table>
</form>