diff --git a/models/account.php b/models/account.php index 13333a6..8945109 100644 --- a/models/account.php +++ b/models/account.php @@ -262,9 +262,6 @@ class Account extends AppModel { )); $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']; @@ -272,10 +269,6 @@ class Account extends AppModel { $unreconciled[$fund]['balance'] = $balance; } - // pull up to the top level if only one fundamental type - if ($fundamental_type) - $unreconciled = $unreconciled[$fundamental_type]; - return $unreconciled; } @@ -294,37 +287,38 @@ class Account extends AppModel { * whatever algorithm is simplest. */ - function reconcileNewLedgerEntry($id, $fund, $amount) { + function reconcileNewLedgerEntry($id, $fundamental_type, $amount) { + $ofund = $this->fundamentalOpposite($fundamental_type); + $unreconciled = array($ofund => array('entries'=>array(), 'balance'=>0)); + $applied = 0; + // 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); + if ($amount > 0) { + $unreconciled = $this->findUnreconciledLedgerEntries($id, $ofund); - $fund = $this->fundamentalOpposite($fund); - $unreconciled = $this->findUnreconciledLedgerEntries($id, $fund); + foreach ($unreconciled[$ofund]['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; + } - $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; } - - $entry['applied'] = $apply; - $entry['reconciled'] += $apply; - $entry['balance'] -= $apply; - $applied += $apply; - $amount -= $apply; } - $unreconciled['unapplied'] = $amount; - $unreconciled['applied'] = $applied; - $unreconciled['balance'] -= $applied; + $unreconciled[$ofund]['unapplied'] = $amount; + $unreconciled[$ofund]['applied'] = $applied; + $unreconciled[$ofund]['balance'] -= $applied; return $unreconciled; } diff --git a/models/customer.php b/models/customer.php index 2aa94e5..e6475d5 100644 --- a/models/customer.php +++ b/models/customer.php @@ -28,6 +28,46 @@ class Customer extends AppModel { ); + /************************************************************************** + ************************************************************************** + ************************************************************************** + * function: accountId + * - Returns the account ID for the given customer + */ + function accountId($id) { + $this->cacheQueries = true; + $customer = $this->find('first', + array('contain' => false, + 'fields' => array('account_id'), + 'conditions' => array(array('Customer.id' => $id)))); + $this->cacheQueries = false; + + return $customer['Customer']['account_id']; + } + + + /************************************************************************** + ************************************************************************** + ************************************************************************** + * function: leaseIds + * - Returns the lease IDs for the given customer + */ + function leaseIds($id) { + $this->cacheQueries = true; + $customer = $this->find('first', + array('contain' => + array('Lease' => array('fields' => array('id'))), + 'fields' => array(), + 'conditions' => array(array('Customer.id' => $id)))); + $this->cacheQueries = false; + + $ids = array(); + foreach ($customer['Lease'] AS $lease) + $ids[] = $lease['id']; + + return $ids; + } + /************************************************************************** ************************************************************************** ************************************************************************** @@ -39,19 +79,13 @@ class Customer extends AppModel { /* 'args' => compact('id', 'link'), */ /* )); */ - $customer = $this->find('first', - array('contain' => - array('Lease' => array('fields' => array('id'))), - 'fields' => array('account_id'), - 'conditions' => array(array('Customer.id' => $id)))); - $entries = $this->Account->findLedgerEntriesRelatedToAccount - ($customer['Customer']['account_id'], + ($this->accountId($id), $this->Account->securityDepositAccountID(), true, null, $link); - foreach ($customer['Lease'] AS $lease) { - $ledger_entries = $this->Lease->findSecurityDeposits($lease['id'], $link); + foreach ($this->leaseIds($id) AS $lease_id) { + $ledger_entries = $this->Lease->findSecurityDeposits($lease_id, $link); $this->statsMerge($entries['summary'], $ledger_entries['summary']); $entries['Entries'] = array_merge($entries['Entries'], $ledger_entries['Entries']); @@ -76,53 +110,21 @@ class Customer extends AppModel { */ function findUnreconciledLedgerEntries($id = null, $fundamental_type = null) { - $customer = $this->find('first', - array('contain' => - array('Lease' => array('fields' => array('id'))), - 'fields' => array('account_id'), - 'conditions' => array(array('Customer.id' => $id)))); $unreconciled = $this->Account->findUnreconciledLedgerEntries - ($customer['Customer']['account_id'], $fundamental_type); + ($this->accountId($id), $fundamental_type); - foreach ($customer['Lease'] AS $lease) { - $unrec = $this->Lease->findUnreconciledLedgerEntries($lease['id'], + foreach ($this->leaseIds($id) AS $lease_id) { + $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]; - } + foreach (array_keys($unreconciled) AS $type) { + $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; @@ -143,35 +145,20 @@ class Customer extends AppModel { * 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)))); + function reconcileNewLedgerEntry($id, $fundamental_type, $amount) { $reconciled = $this->Account->reconcileNewLedgerEntry - ($customer['Customer']['account_id'], $fund, $amount); + ($this->accountId($id), $fundamental_type, $amount); - foreach ($customer['Lease'] AS $lease) { - $rec = $this->Lease->reconcileNewLedgerEntry($lease['id'], - $fund, - $reconciled['unapplied']); + foreach ($this->leaseIds($id) AS $lease_id) { + foreach (array_keys($reconciled) AS $type) { + $rec = $this->Lease->reconcileNewLedgerEntry($lease_id, + $fundamental_type, + $reconciled[$type]['unapplied']); - //pr(compact('reconciled', 'rec')); - foreach (array('debit', 'credit', 'entries') AS $type) { - if (!isset($reconciled[$type])) - continue; + $left = &$reconciled[$type]; + $right = &$rec[$type]; - 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']; @@ -179,17 +166,6 @@ class Customer extends AppModel { } } -/* 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; } diff --git a/models/lease.php b/models/lease.php index c6e8175..73b9950 100644 --- a/models/lease.php +++ b/models/lease.php @@ -126,9 +126,9 @@ class Lease extends AppModel { * whatever algorithm is simplest. */ - function reconcileNewLedgerEntry($id, $fund, $amount) { + function reconcileNewLedgerEntry($id, $fundamental_type, $amount) { return $this->Account->reconcileNewLedgerEntry - ($this->accountId($id), $fund, $amount); + ($this->accountId($id), $fundamental_type, $amount); }