diff --git a/controllers/units_controller.php b/controllers/units_controller.php index 1a9c96a..699d23a 100644 --- a/controllers/units_controller.php +++ b/controllers/units_controller.php @@ -236,6 +236,10 @@ class UnitsController extends AppController { $this->sidemenu_links[] = array('name' => 'Operations', 'header' => true); + $this->sidemenu_links[] = + array('name' => 'Edit', 'url' => array('action' => 'edit', + $id)); + if (isset($unit['CurrentLease']['id']) && !isset($unit['CurrentLease']['moveout_date'])) { $this->sidemenu_links[] = @@ -249,6 +253,10 @@ class UnitsController extends AppController { if (isset($unit['CurrentLease']['id']) && !isset($unit['CurrentLease']['close_date'])) { + $this->sidemenu_links[] = + array('name' => 'Charge', 'url' => array('controller' => 'leases', + 'action' => 'invoice', + $unit['CurrentLease']['id'])); $this->sidemenu_links[] = array('name' => 'Payment', 'url' => array('controller' => 'customers', 'action' => 'receipt', @@ -261,4 +269,71 @@ class UnitsController extends AppController { 'outstanding_balance', 'outstanding_deposit')); } + + + /************************************************************************** + ************************************************************************** + ************************************************************************** + * action: edit + * - Edit unit information + */ + + function edit($id = null) { + if (isset($this->data)) { + // Check to see if the operation was cancelled. + if (isset($this->params['form']['cancel'])) { + if (empty($this->data['Unit']['id'])) + $this->redirect(array('action'=>'index')); + + $this->redirect(array('action'=>'view', $this->data['Unit']['id'])); + } + + // Make sure we have unit data + if (empty($this->data['Unit'])) + $this->redirect(array('action'=>'index')); + + // Make sure we have a rental rate + if (empty($this->data['Unit']['rent'])) + $this->redirect(array('action'=>'view', $this->data['Unit']['id'])); + + // Save the unit and all associated data + $this->Unit->create(); + $this->Unit->id = $this->data['Unit']['id']; + if (!$this->Unit->save($this->data, false)) { + $this->Session->setFlash("UNIT SAVE FAILED", true); + pr("UNIT SAVE FAILED"); + } + + $this->redirect(array('action'=>'view', $this->Unit->id)); + + // For debugging, only if the redirects above have been + // commented out, otherwise this section isn't reached. + $this->render('/fake'); + return; + } + + if ($id) { + $this->data = $this->Unit->findById($id); + $title = 'Unit ' . $this->data['Unit']['name'] . " : Edit"; + } + else { + $title = "Enter New Unit"; + $this->data = array(); + } + + $statusEnums = $this->Unit->allowedStatusSet($id); + $statusEnums = array_combine(array_keys($statusEnums), + array_keys($statusEnums)); + $this->set(compact('statusEnums')); + + $unit_sizes = $this->Unit->UnitSize->find + ('list', array('order' => array('unit_type_id', 'width', 'depth', 'id'))); + $this->set(compact('unit_sizes')); + + // Prepare to render. + pr($this->data); + $this->set(compact('title')); + } + + } diff --git a/models/unit.php b/models/unit.php index a5abd89..9e2396b 100644 --- a/models/unit.php +++ b/models/unit.php @@ -27,6 +27,8 @@ class Unit extends AppModel { 'Lease', ); + //var $default_log_level = array('log' => 30, 'show' => 15); + /************************************************************************** ************************************************************************** ************************************************************************** @@ -50,7 +52,7 @@ class Unit extends AppModel { } function occupiedEnumValue() { - return statusValue('OCCUPIED'); + return $this->statusValue('OCCUPIED'); } function conditionOccupied() { @@ -68,18 +70,71 @@ class Unit extends AppModel { return ('Unit.status <= ' . $this->statusValue('UNAVAILABLE')); } + + /************************************************************************** + ************************************************************************** + ************************************************************************** + * function: allowedStatusSet + * - Returns the status set allowed for the given unit + */ + function allowedStatusSet($id) { + $this->prEnter(compact('id')); + $this->id = $id; + $old_status = $this->field('status'); + $old_val = $this->statusValue($old_status); + $this->pr(17, compact('old_status', 'old_val')); + + $enums = $this->activeStatusEnums(); + $this->pr(21, compact('enums')); + + foreach ($enums AS $enum => $val) { + if (($old_val < $this->occupiedEnumValue()) != + ($val < $this->occupiedEnumValue())) { + unset($enums[$enum]); + } + } + + return $this->prReturn($enums); + } + + /************************************************************************** ************************************************************************** ************************************************************************** * function: updateStatus * - Update the given unit to the given status */ - function updateStatus($id, $status) { + function updateStatus($id, $status, $check = false) { + $this->prEnter(compact('id', 'status', 'check')); + +/* if ($check) { */ +/* $old_status = $this->field('status'); */ +/* $this->pr(17, compact('old_status')); */ +/* if ($this->statusValue($old_status) < $this->occupiedEnumValue() && */ +/* $this->statusValue($status) >= $this->occupiedEnumValue()) */ +/* { */ +/* die("Can't transition a unit from vacant to occupied"); */ +/* return $this->prReturn(false); */ +/* } */ +/* if ($this->statusValue($old_status) >= $this->occupiedEnumValue() && */ +/* $this->statusValue($status) < $this->occupiedEnumValue()) */ +/* { */ +/* die("Can't transition a unit from occupied to vacant"); */ +/* return $this->prReturn(false); */ +/* } */ +/* } */ + + if ($check) { + if (!array_key_exists($status, $this->allowedStatusSet($id))) + return $this->prReturn(false); + } + $this->id = $id; - //pr(compact('id', 'status')); $this->saveField('status', $status); + return $this->prReturn(true); } + /************************************************************************** ************************************************************************** ************************************************************************** diff --git a/views/units/edit.ctp b/views/units/edit.ctp new file mode 100644 index 0000000..22e389b --- /dev/null +++ b/views/units/edit.ctp @@ -0,0 +1,27 @@ +' . "\n"; + +echo $form->create('Unit', array('action' => 'edit')) . "\n"; +echo $form->input('id') . "\n"; + +echo($this->element + ('form_table', + array('class' => 'item unit detail', + 'caption' => isset($this->data['Unit']) ? 'Edit Unit' : 'New Unit', + 'fields' => array + ('name' => true, + 'unit_size_id' => true, + 'status' => array('opts' => + array('options' => $statusEnums, + ), + ), + 'deposit' => true, + 'rent' => true, + 'comment' => true, + ))) . "\n"); + +echo $form->submit('Update') . "\n"; +echo $form->submit('Cancel', array('name' => 'cancel')) . "\n"; +echo $form->end() . "\n"; +echo '' . "\n";