<?
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");

$acpd=mysqli_query($db,"select * from acperiod") or die ('err'.mysqli_error($db));
     $acrow=mysqli_fetch_row($acpd);

      $acf= $acrow[0];
      $act= $acrow[1];
      
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(trim($spline[$x])=='GRN No.')
            $invno_no1 = $x;
      }
    }
    if($j>1)
    {
		 $invno1 = trim($spline[$invno_no1]);
    //print $j.". ".$invno1."<br>";
    if($invno1!=$invno_prv)
    {
        
        $query="select GRNNo from transferin where GRNNo='$invno1' and brcode='$brcode'";
        //print $query;
        //print $j.". ".$query."<br>";
        $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]=='STO Order Type')
          $stotype_no = $x;
          if($spline[$x]=='STO Order Description')
          $stoorderdesc_no = $x;
 
          if($spline[$x]=='Stock Transfer Order')
            $stoorderno_no = $x;
         if($spline[$x]=='STO Date')
          $stodate_no = $x;
         if($spline[$x]=='Supplying Plant Code')
          $suplplant_no = $x;
         if($spline[$x]=='Supplying Plant Name')
          $suplplantname_no = $x;
         if($spline[$x]=='Receiver Plant')
          $recplant_no = $x;
         if($spline[$x]=='Receiver Plant Name')
          $recplantname_no = $x;
          if($spline[$x]=='Invoice No.')
          $invno_no = $x;
          if($spline[$x]=='Invoice Date')
          $invdate_no = $x;
         if($spline[$x]=='Invoice Taxable Value')
          $invoicevalue_no = $x;
         if($spline[$x]=='Dealer Invoice Number')
          $dlrinvno_no = $x;
         if($spline[$x]=='GRN No.')
          $grno_no = $x;
         if($spline[$x]=='GRN Date')
          $grndate_no = $x;
         if($spline[$x]=='GR AMOUNT')
          $gramount_no = $x;
         if($spline[$x]=='Integrated GST (%)')
          $igstp_no = $x;
         if($spline[$x]=='Integrated GST')
          $igstamt_no = $x;
         
               }
    }
    if($i>1)
    {
          $stotype = trim($spline[$stotype_no]);
           $stoorderdesc = trim($spline[$stoorderdesc_no]);
           $stoorderno =trim($spline[$stoorderno_no]);
            $stodate= $spline[$stodate_no];
          $stodate = trim($spline[$stodate_no]);
          $stodate = str_replace('/', '-', $stodate);
          $stodate = implode('-', array_reverse(explode('-',$stodate)));
         //$stodate = date("Y-m-d", strtotime($stodate));
         
          $suplplant = trim($spline[$suplplant_no]);
          $suplplantname = trim($spline[$suplplantname_no]);
          $recplant = trim($spline[$recplant_no]);
          $recplantname = trim($spline[$recplantname_no]);
          $invno = $spline[$invno_no];
          $invdate = trim($spline[$invdate_no]);
          $invdate = str_replace('/', '-', $invdate);
          $invdate = implode('-', array_reverse(explode('-', $invdate)));
         //$invdate = date("Y-m-d", strtotime($invdate));
         
          $invoicevalue = trim($spline[$invoicevalue_no]);
          $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)));
         //$grndate = date("Y-m-d", strtotime($grndate));
         
          $gramount = trim($spline[$gramount_no]);
          $gramount = trim($gramount,'"');
          $gramount = str_replace(',', '', $gramount);
          
         $invoicevalue = trim($invoicevalue,'"');
          $invoicevalue = str_replace(',', '', $invoicevalue);
       
           $suplplantname=mysqli_escape_string($suplplantname);
          
          
          $igstp = trim($spline[$igstp_no]);
          $igstp = trim($igstp,'"');
          $igstp = str_replace(',', '', $igstp);
       
       $igstamt = trim($spline[$igstamt_no]);
          $igstamt = trim($igstamt,'"');
          $igstamt = str_replace(',', '', $igstamt);
       
         //$stotype_no<=''||
         
         
         $query=mysqli_query($db,"select brman from branch where brman='$recplant' and brcode='$brcode'");
        if(mysqli_num_rows($query)<=0 && $recplant!='')
        {
            print "<script>alert('You Are Selected a Wrong Branch...!!!');</script>";
            return;
        }
        
    if( $invdate_no<='' || $stoorderdesc_no<='' )
    {
        print "<script>alert('You Are Selected a Wrong File...!!!');</script>";
        return;
    }
      $k=$i-1;
      if(strlen($grno)>0 &&($grndate>=$acf && $grndate<=$act))
      {
          if($suplplantname!='')
      {
          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 transferin where brcode='$brcode'");
        $row=mysqli_fetch_row($query);
        if(mysqli_num_rows($query)<=0)
            $purno=1;
        else
            $purno=$row[0]+1;
        }
                         
          
        $sql="insert into transferin (STOOrderType ,STOOrderDescription ,StockTransferOrder ,STODate,SupplyingPlantCode ,SupplyingPlantName ,ReceiverPlant ,ReceiverPlantName ,InvoiceNo ,InvoiceDate,InvoiceValue ,DealerInvoiceNumber ,GRNNo ,GRNDate,GRAMOUNT,purno,brcode,igstp,igstamt)    values('$stotype','$stoorderdesc','$stoorderno','$stodate','$suplplant','$suplplantname','$recplant','$recplantname','$invno','$invdate','$invoicevalue','$dlrinvno','$grno','$grndate','$gramount','$purno','$brcode','$igstp','$igstamt')";
    
    
    
        //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....!!!');</script>";
//print "<script> alert('Successfully Uploaded....!!!');document.location='upld_transferin.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 Stock Transfer In </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>