<?
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");
$acpd=mysqli_query($db,"select * from acperiod") or die ('err'.mysqli_error($db));
     $acrow=mysqli_fetch_row($acpd);

      $acf= $acrow[0];
      $act= $acrow[1];
      
      $fag10=0;
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='';
  $invno_arr2[]='';
  $invno_prv2='';

  
  
  
  //-------------------------------------------------------------
  $fp=fopen($filepath.$imagename,"r") or die("Cant open file");
  mysqli_query($db,"delete from sapclstk where brcode='$brcode'");
  $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]=='Material Number')
          $itemno_no = $x;
         if($spline[$x]=='Material Description')
          $itemdesc_no = $x;
         if($spline[$x]=='Unrestricted')
          $qty_no = $x;
         if($spline[$x]=='Moving Avg Price')
          $lstprice_no = $x;
         
          if(trim($spline[$x])=='Value Unrestricted')
          $total_no = $x;
       
       
          if($spline[$x]=='Plant')
          $dlco_no = $x;
         
               }
    }
    if($i>1)
    {
         
          
          $itemno = trim($spline[$itemno_no]);
          $itemdesc = trim($spline[$itemdesc_no]);
          $itemdesc =mysqli_escape_string($db,$itemdesc);
          $qty = trim($spline[$qty_no]);
          $lstprice = trim($spline[$lstprice_no]);
          
          $tot = trim($spline[$total_no]);
          
          $dlco = trim($spline[$dlco_no]);
         
         
          
         
          $qty = trim($qty,'"');
          $qty = str_replace(',', '', $qty);
          $lstprice = trim($lstprice,'"');
          $lstprice = str_replace(',', '', $lstprice);
          
          $tot = trim($tot,'"');
          $tot = str_replace(',', '', $tot);
         
          $dlco = str_replace(',', '', $dlco);
          
         

       
       if(strlen($dlco)<4)
            $dlco=substr($invno,0,4);
       $pq="select brman from branch where brman='$dlco' and brcode='$brcode'";
       //print $pq;
       if($dlco!='')
       {
        $query=mysqli_query($db,"select brman from branch where brman='$dlco' and brcode='$brcode'");
        if(mysqli_num_rows($query)<=0 )
        {
            print "<script>alert('You Are Selected a Wrong Branch...!!!');</script>";
            return;
        }
       }
       //*****************************
         print $itemno_no."itemno <br>";
      print $itemdesc_no." itemdes <br>";
      print $qty_no."qty <br>";print $lstprice_no."list <br>";
      //*************************************************************
        if( $itemno_no<='' || $qty_no<='' || $lstprice_no<='')
        
        {
            print "<script>alert('You Are Selected a Wrong  File 2...!!!');</script>";
            return;
        }
       
      $k=$i-1;
      //echo "<br>";
      //echo $invdate."test";
     // echo "<br>";
      //echo $acf."test";
     // echo "<br>";
      //echo $act."test";
      //echo "<br>";
      if(strlen($itemno_no)>0 && strlen($qty_no)>0)
      {
          //print "hello";
        
        
                            
                            $itemdesc=remove_special($itemdesc);
        $sql="insert into sapclstk (partcode,partname,qty,ndp,map,brcode,total) 
        values('$itemno','$itemdesc','$qty','$lstprice','$lstprice','$brcode','$tot')";
         print $sql."<br>";
        mysqli_query($db,$sql) or die('Insert Err:'.mysqli_error($db));
         print "<font color='blue'>Successfuly Inserted.....!!!--".$itemno."<br></font>";
      
          
      }
      else
      {
          //print "no";
      }
     
    }
    $i++;
  }
  print "<script> alert('Successfully Uploaded....!!!');document.location='upld_sapstock1.php';</script>";

}
function remove_special($str){
         $newstr = "";
         for ($ctr=0;$ctr<=strlen($str);$ctr++)
         {
             if(substr($str,$ctr,1)>=' ' && substr($str,$ctr,1)<='}')
                  $newstr=$newstr.substr($str,$ctr,1);
         }
         return $newstr;
     }
?>
<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 SAP Stock</h1>
<tr ><td bgcolor='yellow'><font color='red'><blink>
 </blink></font></td></tr>

<tr><td>File <input type=file name='ff'><input type=submit value='Upload' name='upd'></td></tr>
<tr><td>Always download the Stock from SAP  <br>(Convert it to TAB TEXT Format and upload)
 </td></tr>
</table>
</td></tr>
</table>
</form>