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 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 f as (select rUnique,rValue as VendorClient from Responses where qID=`CaseFirstName`) ~);
//ShowQueryResult(~select * from f~);

ExecuteSQL(~create temporary table g as (select rValue as Vid,rInstance from Responses where qID=`RE_X_VendorClient` and CaseID=`$CaseId` and isDeleted=0) ~);
//ShowQueryResult(~select * from g~);

ExecuteSQL(~create temporary table h as select rInstance,VendorClient from f,g where rUnique=Vid~);

//ShowQueryResult(~select f.*,g.* from f left join g on rUnique=Vid~);
 
//ShowQueryResult(~select * from h~);

//ShowQueryResult(~select b.*,h.* from b,h where h.rInstance=b.rInstance~);

//ShowQueryResult(~select b.*,h.* from b left join h on h.rInstance=b.rInstance~);

//(~select * from h order by rInstance~);
//ShowQueryResult(~select * from b order by rInstance~);

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

echo `<div style=~height:`.($_SESSION[`PM_wHit`]/1.55).`; overflow: scroll;~>`;

global $NOSORT;
$NOSORT=0;

$order=$_REQUEST[`orderby`];
if ($order==``) $orderby=`Date`;
else $orderby=$order.`, Date`;

//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, VendorClient,  Description, Date, Amount from b,c,d,h where c.rInstance=b.rInstance and d.rInstance=b.rInstance and h.rInstance=b.rInstance order by $orderby DESC~);

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, VendorClient,  Description, Date, Amount from b left join h on h.rInstance=b.rInstance ,c,d where c.rInstance=b.rInstance and d.rInstance=b.rInstance order by $orderby DESC~);

//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,  Description, Date, Amount from b,c,d where c.rInstance=b.rInstance and d.rInstance=b.rInstance order by $orderby DESC~);

echo ~</div>~;

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

