<?

$ProjectId=$_SESSION[`ProjectId`];

$tmpREyear=$_REQUEST[`tmpREyear`];

if($tmpREyear==``) $tmpREyear==substr(NOW(),0,4); 

echo ~<center>Year: <select name=tmpREyear size=1 onchange=`FreshenItNew(tmpREyear.value);`>~;
echo `<option value=~~></option>`;

OptionQueryResult(~select distinct substring(rValue,1,4), substring(rValue,1,4) from Responses where IsDeleted=0 and rValue<>`` and qID=`RE_X_Date` order by rValue desc ~,$tmpREyear);

echo `<option value=~All years~>All years</option>`;

echo `</select><p>`;

if ($tmpREyear==`All years`) $filt=``;
else $filt=~and substring(rValue,1,4)=`$tmpREyear`~;

GLOBAL $LINENUMBERS,$NOSORT;
$LINENUMBERS=1;
$NOSORT=1;

ExecuteSQL(~create temporary table a as (select rInstance from Responses where ProjectId=$ProjectId and qID=`RE_X_Date` $filt and isDeleted=0) ~);

ExecuteSQL(~create temporary table b as (select rValue as Amount from Responses,a where ProjectId=$ProjectId and qID=`RE_X_Amount` and a.rInstance=Responses.rInstance and isDeleted=0) ~);

$tr=ShowQueryResultScalar(~select -sum(Amount) from b where Amount<0~);
$te=ShowQueryResultScalar(~select sum(Amount) from b where Amount>0~);
$ni=$tr-$te;
/*
ShowQueryResult(~select Responses.* from Responses,a where ProjectId=$ProjectId and qID=`RE_X_Amount` and a.rInstance=Responses.rInstance and isDeleted=0 order by rValue~);
*/
echo `<body bgcolor=#F0EFFF><form name=CashFlow method=~POST~>`;

echo `<p><table border=5 bordercolor=darkblue bgcolor=beige cellpadding=10 cellspacing=0><tr><td align=center>`;
echo ~<p><b style=font-size:20;>Cash Flow</b><br><b style=font-size:10;>as of: ~ , date(~Y-m-d h:i:sa~, time()), ~</b>

<p><table border=1 cellspacing=5 cellpadding=5 bordercolor=darkblue bgcolor=#DFFFFF><tr><td><table border=0>~;
echo ~<tr><td>Total Revenue:</td><td align=right><b>$~ , number_format($tr,2) , ~</b></td></tr>~;
echo ~<tr><td style=`color:red`;>Total Expense:</td><td align=right style=`color:red`;><b>$~ , number_format($te,2) , ~</b></td></tr>~;
echo ~<tr><td>Net Income:</td><td align=right><b>$~ , number_format($ni,2) , ~</b></td></tr>~;

echo `</table></td></tr></table></td></tr></table></center>`;

echo `
<script language=javascript>
function FreshenItNew(prm)
{
// alert(~Starting FreshenItNew prm=~+prm);
	url=~`,$_SERVER[`REQUEST_URI`],`~+~&tmpREyear=~+prm;
// alert(~url=~+url);
	window.location.href=url;
 	return false;
}
</script>`;

?>
