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

@@ -214,7 +214,7 @@ class CustomersController extends AppController {
$customer = $this->Customer->details($id);
//pr($customer);
$outstanding_balance = $customer['stats']['balance'];
$outstanding_deposit = $customer['deposits']['summary']['Payment']['reconciled'];
$outstanding_deposit = $this->Customer->securityDepositBalance($id);
// Figure out if this customer has any non-closed leases
$show_moveout = false;

View File

@@ -222,13 +222,15 @@ class LeasesController extends AppController {
* to prevent feature overload on the receipt page.
*/
function apply_deposit($id) {
function apply_deposit($id = null) {
// Create some models for convenience
$A = new Account();
if ($this->data) {
// Handle the move out based on the data given
//pr($this->data);
pr($this->data);
$this->Lease->releaseSecurityDeposits($this->data['Lease']['id']);
die();
// Assume this will succeed
$ret = true;
@@ -308,8 +310,8 @@ class LeasesController extends AppController {
array('stats' => $this->Lease->stats($id)));
// Determine the lease security deposit
$deposit = $this->Lease->securityDeposits($lease['Lease']['id']);
$this->set(compact('deposit'));
$deposit_balance = $this->Lease->securityDeposits($lease['Lease']['id']);
$this->set(compact('deposit_balance'));
$this->set('customer', $lease['Customer']);
$this->set('unit', $lease['Unit']);
$this->set('lease', $lease['Lease']);
@@ -364,8 +366,8 @@ class LeasesController extends AppController {
array('stats' => $this->Lease->stats($id)));
// Determine the lease security deposit
$deposit = $this->Lease->securityDeposits($lease['Lease']['id']);
if ($deposit['summary']['balance'] > 0)
$deposit_balance = $this->Lease->securityDepositBalance($lease['Lease']['id']);
if ($deposit_balance > 0)
die("Still have un-utilized security deposit");
$this->set('customer', $lease['Customer']);
@@ -508,8 +510,7 @@ class LeasesController extends AppController {
$outstanding_balance = $lease['Lease']['stats']['balance'];
// Determine the lease security deposit
$deposits = $this->Lease->securityDeposits($lease['Lease']['id']);
$outstanding_deposit = $deposits['summary']['balance'];
$outstanding_deposit = $this->Lease->securityDepositBalance($lease['Lease']['id']);
// Set up dynamic menu items
if (!isset($lease['Lease']['close_date'])) {

View File

@@ -229,7 +229,7 @@ class UnitsController extends AppController {
// Figure out the total security deposit for the current lease.
$deposits = $this->Unit->Lease->securityDeposits($unit['CurrentLease']['id']);
$outstanding_deposit = $deposits['summary']['Payment']['reconciled'];
$outstanding_deposit = $this->Unit->Lease->securityDepositBalance($unit['CurrentLease']['id']);
}
// Set up dynamic menu items

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!

View File

@@ -11,8 +11,15 @@ echo ('<DIV CLASS="apply-deposit grid-selection-text">' .
'<DIV CLASS="supporting">' .
'<TABLE>' .
'<TR><TD CLASS="field">Balance:</TD><TD CLASS="value">'.$lease['stats']['balance'].'</TD></TR>' .
'<TR><TD CLASS="field">Deposit:</TD><TD CLASS="value">'.$deposit['summary']['balance'].'</TD></TR>' .
/* '<TR><TD CLASS="field">Balance:</TD><TD CLASS="value">' . */
/* FormatHelper::currency($lease['stats']['balance']) . */
/* '</TD></TR>' . */
'<TR><TD CLASS="field">Deposit:</TD><TD CLASS="value">' .
FormatHelper::currency($depositBalance) .
'</TD></TR>' .
'</TABLE>' .
'</DIV>' .
@@ -20,8 +27,8 @@ echo ('<DIV CLASS="apply-deposit grid-selection-text">' .
echo $form->create(null, array('id' => 'apply-deposit-form',
/* 'url' => array('controller' => 'lease', */
/* 'action' => 'apply_deposit') */
'url' => array('controller' => 'leases',
'action' => 'apply_deposit')
)
);
@@ -50,8 +57,7 @@ echo $this->element('form_table',
'between' => '<A HREF="#" ONCLICK="datepickerNow(\'TransactionStamp\'); return false;">Now</A>',
),
"amount" => array('prefix' => 'LedgerEntry',
'value' => min($lease['stats']['balance'],
$deposit['summary']['balance']),
'opts' => array('value' => $depositBalance),
),
"comment" => array('opts' => array('size' => 50),
),