Added transaction stats. Added (possibly unwanted) columns.
git-svn-id: file:///svn-source/pmgr/branches/yafr_20090716/site@389 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
@@ -107,9 +107,10 @@ class Account extends AppModel {
|
||||
* on the overall balance of the account.
|
||||
*/
|
||||
|
||||
function debitCreditFields($sum = false, $entry_name = 'LedgerEntry', $account_name = 'Account') {
|
||||
function debitCreditFields($sum = false, $balance = true,
|
||||
$entry_name = 'LedgerEntry', $account_name = 'Account') {
|
||||
return $this->LedgerEntry->debitCreditFields
|
||||
($sum, $entry_name, $account_name);
|
||||
($sum, $balance, $entry_name, $account_name);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -110,9 +110,10 @@ class Ledger extends AppModel {
|
||||
* entries are a debit, or a credit, and also the effect each have
|
||||
* on the overall balance of the ledger.
|
||||
*/
|
||||
function debitCreditFields($sum = false, $entry_name = 'LedgerEntry', $account_name = 'Account') {
|
||||
function debitCreditFields($sum = false, $balance = true,
|
||||
$entry_name = 'LedgerEntry', $account_name = 'Account') {
|
||||
return $this->LedgerEntry->debitCreditFields
|
||||
($sum, $entry_name, $account_name);
|
||||
($sum, $balance, $entry_name, $account_name);
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
|
||||
@@ -44,7 +44,8 @@ class LedgerEntry extends AppModel {
|
||||
* on the overall balance of the account/ledger.
|
||||
*/
|
||||
|
||||
function debitCreditFields($sum = false, $entry_name = 'LedgerEntry', $account_name = 'Account') {
|
||||
function debitCreditFields($sum = false, $balance = true,
|
||||
$entry_name = 'LedgerEntry', $account_name = 'Account') {
|
||||
$fields = array
|
||||
(
|
||||
($sum ? 'SUM(' : '') .
|
||||
@@ -56,15 +57,17 @@ class LedgerEntry extends AppModel {
|
||||
"IF({$entry_name}.crdr = 'CREDIT'," .
|
||||
" {$entry_name}.amount, NULL)" .
|
||||
($sum ? ')' : '') . ' AS credit' . ($sum ? 's' : ''),
|
||||
|
||||
($sum ? 'SUM(' : '') .
|
||||
"IF(${account_name}.type IN ('ASSET', 'EXPENSE')," .
|
||||
" IF({$entry_name}.crdr = 'DEBIT', 1, -1)," .
|
||||
" IF({$entry_name}.crdr = 'CREDIT', 1, -1))" .
|
||||
" * IF({$entry_name}.amount, {$entry_name}.amount, 0)" .
|
||||
($sum ? ')' : '') . ' AS balance',
|
||||
);
|
||||
|
||||
if ($balance)
|
||||
$fields[] =
|
||||
($sum ? 'SUM(' : '') .
|
||||
"IF(${account_name}.type IN ('ASSET', 'EXPENSE')," .
|
||||
" IF({$entry_name}.crdr = 'DEBIT', 1, -1)," .
|
||||
" IF({$entry_name}.crdr = 'CREDIT', 1, -1))" .
|
||||
" * IF({$entry_name}.amount, {$entry_name}.amount, 0)" .
|
||||
($sum ? ')' : '') . ' AS balance';
|
||||
|
||||
if ($sum)
|
||||
$fields[] = "COUNT({$entry_name}.id) AS entries";
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user