From e6b03135235e2e6095b0fc85ceb4f87c11f7fbed Mon Sep 17 00:00:00 2001 From: abijah Date: Sat, 8 Aug 2009 04:27:02 +0000 Subject: [PATCH] Add refund capability to the customer, and in fact only the customer, as we've revoked refund capability from the lease. This is to help work through various issues surrounding use of security deposits and general refund functionality. For example, a customer who has overpayed (customer surplus, with zero balance on lease), and then moves out. Where that security deposit surplus goes has been a bit of a thorn. Hopefully, this resolves the issue, although there may still be some bugs to flush out. git-svn-id: file:///svn-source/pmgr/branches/yafr_20090716@504 97e9348a-65ac-dc4b-aefc-98561f571b83 --- site/controllers/customers_controller.php | 33 ++++++++++++----- site/controllers/leases_controller.php | 16 +++++++-- site/models/lease.php | 36 ++++++++++--------- .../views/{leases => transactions}/refund.ctp | 0 4 files changed, 58 insertions(+), 27 deletions(-) rename site/views/{leases => transactions}/refund.ctp (100%) diff --git a/site/controllers/customers_controller.php b/site/controllers/customers_controller.php index cd6c997..ad97b2b 100644 --- a/site/controllers/customers_controller.php +++ b/site/controllers/customers_controller.php @@ -395,18 +395,35 @@ class CustomersController extends AppController { * - Refunds customer charges */ - function refund() { - $entries = $this->Customer->StatementEntry->find - ('all', array + function refund($id) { + $customer = $this->Customer->find + ('first', array ('contain' => false, - 'conditions' => array('StatementEntry.id' => - //array(199,200,201) - 61 + 'conditions' => array(array('Customer.id' => $id), ), )); - pr(compact('entries')); + if (empty($customer)) { + $this->redirect(array('action'=>'view', $id)); + } - $this->Customer->refund($entries); + // Determine the customer balance, bailing if the customer owes money + $balance = $this->Customer->balance($id); + if ($balance >= 0) { + $this->redirect(array('action'=>'view', $id)); + } + + // The refund will be for a positive amount + $balance *= -1; + + // Get the accounts capable of paying the refund + $refundAccounts = $this->Customer->StatementEntry->Account->refundAccounts(); + $defaultAccount = current($refundAccounts); + $this->set(compact('refundAccounts', 'defaultAccount')); + + // Prepare to render + $title = ($customer['Customer']['name'] . ': Refund'); + $this->set(compact('title', 'customer', 'balance')); + $this->render('/transactions/refund'); } diff --git a/site/controllers/leases_controller.php b/site/controllers/leases_controller.php index 4e043c8..9f709de 100644 --- a/site/controllers/leases_controller.php +++ b/site/controllers/leases_controller.php @@ -278,6 +278,7 @@ class LeasesController extends AppController { $lease['Unit']['name'] . ': ' . $lease['Customer']['name'] . ': Refund'); $this->set(compact('title', 'lease', 'balance')); + $this->render('/transactions/refund'); } @@ -459,10 +460,19 @@ class LeasesController extends AppController { /* array('name' => 'Transfer Credit to Customer', */ /* 'url' => array('action' => 'promote_surplus', $id)); */ + // REVISIT : + // Not allowing refund to be issued from the lease, as + // in fact, we should never have a positive lease balance. + // I'll flag this at the moment, since we might get one + // when a charge is reimbursed; a bug that we'll either + // need to fix, or we'll have to revisit this assumption. if ($outstanding_balance < 0) - $this->sidemenu_links[] = - array('name' => 'Issue Refund', - 'url' => array('action' => 'refund', $id)); + INTERNAL_ERROR("Should not have a customer lease credit."); + +/* if ($outstanding_balance < 0) */ +/* $this->sidemenu_links[] = */ +/* array('name' => 'Issue Refund', */ +/* 'url' => array('action' => 'refund', $id)); */ if (isset($lease['Lease']['moveout_date']) && $outstanding_balance > 0) $this->sidemenu_links[] = diff --git a/site/models/lease.php b/site/models/lease.php index 3785fbf..a277e38 100644 --- a/site/models/lease.php +++ b/site/models/lease.php @@ -44,26 +44,26 @@ class Lease extends AppModel { $this->prEnter(compact('id', 'query')); $this->queryInit($query); + // REVISIT : 20090807 + // Let's try simplifying the security deposit issue. + // Presume that security deposits are NOT used at all, + // until the customer moves out of the unit. At that + // time, the ENTIRE deposit is converted to customer + // credit. Piece of cake. + // For more information, see file revision history, + // including the revision just before this, r503. + + $this->id = $id; + $moveout_date = $this->field('moveout_date'); + if (!empty($moveout_date)) + return $this->prReturn(0); + $query['conditions'][] = array('StatementEntry.lease_id' => $id); $query['conditions'][] = array('StatementEntry.account_id' => $this->StatementEntry->Account->securityDepositAccountID()); - // REVISIT : 20090804 - // Dilemma... how to handle security deposits used to pay - // charges on the lease, yet are not part of the lease - // security deposit(s)? For example, Lease A & Lease B, - // each with $25 Sec. Dep. Move out of Lease A, and - // promote the lease surplus to the customer. A new - // charge on Lease B for $15, which is assigned $15/$25 - // from the promoted Lease A surplus. They way this - // function works at present, it will presume the $15 is - // part of the security deposit balance, and will end up - // calculating it as only $10, which is wrong. Perhaps - // the fix is to release security deposits into some sort - // of income account. - $stats = $this->StatementEntry->stats(null, $query); - return $this->prReturn($stats['account_balance']); + return $this->prReturn($stats['Charge']['disbursement']); } @@ -113,8 +113,12 @@ class Lease extends AppModel { $customer_id = $secdeps[0]['StatementEntry']['customer_id']; $lease_id = $secdeps[0]['StatementEntry']['lease_id']; + // Add receipt of the security deposit funds. Do NOT + // flag them as part of the lease, as all received funds + // are only associated with the customer, for future + // (or present) disbursement on any lease. $result = $this->StatementEntry->Transaction->addReceipt - ($release, $customer_id, $lease_id); + ($release, $customer_id, null); return $this->prReturn($result); } diff --git a/site/views/leases/refund.ctp b/site/views/transactions/refund.ctp similarity index 100% rename from site/views/leases/refund.ctp rename to site/views/transactions/refund.ctp