<?
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]))
{
   $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");
   $i=1;
   $dt=date("Y-m-d H:i:s");
   while(!feof($fp))
   {
      $line=fgets($fp,1024);

      $spline=explode("\t",$line);
      {
             /* $docdate=implode("-",array_reverse(explode(".",$spline[5])));
              $postdt=implode("-",array_reverse(explode(".",$spline[6])));
              //echo "<br>";
              //echo $docdate;
              //echo "<br>";
              //echo $postdt;
              //echo "<br>";
              $brnchcd= substr($spline[21],0,4);
              //echo $brnchcd;
              //echo "<br>";
              if($spline[15]=='Debit')
              {
                  $dramt=$spline[16];
                  $cramt=0;
                  $dbtvl='Dr';
              }
              else
              {
                  $dramt=0;
                  $cramt=$spline[16];
                  $dbtvl='Cr';
              }
              $mledgercode = $spline[8];
              $mledgerdescription = $spline[9];
              if($spline[10]>'')
              {
                  $mledgercode = $spline[10];
                  $mledgerdescription = $spline[11];
              }
              if($spline[12]>'')
              {
                  $mledgercode = $spline[12];
                  $mledgerdescription = $spline[13];
              }*/
            if($i>1)
              {
$cname = $spline[28];
$cname=str_replace('"','',$cname);
              print $sql="insert into supsap values('".addslashes(trim($spline[13]))."','$cname','".addslashes(trim($spline[17]))."','$spline[11]','$brcode')";
              //print ". ";
              mysqli_query($db,$sql) or die(mysqli_error($db));
             }
      }
$i++;
   }
   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='upldsup.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>
