Significant changes to work with the new account/ledger structure. Removed much of the auto-generated model association code. Added helper functions into the models to perform model related work, such as model 'stats' (a bad name for a function to return a summary of pertinent financial information from a given model instance). There is a ton of cleanup to do, but first I want to get it all captured.
git-svn-id: file:///svn-source/pmgr/branches/ledger_transactions_20090605/site@81 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
@@ -9,35 +9,89 @@ class LedgerEntry extends AppModel {
|
||||
);
|
||||
|
||||
var $belongsTo = array(
|
||||
'MonetarySource' => array(
|
||||
'className' => 'MonetarySource',
|
||||
'foreignKey' => 'monetary_source_id',
|
||||
'conditions' => '',
|
||||
'fields' => '',
|
||||
'order' => ''
|
||||
),
|
||||
'Transaction' => array(
|
||||
'className' => 'Transaction',
|
||||
'foreignKey' => 'transaction_id',
|
||||
'conditions' => '',
|
||||
'fields' => '',
|
||||
'order' => ''
|
||||
),
|
||||
'MonetarySource',
|
||||
'Transaction',
|
||||
|
||||
'DebitLedger' => array(
|
||||
'className' => 'Ledger',
|
||||
'foreignKey' => 'debit_ledger_id',
|
||||
'conditions' => '',
|
||||
'fields' => '',
|
||||
'order' => ''
|
||||
),
|
||||
'CreditLedger' => array(
|
||||
'className' => 'Ledger',
|
||||
'foreignKey' => 'credit_ledger_id',
|
||||
'conditions' => '',
|
||||
'fields' => '',
|
||||
'order' => ''
|
||||
),
|
||||
);
|
||||
|
||||
|
||||
/**************************************************************************
|
||||
**************************************************************************
|
||||
**************************************************************************
|
||||
* function:
|
||||
* -
|
||||
*/
|
||||
function conditionEntryAsCreditOrDebit($ledger_ids) {
|
||||
return array('OR' =>
|
||||
array(array('debit_ledger_id' => $ledger_ids),
|
||||
array('credit_ledger_id' => $ledger_ids)));
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
**************************************************************************
|
||||
**************************************************************************
|
||||
* function: findInLedgerContext
|
||||
* - Returns an array of ledger entries that belong to a given ledger.
|
||||
* There is extra logic to also figure out whether the ledger_entry
|
||||
* amount is either a credit, or a debit, depending on how it was
|
||||
* written into the ledger, as well as whether the amount increases or
|
||||
* decreases the balance depending on the particular account type of
|
||||
* the ledger.
|
||||
*/
|
||||
function findInLedgerContext($ledger_id, $account_type, $cond = null, $link = null) {
|
||||
if (!isset($link))
|
||||
$link = array('Transaction');
|
||||
|
||||
if (in_array($account_type, array('ASSET', 'EXPENSE')))
|
||||
$ledger_type = 'debit';
|
||||
else
|
||||
$ledger_type = 'credit';
|
||||
|
||||
$entries = $this->find
|
||||
('all',
|
||||
array('link' => $link,
|
||||
|
||||
'fields' =>
|
||||
array('id', 'name', 'comment',
|
||||
"IF(LedgerEntry.debit_ledger_id = $ledger_id," .
|
||||
" LedgerEntry.amount, NULL) AS debit",
|
||||
"IF(LedgerEntry.credit_ledger_id = $ledger_id," .
|
||||
" LedgerEntry.amount, NULL) AS credit",
|
||||
"(IF(LedgerEntry.{$ledger_type}_ledger_id = $ledger_id, 1, -1)" .
|
||||
" * LedgerEntry.amount) AS balance"),
|
||||
|
||||
'conditions' =>
|
||||
array(isset($cond) ? $cond : array(),
|
||||
'OR' =>
|
||||
array(array('LedgerEntry.debit_ledger_id' => $ledger_id),
|
||||
array('LedgerEntry.credit_ledger_id' => $ledger_id))),
|
||||
|
||||
'order' =>
|
||||
array('Transaction.stamp'),
|
||||
));
|
||||
|
||||
/* $entries['summary'] = array('balance' => null, 'debit' => null, 'credit' => null); */
|
||||
/* foreach($entries AS $entry) */
|
||||
/* $this->statsMerge($entries['summary'], $entry[0]); */
|
||||
|
||||
/* //if (isset($entries['summary']['debit']) || isset($entries['summary']['credit'])) { */
|
||||
/* $entries['summary']['debits'] = $entries['summary']['debit']; */
|
||||
/* $entries['summary']['credits'] = $entries['summary']['credit']; */
|
||||
/* unset($entries['summary']['debit']); */
|
||||
/* unset($entries['summary']['credit']); */
|
||||
/* //} */
|
||||
|
||||
|
||||
return $entries;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user