diff --git a/controllers/customers_controller.php b/controllers/customers_controller.php index 9d30858..3463f8a 100644 --- a/controllers/customers_controller.php +++ b/controllers/customers_controller.php @@ -140,6 +140,42 @@ class CustomersController extends AppController { } + /************************************************************************** + ************************************************************************** + ************************************************************************** + * action: move_out + * - prepare to move a customer out of one of their units + */ + + function move_out($id) { + + $customer = $this->Customer->find + ('first', array + ('contain' => array + (// Models + 'Lease' => + array('conditions' => array('Lease.moveout_date' => null), + // Models + 'Unit' => + array('order' => array('sort_order'), + 'fields' => array('id', 'name'), + ), + ), + ), + + 'conditions' => array('Customer.id' => $id), + )); + + $redirect = array('controller' => 'customers', + 'action' => 'view', + $id); + + $title = $customer['Customer']['name'] . ': Prepare Move-Out'; + $this->set(compact('title', 'customer', 'redirect')); + $this->render('/leases/move_out'); + } + + /************************************************************************** ************************************************************************** ************************************************************************** @@ -158,12 +194,31 @@ class CustomersController extends AppController { $outstanding_balance = $customer['stats']['balance']; $outstanding_deposit = $customer['deposits']['summary']['balance']; - $this->sidemenu_links[] = - array('name' => 'Operations', 'header' => true); - $this->sidemenu_links[] = - array('name' => 'Payment', 'url' => array('action' => 'payment', $id)); - $this->sidemenu_links[] = - array('name' => 'Move-Out', 'url' => array('controller' => 'units', 'action' => 'move-out')); + // Figure out if this customer has any non-closed leases + $show_moveout = false; + $show_payment = false; + foreach ($customer['Lease'] AS $lease) { + if (!isset($lease['close_date'])) + $show_payment = true; + if (!isset($lease['moveout_date'])) + $show_moveout = true; + } + + // Set up dynamic menu items + if ($show_moveout || $show_payment) { + $this->sidemenu_links[] = + array('name' => 'Operations', 'header' => true); + + if ($show_moveout) { + $this->sidemenu_links[] = + array('name' => 'Move-Out', 'url' => array('action' => 'move_out', + $id)); + } + + $this->sidemenu_links[] = + array('name' => 'Payment', 'url' => array('action' => 'payment', + $id)); + } // Prepare to render. $title = $customer['Customer']['name']; diff --git a/controllers/leases_controller.php b/controllers/leases_controller.php index b0942b3..978b2d5 100644 --- a/controllers/leases_controller.php +++ b/controllers/leases_controller.php @@ -88,6 +88,57 @@ class LeasesController extends AppController { } + /************************************************************************** + ************************************************************************** + ************************************************************************** + * action: move_out + * - prepare or execute a move out on a specific lease + */ + + function move_out($id = null) { + if ($this->data) { + // Handle the move out based on the data given + pr($this->data); + + $this->Lease->moveOut($this->data['Lease']['id']); + $this->redirect($this->data['redirect']); + //$this->autoRender = false; + return; + } + + if (!isset($id)) + die("Oh Nooooo!!"); + + $lease = $this->Lease->find + ('first', array + ('contain' => array + (// Models + 'Unit' => + array('order' => array('sort_order'), + 'fields' => array('id', 'name'), + ), + + 'Customer' => + array('fields' => array('id', 'name'), + ), + ), + + 'conditions' => array(array('Lease.id' => $id), + array('Lease.close_date' => null), + ), + )); + + $redirect = array('controller' => 'leases', + 'action' => 'view', + $id); + + $title = ('Lease #' . $lease['Lease']['number'] . ': ' . + $lease['Unit']['name'] . ': ' . + $lease['Customer']['name'] . ': Prepare Move-Out'); + $this->set(compact('title', 'lease', 'redirect')); + } + + /************************************************************************** ************************************************************************** ************************************************************************** @@ -111,7 +162,6 @@ class LeasesController extends AppController { 'Customer', ), 'conditions' => array(array('Lease.id' => $id)), - 'limit' => 2 ) ); @@ -124,6 +174,22 @@ class LeasesController extends AppController { $deposits = $this->Lease->findSecurityDeposits($lease['Lease']['id']); $outstanding_deposit = $deposits['summary']['balance']; + // Set up dynamic menu items + if (!isset($lease['Lease']['close_date'])) { + $this->sidemenu_links[] = + array('name' => 'Operations', 'header' => true); + + if (!isset($lease['Lease']['moveout_date'])) { + $this->sidemenu_links[] = + array('name' => 'Move-Out', 'url' => array('action' => 'move_out', + $id)); + } + + $this->sidemenu_links[] = + array('name' => 'Payment', 'url' => array('action' => 'payment', + $id)); + } + // Prepare to render $title = 'Lease: #' . $lease['Lease']['id']; $this->set(compact('lease', 'title', diff --git a/controllers/maps_controller.php b/controllers/maps_controller.php index 90ae663..1684ce8 100644 --- a/controllers/maps_controller.php +++ b/controllers/maps_controller.php @@ -53,6 +53,7 @@ class MapsController extends AppController { $this->redirect(array('action'=>'index')); } $this->set('info', $this->mapInfo($id, $requested_width)); + $this->set('title', "Site Map"); } diff --git a/controllers/units_controller.php b/controllers/units_controller.php index ee409bb..d0cafc0 100644 --- a/controllers/units_controller.php +++ b/controllers/units_controller.php @@ -94,6 +94,42 @@ class UnitsController extends AppController { } + /************************************************************************** + ************************************************************************** + ************************************************************************** + * action: move_out + * - prepare or execute a move out on a specific lease + */ + + function move_out($id) { + + $unit = $this->Unit->find + ('first', array + ('contain' => array + (// Models + 'CurrentLease' => + array(//'conditions' => array('Lease.moveout_date' => null), + // Models + 'Customer' => + array('fields' => array('id', 'name'), + ), + ), + ), + 'conditions' => array('Unit.id' => $id), + )); + + $redirect = array('controller' => 'units', + 'action' => 'view', + $id); + + $title = ('Lease' . $unit['CurrentLease']['number'] . ': ' . + $unit['Unit']['name'] . ': ' . + $unit['CurrentLease']['Customer']['name'] . ': Prepare Move-Out'); + $this->set(compact('title', 'unit', 'redirect')); + $this->render('/leases/move_out'); + } + + /************************************************************************** ************************************************************************** ************************************************************************** @@ -137,10 +173,22 @@ class UnitsController extends AppController { $outstanding_deposit = $deposits['summary']['balance']; } - $this->sidemenu_links[] = - array('name' => 'Operations', 'header' => true); - $this->sidemenu_links[] = - array('name' => 'Move-Out', 'url' => array('controller' => 'units', 'action' => 'move-out')); + // Set up dynamic menu items + if (isset($unit['CurrentLease']['id']) && + !isset($unit['CurrentLease']['close_date'])) { + $this->sidemenu_links[] = + array('name' => 'Operations', 'header' => true); + + if (!isset($unit['CurrentLease']['moveout_date'])) { + $this->sidemenu_links[] = + array('name' => 'Move-Out', 'url' => array('action' => 'move_out', + $id)); + } + + $this->sidemenu_links[] = + array('name' => 'Payment', 'url' => array('action' => 'payment', + $id)); + } // Prepare to render. $title = 'Unit ' . $unit['Unit']['name']; diff --git a/models/lease.php b/models/lease.php index 66d2a47..5c152b9 100644 --- a/models/lease.php +++ b/models/lease.php @@ -31,9 +31,6 @@ class Lease extends AppModel { var $hasMany = array( 'LedgerEntry', - - // Cheat to get Account set as part of this class - 'Account', ); @@ -44,7 +41,8 @@ class Lease extends AppModel { * - Returns the accountId of the given lease */ function accountId($id) { - return $this->Account->invoiceAccountID(); + $A = new Account(); + return $A->invoiceAccountID(); } @@ -64,8 +62,9 @@ class Lease extends AppModel { $cond = array(); $cond[] = array('LedgerEntry.lease_id' => $id); - $entries = $this->Account->findLedgerEntries($this->accountId($id), - $all, $cond, $link); + $A = new Account(); + $entries = $A->findLedgerEntries($this->accountId($id), + $all, $cond, $link); /* pr(array('function' => 'Lease::findAccountEntries', */ /* 'args' => compact('id', 'all', 'cond', 'link'), */ @@ -87,9 +86,10 @@ class Lease extends AppModel { /* 'args' => compact('id', 'link'), */ /* )); */ - $entries = $this->Account->findLedgerEntriesRelatedToAccount + $A = new Account(); + $entries = $A->findLedgerEntriesRelatedToAccount ($this->accountId($id), - $this->Account->securityDepositAccountID(), + $A->securityDepositAccountID(), true, array('LedgerEntry.lease_id' => $id), $link); /* pr(array('function' => 'Lease::findSecurityDeposits', */ @@ -110,7 +110,8 @@ class Lease extends AppModel { */ function findUnreconciledLedgerEntries($id = null, $fundamental_type = null) { - return $this->Account->findUnreconciledLedgerEntries + $A = new Account(); + return $A->findUnreconciledLedgerEntries ($this->accountId($id), $fundamental_type, array('LedgerEntry.lease_id' => $id)); } @@ -130,11 +131,42 @@ class Lease extends AppModel { */ function reconcileNewLedgerEntry($id, $fundamental_type, $amount) { - return $this->Account->reconcileNewLedgerEntry + $A = new Account(); + return $A->reconcileNewLedgerEntry ($this->accountId($id), $fundamental_type, $amount, array('LedgerEntry.lease_id' => $id)); } + /************************************************************************** + ************************************************************************** + ************************************************************************** + * function: moveOut + * - Moves the customer out of the specified lease + */ + + function moveOut($id = null, $status = 'VACANT', + $stamp = null, $close = false) { + $this->create(false); + $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; + + // Save it! + $this->save($this->data, false); + + // Finally, update the unit status + $this->recursive = -1; + $this->read(); + $this->Unit->updateStatus($this->data['Lease']['unit_id'], $status); + } + /************************************************************************** ************************************************************************** ************************************************************************** @@ -146,8 +178,9 @@ class Lease extends AppModel { if (!$id) return null; - $stats = $this->Account->stats($this->Account->accountReceivableAccountID(), true, - array('LedgerEntry.lease_id' => $id)); + $A = new Account(); + $stats = $A->stats($A->accountReceivableAccountID(), true, + array('LedgerEntry.lease_id' => $id)); // Pull to the top level and return $stats = $stats['Ledger']; diff --git a/models/unit.php b/models/unit.php index 69b92e7..cc4646f 100644 --- a/models/unit.php +++ b/models/unit.php @@ -19,7 +19,7 @@ class Unit extends AppModel { var $hasOne = array( 'CurrentLease' => array( 'className' => 'Lease', - 'conditions' => 'CurrentLease.close_date IS NULL', + 'conditions' => 'CurrentLease.moveout_date IS NULL', ), ); @@ -27,36 +27,54 @@ class Unit extends AppModel { 'Lease', ); - function statusEnums() { - static $status_enums; - if (!isset($status_enums)) - $status_enums = $this->getEnumValues('status'); - return $status_enums; - } + /************************************************************************** + ************************************************************************** + ************************************************************************** + * helpers: status enumerations + */ - function statusValue($enum) { - $enums = $this->statusEnums(); - return $enums[$enum]; - } + function statusEnums() { + static $status_enums; + if (!isset($status_enums)) + $status_enums = $this->getEnumValues('status'); + return $status_enums; + } - function occupiedEnumValue() { - return statusValue('OCCUPIED'); - } + function statusValue($enum) { + $enums = $this->statusEnums(); + return $enums[$enum]; + } - function conditionOccupied() { - return ('Unit.status >= ' . $this->statusValue('OCCUPIED')); - } + function occupiedEnumValue() { + return statusValue('OCCUPIED'); + } - function conditionVacant() { - return ('Unit.status BETWEEN ' . - ($this->statusValue('UNAVAILABLE')+1) . - ' AND ' . - ($this->statusValue('OCCUPIED')-1)); - } + function conditionOccupied() { + return ('Unit.status >= ' . $this->statusValue('OCCUPIED')); + } - function conditionUnavailable() { - return ('Unit.status <= ' . $this->statusValue('UNAVAILABLE')); - } + function conditionVacant() { + return ('Unit.status BETWEEN ' . + ($this->statusValue('UNAVAILABLE')+1) . + ' AND ' . + ($this->statusValue('OCCUPIED')-1)); + } + + function conditionUnavailable() { + return ('Unit.status <= ' . $this->statusValue('UNAVAILABLE')); + } + + /************************************************************************** + ************************************************************************** + ************************************************************************** + * function: updateStatus + * - Update the given unit to the given status + */ + function updateStatus($id, $status) { + $this->id = $id; + pr(compact('id', 'status')); + $this->saveField('status', $status); + } /************************************************************************** ************************************************************************** diff --git a/views/leases/move_out.ctp b/views/leases/move_out.ctp new file mode 100644 index 0000000..ef2b5fe --- /dev/null +++ b/views/leases/move_out.ctp @@ -0,0 +1,72 @@ +1)); + $data['Lease'] = array_diff_key($customer['Lease'][0], array('Unit'=>1)); + $data['Unit'] = $customer['Lease'][0]['Unit']; +} +elseif (isset($customer)) { + $class .= ' customer'; + $data = $customer; + $multiple = true; +} +elseif (isset($unit)) { + $class .= ' unit'; + $data = array_diff_key($unit, array('CurrentLease'=>1)); + $data['Lease'] = array_diff_key($unit['CurrentLease'], array('Customer'=>1)); + $data['Customer'] = $unit['CurrentLease']['Customer']; +} +else { + die("INTERNAL ERROR"); +} + +//pr(compact('customer', 'lease', 'unit', 'class', 'multiple', 'data', 'redirect')); + +echo '
' . "\n"; +echo('

Move Out: '. $data['Customer']['name'] . + (!$multiple ? ': Unit ' . $data['Unit']['name'] : '') . + '

' . "\n"); +echo('

Be sure that you really want to move this customer out, ' . + ($multiple ? 'select the correct unit to move out of, ' : '') . + 'and press the "Perform Move Out" button.' . "\n"); +echo '


' . "\n"; + +echo $form->create(null, array('id' => 'move-out-form', + 'url' => array('controller' => 'leases', + 'action' => 'move_out'))); + +if ($multiple) { + $options = array(); + foreach ($data['Lease'] AS $lease) + $options[$lease['id']] = $lease['Unit']['name']; + echo $form->input('Lease.id', array('label' => 'Move Out of Unit: ', + 'options' => $options)); +} +else { + echo $form->input('Lease.id', + array('type' => 'hidden', + 'value' => $data['Lease']['id'], + )); +} + +// Set up a redirect page. I use lower case 'redirect' here +// to avoid the model convention, which starts with upper-case. +foreach ($redirect AS $name => $value) { + echo $form->input("redirect.$name", + array('type' => 'hidden', + 'value' => $value, + )); +} + +echo $form->end('Perform Move Out'); + +// End page div +echo '

' . "\n"; diff --git a/views/units/view.ctp b/views/units/view.ctp index 2ee8d08..4fed8be 100644 --- a/views/units/view.ctp +++ b/views/units/view.ctp @@ -59,14 +59,16 @@ echo $this->element('leases', /********************************************************************** * Current Tenant Lease Account History */ - -echo $this->element('ledger_entries', - array('caption' => ('Current Lease Account (' . - $unit['CurrentLease']['Customer']['name'] - . ')'), - 'ar_account' => true, - 'lease_id' => $unit['CurrentLease']['id'], - )); +if (isset($unit['CurrentLease']['id'])) { + echo $this->element('ledger_entries', + array('caption' => + ('Current Lease Account (' + . $unit['CurrentLease']['Customer']['name'] + . ')'), + 'ar_account' => true, + 'lease_id' => $unit['CurrentLease']['id'], + )); +} /* End "detail supporting" div */