<?
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>HSN CODE SUMMART REPORT</H4>";

print " from ".implode('-', array_reverse(explode('-', $dfrom))).' to '.implode('-', array_reverse(explode('-', $dto)));
?>
</td></tr>
</table>
<?
$gtot1=0;
$ttx=0;
$tcess=0;
$ttot=0;
$ttrnd=0;
$tqty=0;
$tbl="hsnsummary".$brcode;
mysqli_query($db,"drop table $tbl");
mysqli_query($db,"CREATE TABLE $tbl (hsncode varchar(20),qty double(10,2) default 0,aprice double(10,2) default 0,igstamt double(10,2) default 0,cgstamt double(10,2) default 0,sgstamt double(10,2) default 0,cessamt double(10,2),item_type varchar(40))");
mysqli_query($db,"delete from $tbl");
mysqli_query($db,"insert into $tbl (hsncode,qty,aprice,igstamt,cgstamt,sgstamt,cessamt,item_type) select hsncode,sum(qty) qty,sum(aprice)aprice,sum(igstamt)igstamt,sum(cgstamt)cgstamt,sum(sgstamt)sgstamt,sum(cessamt)cessamt,'Automobiles Parts' from spareinvoice  where brcode='$brcode' and invdate>='$dfrom' and invdate<='$dto'  group by hsncode") or die('err12'.mysqli_error($db));
mysqli_query($db,"insert into $tbl (hsncode,qty,aprice,igstamt,cgstamt,sgstamt,cessamt,item_type) select hsncode,sum(qty) qty,sum(aprice)aprice,sum(igstamt)igstamt,sum(cgstamt)cgstamt,sum(sgstamt)sgstamt,sum(cessamt)cessamt,'Automobiles Parts' from spareinvoice_oth  where brcode='$brcode' and invdate>='$dfrom' and invdate<='$dto'  group by hsncode") or die('err12'.mysqli_error($db));
mysqli_query($db,"insert into $tbl (hsncode,qty,aprice,igstamt,cgstamt,sgstamt,cessamt,item_type) select hsncode,sum(qty) qty,sum(amt)aprice,sum(igstamt)igstamt,sum(cgstamt)cgstamt,sum(sgstamt)sgstamt,sum(cessamt)cessamt,'Automobiles Parts' from jcardparts  where brcode='$brcode' and date>='$dfrom' and date<='$dto'  group by hsncode") or die('err12'.mysqli_error($db));
mysqli_query($db,"insert into $tbl (hsncode,qty,aprice,igstamt,cgstamt,sgstamt,cessamt,item_type) select hsncode,1,sum(servamt)aprice,sum(igstamt)igstamt,sum(cgstamt)cgstamt,sum(sgstamt)sgstamt,1,'Service' from jcardserv,jobcard where jcardserv.brcode='$brcode' and jobcard.invno= jcardserv.invno and date>='$dfrom' and date<='$dto'  group by hsncode") or die('err12'.mysqli_error($db));
mysqli_query($db,"insert into $tbl (hsncode,qty,aprice,igstamt,cgstamt,sgstamt,cessamt,item_type) select hsncode,1,sum(beftax)aprice,sum(igstamt)igstamt,sum(cgstamt)cgstamt,sum(sgstamt)sgstamt,1,'Automobiles Parts' from wargenerated where brcode='$brcode' and entrydate>='$dfrom' and entrydate<='$dto' and hsncode!='998714' group by hsncode") or die('err12'.mysqli_error($db));
mysqli_query($db,"insert into $tbl (hsncode,qty,aprice,igstamt,cgstamt,sgstamt,cessamt,item_type) select hsncode,1,sum(beftax)aprice,sum(igstamt)igstamt,sum(cgstamt)cgstamt,sum(sgstamt)sgstamt,1,'Service' from wargenerated where brcode='$brcode' and entrydate>='$dfrom' and entrydate<='$dto' and hsncode='998714' group by hsncode") or die('err12'.mysqli_error($db));
$re=mysqli_query($db,"select hsncode,sum(qty) qty,sum(aprice)aprice,sum(igstamt)igstamt,sum(cgstamt)cgstamt,sum(sgstamt)sgstamt,sum(cessamt)cess,item_type from $tbl group by hsncode")or die('err'.mysqli_error($db));
print "<table border=0 cellspacing=1 cellpadding=4 bgcolor='lightsteelblue'>";
print "<tr><th>HSN Code No <th>Description<th>UQC<th>Total Quantity<th>Total Value<th>Taxable Value<th>Integrated Tax Amount<th>Central Tax Amount<th>State/UT Tax Amount<th>Cess Amount";
while($row=mysqli_fetch_array($re))
{
    print "\n<tr bgcolor=white><td>$row[hsncode]</td><td>$row[item_type]</td><td>Nos</td>";
    print "<td align=right>".$row['qty']."</td>";
    print "<td align=right>".$row['aprice']."</td>";
    print "<td align=right>".$row['aprice']."</td>";
    print "<td align=right>".putComa($row['igstamt'])."</td><td align=right>".putComa($row['cgstamt'])."</td><td align=right>".putComa($row['sgstamt'])."</td>";
    print "<td align=right>".putComa($row['cess'])."</td>";
    $tqty+=round($row['qty']);
    $gtot1+=$row['aprice'];
    $tcess+=$row['cess'];
    $grand_cgsttx+=$row['cgstamt'];
    $grand_sgsttx+=$row['sgstamt'];
    $grand_igsttx+=$row['igstamt'];
}
print "<tr><td colspan=2><center><b>Total</td><td align=right></td><td align=right>".putComa($tqty)."</td>";
print "<td align=right>".putComa($gtot1)."</td>";
print "<td align=right>".putComa($gtot1)."</td>";
print "<td align=right>".putComa($grand_igsttx)."</td>";
print "<td align=right>".putComa($grand_cgsttx)."</td>";
print "<td align=right>".putComa($grand_sgsttx)."</td>";
print "<td align=right>".putComa($tcess)."</td>";
$grand_gtot1+=$gtot1;
$grand_ttx+=$ttx;
$grand_tcess+=$tcess;
$grand_ttot+=$ttot;
$grand_rndtot+=$rndtot;
$grand_ttrnd+=$ttrnd;
print "</table>";
return;
}
?>

<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>


