<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];

date_default_timezone_set('Asia/Kolkata');

$logdt=date("Y-m-d");
$logtime=date("H:i:s");

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]=='Dealer Invoice Number')
                $invno_no1 = $x;
            }
        }
        if($j>1)
        {
		    $invno1 = trim($spline[$invno_no1]);
            if($invno1!=$invno_prv)
            {
                $query="select saprefno from sparetransout where saprefno='$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]=='Invoice No.')
                    $ptype_no = $x;
                if($spline[$x]=='Dealer Invoice Number')
                    $grno_no = $x;
                   if($spline[$x]=='GRN No.')
                    $grr_no = $x; 
                     if($spline[$x]=='GRN Date')
                    $grr_date = $x;
                    
                    
                    
                    
            }
        }
        if($i>1)
        {
            $ptype = trim($spline[$ptype_no]);
            $grno = trim($spline[$grno_no]);
            $grr = trim($spline[$grr_no]);
            
            print $grrdate = trim($spline[$grr_date]);
            $grrdate = str_replace('/', '-', $grrdate);
            $grrdate = implode('-', array_reverse(explode('-', $grrdate)));
          
            
            $grno = str_replace(',', '', $grno);
            if($ptype_no<='' || $grno_no<='' )
            {
                //print "<script>alert('You Are Selected a Wrong File...!!!');</script>";
                //return;
            }
            $k=$i-1;
            if($ptype!='')
            {
                $re1=mysqli_query($db,"select saprefno from sparetransout where saprefno='$grno'")or die('err2'.mysqli_error($db));
             
                $rr1=mysqli_fetch_row($re1);
                print $sapno=$rr1[0];
                
                if($sapno>'')
                {
                    if($grrdate=='')
                    {
                     $grrdate='0000-00-00';   
                    }
                    
                     $sql="update sparetransout set grno='$grr',grdate='$grrdate' where saprefno='$grno'";
                     print $i.". ".$sql."<br>";
                 
                    mysqli_query($db,$sql) or die('Insert Err2:'.mysqli_error($db));
                    
                    print "<font color='red'>Already Updatedd.....!!!--".$grno."<br></font>";
                }
                else
                {
                    
                    
                    
                    $tno = substr($ptype,0,2);
                    
                    if($tno=='00')
                    {
                    $trno = substr($ptype,-8);
                    }
                    
                    else
                    {
                       $trno = $ptype; 
                    }
                    
                    if($grrdate=='')
                    {
                     $grrdate='0000-00-00';   
                    }
                    
                    $sql="update sparetransout set saprefno='$grno',grno='$grr', grdate='$grrdate' where tr_no='$trno'";
                    
                     
                    print $i.". ".$sql."<br>";
                    print "<font color='blue'>Successfuly Updated.....!!!--".$grno."<br></font>";
                    mysqli_query($db,$sql) or die('Insert Err:'.mysqli_error($db));
                }
            }
        }
        $i++;
    }
    
    //print "<script> alert('Successfully Uploaded....!!!');document.location='upld_transin.php';</script>";
    
   print  $sql="select sum(qty*amount),brcode,saprefno,entrydate,to_branch,grno,taxamt from sparetransout where jvno<=0 and length(trim(saprefno)) >= 13 group by saprefno";
    $pres=mysqli_query($db,$sql) or die('post err'.mysqli_error($db));
    while($row23=mysqli_fetch_array($pres))
    {
        $jvres=mysqli_query($db,"select max(jvno) from journal2 where brcode='$row23[brcode]'")or die('err'.mysqli_error($db));
        $jvrow=mysqli_fetch_array($jvres)or die('err1'.mysqli_error($db));
        if($jvrow[0]>0)
            $jvno=($jvrow[0]+1);
        else
            $jvno=1;
        $re=mysqli_query($db,"select brled from branch where brcode='$row23[to_branch]'")or die('err2'.mysqli_error($db));
        $rr=mysqli_fetch_row($re);
        $branch_na=$rr[0];
        $sql="update sparetransout set jvno='$jvno' where saprefno='$row23[saprefno]' and brcode = '$row23[brcode]'";
        print $sql."<br>";
        mysqli_query($db,$sql) or die('Insert Err:'.mysqli_error($db));
        //*************** posting
        
        print "insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no,prftcntr,docdt) values('$row23[entrydate]','$jvno','Cr','STOCK TRANSFER OUT','0','$row23[0]','Spare Transfer:$row23[saprefno]','$row23[brcode]','Spare trans out','Trans no','$row23[grno]','$row23[saprefno]','$logtime $user','$logdt')<br>";
        print "insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no,prftcntr,docdt) values('$row23[entrydate]','$jvno','Dr','$branch_na','$row23[0]','0','Spare Transfer out:$row23[saprefno]','$row23[brcode]','Spare trans out','Trans no','$row23[grno]','$row23[saprefno]','$logtime $user','$logdt')<br>";
        mysqli_query($db,"insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no,prftcntr,docdt) values('$row23[entrydate]','$jvno','Cr','STOCK TRANSFER OUT','0','$row23[0]','Stock Transfer Out No:$row23[saprefno]','$row23[brcode]','Spare trans out','Trans no','$row23[grno]','$row23[saprefno]','$logtime $user','$logdt')");
        if($row23[taxamt]<=0)
        {
            
        mysqli_query($db,"insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no,prftcntr,docdt) values('$row23[entrydate]','$jvno','Dr','$branch_na','$row23[0]','0','Stock Transfer Out No::$row23[saprefno]','$row23[brcode]','Spare trans out','Trans no','$row23[grno]','$row23[saprefno]','$logtime $user','$logdt')");
        }
        else
        {
           $taxx=$row23[taxamt];
           $taxx=number_format($taxx,2,".","");
           
           $amtt=$row23[0];
           $amtt=number_format($amtt,2,".","");
           $perr=$taxx/$amtt*100;
           
           $netamt=$amtt+$taxx;
           
           $perr=number_format($perr,2,".","");
           if($perr<19)
           {
          $taxled='OUTPUT IGST STOCK TRANSFER OUT 18.00';
           }
           if($perr>19)
           {
          $taxled='OUTPUT IGST STOCK TRANSFER OUT 28.00';
           }
           
            mysqli_query($db,"insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no,prftcntr,docdt) values('$row23[entrydate]','$jvno','Cr','$taxled','0','$taxx','Stock Transfer Out No::$row23[saprefno]','$row23[brcode]','Spare trans out','Trans no','$row23[grno]','$row23[saprefno]','$logtime $user','$logdt')");
            
             mysqli_query($db,"insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no,prftcntr,docdt) values('$row23[entrydate]','$jvno','Dr','$branch_na','$netamt','0','Stock Transfer Out No::$row23[saprefno]','$row23[brcode]','Spare trans out','Trans no','$row23[grno]','$row23[saprefno]','$logtime $user','$logdt')");
             
           
        }
        
        
        
    }
    print "<font color='blue'>Uploading Completed....!!!--<br></font>";
}
?>
<link rel="stylesheet" href="style.css" type="text/css" />
<style>
body,td{font-family:'calibri';letter-spacing:1;}
tr{background-color:rgb(95, 197, 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>Update Transwer 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>


