diff --git a/site/controllers/leases_controller.php b/site/controllers/leases_controller.php index 6ac7295..25917bf 100644 --- a/site/controllers/leases_controller.php +++ b/site/controllers/leases_controller.php @@ -170,9 +170,15 @@ class LeasesController extends AppController { // Handle the move out based on the data given //pr($this->data); - $this->Lease->moveOut($this->data['Lease']['id']); + $this->Lease->moveOut($this->data['Lease']['id'], + 'VACANT', + $this->data['Lease']['moveout_date'], + //true // Close this lease, if able + false + ); + $this->redirect($this->data['redirect']); - //$this->autoRender = false; + $this->autoRender = false; return; } @@ -213,6 +219,21 @@ class LeasesController extends AppController { } + /************************************************************************** + ************************************************************************** + ************************************************************************** + * action: close + * - Closes a lease to any further action + */ + + function close($id) { + // REVISIT : 20090708 + // We should probably seek confirmation first... + $this->Lease->close($id); + $this->redirect(array('action'=>'view', $id)); + } + + /************************************************************************** ************************************************************************** ************************************************************************** @@ -299,11 +320,10 @@ class LeasesController extends AppController { $this->sidemenu_links[] = array('name' => 'Operations', 'header' => true); - if (!isset($lease['Lease']['moveout_date'])) { + if (!isset($lease['Lease']['moveout_date'])) $this->sidemenu_links[] = array('name' => 'Move-Out', 'url' => array('action' => 'move_out', $id)); - } $this->sidemenu_links[] = array('name' => 'Charges', 'url' => array('action' => 'invoice', @@ -313,6 +333,11 @@ class LeasesController extends AppController { array('name' => 'Payments', 'url' => array('controller' => 'customers', 'action' => 'receipt', $lease['Customer']['id'])); + + if ($this->Lease->closeable($id)) + $this->sidemenu_links[] = + array('name' => 'Close', 'url' => array('action' => 'close', + $id)); } // Prepare to render diff --git a/site/models/lease.php b/site/models/lease.php index 8bf7acd..5ff8230 100644 --- a/site/models/lease.php +++ b/site/models/lease.php @@ -228,27 +228,90 @@ class Lease extends AppModel { * - Moves the customer out of the specified lease */ - function moveOut($id = null, $status = 'VACANT', + function moveOut($id, $status = 'VACANT', $stamp = null, $close = false) { - $this->create(false); + // Use NOW if not given a moveout date + if (!isset($stamp)) + $stamp = date('Y-m-d G:i:s'); + + // Reset the data + $this->create(); + $this->id = $id; + + // Set the customer move-out date + $this->data['Lease']['moveout_date'] = $stamp; + + // Save it! + $this->save($this->data, false); + + // Close the lease, if so requested + if ($close) + $this->close($id, $stamp); + + // Finally, update the unit status + $this->recursive = -1; + $this->read(); + $this->Unit->updateStatus($this->data['Lease']['unit_id'], $status); + } + + + /************************************************************************** + ************************************************************************** + ************************************************************************** + * function: close + * - Closes the lease to further action + */ + + function close($id, $stamp = null) { + if (!$this->closeable($id)) + return false; + + // Reset the data + $this->create(); $this->id = $id; // Use NOW if not given a moveout date if (!isset($stamp)) $stamp = date('Y-m-d G:i:s'); - // Move customer out of the lease, and possibly close it - $this->data['Lease']['moveout_date'] = $stamp; - if ($close) - $this->data['Lease']['close_date'] = $stamp; + // Set the close date + $this->data['Lease']['close_date'] = $stamp; // Save it! $this->save($this->data, false); + return true; + } - // Finally, update the unit status + + /************************************************************************** + ************************************************************************** + ************************************************************************** + * function: closeable + * - Indicates whether or not the lease can be closed + */ + + function closeable($id) { $this->recursive = -1; - $this->read(); - $this->Unit->updateStatus($this->data['Lease']['unit_id'], $status); + $this->read(null, $id); + + // We can't close a lease that's still in use + if (!isset($this->data['Lease']['moveout_date'])) + return false; + + // We can't close a lease that's already closed + if (isset($this->data['Lease']['close_date'])) + return false; + + $deposits = $this->findSecurityDeposits($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) + return false; + + // Apparently this lease meets all the criteria! + return true; }