GLOBAL $LINENUMBERS;
$LINENUMBERS=0;

global $FCOLOR;
$FCOLOR=`RED`;

$CaseId=$_SESSION[`CaseId`];

$sel=$_REQUEST[`ExpenseSelect`];
if ($sel!=~~)
{
   $_SESSION[`Instance`]=ShowQueryResultScalar(~select rInstance from Responses where rUnique=`$sel` and isDeleted=0~);
   SetForm(`Transaction_Detail`);
}

echo ~<input type=hidden name=ExpenseSelect>~;

$tmpREyear=$_SESSION[`PM_REyear`];
//$tmpREyear=$_POST[`tmpREyear`];

if ($tmpREyear==~All years~ or $tmpREyear==``) $Yfilter=``;
else
  {
     $Yfilter=~ and substring(rValue,1,4)=`$tmpREyear` ~;
  }

//echo ~<br>tmpREyear=$tmpREyear Yfilter=$Yfilter<br>~;

$tmpREcase=$_REQUEST[`tmpREcase`];
if ($tmoREcase*=``) 
 {
  $CaseId=$tmpREcase;
  $_SESSION[`CaseId`]=$CaseId;
 }

//echo ~<h4><u>$CaseId</u> Transactions</h4> ~;

/*
  ExecuteSQL(~create temporary table a as (select rUnique,tText as Category,rInstance from Responses,ResponseType where qID=`RE_X_Category` and CaseID=`$CaseId` and isDeleted=0 and tID=`RE_X_Categories` and tValue=rValue) ~);
*/

ExecuteSQL(~create temporary table b as (select rUnique,rValue as Description,rInstance from Responses where qID=`RE_X_Description` and CaseID=`$CaseId` and isDeleted=0) ~);
ExecuteSQL(~create temporary table c as (select rUnique,rValue as Date,rInstance from Responses where qID=`RE_X_Date` and CaseID=`$CaseId` and isDeleted=0 $Yfilter) ~);
ExecuteSQL(~create temporary table d as (select rUnique,cast(rValue as decimal(8,2)) as Amount,rInstance from Responses where qID=`RE_X_Amount` and CaseID=`$CaseId` and isDeleted=0) ~);

/*
  ExecuteSQL(~create temporary table e as (select rUnique,tText as Paid,rInstance from Responses,ResponseType where qID=`RE_X_Paid` and CaseID=`$CaseId` and isDeleted=0 and tID=`YesNo` and tValue=rValue ) ~);
  ExecuteSQL(~create temporary table f as (select rUnique,rValue as VendorClient from Responses where qID=`VC_Name`) ~);
  ExecuteSQL(~create temporary table g as (select rUnique,rValue as Vid,rInstance from Responses where qID=`RE_X_VendorClient` and CaseID=`$CaseId` and isDeleted=0) ~);
  $ttl=ShowQueryResultScalar(~select sum(Amount) from d~);
*/

$ttl=ShowQueryResultScalar(~select sum(Amount) from d,c where d.rInstance=c.rInstance~);

echo `<div style=~height:`.($_SESSION[`PM_wHit`]/1.55).`; overflow: scroll;~>`;
ShowQueryResult(~select distinct concat(`<input type=button value=`,substring(replace(cast(100000+cast(b.rInstance as unsigned) as char(6)),` `,`0`),2,5),` onclick=javascript:Xsel(`,b.rUnique,`);>`) as Detail,/* Category,VendorClient,*/Description,Date,Amount from b,c,d where /*b.rInstance=a.rInstance and */ 
c.rInstance=b.rInstance and d.rInstance=b.rInstance /*and e.rInstance=b.rInstance and g.rInstance=b.rInstance and f.rUnique=g.Vid*/ order by Date DESC~);
echo ~</div>~;

echo ~<p>~;
echo ~Total Expenses: <b style=`color:red;`>$~,number_format($ttl,2),~</b>   ~;
