<?
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]=='GRN No')
            $invno_no1 = $x;
      }
    }
    if($j>1)
    {
		 $invno1 = trim($spline[$invno_no1]);
    
    if($invno1!=$invno_prv)
    {
        $query="select GRNNo from eicherpurchase where GRNNo='$invno1' and brcode='$brcode'";
        $rs=mysqli_query($db,$query);
        if(mysqli_num_rows($rs)>0)
        {
            $invno_arr[$ctr++]=$invno1;
        }
    }
        $invno_prv=$invno1;
        
    }$j++;
  }
  
  $acpd=mysqli_query($db,"select * from acperiod") or die ('err'.mysqli_error($db));
     $acrow=mysqli_fetch_row($acpd);

      $acf= $acrow[0];
      $act= $acrow[1];
  
  //-------------------------------------------------------------
  $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]=='S.NO')
          $stotype_no = $x;
          if($spline[$x]=='Vendor Name')
          $suplplant_no = $x;
          
          if($spline[$x]=='Invoice No')
          $invno_no = $x;
          if($spline[$x]=='Invoice Date')
          $invdate_no = $x;
          if($spline[$x]=='Inbound Delivery')
          $dlrinvno_no = $x;
       
          if($spline[$x]=='GRN No')
          $grno_no = $x;
         if($spline[$x]=='GRN Date')
          $grndate_no = $x;
          
         if($spline[$x]=='Tax Type')
            $stotaxtype_no = $x;
         if($spline[$x]=='Tax %')
          $stotaxp_no = $x;
          
         if($spline[$x]=='Taxable Amt')
          $btamt_no = $x;
         if($spline[$x]=='Tax Amt')
          $taxamt_no = $x;
          
         if($spline[$x]=='Handling Charges')
          $hand_no = $x;
          if($spline[$x]=='Discount')
          $disc_no = $x;
         if($spline[$x]=='Other Charges')
          $other_no = $x;
        
           if(trim($spline[$x])=='Invoice Amt')
          $invamount1_no = $x;
         
         
               }
    }
    if($i>1)
    {
          $stotype = trim($spline[$stotype_no]);
         $suplplant = trim($spline[$suplplant_no]);
         $invno = trim($spline[$invno_no]);
          $invdate = trim($spline[$invdate_no]);
          $invdate = str_replace('/', '-', $invdate);
          $invdate = implode('-', array_reverse(explode('-', $invdate)));
           $dlrinvno = trim($spline[$dlrinvno_no]);
           $grno = trim($spline[$grno_no]);
           $grndate = trim($spline[$grndate_no]);
          $grndate = str_replace('/', '-', $grndate);
          $grndate = implode('-', array_reverse(explode('-', $grndate)));
            $stotaxtype = trim($spline[$stotaxtype_no]);
          $stotaxp = trim($spline[$stotaxp_no]);
             //$invdate = date("Y-m-d", strtotime($invdate));
         
          $btamt = trim($spline[$btamt_no]);
          $btamt = trim($btamt,'"');
          $btamt = str_replace(',', '', $btamt);
          
          $taxamt = trim($spline[$taxamt_no]);
          $taxamt = trim($taxamt,'"');
          $taxamt = str_replace(',', '', $taxamt);
          
          $hand = trim($spline[$hand_no]);
          $hand = trim($hand,'"');
          $hand = str_replace(',', '', $hand);
          
          $disc = trim($spline[$disc_no]);
          $disc = trim($disc,'"');
          $disc = str_replace(',', '', $disc);
          
          $other = trim($spline[$other_no]);
          $other = trim($other,'"');
          $other = str_replace(',', '', $other);
          
          $invamount1 = trim($spline[$invamount1_no]);
          $invamount1 = trim($invamount1,'"');
          $invamount1 = str_replace(',', '', $invamount1);
          
        // $invoicevalue = trim($invoicevalue,'"');
          //$invoicevalue = str_replace(',', '', $invoicevalue);
       
          // $suplplantname=mysqli_escape_string($suplplantname);
          
         //$stotype_no<=''||
         
    if( $invdate_no<='' || $invno_no<='' )
    {
        print "<script>alert('You Are Selected a Wrong File...!!!');</script>";
        return;
    }
      $k=$i-1;
      if(strlen($grno)>0)
      {
          if($suplplant_no!='')
      {
          if(array_search($grno,$invno_arr)>0)
           {
               print "<font color='red'>Already Inserted.....!!!--".$grno."<br></font>";
           }
           else
           {
        if($invpr!=$grno) 
        {
        $query=mysqli_query($db,"select max(purno) from eicherpurchase where brcode='$brcode'");
        $row=mysqli_fetch_row($query);
        if(mysqli_num_rows($query)<=0)
            $purno=1;
        else
            $purno=$row[0]+1;
        }
                         
          if($grndate>=$acf && $grndate<=$act)
             {
        $sql="insert into eicherpurchase (vendorCode,InvoiceNo,InvoiceDate,GRNNo,GRNDate,InboundNumber,BTAmt,Taxp,TaxType,TaxAmt,Hand,Disc,Other,InvAmt,purno,brcode)  
        values('$suplplant','$invno','$invdate','$grno','$grndate','$dlrinvno','$btamt','$stotaxp','$stotaxtype','$taxamt','$hand','$disc','$other','$invamount1','$purno','$brcode')";
    
    
    
        print $i.". ".$sql."<br>";
        //print "<font color='blue'>Successfuly Inserted.....!!!--".$invno."<br></font>";
       mysqli_query($db,$sql) or die('Insert Err:'.mysqli_error($db));
        $cname="";
             }
           }
      }
      }
    }
    $invpr=$grno;
    $i++;
  }
 print "<script> alert('Successfully Uploaded....!!!');document.location='upld_eicherpur.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, 117);}
.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 Eicher Purchase </h1>
<tr><td>File <input type=file name='ff'><input type=submit value='Upload' name='upd'></td></tr>
<tr><td>This a user format and cannot be downloaed from sap<br>(Convert it to TAB TEXT Format and upload)
 </td></tr>
</table>
</td></tr>
</table>
</form>