git-svn-id: file:///svn-source/pmgr/branches/ledger_transactions_20090605@152 97e9348a-65ac-dc4b-aefc-98561f571b83
157 lines
5.5 KiB
PHP
157 lines
5.5 KiB
PHP
<?php
|
|
class LedgerEntry extends AppModel {
|
|
|
|
var $name = 'LedgerEntry';
|
|
var $validate = array(
|
|
'id' => array('numeric'),
|
|
'transaction_id' => array('numeric'),
|
|
'amount' => array('money')
|
|
);
|
|
|
|
var $belongsTo = array(
|
|
'MonetarySource',
|
|
'Transaction',
|
|
|
|
'DebitLedger' => array(
|
|
'className' => 'Ledger',
|
|
'foreignKey' => 'debit_ledger_id',
|
|
),
|
|
'CreditLedger' => array(
|
|
'className' => 'Ledger',
|
|
'foreignKey' => 'credit_ledger_id',
|
|
),
|
|
);
|
|
|
|
var $hasAndBelongsToMany = array(
|
|
'DebitReconciliationLedgerEntry' => array(
|
|
'className' => 'LedgerEntry',
|
|
'joinTable' => 'reconciliations',
|
|
'foreignKey' => 'credit_ledger_entry_id',
|
|
'associationForeignKey' => 'debit_ledger_entry_id',
|
|
),
|
|
'CreditReconciliationLedgerEntry' => array(
|
|
'className' => 'LedgerEntry',
|
|
'joinTable' => 'reconciliations',
|
|
'foreignKey' => 'debit_ledger_entry_id',
|
|
'associationForeignKey' => 'credit_ledger_entry_id',
|
|
),
|
|
);
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* function: conditionEntryAsCreditOrDebit
|
|
* - returns the condition necessary to match a set of
|
|
* Ledgers to all related LedgerEntries
|
|
*/
|
|
function conditionEntryAsCreditOrDebit($ledger_ids) {
|
|
return array('OR' =>
|
|
array(array('debit_ledger_id' => $ledger_ids),
|
|
array('credit_ledger_id' => $ledger_ids)));
|
|
}
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* function: ledgerContext query helpers
|
|
* - Returns parameters necessary to generate a query which
|
|
* puts ledger entries into the context of a ledger. Since
|
|
* debit/credit depends on the account type, it is required
|
|
* as an argument for each function to avoid having to
|
|
* query the ledger/account to find it out.
|
|
*/
|
|
function ledgerContextFields($ledger_id, $account_type) {
|
|
if (in_array($account_type, array('ASSET', 'EXPENSE')))
|
|
$ledger_type = 'debit';
|
|
else
|
|
$ledger_type = 'credit';
|
|
|
|
return 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",
|
|
);
|
|
}
|
|
|
|
function ledgerContextConditions($ledger_id, $account_type) {
|
|
return array
|
|
('OR' =>
|
|
array(array('LedgerEntry.debit_ledger_id' => $ledger_id),
|
|
array('LedgerEntry.credit_ledger_id' => $ledger_id)),
|
|
);
|
|
}
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* 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 (!isset($cond))
|
|
$cond = array();
|
|
|
|
$fields = $this->ledgerContextFields($ledger_id, $account_type);
|
|
$cond[] = $this->ledgerContextConditions($ledger_id, $account_type);
|
|
$order = array('Transaction.stamp');
|
|
|
|
$entries = $this->find
|
|
('all',
|
|
array('link' => $link,
|
|
'fields' => $fields,
|
|
'conditions' => $cond,
|
|
'order' => $order,
|
|
));
|
|
|
|
return $entries;
|
|
}
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* function: stats
|
|
* - Returns summary data from the requested ledger entry
|
|
*/
|
|
function stats($id) {
|
|
|
|
$query = array
|
|
(
|
|
'fields' => array("SUM(Reconciliation.amount) AS 'reconciled'"),
|
|
|
|
'conditions' => array(isset($cond) ? $cond : array(),
|
|
array('LedgerEntry.id' => $id)),
|
|
|
|
'group' => 'LedgerEntry.id',
|
|
);
|
|
|
|
// Get the applied amounts on the debit side
|
|
$query['link'] =
|
|
array('DebitReconciliationLedgerEntry' => array('alias' => 'DRLE', 'DRLETransaction' => array('class' => 'Transaction')));
|
|
$tmpstats = $this->find('first', $query);
|
|
$stats['debit_amount_reconciled'] = $tmpstats[0]['reconciled'];
|
|
|
|
// Get the applied amounts on the credit side
|
|
$query['link'] =
|
|
array('CreditReconciliationLedgerEntry' => array('alias' => 'CRLE', 'CRLETransaction' => array('class' => 'Transaction')));
|
|
$tmpstats = $this->find('first', $query);
|
|
$stats['credit_amount_reconciled'] = $tmpstats[0]['reconciled'];
|
|
|
|
return $stats;
|
|
}
|
|
|
|
}
|