<?
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]=='SAP Invoice no')
                {
                    $invno_no1 = $x;
                }
            } 
        }
        if($j>2)
        {
            $invno1 = trim($spline[$invno_no1]);
            if($invno1!=$invno_prv)
            {
                $query="select tr_no from sparetransout where tr_no='$invno1'";
        //echo $query;
                $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]=='Invoice date')
                {
                    $invdate_no = $x;
                }
                if($spline[$x]=='SAP Invoice no')
                {
                    $ptype_no = $x;
                }
                if($spline[$x]=='Customer Code')
                {
                    $custcode_no = $x;  
                }
                if($spline[$x]=='Material')
                {
                    $itemno_no = $x;
                }
                if($spline[$x]=='Material Desc.')
                {
                    $itemdesc_no = $x;
                }
                if($spline[$x]=='Invoice Qty')
                {
                    $qty_no = $x;
                }
                if($spline[$x]=='Customer GST No')
                {
                    $gstname_no = $x;
                }
                if($spline[$x]=='Billed Value NDP')
                {
                    $unitprice_no = $x;
                }
                if(trim($spline[$x])=='Purchase order number')
                {
                    $purorder_no = $x;
                }
                if(($spline[$x])=='Dealer Plant')
                {
                    $brcode_no = $x;
                }
            }
        }
        if($i>1)
        {
        
            $qty = trim($spline[$qty_no]);
            $unitprice = trim($spline[$unitprice_no]);
            $qty = trim($qty,'"');
            $qty = str_replace(',', '', $qty);
            $unitprice = trim($unitprice,'"');
            $unitprice = str_replace(',', '', $unitprice);
            $invdate = trim($spline[$invdate_no]);
            $invdate = str_replace('.', '-', $invdate);
            $invdate = implode('-', array_reverse(explode('-', $invdate)));
            $sapinvno = trim($spline[$ptype_no]);
            $custcode = trim($spline[$custcode_no]);
            $custcode = str_replace(',', '', $custcode);
            $itemno = trim($spline[$itemno_no]);
            $itemdesc = trim($spline[$itemdesc_no]);
            $itemdesc =mysqli_escape_string($itemdesc);
            $gstnameno=trim($spline[$gstname_no]);
            $gstnameno = str_replace(',', '', $gstnameno);
            $purorderno=trim($spline[$purorder_no]);
            $brcodeno=trim($spline[$brcode_no]);
        }
        if(($i>1) && ($sapinvno!=''))
        {
            if(array_search($sapinvno,$invno_arr)>0)
            {
                print "<font color='red'>Already Inserted.....!!!--".$sapinvno."<br></font>";
            }
            else
            {
                $aprice=$qty*$unitprice;
                $gtot=$aprice+$tax;
                $taxp=$tax/$aprice*100;
                $taxp=round($taxp);
                if(($brcodeno=='3776')&&($gstnameno=='32AABCA9447K2ZJ'))
                {
                    $custcode=preg_replace("/[^0-9]/", "", $custcode);
                    $sql="insert into sparetransout (tr_no,trdate,entrydate,partcode,partname,pord_no,qty,to_branch,brcode,amount,saprefno,grdate,taxamt,aprice,taxp,gtot,trfrtype) values('$sapinvno','$invdate','$invdate','$itemno','".addslashes($itemdesc)."','$purorderno','$qty','$custcode','$brcodeno','$unitprice','$purorderno','$invdate','0','$aprice','0','$gtot','aggr')";
                    print $i.". ".$sql."<br>";
                    print "<font color='blue'>Successfuly Inserted.....!!!--".$sapinvno."<br></font>";
       // mysqli_query($db,$sql) or die('Insert Err:'.mysqli_error($db));
                }
           }
        }
    $i++; 
    }
    unlink($filepath.$imagename);
  //print "<script> alert('Successfully Uploaded....!!!');</script>";
}
?>
<link rel="stylesheet" href="style.css" type="text/css" />
<style>
body,td{font-family:'calibri';letter-spacing:1;}
tr{background-color:rgb(95, 198, 547);}
.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 Transfer Out</h1>
<tr><td>File <input type=file name='ff'><input type=submit value='Upload' name='upd'></td></tr>
<tr><td>(Convert it to TAB TEXT Format and upload)
 </td></tr>
</table>
</td></tr>
</table>
</form>