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:
@@ -103,11 +103,24 @@ class TransactionsController extends AppController {
|
||||
*/
|
||||
|
||||
function postReceipt() {
|
||||
if ($this->RequestHandler->isPost()) {
|
||||
pr($this->data);
|
||||
//$this->redirect(array('action'=>'index'));
|
||||
}
|
||||
$this->autoRender = false;
|
||||
|
||||
if (!$this->RequestHandler->isPost()) {
|
||||
echo('<H2>THIS IS NOT A POST FOR SOME REASON</H2>');
|
||||
return;
|
||||
}
|
||||
//pr($this->data);
|
||||
|
||||
$amount = 0;
|
||||
foreach ($this->data['LedgerEntry'] AS $entry) {
|
||||
$amount += isset($entry['amount']) ? $entry['amount'] : 0;
|
||||
}
|
||||
|
||||
$cust_id = $this->data['Customer']['id'];
|
||||
$cust = new Customer();
|
||||
$unreconciled = $cust->findUnreconciledLedgerEntries($cust_id);
|
||||
$reconciled = $cust->reconcileNewLedgerEntry($cust_id, 'credit', $amount);
|
||||
pr(compact('amount', 'unreconciled', 'reconciled'));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
**************************************************************************
|
||||
**************************************************************************
|
||||
|
||||
@@ -75,7 +75,7 @@ class Customer extends AppModel {
|
||||
* (such as charges not paid).
|
||||
*/
|
||||
|
||||
function findUnreconciledLedgerEntries($id = null) {
|
||||
function findUnreconciledLedgerEntries($id = null, $fundamental_type = null) {
|
||||
$customer = $this->find('first',
|
||||
array('contain' =>
|
||||
array('Lease' => array('fields' => array('id'))),
|
||||
@@ -83,20 +83,117 @@ class Customer extends AppModel {
|
||||
'conditions' => array(array('Customer.id' => $id))));
|
||||
|
||||
$unreconciled = $this->Account->findUnreconciledLedgerEntries
|
||||
($customer['Customer']['account_id']);
|
||||
($customer['Customer']['account_id'], $fundamental_type);
|
||||
|
||||
foreach ($customer['Lease'] AS $lease) {
|
||||
$lease_unrec = $this->Lease->findUnreconciledLedgerEntries($lease['id']);
|
||||
$unreconciled['debits'] = array_merge($unreconciled['debits'],
|
||||
$lease_unrec['debits']);
|
||||
$unreconciled['credits'] = array_merge($unreconciled['credits'],
|
||||
$lease_unrec['credits']);
|
||||
$unrec = $this->Lease->findUnreconciledLedgerEntries($lease['id'],
|
||||
$fundamental_type);
|
||||
|
||||
foreach (array('debit', 'credit', 'entries') AS $type) {
|
||||
if (!isset($unreconciled[$type]))
|
||||
continue;
|
||||
|
||||
if ($type == 'entries') {
|
||||
$left = &$unreconciled;
|
||||
$right = &$unrec;
|
||||
} else {
|
||||
$left = &$unreconciled[$type];
|
||||
$right = &$unrec[$type];
|
||||
}
|
||||
|
||||
$left['entries'] = array_merge($left['entries'], $right['entries']);
|
||||
$left['balance'] += $right['balance'];
|
||||
}
|
||||
|
||||
/* if ($fundamental_type) { */
|
||||
/* $unreconciled['entries'] */
|
||||
/* = array_merge($unreconciled['entries'], $unrec['entries']); */
|
||||
/* } */
|
||||
/* else { */
|
||||
/* foreach (array_keys($unreconciled) AS $type) { */
|
||||
/* $unreconciled[$type]['entries'] */
|
||||
/* = array_merge($unreconciled[$type]['entries'], */
|
||||
/* $unrec[$type]['entries']); */
|
||||
/* $unreconciled['balance'] += $unrec['balance']; */
|
||||
/* } */
|
||||
/* } */
|
||||
|
||||
/* $balance = $unreconciled['balance']; */
|
||||
/* $unreconciled = array_merge_recursive */
|
||||
/* (array_diff_key($unreconciled, array('balance'=>1)), */
|
||||
/* $this->Lease->findUnreconciledLedgerEntries($lease['id'])); */
|
||||
/* $unreconciled['balance'] += $balance; */
|
||||
}
|
||||
|
||||
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) {
|
||||
$customer = $this->find('first',
|
||||
array('contain' =>
|
||||
array('Lease' => array('fields' => array('id'))),
|
||||
'fields' => array('account_id'),
|
||||
'conditions' => array(array('Customer.id' => $id))));
|
||||
|
||||
$reconciled = $this->Account->reconcileNewLedgerEntry
|
||||
($customer['Customer']['account_id'], $fund, $amount);
|
||||
|
||||
foreach ($customer['Lease'] AS $lease) {
|
||||
$rec = $this->Lease->reconcileNewLedgerEntry($lease['id'],
|
||||
$fund,
|
||||
$reconciled['unapplied']);
|
||||
|
||||
//pr(compact('reconciled', 'rec'));
|
||||
foreach (array('debit', 'credit', 'entries') AS $type) {
|
||||
if (!isset($reconciled[$type]))
|
||||
continue;
|
||||
|
||||
if ($type == 'entries') {
|
||||
$left = &$reconciled;
|
||||
$right = &$rec;
|
||||
} else {
|
||||
$left = &$reconciled[$type];
|
||||
$right = &$rec[$type];
|
||||
}
|
||||
|
||||
//pr(compact('type', 'left', 'right'));
|
||||
$left['entries'] = array_merge($left['entries'], $right['entries']);
|
||||
$left['balance'] += $right['balance'];
|
||||
$left['applied'] += $right['applied'];
|
||||
$left['unapplied'] = $right['unapplied'];
|
||||
}
|
||||
}
|
||||
|
||||
/* foreach ($customer['Lease'] AS $lease) { */
|
||||
/* $unapplied = $unreconciled['unapplied']; */
|
||||
/* $balance = $unreconciled['balance']; */
|
||||
/* $unreconciled = array_merge_recursive */
|
||||
/* (array_diff_key($unreconciled, array('unapplied'=>1, 'balance'=>1)), */
|
||||
/* $this->Lease->reconcileNewLedgerEntry($lease['id'], */
|
||||
/* $fund, */
|
||||
/* $unreconciled['unapplied'])); */
|
||||
/* $unreconciled['balance'] += $balance; */
|
||||
/* } */
|
||||
|
||||
return $reconciled;
|
||||
}
|
||||
|
||||
|
||||
/**************************************************************************
|
||||
**************************************************************************
|
||||
**************************************************************************
|
||||
|
||||
@@ -30,6 +30,24 @@ class Lease extends AppModel {
|
||||
'LateSchedule',
|
||||
);
|
||||
|
||||
/**************************************************************************
|
||||
**************************************************************************
|
||||
**************************************************************************
|
||||
* function: accountId
|
||||
* - Returns the accountId of the given lease
|
||||
*/
|
||||
function accountId($id) {
|
||||
$this->cacheQueries = true;
|
||||
$lease = $this->find('first', array
|
||||
('recursive' => -1,
|
||||
'fields' => array('account_id'),
|
||||
'conditions' => array(array('id' => $id)),
|
||||
));
|
||||
$this->cacheQueries = false;
|
||||
|
||||
return $lease['Lease']['account_id'];
|
||||
}
|
||||
|
||||
|
||||
/**************************************************************************
|
||||
**************************************************************************
|
||||
@@ -43,14 +61,9 @@ class Lease extends AppModel {
|
||||
/* 'args' => compact('id', 'all', 'cond', 'link'), */
|
||||
/* )); */
|
||||
|
||||
$lease = $this->find('first', array
|
||||
('recursive' => -1,
|
||||
'fields' => array('account_id'),
|
||||
'conditions' => array(array('id' => $id)),
|
||||
));
|
||||
|
||||
$entries = $this->Account->findLedgerEntries($lease['Lease']['account_id'],
|
||||
$entries = $this->Account->findLedgerEntries($this->accountId($id),
|
||||
$all, $cond, $link);
|
||||
|
||||
/* pr(array('function' => 'Lease::findAccountEntries', */
|
||||
/* 'args' => compact('id', 'all', 'cond', 'link'), */
|
||||
/* 'vars' => compact('lease'), */
|
||||
@@ -71,14 +84,8 @@ class Lease extends AppModel {
|
||||
/* 'args' => compact('id', 'link'), */
|
||||
/* )); */
|
||||
|
||||
$lease = $this->find('first', array
|
||||
('recursive' => -1,
|
||||
'fields' => array('account_id'),
|
||||
'conditions' => array(array('id' => $id)),
|
||||
));
|
||||
|
||||
$entries = $this->Account->findLedgerEntriesRelatedToAccount
|
||||
($lease['Lease']['account_id'],
|
||||
($this->accountId($id),
|
||||
$this->Account->securityDepositAccountID(),
|
||||
true, null, $link);
|
||||
|
||||
@@ -99,14 +106,29 @@ class Lease extends AppModel {
|
||||
* (such as charges not paid).
|
||||
*/
|
||||
|
||||
function findUnreconciledLedgerEntries($id = null) {
|
||||
$lease = $this->find('first', array
|
||||
('recursive' => -1,
|
||||
'fields' => array('account_id'),
|
||||
'conditions' => array(array('id' => $id)),
|
||||
));
|
||||
function findUnreconciledLedgerEntries($id = null, $fundamental_type = null) {
|
||||
return $this->Account->findUnreconciledLedgerEntries
|
||||
($this->accountId($id), $fundamental_type);
|
||||
}
|
||||
|
||||
return $this->Account->findUnreconciledLedgerEntries($lease['Lease']['account_id']);
|
||||
|
||||
/**************************************************************************
|
||||
**************************************************************************
|
||||
**************************************************************************
|
||||
* 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) {
|
||||
return $this->Account->reconcileNewLedgerEntry
|
||||
($this->accountId($id), $fund, $amount);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user