diff --git a/controllers/customers_controller.php b/controllers/customers_controller.php index 8de75cf..d4e710e 100644 --- a/controllers/customers_controller.php +++ b/controllers/customers_controller.php @@ -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; diff --git a/controllers/leases_controller.php b/controllers/leases_controller.php index 064e62d..e1784b9 100644 --- a/controllers/leases_controller.php +++ b/controllers/leases_controller.php @@ -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'])) { diff --git a/controllers/units_controller.php b/controllers/units_controller.php index ef7af90..6ee1a15 100644 --- a/controllers/units_controller.php +++ b/controllers/units_controller.php @@ -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 diff --git a/models/customer.php b/models/customer.php index 1f1c9f3..18393e1 100644 --- a/models/customer.php +++ b/models/customer.php @@ -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); + } + + /************************************************************************** ************************************************************************** ************************************************************************** diff --git a/models/lease.php b/models/lease.php index 6cf643a..0e4c924 100644 --- a/models/lease.php +++ b/models/lease.php @@ -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 : 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! diff --git a/views/leases/apply_deposit.ctp b/views/leases/apply_deposit.ctp index a1e77f7..e5d1496 100644 --- a/views/leases/apply_deposit.ctp +++ b/views/leases/apply_deposit.ctp @@ -11,8 +11,15 @@ echo ('
' . '
' . '' . - '' . - '' . + +/* '' . */ + + '' . + '
Balance:'.$lease['stats']['balance'].'
Deposit:'.$deposit['summary']['balance'].'
Balance:' . */ +/* FormatHelper::currency($lease['stats']['balance']) . */ +/* '
Deposit:' . + FormatHelper::currency($depositBalance) . + '
' . '
' . @@ -20,8 +27,8 @@ echo ('
' . 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' => 'Now', ), "amount" => array('prefix' => 'LedgerEntry', - 'value' => min($lease['stats']['balance'], - $deposit['summary']['balance']), + 'opts' => array('value' => $depositBalance), ), "comment" => array('opts' => array('size' => 50), ),