<?
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='';
    print "<table>";
    while(!feof($fp))
    {
        $line=fgets($fp,1024);
        $spline=explode("\t",$line);
        $no_col=sizeof($spline);
        //print $line."<br>";
        if($j==12)
        {
            $mbraddr = substr($spline[2],0,50);
        }
        if($j==13)
        {
            $invno = trim($spline[32].$spline[33].$spline[34].$spline[35].$spline[36].$spline[37].$spline[38]);
            print $invno;
            mysqli_query($db,"delete from sparetransout_eway where tr_no='$invno' and brcode='$brcode'");
        }
        if($j==11)
        {
            $mdate1 = $spline[32].$spline[33].$spline[34].$spline[35].$spline[36].$spline[37].$spline[38];
            $mentrydate = substr(trim($mdate1),6,4).substr(trim($mdate1),3,2).substr(trim($mdate1),0,2);
            print $mentrydate;
        }   
        if($j==14)
        {
            $mbrcity = substr($spline[2],0,50);
        }   
        if($j==16)
        {
            //print $spline[2]."<br>";
            $mbrpin = substr($spline[2],strpos($spline[2],' 67')+1,6);
            print $mbraddr."<br>".$mbrcity."<br>".$mbrpin;
        }   
        $j++;
        if($spline[2]>0 && strlen(trim($spline[2]))<6 )
        {
            print "<tr>";
            $mqty = $spline[23].$spline[24].$spline[25].$spline[26].$spline[27];
            $mamount = $spline[30].$spline[31].$spline[32].$spline[33].$spline[34].$spline[35];
            for($x=0;$x<=$no_col;$x++)
            { 
                $mpos = strpos($spline[$x],"NOS");
                if($mpos>0)
                {
                    $mstr = substr($spline[$x],$mpos,strlen($spline[$x]));
                    if(strlen($mstr)>3)
                        $mqty = substr($mstr,strpos($mstr,"NOS")+3,strlen($mstr));
                }
                //print "<td>".$spline[$x]."</td>";
            }
            $mpartcode = "";
            if(substr($spline[4],0,2)>'99')
            {
                $mpartcode = substr($spline[4],0,8);
                if(strpos($spline[4],"NOS")>0)
                    $mpos1=strpos($spline[4],"NOS")-8;
                else
                    $mpos1=strlen($spline[4]);
                $mpartname = substr($spline[4],10,$mpos1).$spline[8].$spline[9].$spline[10].$spline[11].$spline[12];
            }
            else
            {
                $mpartcode = substr($spline[4],0,10);
                if(strpos($spline[4],"NOS")>0)
                    $mpos1=strpos($spline[4],"NOS")-10;
                else
                    $mpos1=strlen($spline[4]);
                $mpartname = substr($spline[4],10,$mpos1).$spline[8].$spline[9].$spline[10].$spline[11].$spline[12];
            }
            $mslno = $spline[2];
            $line=fgets($fp,1024);
            $spline=explode("\t",$line);
            $no_col=sizeof($spline);
            $mamount = str_replace(',', '', $mamount);
            $mhsn = substr($spline[4],1,strpos($spline[4],')')-1);
            if($mbrcity=='Malappuram 676121 0497-2836656')
                $mbrpin = '676121';
            print "<tr><td>".$mslno."</td><td>".$mpartcode."</td><td>".$mpartname."</td><td>".$mqty."</td><td>".$mamount."</td><td>".$mhsn."</td>";
            print "</tr>";
            mysqli_query($db,"insert into sparetransout_eway (tr_no,entrydate,partcode,partname,qty,brcode,amount,hsncode,braddr,brcity,brpin,telcqty) values ('".$invno."','".$mentrydate."','".$mpartcode."','".$mpartname."',".$mqty.",'".$brcode."',".$mamount.",'".$mhsn."','".$mbraddr."','".$mbrcity."','".$mbrpin."',".$mslno.")");     
            //print "insert into sparetransout_eway (tr_no,entrydate,partcode,partname,qty,brcode,amount,hsncode,braddr,brcity,brpin) values ('".$invno."','".$mentrydate."','".$mpartcode."','".$mpartname."',".$mqty.",'".$brcode."',".$mamount.",'".$mhsn."','".$mbraddr."','".$mbrcity."','".$mbrpin."')";
        }
    }
   // print "<script> alert('Successfully Uploaded....!!!');document.location='upld_sparepurchase.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 Spare Transfer</h1>
<tr><td>File <input type=file name='ff'><input type=submit value='Upload' name='upd'></td></tr>

</table>
</td></tr>
</table>
</form>