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(`RE_Expenses_Detail`);
}

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

echo "<h4><u>$CaseId</u> Expenses</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) ");
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 ) ");
$ttl=ShowQueryResultScalar("select sum(Amount) from d");

echo `<div style="height:275; overflow: scroll;">`;
ShowQueryResult("select distinct concat(`<input type=button value=`,substring(replace(cast(100000+cast(a.rInstance as unsigned) as char(6)),` `,`0`),2,5),` onclick=javascript:Xsel(`,a.rUnique,`);>`) as Detail, Category,Description,Date,Amount,Paid from a,b,c,d,e where b.rInstance=a.rInstance and c.rInstance=a.rInstance and d.rInstance=a.rInstance and e.rInstance=a.rInstance order by Date DESC,Category ");
echo "</div>";

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