<?
include_once('includes/top.php');
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
//$br=$_SESSION['branch'];
if(strlen($user)<=0)
{
  print "<script> alert('Invalid User'); document.location='inde.php' </script>";
  return;
}
include_once('opendb.php');
if(isset($_POST['upd']))
{
       print $i=1;
   $ff=$_POST['ff'];
   if($ff !="none")
   {
       $target_dir = "upld/";
       $target_file = $target_dir . basename($_FILES["ff"]["name"]);
       $uploadOk = 1;
       $fl=$_FILES['ff']['name'];
       //echo $fl;
       if (move_uploaded_file($_FILES["ff"]["tmp_name"], $target_file)) {
           echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
       } else {
           echo "Sorry, there was an error uploading your file.";
       }
       print "=> File copied :".$fl."<br>";
   }
   $fp=fopen($target_file,"r") or die("Cant open file");
   $fp1=fopen($target_file,"r") or die("Cant open file");
   

  $invno1=1;    
print $i;
   $dt=date("Y-m-d H:i:s");
   
     $linee=fgets($fp1,1024);
      $splinee=explode("\t",$linee);
      
      $invno=trim($splinee[3]);
  
   
   while(!feof($fp))
   {
      $line=fgets($fp,1024);
      $spline=explode("\t",$line);
      
      $invno=trim($spline[3]);
      
      if($invno1!=$invno)
      {
          mysqli_query($db,"delete from sparetransout_eway where brcode='$brcode' and tr_no='$invno'") or die ('err'.mysqli_error($db));
       $my=("delete from sparetransout_eway where brcode='$brcode' and tr_no='$invno'");
      }
      $invno1=$invno;
      
      
             //print $dt1 = str_replace('.', '-', $spline[3]);
              $dt1 = $spline[4];
      
           $dt=explode("-",$dt1);
          // $dt=explode("/",$dt1);
           
           
          $rplant=trim($spline[12]);
          $ac=("select braddr,brcity,brphone from branch where brcode='$rplant'") ;
          
          
      
          
          
           $ac=mysqli_query($db,"select braddr,brcity,brphone from branch where brcode='$rplant'") or die ('err'.mysqli_error($db));
           $acrow=mysqli_fetch_row($ac);
          
          
          $invno=trim($spline[3]);
        // $mentrydate=trim("20".$dt[2])."-".trim($dt[1])."-".trim($dt[0]);
        $mentrydate=trim($dt[2])."-".trim($dt[1])."-".trim($dt[0]);
       
        
          $mpartcode="partcode";
          $mpartname="partname";
          $mqty=$spline[21];
           
         
         $mamount=$spline[22] ;
         //$mamount=preg_replace("/[^ \w]+/", "", $mamount);
          
          $mamount = str_replace('"', '', $mamount);
          $mamount = str_replace(',', '', $mamount);
           $uprice=$mamount/$mqty;
           $uprice = str_replace(',', '', $uprice);
         $mhsn=trim($spline[20]);
          $mbraddr=trim($spline[16]);
          $mbrcity=trim($spline[17]);
          $mbrpin=trim($spline[18]);
          $mslno=trim($spline[0]);
          
           
          
          if(strlen($invno>0))
          {
              print  $my=("insert into sparetransout_eway (tr_no,entrydate,partcode,partname,qty,brcode,amount,hsncode,braddr,brcity,brpin,telcqty) values ('".$invno."','".$mentrydate."','".$mpartcode."','".$mpartname."',".$mqty.",'".$brcode."',".$uprice.",'".$mhsn."','".$mbraddr."','".$mbrcity."','".$mbrpin."',".$i.")"); 
     mysqli_query($db,"insert into sparetransout_eway (tr_no,entrydate,partcode,partname,qty,brcode,amount,hsncode,braddr,brcity,brpin,telcqty) values ('".$invno."','".$mentrydate."','".$mpartcode."','".$mpartname."',".$mqty.",'".$brcode."',".$uprice.",'".$mhsn."','".$mbraddr."','".$mbrcity."','".$mbrpin."',".$i.")")or die ('err22'.mysqli_error($db));  
     
     
     
        
      }

}
 
  

 
   print "<script> alert('Done');</script>";
}
?>
<link type="text/css" href="includes/style1.css" rel="stylesheet" />
<link type="text/css" href="css/form1.css" rel="stylesheet" />
<style>
tr{background-color:c1d799}
.mnu{margin-left:5}
</style>
<center>
<br><br><br><br><br><br>
<form action='upld_ewaysto.php' method='post' ENCTYPE="multipart/form-data">
<table border=1>
<tr><td>
<table>
<th colspan=2>Upload SAP Data
<tr><td>File </td><td><input type=file name='ff'></td></tr>
<tr><td><input type=submit value='Upload' name='upd'></td></tr>
</table>
</td></tr>
</table>
</form>
