<? 
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("test.php");
include("../opendb.php");
include ("../test.php");
$re1=mysqli_query($db,"select distinct(invno) from tbl_einvoice where brcode='$brcode' order by invno desc limit 30");
if(isset($_POST['upd']))
{
   
    
    $ff=$_REQUEST['ff'];
    if($ff !="none")
    {
        $fl=$_FILES["ff"]["name"];
        $filepath="txtfiles/";
        $tmp_name=$_FILES["ff"]["tmp_name"];
        move_uploaded_file($tmp_name, $filepath . $fl) or die(' err:could not copy file');
        
    }
    
	$filename="txtfiles/".$fl;
    $str = file_get_contents ($filename);
	
    $data = json_decode($str,true);
    $msg= $data['SignedQRCode'];
    $acknono= $data['AckNo'];
    $acknodate= $data['AckDt']; 
    $irnno= $data['Irn'];
 
    $einvoice=$data['SignedInvoice'];
    
    $signed=explode('.',$einvoice);
    $invval=base64_decode($signed[1]);
    
    $tttt2=explode('"',$invval);
    $invnovl=$tttt2[44];
    $invnovl=preg_replace("/[^ \w]+/", "", $invnovl);
 if(substr($invnovl,0,3)=='INV' || substr($invnovl,0,3)=='CRN')
    {
        $invnovl=$tttt2[48];
        $invnovl=substr($invnovl,0,strlen($invnovl)-1);
    }
 
 
 
    //$re1=mysqli_query($db,"insert into tbl_einvoice (brcode,invno,ackno,ackdt,irnno,SignedQRCode) values('$brcode','$invno','$acknono','$acknodate','$irnno','$msg')");
    $re1=mysqli_query($db,"update tbl_einvoice set ackno='$acknono',ackdt='$acknodate',irnno='$irnno',SignedQRCode='$msg',user2='$user' where brcode='$brcode' and invno='$invnovl'");
    unlink($filepath . $fl);
    print "<script> alert('Data has been Saved. Invno=$invnovl'); </script>";
    echo "<script> document.location='upldjson_spr.php';</script>";

    

}
?>

<center>
<form action='' method='POST' ENCTYPE="multipart/form-data">
<table cellspacing=1 cellpadding=2 class='form'>
<tr height=21><td colspan=2 align=center bgcolor='steelblue'><FONT COLOR=white><B>Upload Json File</td></tr>
</td></tr>




<tr><td>File </td><td><input type=file name='ff'></td></tr>
<tr bgColor='lightsteelblue'><td align=center colspan=2 ><input type=submit value='Upload' name='upd'></td></tr>







</table>
</form>


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