<?session_start();
$brcode = $_SESSION['brcode'];
$user=$_SESSION['user'];
include("includes/top.php");

include("opendb.php");
//print sizeof($a);
//$upd = $_REQUEST['upd'];
if(isset($_POST['upd']))
{
$ff = $_REQUEST['ff'];
if($ff !="none")
{
   /*$fl=$HTTP_POST_FILES['ff']['name'];
   $file_dir=".";
  $tmp_name=$_FILES['ff']['tmp_name'];
   move_uploaded_file($tmp_name,"./txtfiles/$fl") or die(' err:could not copy file'.$fl);
  print "File copied :".$fl;
  */
  
  $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("../txtfiles/$fl","r") or die("Cant open file");
$fp=fopen($filepath.$imagename,"r") or die("Cant open file");

$i=1;
//include("includes/top.php");

include("opendb.php");
mysqli_query($db,"delete from sapclstk where brcode='$brcode'");
$dt=date("Y-m-d H:i:s");
$i=1;
while(!feof($fp))
{
 $line=fgets($fp,1024);
 $spline=explode("\t",$line);
if(strlen($spline[0])>0)
{
     $itemcode=$spline[0];
    $itemdesc=$spline[1];
     $qty=$spline[3];
    $ndp=$spline[5];
     $map=$spline[6];
    $tot=$spline[16];
    
    $itemcode=remove_special($itemcode);
    $itemdesc=remove_special($itemdesc);
     $qty=remove_special($qty);
    $ndp=remove_special($ndp);
     $map=remove_special($map);
    $tot=remove_special($tot);
    
    $itemcode = trim($itemcode, '"');
    $itemdesc = trim($itemdesc, '"');
    $qty = trim($qty, '"');
    $ndp = trim($ndp, '"');
    $map = trim($map, '"');
    $tot = trim($tot, '"');
    
    $qty = str_replace(',', '', $qty);
    $ndp = str_replace(',', '', $ndp);
    $map = str_replace(',', '', $map);
    $tot = str_replace(',', '', $tot);
    
$sql="insert into sapclstk values('".(trim($itemcode))."','".(trim($itemdesc))."','".(trim($qty))."','".(trim($ndp))."','".(trim($map))."','".(trim($spline[8]))."','".(trim($tot))."');";
print $sql."<br>";

//print $i++.". ".$sql."<br>";
//mysqli_query($db,$sql) or die('Insert Err:'.mysqli_error($db));
}
}
print "<script> alert('Done');</script>";
}

function remove_special($str){
     $newstr = "";
     for ($ctr=0;$ctr<=strlen($str);$ctr++)
     {
         if(substr($str,$ctr,1)>=' ' && substr($str,$ctr,1)<='}')
         {
             if(substr($str,$ctr,1)=="'")
                $ggg=0;
             else
                $newstr=$newstr.substr($str,$ctr,1);
         }
     }
     return $newstr;
}
?>

<link rel="stylesheet" href="style.css" type="text/css" />
<style>
body,td{font-family:'arial narrow';letter-spacing:1;}
//etr{background-color:c1d799}
.mnu{margin-left:5}
</style>
<center>
<br><br><br><br><br><br>
<form action='<?print $PHP_SELF?>' method='post' ENCTYPE="multipart/form-data">
<table border=0 width=60% cellspacing=1 cellpadding=1 bgcolor='darkbrown'>
<tr><td align=center bgcolor=white>
<table >
<th colspan=2 bgColor=c1d799>Upload SAP Closing Stock
<tr><td>File </td><td><input type=file name='ff'></td></tr>
<tr bgColor='lightsteelblue'><td align=center colspan=2 ><input type=submit value='Upload' name='upd'></td></tr>
</table>
</td></tr>
</table>
</form>
