<?
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 - VAT</H4>";
    print " from ".implode('-', array_reverse(explode('-', $dfrom))).' to '.implode('-', array_reverse(explode('-', $dto)));
    ?>
    </td></tr>
    </table>
    <?
    $brcode1=$ordby;
    $tbl="gst_item".$brcode.$user;
    echo $ordby;
    if($ordby=='All')
        $br_str = "";
    else
        $br_str = " brcode = '$brcode1' and ";
        
          
    $re=mysqli_query($db,"create table $tbl (partcode varchar(40),partname varchar(150),aprice double(12,2),cgstp double(12,2),cgstamt double(12,2),sgstp double(12,2),sgstamt double(12,2),igstp double(12,2),igstamt double(12,2),tot double(12,2),hsncode varchar(20),brcode varchar(10))")or die('err create'.mysqli_error($db));    
    $re=mysqli_query($db,"delete from $tbl")or die('err delete'.mysqli_error($db));        
    $re=mysqli_query($db,"insert into $tbl select spareinvoice.partcode,spareinvoice.partname,aprice,cgstp,cgstamt,sgstp,sgstamt,igstp,igstamt,tot,partmaster.hsncode,spareinvoice.brcode from spareinvoice,partmaster where ".$br_str." invdate>='$dfrom' and invdate<='$dto' and partmaster.partcode=spareinvoice.partcode and (brcode !='ZZ123' and brcode != 'EIC001') ")or die('err'.mysqli_error($db));    
    $re=mysqli_query($db,"insert into $tbl select jcardparts.partcode,jcardparts.partname,amt,cgstp,cgstamt,sgstp,sgstamt,igstp,igstamt,gtot,partmaster.hsncode,brcode from jcardparts,partmaster where ".$br_str." date>='$dfrom' and date<='$dto' and partmaster.partcode=jcardparts.partcode and (brcode !='ZZ123' and brcode != 'EIC001')")or die('err'.mysqli_error($db));    
    $re1=mysqli_query($db,"select invno,brcode from jobcard where ".$br_str." entrydate >='$dfrom' and entrydate<='$dto' and (brcode !='ZZ123' and brcode != 'EIC001')")or die('err'.mysqli_error($db));        
  //  print "select invno from jobcard where ".$br_str." entrydate >='$dfrom' and entrydate<='$dto' and brcode !='ZZ123' and brcode != 'EIC001'";
    while($row1=mysqli_fetch_array($re1))
    {
        $re=mysqli_query($db,"insert into $tbl select 'SERVICE',service,servamt,cgstp,cgstamt,sgstp,sgstamt,igstp,igstamt,tot,'998714',brcode from jcardserv where brcode='$row1[1]' and invno = '$row1[0]'")or die('err'.mysqli_error($db));    
//        print "insert into $tbl select 'SERVICE',service,servamt,cgstp,cgstamt,sgstp,sgstamt,igstp,igstamt,tot,'998714',brcode from jcardserv where brcode='$brcode1' and invno = '$row1[0]'";
    }
    print "<table border=0 cellspacing=1 cellpadding=4 bgcolor='lightsteelblue'>";
    print "<th colspan=10><h3>Invoice Type $invrow[0]</th></tr>";
    print "<tr><th>Partcode<th>Partname<th>Before Tax <th>CGST%<th>CGST Amt <th>SGST%<th>SGST Amt <th>IGST%<th>IGST Amt <th>Total <th>HSNCode<th>Branch";
    $re=mysqli_query($db,"select * from $tbl order by hsncode,partcode")or die('err'.mysqli_error($db));        
    $mtot1=0;
    $mtot2=0;
    $mtot3=0;
    $mtot4=0;
    $mtot5=0;
    while($row=mysqli_fetch_array($re))
    {
        print "\n<tr bgcolor=white><td>$row[0]</td><td>$row[1]</td><td align='right'>$row[2]</td><td align='right'>$row[3]</td><td align='right'>$row[4]</td><td align='right'>$row[5]</td><td align='right'>$row[6]</td><td align='right'>$row[7]</td>";
        print "<td align='right'>$row[8]</td><td align='right'>$row[9]</td><td align='right'>$row[10]</td><td align='right'>$row[11]</td></tr>";
        $mtot1+=$row[2];
        $mtot2+=$row[4];
        $mtot3+=$row[6];
        $mtot4+=$row[8];
        $mtot5+=$row[9];
    }
    print "\n<tr bgcolor=yellow><td></td><td></td><td align='right'>$mtot1</td><td align='right'></td><td align='right'>$mtot2</td><td align='right'></td><td align='right'>$mtot3</td><td align='right'>$row[7]</td>";
    print "<td align='right'>$mtot4</td><td align='right'>$mtot5</td><td align='right'>$row[10]</td><td align='right'>$row[11]</td></tr>";
    $re=mysqli_query($db,"drop table $tbl")or die('err drop'.mysqli_error($db));  
    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>Report By</td><td><select name=ordby>
<option>All</option>
<?
$re1=mysqli_query($db,"select brcode from branch")or die('err'.mysqli_error($db));        
while($row1=mysqli_fetch_array($re1))
{?>
    <option><?print $row1[0]?></option>
<?}?>
</select></td></td></tr>
<tr><td colspan=2 align=center bgcolor='lightsteelblue'><input type=submit value='Show' name='show'></td></tr>

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



