<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../index.html'; </script>";
 return;
}
include("../opendb.php");
include("../test.php");
if(isset($_POST['show']))
{
    ?>
    <style>
    td,th,body{font-size:14;font-family:'arial narrow';letter-spacing:1}
    </style>
    <center>
    <?
    $d1=$_REQUEST['d1'];
    $m1=$_REQUEST['m1'];
    $y1=$_REQUEST['y1'];
    
    $d2=$_REQUEST['d2'];
    $m2=$_REQUEST['m2'];
    $y2=$_REQUEST['y2'];
    $dfrom=$y1.'-'.$m1.'-'.$d1;
    $dto=$y2.'-'.$m2.'-'.$d2;
    $ordby=$_REQUEST['ordby'];
    $brres=mysqli_query($db,"select * from branch where brcode='$brcode'");
    $brrow=mysqli_fetch_array($brres);
    ?>
    <table>
    <TR><TD COLSPAN=2 ALIGN=CENTER><b> PSN AUTOMOBILES P LTD </b><BR>
	<?print $brrow['braddr'].",".$brrow['brcity'].",<br><u> PH - ".$brrow['brphone']?></u>
    TIN: <?print $brrow['tin']?>, CST: <?print $brrow['cstno']?>&nbsp;
    </td></tr>
    <tr><td COLSPAN=2 ALIGN=CENTER>
    <?print "<h4><br>SPARE INVOICE REPORT</H4>";
    print " from ".implode('-', array_reverse(explode('-', $dfrom))).' to '.implode('-', array_reverse(explode('-', $dto)));
    ?>
    </td></tr>
    </table>
    <?
    $tbl="b2b".$brcode;
    mysqli_query($db,"drop table $tbl");
    mysqli_query($db,"CREATE TABLE $tbl (gstin varchar(20),invno varchar(20),invdate date,tot double(10,2) default 0,state varchar(40),tax_rate double(10,2),bef_tax double(10,2),cessamt double(10,2))");
    mysqli_query($db,"delete from $tbl");
    $invre=mysqli_query($db,"insert into $tbl (invno,invdate,tot,state,tax_rate,bef_tax,cessamt)select invno,entrydate,sum(tot),'KERALA',(cgstp+sgstp),sum(aprice),sum(cessamt) from spareinvoice where brcode='$brcode' and invdate>='$dfrom' and invdate<='$dto' group by invno,cgstp");
    $invre=mysqli_query($db,"insert into $tbl (invno,invdate,tot,state,tax_rate,bef_tax,cessamt)select invno,date,sum(gtot),'KERALA',(cgstp+sgstp),sum(amt),sum(cessamt) from jcardparts,jobcard where brcode='$brcode' and date>='$dfrom' and date<='$dto' and jobcard.invno=jcardparts.invno group by invno,cgstp");
    $invre=mysqli_query($db,"insert into $tbl (invno,invdate,tot,state,tax_rate,bef_tax,cessamt)select invno,entrydate,sum(tot),'KERALA',(cgstp+sgstp),sum(servamt),0 from jcardserv,jobcard where brcode='$brcode' and date>='$dfrom' and date<='$dto' and jobcard.invno=jcardparts.invno group by invno,cgstp");
    $invre=mysqli_query($db,"select gstin,invno,invdate,tot,state,tax_rate,bef_tax,cessamt from $tbl group by invdate,invno,tax_rate")or die('err'.mysqli_error($db));    
    print "<table border=1 cellspacing=1 cellpadding=4 bgcolor='lightsteelblue'>";
    print "<th colspan=10><h3>B2B Sheet for GST Upload</th></tr>";
    print "<tr><th>GSTIN <th>INVOICE NO<th>INV. DATE<th>INVOICE VALUE<th>PLACE OF SUPPLY<th>REVERSE CHARGE<th>INVOICE TYPE<th>E-Commerce GSTIN<th>RATE<th>Taxable Value<th>Cess Amount</tr>";
    while($invrow=mysqli_fetch_array($invre))
    {
        print "<tr><td>".$invrow['gstin']."</td><td>".$invrow['invno']."</td><td>".$invrow['invdate']."</td><td align='right'>".$invrow['tot']."</td><td>".$invrow['state']."</td><td></td><td></td><td></td><td align='right'>".$invrow['tax_rate']."</td><td align='right'>".$invrow['bef_tax']."</td><td align='right'>".$invrow['cessamt']."</td></tr>";
        $mtot     += $invrow['tot'];
        $mbef_tax += $invrow['bef_tax'];
        $mcessamt += $invrow['cessamt'];
    }
    print "<tr><td colspan = 3><b>T O T A L</td><td align='right'><b>".putcoma($mtot)."</td><td colspan=5></td><td align='right'><b>".putcoma($mbef_tax)."</td><td align='right'><b>".putcoma($mcessamt)."</td></tr>";
    print "</table>";
    return;
}// end if
?>

<link rel="stylesheet" href="../style.css" type="text/css" />
<style>
//	tr{background-color:white}
	th{background-color:steelblue;color:white}
	td{font-size:13}
.right{text-align:right};
</style>

<center>
<h3>SPARE INVOICE REPORT -2</H3>
<form action='<?Print $PHP_SELF?>' method='post'>
<table cellspacing=1 cellpadding=1 bgcolor='steelblue'>
<tr><td bgcolor='white' align=center>
<table class=noprint>
<tr><td>Date from </td><td>dd <input type=text name='d1' size=2 value='<?print date("d")?>'>
mm <input type=text name='m1' size=2 value='<?print date("m")?>'>
yy <input type=text name='y1' size=4 value='<?print date("Y")?>'></td></tr>

<tr><td align=right>to </td><td>dd <input type=text name='d2' size=2 value='<?print date("d")?>'>
mm <input type=text name='m2' size=2 value='<?print date("m")?>'>
yy <input type=text name='y2' size=4 value='<?print date("Y")?>'>
</td></tr>
<tr><td colspan=2 align=center bgcolor='lightsteelblue'><input type=submit value='Show' name='show'></td></tr>
</table>
</td></tr>
</table>
</form>


