<?
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;
}
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 :".$filepath;
    }
    ?>
    <table width=70%   align='center' class='tbl1' border='1'>
    <?
    
    $string = file_get_contents($imagename);
    $json = json_decode($string, true);
    print $json;
    $i=0;
    print "<tr><td><b>GSTIN</td><td><b>Invoice No</td><td><b>Invoice Date</td><td><b>Status</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;
    print count($json['b2b']);
    for($i=0;$i<=count($json['b2b']);$i++)
   
    {
        $mctr1 = count($json['b2b'][$i]['inv']);
        print $mctr1;
        for($a=0;$a<=count($json['b2b'][$i]['inv']);$a++)
        
        {
              if($a<$mctr1)
              {
                  print "<tr><td>".$json['b2b'][$i]['ctin']."</td>";
                  print "<td>".$json['b2b'][$i]['inv'][$a]['inum']."</td>";
                  print "<td>".$json['b2b'][$i]['inv'][$a]['idt']."</td>";
                  print "<td>".$json['b2b'][$i]['inv'][$a]['flag']."</td>";
              }
              $mctr = count($json['b2b'][$i]['inv'][$a]['itms']);
              for($b=0;$b<=count($json['b2b'][$i]['inv'][$a]['itms']);$b++)
              {
                  if($b<=$mctr-1)
                  {
                      print "<td align='right'>".number_format($json['b2b'][$i]['inv'][$a]['itms'][$b]['itm_det']['txval'],2)."</td>";
                      print "<td align='right'>".number_format($json['b2b'][$i]['inv'][$a]['itms'][$b]['itm_det']['rt'],2)."%</td>";
                      print "<td align='right'>".number_format($json['b2b'][$i]['inv'][$a]['itms'][$b]['itm_det']['camt'],2)."</td>";
                      print "<td align='right'>".number_format($json['b2b'][$i]['inv'][$a]['itms'][$b]['itm_det']['samt'],2)."</td>";
                      print "<td align='right'>".number_format($json['b2b'][$i]['inv'][$a]['itms'][$b]['itm_det']['iamt'],2)."</td>";
                      $bef_tot +=$json['b2b'][$i]['inv'][$a]['itms'][$b]['itm_det']['txval'];
                      $camt_tot+=$json['b2b'][$i]['inv'][$a]['itms'][$b]['itm_det']['camt'];
                      $samt_tot+=$json['b2b'][$i]['inv'][$a]['itms'][$b]['itm_det']['samt'];
                      $iamt_tot+=$json['b2b'][$i]['inv'][$a]['itms'][$b]['itm_det']['iamt'];
                  }
                  if($b<$mctr-1)
                  {
                      print "<tr><td>".$json['b2b'][$i]['ctin']."</td>";
                      print "<td>".$json['b2b'][$i]['inv'][$a]['inum']."</td>";
                      print "<td>".$json['b2b'][$i]['inv'][$a]['idt']."</td>";
                      print "<td>".$json['b2b'][$i]['inv'][$a]['flag']."</td>";
                  }
              }
              if($a<$mctr1)
              {
                  print "<td align='right'><b>".number_format($json['b2b'][$i]['inv'][$a]['val'],2)."</td>";
                  $tot_tot +=$json['b2b'][$i]['inv'][$a]['val'];
              }
        }
    }
    print "<tr><td colspan = 4 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>