<?
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");
  $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]=='Phys. Inventory Doc.')
            $invno_no1 = $x;
      }
    }
    if($j>1)
    {
		 $invno1 = trim($spline[$invno_no1]);
    
    if($invno1!=$invno_prv)
    {
        $query="select refno from adjstock where refno='$invno1' and brcode='$brcode'";
        
        $rs=mysqli_query($db,$query);
        if(mysqli_num_rows($rs)>0)
        {
            $invno_arr[$ctr++]=$invno1;
        }
    }
        $invno_prv=$invno1;
        
    }$j++;
  }
  
  
  
  //-------------------------------------------------------------
  $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]=='Phys. Inventory Doc.')
                    $ptype_no = $x;
          
                if($spline[$x]=='Material')
                    $pmatrial_no = $x;
                if($spline[$x]=='Plant')
                    $pbrcod_no = $x;
                if($spline[$x]=='Difference Quantity')
                    $itemqty_no = $x;
          
                if($spline[$x]=='Difference amount')
                    $itemdesc_no = $x;
        
            }
        }
        if($i>1)
        {
            $ptype = trim($spline[$ptype_no]);
          //print_r($ptype);
            //$invdate = trim($spline[$invdate_no]);
            $invdate = date('Y-m-d');
            $pmatrial = trim($spline[$pmatrial_no]);
            $pbrcod = trim($spline[$pbrcod_no]);
            $itemqty = trim($spline[$itemqty_no]);
            $itemdesc = trim($spline[$itemdesc_no]);
            
            
            
            $ptype = trim($ptype,'"');
            $ptype = str_replace(',', '', $ptype);
            $pmatrial = trim($pmatrial,'"');
            $pmatrial = str_replace(',', '', $pmatrial);
            $itemqty = trim($itemqty,'"');
            $itemqty = str_replace(',', '', $itemqty);
            $itemdesc = trim($itemdesc,'"');
            $itemdesc = str_replace(',', '', $itemdesc);
            
            
            
          if($itemdesc=='')
          {
            $itemdesc=0;
          }

            if($ptype_no<='' || $pmatrial_no<='' || $pbrcod_no<='' || $itemqty_no<='' || $itemdesc_no<='')
            {
                //print "<script>alert('You Are Selected a Wrong File...!!!');</script>";
                //return;
            }
            $k=$i-1;
            if($ptype>0)
            {
                if(array_search($ptype,$invno_arr)>0)
                {
                    print "<font color='red'>Already Inserted.....!!!--".$ptype."<br></font>";
                }
                else
                {
               /*
                            if($itemtype=='ZLUB')
                                $mitemtype='Lubricant';
                            else
                                $mitemtype='Spares';

    */
                    if($itemqty>0)
                    {
                        $qty=$itemqty;
                        $outqty=0;
                    }
                    if($itemqty<0)
                    {
                        $outqty=$itemqty;
                        $qty=0;
                    }

                    $sql="insert into adjstock (entrydate,brcode,refno,partcode,rate,qty,outqty,user) values('$invdate','$brcode','$ptype','$pmatrial','$itemdesc','$qty','$outqty','$user')";
                    //print $i.". ".$sql."<br>";
                    print "<font color='blue'>Successfuly Inserted.....!!!--".$ptype."<br></font>";
                    mysqli_query($db,$sql) or die('Insert Err:'.mysqli_error($db));
        print "<script> alert('Successfully Uploaded....!!!');document.location='upld_adjstock.php';</script>";
                }
            }
        }
    $i++;
  }
  

}
?>
<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 Adj Stock</h1>
<tr><td>File <input type=file name='ff'><input type=submit value='Upload' name='upd'></td></tr>
<tr><td>Always download the spare purchases from SAP with below columns in ANY ORDER (Dealer Invoice no,Invoice date,Material,Material Desc.,<br>Delivered Qty,List price Per Unit,Category Discount Value,Total taxable,Handling charges,VAT Rate,VAT Amount,Grand Total,Material Type)<br>(Convert it to TAB TEXT Format and upload)
 </td></tr>
</table>
</td></tr>
</table>
</form>