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/site@161 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
/**************************************************************************
|
||||
**************************************************************************
|
||||
**************************************************************************
|
||||
|
||||
Reference in New Issue
Block a user