<?
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;
    }
    ?>
    <table width=70%   align='center' class='tbl1' border='1'>
    <?
    $string = file_get_contents($imagename);
    $json = json_decode($string, true);
    $i=0;
    print "<tr><td><b>GSTIN</td><td><b>Invoice No</td><td><b>Invoice Date</td><td align='right'><b>Before Tax</td><td><b>Tax%</td><td align='right'><b>CGST Amt</td><td align='right'><b>SGST Amt</td><td align='right'><b>IGST Amt</td><td align='right'><b>TOTAL</td></tr>";
    $bef_tot=0;
    $camt_tot=0;
    $samt_tot=0;
    $iamt_tot=0;
    $tot_tot=0;
	for($i=0;$i<=035023;$i++)
    {
//		print "<tr><td>".$json['b2b'][$i]['ctin']."</td>";
		$a = count($json['b2b'][$i]['inv']); 
		
		for($ctr=0;$ctr<=$a;$ctr++)
	    {
			if(strlen($json['b2b'][$i]['inv'][$ctr]['inum'])>0)
			{
//				if($ctr>0)	
//					print "<tr><td>".$json['b2b'][$i]['ctin']."</td><td>".$json['b2b'][$i]['inv'][$ctr]['inum']."</td>";	
//				else
//					print "<td>".$json['b2b'][$i]['inv'][$ctr]['inum']."</td>";	
//				print "<td>".$json['b2b'][$i]['inv'][$ctr]['idt']."</td>";
				$mctr = count($json['b2b'][$i]['inv'][$ctr]['itms'])-1;
//				print "<td>".$mctr."</td>";
				for($b=0;$b<=$mctr;$b++)
				{
					print "<tr><td>".$json['b2b'][$i]['ctin']."</td><td>".$json['b2b'][$i]['inv'][$ctr]['inum']."</td>";
					print "<td>".$json['b2b'][$i]['inv'][$ctr]['idt']."</td>";
					print "<td align='right'>".number_format($json['b2b'][$i]['inv'][$ctr]['itms'][$b]['itm_det']['txval'],2)."</td>";
					print "<td align='right'>".number_format($json['b2b'][$i]['inv'][$ctr]['itms'][$b]['itm_det']['rt'],2)."%</td>";
                    print "<td align='right'>".number_format($json['b2b'][$i]['inv'][$ctr]['itms'][$b]['itm_det']['camt'],2)."</td>";
                    print "<td align='right'>".number_format($json['b2b'][$i]['inv'][$ctr]['itms'][$b]['itm_det']['samt'],2)."</td>";
                    print "<td align='right'>".number_format($json['b2b'][$i]['inv'][$ctr]['itms'][$b]['itm_det']['iamt'],2)."</td>";
					$mtot=$json['b2b'][$i]['inv'][$ctr]['itms'][$b]['itm_det']['txval']+$json['b2b'][$i]['inv'][$ctr]['itms'][$b]['itm_det']['camt']+$json['b2b'][$i]['inv'][$ctr]['itms'][$b]['itm_det']['samt']+$json['b2b'][$i]['inv'][$ctr]['itms'][$b]['itm_det']['iamt'];
					print "<td align='right'>".number_format($mtot)."</td></tr>";
				}
			}
			
		}
		//print "</tr>";
	}
    //print "<tr><td colspan = 3 align='center'><b>T O T A L</td><td align='right'><b>".$bef_tot."</td><td></td><td align='right'><b>".$camt_tot."</td><td align='right'><b>".$samt_tot."</td><td align='right'><b>".$iamt_tot."</td><td align='right'><b>".$tot_tot."</td></tr>";
}

?>
<link rel="stylesheet" href="style.css" type="text/css" />
<style>
body,td{font-family:'calibri';letter-spacing:1;}
tr{background-color:rgb(95, 197, 17);}
.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><h1>Upload GST JSON File</h1>
<tr><td>File <input type=file name='ff'><input type=submit value='Upload' name='upd'></td></tr>
</table>
</td></tr>
</table>
</form>