More work on handling security deposit utilizations. This is headed in the right direction, but unfortunately, there is a problem if the security deposits have not been paid. I could just ignore it, because it's a low priority problem, but I think the solution might be to just provide a waiver on the unpaid charges. Since I need to implement fee waivers anyway, I'll just move onto that next, and then finish out the security deposit work.

git-svn-id: file:///svn-source/pmgr/branches/yafr_20090716@463 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
abijah
2009-07-31 21:11:00 +00:00
parent 44e4477d38
commit 15a4528e75
6 changed files with 149 additions and 30 deletions

View File

@@ -70,6 +70,7 @@ class Customer extends AppModel {
return $ids;
}
/**************************************************************************
**************************************************************************
**************************************************************************
@@ -80,21 +81,46 @@ class Customer extends AppModel {
$this->prEnter(compact('id', 'query'));
$this->queryInit($query);
$A = new Account();
if (!isset($query['link']['Customer']))
$query['link']['Customer'] = array();
if (!isset($query['link']['Customer']['fields']))
$query['link']['Customer']['fields'] = array();
$query['conditions'][] = array('Customer.id' => $id);
$query['conditions'][] = array('StatementEntry.account_id' => $A->securityDepositAccountID());
$query['conditions'][] = array('StatementEntry.account_id' =>
$this->StatementEntry->Account->securityDepositAccountID());
$set = $this->StatementEntry->reconciledSet('CHARGE', $query);
$set = $this->StatementEntry->reconciledSet('CHARGE', $query, false, true);
return $this->prReturn($set);
}
/**************************************************************************
**************************************************************************
**************************************************************************
* function: securityDepositBalance
* - Returns the balance of the customer security deposit(s)
*/
function securityDepositBalance($id, $query = null) {
$this->prEnter(compact('id', 'query'));
$this->queryInit($query);
if (!isset($query['link']['Lease']))
$query['link']['Lease'] = array();
if (!isset($query['link']['Lease']['fields']))
$query['link']['Lease']['fields'] = array();
$query['conditions'][] = array('Lease.id' => $id);
$query['conditions'][] = array('StatementEntry.account_id' =>
$this->StatementEntry->Account->securityDepositAccountID());
$stats = $this->StatementEntry->stats(null, $query);
$balance = $stats['Charge']['reconciled'] - $stats['Payment']['reconciled'];
return $this->prReturn($balance);
}
/**************************************************************************
**************************************************************************
**************************************************************************

View File

@@ -20,30 +20,116 @@ class Lease extends AppModel {
* - Returns an array of security deposit entries
*/
function securityDeposits($id, $query = null) {
$this->prFunctionLevel(30);
$this->prEnter(compact('id', 'query'));
$this->queryInit($query);
$A = new Account();
if (!isset($query['link']['Lease']))
$query['link']['Lease'] = array();
if (!isset($query['link']['Lease']['fields']))
$query['link']['Lease']['fields'] = array();
$query['conditions'][] = array('Lease.id' => $id);
$query['conditions'][] = array('StatementEntry.account_id' => $A->securityDepositAccountID());
$query['conditions'][] = array('StatementEntry.account_id' =>
$this->StatementEntry->Account->securityDepositAccountID());
$set = $this->StatementEntry->reconciledSet('CHARGE', $query);
$set = $this->StatementEntry->reconciledSet('CHARGE', $query, false, true);
$set['summary'] = array('total' => $set['summary']['Charge']['total'],
'balance' => $set['summary']['Charge']['reconciled'],
);
/* $set['summary'] = array('total' => $set['summary']['Charge']['total'], */
/* 'balance' => $set['summary']['Charge']['reconciled'], */
/* ); */
return $this->prReturn($set);
}
/**************************************************************************
**************************************************************************
**************************************************************************
* function: securityDepositBalance
* - Returns the balance of the lease security deposit(s)
*/
function securityDepositBalance($id, $query = null) {
$this->prEnter(compact('id', 'query'));
$this->queryInit($query);
if (!isset($query['link']['Lease']))
$query['link']['Lease'] = array();
if (!isset($query['link']['Lease']['fields']))
$query['link']['Lease']['fields'] = array();
$query['conditions'][] = array('Lease.id' => $id);
$query['conditions'][] = array('StatementEntry.account_id' =>
$this->StatementEntry->Account->securityDepositAccountID());
$stats = $this->StatementEntry->stats(null, $query);
$balance = $stats['Charge']['reconciled'] - $stats['Payment']['reconciled'];
return $this->prReturn($balance);
}
/**************************************************************************
**************************************************************************
**************************************************************************
* function: releaseSecurityDeposits
* - Releases all security deposits associated with this lease.
* That simply makes a payment out of them, which can be used
* to pay outstanding customer charges, or simply to become
* a customer surplus (customer credit).
*/
function releaseSecurityDeposits($id, $query = null) {
$this->prFunctionLevel(30);
$this->prEnter(compact('id', 'query'));
$secdeps = $this->securityDeposits($id, $query);
$secdeps = $secdeps['entries'];
$this->pr(20, compact('secdeps'));
$this->securityDepositBalance($id, $query);
die();
// If there are no paid security deposits, then
// we can consider all security deposits released.
if (count($secdeps) == 0)
return $this->prReturn(true);
// Build a transaction
$release = array('Transaction' => array(), 'Entry' => array());
$release['Transaction']['comment'] = "Security Deposit Release";
foreach ($secdeps AS $charge) {
if ($charge['StatementEntry']['type'] !== 'CHARGE')
die("INTERNAL ERROR: SECURITY DEPOSIT IS NOT CHARGE");
// Since security deposits are being released, this also means
// we're reducing any oustanding amount on a security deposit
// since we no longer expect it to be owed.
// REVISIT <AP>: 20090730
// This is kludgy, and I really don't like it. However, this
// is not presently something that even happens at the moment,
// so this solution will have to work until we come up with
// something more robust, like flagging those charges as defunct.
if ($charge['StatementEntry']['balance'] > 0) {
$this->StatementEntry->id = $charge['StatementEntry']['id'];
$this->StatementEntry->saveField('amount', $charge['StatementEntry']['reconciled']);
}
$release['Entry'][] =
array('amount' => $charge['StatementEntry']['reconciled'],
'account_id' => $this->StatementEntry->Account->securityDepositAccountID(),
'comment' => "Released Security Deposit",
);
}
$customer_id = $secdeps[0]['StatementEntry']['customer_id'];
$lease_id = $secdeps[0]['StatementEntry']['lease_id'];
$result = $this->StatementEntry->Transaction->addReceipt
($release, $customer_id, $lease_id);
return $this->prReturn($result);
}
/**************************************************************************
**************************************************************************
**************************************************************************
@@ -362,12 +448,12 @@ class Lease extends AppModel {
if (isset($this->data['Lease']['close_date']))
return $this->prReturn(false);
$deposits = $this->securityDeposits($id);
$deposit_balance = $this->securityDepositBalance($id);
$stats = $this->stats($id);
// A lease can only be closed if there are no outstanding
// security deposits, and if the account balance is zero.
if ($deposits['summary']['balance'] != 0 || $stats['balance'] != 0)
if ($deposit_balance != 0)
return $this->prReturn(false);
// Apparently this lease meets all the criteria!