Another snapshot. I think I'll be taking the CREDIT/DEBIT reconcile functionality out.
git-svn-id: file:///svn-source/pmgr/branches/yafr_20090716@353 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
@@ -8,6 +8,8 @@ class Ledger extends AppModel {
|
||||
);
|
||||
|
||||
var $hasMany = array(
|
||||
'Entry',
|
||||
|
||||
'DoubleEntry' => array(
|
||||
'foreignKey' => false,
|
||||
|
||||
@@ -27,17 +29,6 @@ class Ledger extends AppModel {
|
||||
|
||||
'counterQuery' => ''
|
||||
),
|
||||
|
||||
'DebitLedgerEntry' => array(
|
||||
'className' => 'DoubleEntry',
|
||||
'foreignKey' => 'debit_ledger_id',
|
||||
'dependent' => false,
|
||||
),
|
||||
'CreditLedgerEntry' => array(
|
||||
'className' => 'DoubleEntry',
|
||||
'foreignKey' => 'credit_ledger_id',
|
||||
'dependent' => false,
|
||||
),
|
||||
);
|
||||
|
||||
|
||||
@@ -129,66 +120,58 @@ 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: ledgerEntries
|
||||
* - Returns an array of ledger entries that belong to a given
|
||||
* ledger. There is extra work done... see the DoubleEntry model.
|
||||
* ledger. There is extra work done to establish debit/credit
|
||||
*/
|
||||
function ledgerEntries($id, $account_type = null, $cond = null, $link = null) {
|
||||
/* pr(array('function' => 'Ledger::findLedgerEntries', */
|
||||
/* 'args' => compact('id', 'account_type', 'cond', 'link'), */
|
||||
/* )); */
|
||||
function ledgerEntries($ids, $cond = null, $link = null) {
|
||||
if (empty($ids))
|
||||
return null;
|
||||
|
||||
if (!isset($account_type)) {
|
||||
$ledger = $this->find('first', array
|
||||
('contain' => array
|
||||
('Account' => array
|
||||
('fields' => array('type'),
|
||||
),
|
||||
),
|
||||
'fields' => array(),
|
||||
'conditions' => array(array('Ledger.id' => $id)),
|
||||
));
|
||||
$account_type = $ledger['Account']['type'];
|
||||
}
|
||||
/* $id = (is_array($ids) ? $ids[0] : $ids); */
|
||||
/* $ftype = strtoupper($this->Account->fundamentalType($this->accountID($id))); */
|
||||
|
||||
// If the requested entries are limited by date, we must calculate
|
||||
// a balance forward, or the resulting balance will be thrown off.
|
||||
//
|
||||
// REVISIT <AP>: This obviously is more general than date.
|
||||
// As such, it will not work (or, only work if the
|
||||
// condition only manages to exclude the first parts
|
||||
// of the ledger, nothing in the middle or at the
|
||||
// end. For now, I'll just create an 'other' entry,
|
||||
// not necessarily a balance forward.
|
||||
$entries = $this->Entry->find
|
||||
('all', array
|
||||
('contain' => array('DoubleEntry' => array('fields' => array('amount'))),
|
||||
'fields' => array_merge(array("Entry.*"),
|
||||
$this->debitCreditFields(is_array($ids) ? $ids[0] : $ids)),
|
||||
/* "IF(Entry.crdr = 'CREDIT', DoubleEntry.amount, NULL) AS credit", */
|
||||
/* "IF(Entry.crdr = 'DEBIT', DoubleEntry.amount, NULL) AS debit", */
|
||||
/* "IF(Entry.crdr = '$ftype', 1, -1) * DoubleEntry.amount AS balance", */
|
||||
|
||||
$bf = array();
|
||||
if (0 && isset($cond)) {
|
||||
//$date = '<NOT IMPLEMENTED>';
|
||||
$stats = $this->stats($id, array('NOT' => array($cond)));
|
||||
$bf = array(array(array('debit' => $stats['debits'],
|
||||
'credit' => $stats['credits'],
|
||||
'balance' => $stats['balance']),
|
||||
'conditions' => array('Entry.ledger_id' => $ids),
|
||||
));
|
||||
|
||||
'DoubleEntry' => array('id' => null,
|
||||
//'comment' => "Balance Forward from $date"),
|
||||
'comment' => "-- SUMMARY OF EXCLUDED ENTRIES --"),
|
||||
|
||||
'Transaction' => array('id' => null,
|
||||
//'stamp' => $date,
|
||||
'stamp' => null,
|
||||
'comment' => null),
|
||||
));
|
||||
}
|
||||
|
||||
$entries = $this->DoubleEntry->findInLedgerContext($id, $account_type, $cond, $link);
|
||||
/* pr(array('function' => 'Ledger::findLedgerEntries', */
|
||||
/* 'args' => compact('id', 'account_type', 'cond', 'link'), */
|
||||
/* 'vars' => compact('ledger'), */
|
||||
/* 'return' => compact('entries'), */
|
||||
/* )); */
|
||||
pr(compact('entries'));
|
||||
return $entries;
|
||||
}
|
||||
|
||||
@@ -209,23 +192,14 @@ class Ledger extends AppModel {
|
||||
('link' =>
|
||||
array(// Models
|
||||
'Account' => array('fields' => array()),
|
||||
//'DoubleEntry' => array('fields' => array()),
|
||||
'DoubleEntry' =>
|
||||
array('fields' => array(),
|
||||
'Transaction' => array('fields' => array('stamp')),
|
||||
),
|
||||
'Entry' => array
|
||||
('DoubleEntry' =>
|
||||
array('fields' => array(),
|
||||
'Transaction' => array('fields' => array('stamp')),
|
||||
),
|
||||
),
|
||||
),
|
||||
'fields' =>
|
||||
array("SUM(IF(DoubleEntry.debit_ledger_id = Ledger.id,
|
||||
DoubleEntry.amount, NULL)) AS debits",
|
||||
"SUM(IF(DoubleEntry.credit_ledger_id = Ledger.id,
|
||||
DoubleEntry.amount, NULL)) AS credits",
|
||||
"SUM(IF(Account.type IN ('ASSET', 'EXPENSE'),
|
||||
IF(DoubleEntry.debit_ledger_id = Ledger.id, 1, -1),
|
||||
IF(DoubleEntry.credit_ledger_id = Ledger.id, 1, -1)
|
||||
) * IF(DoubleEntry.amount, DoubleEntry.amount, 0)
|
||||
) AS balance",
|
||||
"COUNT(DoubleEntry.id) AS entries"),
|
||||
'fields' => $this->debitCreditFields($id, true),
|
||||
'conditions' => $cond,
|
||||
'group' => 'Ledger.id',
|
||||
));
|
||||
|
||||
Reference in New Issue
Block a user