Added transaction stats. Added (possibly unwanted) columns.

git-svn-id: file:///svn-source/pmgr/branches/yafr_20090716@389 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
abijah
2009-07-25 08:06:50 +00:00
parent d8f10bfd13
commit f9635419dd
9 changed files with 192 additions and 20 deletions

View File

@@ -9,6 +9,27 @@ class Transaction extends AppModel {
var $hasMany = array(
'LedgerEntry',
'StatementEntry',
'Charge' => array(
'className' => 'StatementEntry',
'conditions' => array('Charge.type' => 'CHARGE')
),
'Payment' => array(
'className' => 'StatementEntry',
'conditions' => array('Payment.type' => 'PAYMENT')
),
'Debit' => array(
'className' => 'LedgerEntry',
'conditions' => array('Debit.crdr' => 'DEBIT')
),
'Credit' => array(
'className' => 'LedgerEntry',
'conditions' => array('Credit.crdr' => 'CREDIT')
),
);
@@ -340,5 +361,62 @@ class Transaction extends AppModel {
}
/**************************************************************************
**************************************************************************
**************************************************************************
* function: stats
* - Returns summary data from the requested transaction
*/
function stats($id = null, $query = null) {
//pr(array('Transaction::stats' => compact('id', 'query')));
$this->queryInit($query);
unset($query['group']);
if (isset($id)) {
$query['conditions'][] = array('Transaction.id' => $id);
$query['group'] = 'Transaction.id';
}
else
// CakePHP seems to automagically add in our ID as a part
// of the query conditions, but only on a 'first' query,
// not an 'all'. I suppose this is helpful :-/
unset($this->id);
if (empty($query['fields']))
$query['fields'] = array();
$stats = array();
foreach (array_keys($this->hasMany) AS $table) {
$squery = $query;
$squery['link'][$table] = array('fields' => array());
if ($table == 'LedgerEntry') {
$squery['fields'] = array_merge($squery['fields'],
$this->LedgerEntry->debitCreditFields(true, false));
}
elseif ($table == 'StatementEntry') {
$squery['fields'] = array_merge($squery['fields'],
$this->StatementEntry->chargePaymentFields(true));
}
else {
$squery['fields'][] = "SUM({$table}.amount) AS total";
$squery['fields'][] = "COUNT({$table}.id) AS entries";
}
$stats[$table] = $this->find('first', $squery);
// REVISIT <AP>: 20090724
// [0][0] is for when we do an 'all' query. This can
// be removed at some point, but I'm keeping it while
// toggling between 'all' and 'first' (testing).
if (isset($stats[$table][0][0]))
$stats[$table] += $stats[$table][0][0];
else
$stats[$table] += $stats[$table][0];
unset($stats[$table][0]);
}
//pr(array('Transaction::stats' => array('return' => compact('stats'))));
return $stats;
}
}
?>