<? 
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='index.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");
  
  $i=1;
  $j=1;
  $invno_prv1='';
  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(trim($spline[$x])=='Ref. Document #')
		  $invno_no = $x;
		  
	 	 if($spline[$x]=='Debit Amount')
		  $amt_t = $x;
		  
		  	 if($spline[$x]=='Posting Date')
		     $p_date = $x;
		  
		  
               }
    }
    
    if($i>1)
    {
        $invno = trim($spline[$invno_no]);
        $pdate = trim($spline[$p_date]);
        
      $in=substr($invno,0,2);
        
        if($in=='MP' || $in=='KA')
        {
          $amt = trim($spline[$amt_t]);
        
           
               
      $k=$i-1;
      
           
    
	if($amt>0)
	{
	    
	    
	    print $query="select invno from purchreco where invno='$invno'";

        $rs=mysqli_query($db,$query);
        if(mysqli_num_rows($rs)>0)
        {
             print "<font color='red'>Already Inserted.....!!!--".$invno."<br></font>";
             $amt>0;
             
        } 
	    else
	    {
	        
	        
	        
	        $pd=explode(".",$pdate);
	        
	        $dt= $pd[2]."-".$pd[1]."-".$pd[0];
	        
	  $sql1="insert into purchreco (invno,invdate,amount,brcode,reco) values('$invno','$dt','$amt','$brcode','n')";
 	      print "$sql1<br>";
 	       mysqli_query($db,$sql1) or die('Insert Err1:'.mysqli_error($db));
 	       
 	       $amt=0;  
	    }
	    
	    
	}


	
        
      }
    }
    $invno_prv1=$invno;
    $i++;
  }
  //$sqlupd="update warspareinvoice set tax='15.00' where tax='14.00' and brcode='$brcode'";
 	      // mysqli_query($db,$sqlupd) or die('update Err1:'.mysqli_error($db));



  print "<script> alert('Successfully Uploaded....!!!');document.location='upldpur_reco.php';</script>";
  return;

}
?>
<link rel="stylesheet" href="style.css" type="text/css" />
<link rel="stylesheet" href="css/form.css" type="text/css" />
<style>
body,td{font-family:'calibri';letter-spacing:1;}
tr{background-color:rgb(95, 197, 17);}
.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><h3>Upload VECV Portal Invoices-Purchase </h3>
<tr><td>File <input type=file name='ff'><input type=submit value='Upload' name='upd'></td></tr>
<tr><td>Always download the Jobcard invoices from SAP with below columns in ANY ORDER (Job Card No.,Invoice Date,Claim Number,Billing Type,<br>Item Type,Customer Name,Item No.,Item Description,Quantity Billed,List Price,Net Taxable Amount,Discount Amount,Tax Rate(%),Handling,Tax Amount,Total Claim value,MRP,Dealer Code)<br>(Convert it to TAB TEXT Format and upload)
 </td></tr>
</table>
</td></tr>
</table>
</form>

