Another snapshot
git-svn-id: file:///svn-source/pmgr/branches/yafr_20090716/site@356 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
@@ -41,10 +41,11 @@ class Ledger extends AppModel {
|
||||
function accountID($id) {
|
||||
$this->cacheQueries = true;
|
||||
$item = $this->find('first', array
|
||||
('contain' => 'Account.id',
|
||||
('link' => array('Account'),
|
||||
'conditions' => array('Ledger.id' => $id),
|
||||
));
|
||||
$this->cacheQueries = false;
|
||||
//pr(compact('id', 'item'));
|
||||
return $item['Account']['id'];
|
||||
}
|
||||
|
||||
@@ -120,29 +121,18 @@ class Ledger extends AppModel {
|
||||
}
|
||||
|
||||
|
||||
function debitCreditFields($id, $sum = false, $entry_name = 'Entry', $double_name = 'DoubleEntry') {
|
||||
$ftype = strtoupper($this->Account->fundamentalType($this->accountID($id)));
|
||||
|
||||
$fields = array
|
||||
(
|
||||
($sum ? 'SUM(' : '') .
|
||||
"IF({$entry_name}.crdr = 'DEBIT', {$double_name}.amount, NULL)" .
|
||||
($sum ? ')' : '') . ' AS debit' . ($sum ? 's' : ''),
|
||||
|
||||
($sum ? 'SUM(' : '') .
|
||||
"IF({$entry_name}.crdr = 'CREDIT', {$double_name}.amount, NULL)" .
|
||||
($sum ? ')' : '') . ' AS credit' . ($sum ? 's' : ''),
|
||||
|
||||
($sum ? 'SUM(' : '') .
|
||||
"IF({$entry_name}.crdr = '$ftype', 1, -1) * {$double_name}.amount" .
|
||||
// IF({$double_name}.amount, {$double_name}.amount, 0)" .
|
||||
($sum ? ')' : '') . ' AS balance',
|
||||
);
|
||||
|
||||
if ($sum)
|
||||
$fields[] = "COUNT({$entry_name}.id) AS entries";
|
||||
|
||||
return $fields;
|
||||
/**************************************************************************
|
||||
**************************************************************************
|
||||
**************************************************************************
|
||||
* function: debitCreditFields
|
||||
* - Returns the fields necessary to determine whether the queried
|
||||
* entries are a debit, or a credit, and also the effect each have
|
||||
* on the overall balance of the ledger.
|
||||
*/
|
||||
function debitCreditFields($id, $sum = false,
|
||||
$entry_name = 'Entry', $double_name = 'DoubleEntry') {
|
||||
return $this->Account->debitCreditFields
|
||||
($this->accountID($id), $sum, $entry_name, $double_name);
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
@@ -171,7 +161,7 @@ class Ledger extends AppModel {
|
||||
'conditions' => array('Entry.ledger_id' => $ids),
|
||||
));
|
||||
|
||||
pr(compact('entries'));
|
||||
//pr(compact('entries'));
|
||||
return $entries;
|
||||
}
|
||||
|
||||
@@ -182,27 +172,43 @@ class Ledger extends AppModel {
|
||||
* function: stats
|
||||
* - Returns summary data from the requested ledger.
|
||||
*/
|
||||
function stats($id, $cond = null) {
|
||||
if (!isset($cond))
|
||||
$cond = array();
|
||||
$cond[] = array('Ledger.id' => $id);
|
||||
function stats($id, $query = null) {
|
||||
if (!$id)
|
||||
return null;
|
||||
|
||||
$stats = $this->find
|
||||
('first', array
|
||||
('link' =>
|
||||
array(// Models
|
||||
'Account' => array('fields' => array()),
|
||||
'Entry' => array
|
||||
('DoubleEntry' =>
|
||||
array('fields' => array(),
|
||||
'Transaction' => array('fields' => array('stamp')),
|
||||
),
|
||||
),
|
||||
),
|
||||
'fields' => $this->debitCreditFields($id, true),
|
||||
'conditions' => $cond,
|
||||
'group' => 'Ledger.id',
|
||||
));
|
||||
$this->queryInit($query);
|
||||
|
||||
if (!isset($query['link']['Account']))
|
||||
$query['link']['Account'] = array();
|
||||
if (!isset($query['link']['Account']['fields']))
|
||||
$query['link']['Account']['fields'] = array();
|
||||
if (!isset($query['link']['Entry']))
|
||||
$query['link']['Entry'] = array();
|
||||
if (!isset($query['link']['Entry']['DoubleEntry']))
|
||||
$query['link']['Entry']['DoubleEntry'] = array();
|
||||
if (!isset($query['link']['Entry']['DoubleEntry']['fields']))
|
||||
$query['link']['Entry']['DoubleEntry']['fields'] = array();
|
||||
if (!isset($query['link']['Entry']['DoubleEntry']['Transaction']))
|
||||
$query['link']['Entry']['DoubleEntry']['Transaction'] = array();
|
||||
if (!isset($query['link']['Entry']['DoubleEntry']['Transaction']['fields']))
|
||||
$query['link']['Entry']['DoubleEntry']['Transaction']['fields'] = array();
|
||||
$query['link']['Entry']['DoubleEntry']['Transaction']['fields'][] = 'stamp';
|
||||
|
||||
if (!isset($query['fields']))
|
||||
$query['fields'] = array();
|
||||
|
||||
$query['fields'] = array_merge($query['fields'],
|
||||
$this->debitCreditFields($id, true));
|
||||
|
||||
$query['conditions'][] = array('Ledger.id' => $id);
|
||||
$query['group'][] = 'Ledger.id';
|
||||
|
||||
$stats = $this->find('first', $query);
|
||||
|
||||
unset($query['group']);
|
||||
$query['fields'] = $this->debitCreditFields($id, false);
|
||||
$stats = $this->find('all', $query);
|
||||
pr(compact('query', 'stats'));
|
||||
|
||||
// The fields are all tucked into the [0] index,
|
||||
// and the rest of the array is useless (empty).
|
||||
|
||||
Reference in New Issue
Block a user