<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../ind.php'; </script>";
 return;
}
include("../opendb.php");
include("../test.php");
?>
<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>

<?
if(isset($_POST['show']))
{
$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>
</td></tr>
</table>
<h3>SALES RETURN</H3>
<?
$d=$_REQUEST['d'];
$m=$_REQUEST['m'];
$y=$_REQUEST['y'];
$d1=$_REQUEST['d1'];
$m1=$_REQUEST['m1'];
$y1=$_REQUEST['y1'];
 $dfrom=$y.'-'.$m.'-'.$d;
 $dto=$y1.'-'.$m1.'-'.$d1;

$res=mysqli_query($db,"select distinct retno,invno,entrydate,customer from salesreturn where brcode='$brcode' and entrydate>='$dfrom' and entrydate<='$dto' order by retno")or die(mysqli_error($db));
print "<table border=1 cellpadding=3> <th>Ret No <th>Ret Date<th>Inv No <th>Inv Date<th>Customer  ";
print "<th>Amount <th>Tax Amt <th>Cess <th>Total";
while($row=mysqli_fetch_array($res))
{

print "<tr bgcolor='white'><td>$row[retno]</td>";
  print "<td>".datetohtml($row[entrydate])."</td>";
print "<td>$row[invno]</td>";


if($row[invtype]=='JC')
$sq="select entrydate from jobcard where invno='$row[invno]' and brcode='$brcode'";
else
$sq="select entrydate from spareinvoice where invno='$row[invno]' and brcode='$brcode'";
$invdres=mysqli_query($db,$sq) or die('Err:'.mysqli_error($db));
$invdrow=mysqli_fetch_row($invdres);

print "<td>".datetohtml($invdrow[0])."</td>";

  print "<td>$row[customer]</td>";
 $a=mysqli_query($db,"select sum(aprice)aprice,sum(taxamt)taxamt,sum(cessamt)cessamt,sum(tot)tot from salesreturn where retno='$row[retno]' and brcode='$brcode'");
 $arow=mysqli_fetch_array($a);
  print "<td align=right>".putComa($arow[aprice])."</td>";
  print "<td align=right>".putComa($arow[taxamt])."</td>";
  print "<td align=right>".putComa($arow[cessamt])."</td>";
 print "<td align=right>".putComa($arow[tot])."</td></tr>";//+$row[cessamt]
$t1+=$arow[aprice];
$t2+=$arow[taxamt];
$t3+=$arow[cessamt];
$t4+=($arow[tot]); //+$row[cessamt]);

}
print "<tr><td colspan=5 align=center><b>Total</td>";
print "<td align=right>".putComa($t1)."</td>";
print "<td align=right>".putComa($t2)."</td>";
print "<td align=right>".putComa($t3)."</td>";
print "<td align=right>".putComa($t4)."</td>";

print "</table>";
return;
}// end if
?>

<form action='<?print $PHP_SELF?>' name='f1' method='post'>
<table>
<tr><td bgcolor='steelblue' colspan=2><center><b><font color=white>PURCHASE REPORT</td></tr>
<tr><td>Date From </td><td>dd<input type=text name='d' size=2 value='<?print date("d");?>'>
			mm<input type=text name='m' size=2 value='<?print date("m");?>'>
			yy<input type=text name='y' size=4 value='<?print date("Y");?>'>
</td></tr>
<tr><td>Date To </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>
<th colspan=2><INPUT TYPE=SUBMIT VALUE='SHOW' name='show'></td></tr>
</table>
</form>
