Changed the unreconciled transaction list to always include 'debit' or 'credit', even if the user specifically requests one or the other. Handling it as a special case everywhere was bothering me.
git-svn-id: file:///svn-source/pmgr/branches/ledger_transactions_20090605/site@162 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
@@ -262,9 +262,6 @@ class Account extends AppModel {
|
|||||||
));
|
));
|
||||||
$balance = 0;
|
$balance = 0;
|
||||||
foreach ($unreconciled[$fund]['entries'] AS &$entry) {
|
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 = array_merge(array_diff_key($entry["LedgerEntry"], array(0=>true)),
|
||||||
$entry[0]);
|
$entry[0]);
|
||||||
$balance += $entry['balance'];
|
$balance += $entry['balance'];
|
||||||
@@ -272,10 +269,6 @@ class Account extends AppModel {
|
|||||||
$unreconciled[$fund]['balance'] = $balance;
|
$unreconciled[$fund]['balance'] = $balance;
|
||||||
}
|
}
|
||||||
|
|
||||||
// pull up to the top level if only one fundamental type
|
|
||||||
if ($fundamental_type)
|
|
||||||
$unreconciled = $unreconciled[$fundamental_type];
|
|
||||||
|
|
||||||
return $unreconciled;
|
return $unreconciled;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -294,37 +287,38 @@ class Account extends AppModel {
|
|||||||
* whatever algorithm is simplest.
|
* 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
|
// if there is no money in the entry, it can reconcile nothing
|
||||||
// don't bother wasting time sifting ledger entries.
|
// don't bother wasting time sifting ledger entries.
|
||||||
if ($amount <= 0)
|
if ($amount > 0) {
|
||||||
return array('unapplied' => 0);
|
$unreconciled = $this->findUnreconciledLedgerEntries($id, $ofund);
|
||||||
|
|
||||||
$fund = $this->fundamentalOpposite($fund);
|
foreach ($unreconciled[$ofund]['entries'] AS $i => &$entry) {
|
||||||
$unreconciled = $this->findUnreconciledLedgerEntries($id, $fund);
|
// 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;
|
$entry['applied'] = $apply;
|
||||||
foreach ($unreconciled['entries'] AS $i => &$entry) {
|
$entry['reconciled'] += $apply;
|
||||||
// Determine if amount is sufficient to cover the entry
|
$entry['balance'] -= $apply;
|
||||||
if ($amount > $entry['balance'])
|
$applied += $apply;
|
||||||
$apply = $entry['balance'];
|
$amount -= $apply;
|
||||||
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[$ofund]['unapplied'] = $amount;
|
||||||
$unreconciled['applied'] = $applied;
|
$unreconciled[$ofund]['applied'] = $applied;
|
||||||
$unreconciled['balance'] -= $applied;
|
$unreconciled[$ofund]['balance'] -= $applied;
|
||||||
return $unreconciled;
|
return $unreconciled;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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'), */
|
/* '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
|
$entries = $this->Account->findLedgerEntriesRelatedToAccount
|
||||||
($customer['Customer']['account_id'],
|
($this->accountId($id),
|
||||||
$this->Account->securityDepositAccountID(),
|
$this->Account->securityDepositAccountID(),
|
||||||
true, null, $link);
|
true, null, $link);
|
||||||
|
|
||||||
foreach ($customer['Lease'] AS $lease) {
|
foreach ($this->leaseIds($id) AS $lease_id) {
|
||||||
$ledger_entries = $this->Lease->findSecurityDeposits($lease['id'], $link);
|
$ledger_entries = $this->Lease->findSecurityDeposits($lease_id, $link);
|
||||||
$this->statsMerge($entries['summary'], $ledger_entries['summary']);
|
$this->statsMerge($entries['summary'], $ledger_entries['summary']);
|
||||||
$entries['Entries'] = array_merge($entries['Entries'],
|
$entries['Entries'] = array_merge($entries['Entries'],
|
||||||
$ledger_entries['Entries']);
|
$ledger_entries['Entries']);
|
||||||
@@ -76,53 +110,21 @@ class Customer extends AppModel {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
function findUnreconciledLedgerEntries($id = null, $fundamental_type = null) {
|
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
|
$unreconciled = $this->Account->findUnreconciledLedgerEntries
|
||||||
($customer['Customer']['account_id'], $fundamental_type);
|
($this->accountId($id), $fundamental_type);
|
||||||
|
|
||||||
foreach ($customer['Lease'] AS $lease) {
|
foreach ($this->leaseIds($id) AS $lease_id) {
|
||||||
$unrec = $this->Lease->findUnreconciledLedgerEntries($lease['id'],
|
$unrec = $this->Lease->findUnreconciledLedgerEntries($lease_id,
|
||||||
$fundamental_type);
|
$fundamental_type);
|
||||||
|
|
||||||
foreach (array('debit', 'credit', 'entries') AS $type) {
|
foreach (array_keys($unreconciled) AS $type) {
|
||||||
if (!isset($unreconciled[$type]))
|
$left = &$unreconciled[$type];
|
||||||
continue;
|
$right = &$unrec[$type];
|
||||||
|
|
||||||
if ($type == 'entries') {
|
|
||||||
$left = &$unreconciled;
|
|
||||||
$right = &$unrec;
|
|
||||||
} else {
|
|
||||||
$left = &$unreconciled[$type];
|
|
||||||
$right = &$unrec[$type];
|
|
||||||
}
|
|
||||||
|
|
||||||
$left['entries'] = array_merge($left['entries'], $right['entries']);
|
$left['entries'] = array_merge($left['entries'], $right['entries']);
|
||||||
$left['balance'] += $right['balance'];
|
$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;
|
return $unreconciled;
|
||||||
@@ -143,35 +145,20 @@ class Customer extends AppModel {
|
|||||||
* whatever algorithm is simplest.
|
* whatever algorithm is simplest.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function reconcileNewLedgerEntry($id, $fund, $amount) {
|
function reconcileNewLedgerEntry($id, $fundamental_type, $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
|
$reconciled = $this->Account->reconcileNewLedgerEntry
|
||||||
($customer['Customer']['account_id'], $fund, $amount);
|
($this->accountId($id), $fundamental_type, $amount);
|
||||||
|
|
||||||
foreach ($customer['Lease'] AS $lease) {
|
foreach ($this->leaseIds($id) AS $lease_id) {
|
||||||
$rec = $this->Lease->reconcileNewLedgerEntry($lease['id'],
|
foreach (array_keys($reconciled) AS $type) {
|
||||||
$fund,
|
$rec = $this->Lease->reconcileNewLedgerEntry($lease_id,
|
||||||
$reconciled['unapplied']);
|
$fundamental_type,
|
||||||
|
$reconciled[$type]['unapplied']);
|
||||||
|
|
||||||
//pr(compact('reconciled', 'rec'));
|
$left = &$reconciled[$type];
|
||||||
foreach (array('debit', 'credit', 'entries') AS $type) {
|
$right = &$rec[$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['entries'] = array_merge($left['entries'], $right['entries']);
|
||||||
$left['balance'] += $right['balance'];
|
$left['balance'] += $right['balance'];
|
||||||
$left['applied'] += $right['applied'];
|
$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;
|
return $reconciled;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -126,9 +126,9 @@ class Lease extends AppModel {
|
|||||||
* whatever algorithm is simplest.
|
* whatever algorithm is simplest.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function reconcileNewLedgerEntry($id, $fund, $amount) {
|
function reconcileNewLedgerEntry($id, $fundamental_type, $amount) {
|
||||||
return $this->Account->reconcileNewLedgerEntry
|
return $this->Account->reconcileNewLedgerEntry
|
||||||
($this->accountId($id), $fund, $amount);
|
($this->accountId($id), $fundamental_type, $amount);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user