Added routines to reconcile a new ledger entry against unreconciled entries. I haven't tested it robustly, but it seems to work on the surface at least.

git-svn-id: file:///svn-source/pmgr/branches/ledger_transactions_20090605@161 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
abijah
2009-06-17 18:40:39 +00:00
parent a136fd5313
commit 2e67695154
4 changed files with 312 additions and 103 deletions

View File

@@ -19,7 +19,61 @@ class Account extends AppModel {
'Ledger',
);
var $cache;
/**************************************************************************
**************************************************************************
**************************************************************************
* function: type
* - Returns the type of this account
*/
function type($id) {
$this->cacheQueries = true;
$account = $this->find('first', array
('recursive' => -1,
'fields' => array('type'),
'conditions' => array(array('Account.id' => $id)),
));
$this->cacheQueries = false;
return $account['Account']['type'];
}
/**************************************************************************
**************************************************************************
**************************************************************************
* function: fundamentalType
* - Returns the fundmental type of the account, credit or debit
*/
function fundamentalType($id_or_type) {
if (is_numeric($id_or_type))
$type = $this->type($id_or_type);
else
$type = $id_or_type;
// Asset and Expense accounts are debit accounts
if (in_array($type, array('ASSET', 'EXPENSE')))
return 'debit';
// Otherwise, it's a credit account
return 'credit';
}
/**************************************************************************
**************************************************************************
**************************************************************************
* function: fundamentalOpposite
* - Returns the opposite fundmental type of the account, credit or debit
*/
function fundamentalOpposite($id_or_type) {
$fund = $this->fundamentalType($id_or_type);
if ($fund == 'debit')
return 'credit';
return 'debit';
}
/**************************************************************************
@@ -29,11 +83,13 @@ class Account extends AppModel {
* - Returns the ID of the Security Deposit Account
*/
function securityDepositAccountID() {
$this->cacheQueries = true;
$account = $this->find('first', array
('recursive' => -1,
'conditions' => array
(array('name' => 'Security Deposit')),
));
$this->cacheQueries = false;
return $account['Account']['id'];
}
@@ -45,43 +101,17 @@ class Account extends AppModel {
* - Returns the ID of the Rent Account
*/
function rentAccountID() {
$this->cacheQueries = true;
$account = $this->find('first', array
('recursive' => -1,
'conditions' => array
(array('name' => 'Rent')),
));
$this->cacheQueries = false;
return $account['Account']['id'];
}
/**************************************************************************
**************************************************************************
**************************************************************************
* function: type
* - Returns the type of this array of ledger ids from the given account
*/
function type($id) {
if (isset($this->cache[$id]['type'])) {
return $this->cache[$id]['type'];
}
$account = $this->find('first', array
('recursive' => -1,
'fields' => array('type'),
'conditions' => array(array('Account.id' => $id)),
));
// Save the account type in our cache for future reference
$this->cache[$id]['type'] = $account['Account']['type'];
/* pr(array('function' => 'Account::type', */
/* 'args' => compact('id'), */
/* 'return' => $this->cache[$id]['type'])); */
return $this->cache[$id]['type'];
}
/**************************************************************************
**************************************************************************
**************************************************************************
@@ -201,56 +231,103 @@ class Account extends AppModel {
* (such as charges not paid).
*/
function findUnreconciledLedgerEntries($id = null) {
// Look in the Current Ledger only (for now)
$unreconciled['debits'] = $this->find
('all', array
('link' => array
('Ledger' => array
('fields' => array(),
'DebitLedgerEntry' => array
('fields' => array('id', 'amount'),
'DebitReconciliationLedgerEntry' => array
('fields' => array('COALESCE(SUM(Reconciliation.amount),0) AS "reconciled"',
'DebitLedgerEntry.amount - COALESCE(SUM(Reconciliation.amount),0) AS "balance"',
),
function findUnreconciledLedgerEntries($id = null, $fundamental_type = null) {
foreach (($fundamental_type
? array($fundamental_type)
: array('debit', 'credit')) AS $fund) {
$ucfund = ucfirst($fund);
$unreconciled[$fund]['entries'] = $this->find
('all', array
('link' => array
('Ledger' => array
('fields' => array(),
"LedgerEntry" => array
('class' => "{$ucfund}LedgerEntry",
'fields' => array('id', 'amount'),
"ReconciliationLedgerEntry" => array
('class' => "{$ucfund}ReconciliationLedgerEntry",
'fields' => array
("COALESCE(SUM(Reconciliation.amount),0) AS 'reconciled'",
"LedgerEntry.amount - COALESCE(SUM(Reconciliation.amount),0) AS 'balance'",
),
),
),
),
),
),
),
'group' => ('DebitLedgerEntry.id' .
' HAVING DebitLedgerEntry.amount' .
' <> COALESCE(SUM(Reconciliation.amount),0)'),
'conditions' => array('Account.id' => $id),
'fields' => array(),
));
'group' => ("LedgerEntry.id" .
" HAVING LedgerEntry.amount" .
" <> COALESCE(SUM(Reconciliation.amount),0)"),
'conditions' => array('Account.id' => $id),
'fields' => array(),
));
$balance = 0;
foreach ($unreconciled[$fund]['entries'] AS &$entry) {
//$balance += $entry[0]['balance'];
/* $entry["LedgerEntry"] += $entry[0]; */
/* unset($entry[0]); */
$entry = array_merge(array_diff_key($entry["LedgerEntry"], array(0=>true)),
$entry[0]);
$balance += $entry['balance'];
}
$unreconciled[$fund]['balance'] = $balance;
}
$unreconciled['credits'] = $this->find
('all', array
('link' => array
('Ledger' => array
('fields' => array(),
'CreditLedgerEntry' => array
('fields' => array('id', 'amount'),
'CreditReconciliationLedgerEntry' => array
('fields' => array('COALESCE(SUM(Reconciliation.amount),0) AS "reconciled"',
'CreditLedgerEntry.amount - COALESCE(SUM(Reconciliation.amount),0) AS "balance"',
),
),
),
),
),
'group' => ('CreditLedgerEntry.id' .
' HAVING CreditLedgerEntry.amount' .
' <> COALESCE(SUM(Reconciliation.amount),0)'),
'conditions' => array('Account.id' => $id),
'fields' => array(),
));
// pull up to the top level if only one fundamental type
if ($fundamental_type)
$unreconciled = $unreconciled[$fundamental_type];
return $unreconciled;
}
/**************************************************************************
**************************************************************************
**************************************************************************
* function: reconcileNewLedgerEntry
* - Returns which ledger entries a new credit/debit would
* reconcile, and how much.
*
* - REVISIT <AP> 20090617
* This should be subject to different algorithms, such
* as apply to oldest charges first, newest first, to fees
* before rent, etc. Until we get there, I'll hardcode
* whatever algorithm is simplest.
*/
function reconcileNewLedgerEntry($id, $fund, $amount) {
// if there is no money in the entry, it can reconcile nothing
// don't bother wasting time sifting ledger entries.
if ($amount <= 0)
return array('unapplied' => 0);
$fund = $this->fundamentalOpposite($fund);
$unreconciled = $this->findUnreconciledLedgerEntries($id, $fund);
$applied = 0;
foreach ($unreconciled['entries'] AS $i => &$entry) {
// Determine if amount is sufficient to cover the entry
if ($amount > $entry['balance'])
$apply = $entry['balance'];
elseif ($amount > 0)
$apply = $amount;
else {
unset($unreconciled[$i]);
continue;
}
$entry['applied'] = $apply;
$entry['reconciled'] += $apply;
$entry['balance'] -= $apply;
$applied += $apply;
$amount -= $apply;
}
$unreconciled['unapplied'] = $amount;
$unreconciled['applied'] = $applied;
$unreconciled['balance'] -= $applied;
return $unreconciled;
}
/**************************************************************************
**************************************************************************
**************************************************************************