all(); } function unavailable() { $this->gridView('Unavailable Units'); } function vacant() { $this->gridView('Vacant Units'); } function occupied() { $this->gridView('Occupied Units'); } function locked() { $this->gridView('Overlocked Units'); } function all() { $this->gridView('All Units', 'all'); } function gridView($title, $action = null, $element = null) { $this->addSideMenuLink('Overlocked', array('controller' => 'units', 'action' => 'locked'), null, 'CONTROLLER'); $this->addSideMenuLink('Occupied', array('controller' => 'units', 'action' => 'occupied'), null, 'CONTROLLER'); $this->addSideMenuLink('Vacant', array('controller' => 'units', 'action' => 'vacant'), null, 'CONTROLLER'); $this->addSideMenuLink('Unavailable', array('controller' => 'units', 'action' => 'unavailable'), null, 'CONTROLLER'); $this->addSideMenuLink('All', array('controller' => 'units', 'action' => 'all'), null, 'CONTROLLER'); parent::gridView($title, $action, $element); } /************************************************************************** ************************************************************************** ************************************************************************** * virtuals: gridData * - With the application controller handling the gridData action, * these virtual functions ensure that the correct data is passed * to jqGrid. */ function gridDataSetup(&$params) { parent::gridDataSetup($params); if (!isset($params['action'])) $params['action'] = 'all'; } function gridDataCountTables(&$params, &$model) { return array ('link' => array('UnitSize' => array('fields' => array('id', 'name')), 'CurrentLease' => array('fields' => array('id')))); /* if ($params['action'] === 'occupied') */ /* $link['Lease'] = array('fields' => array(), */ /* // Models */ /* 'Contact' => array('fields' => array('display_name'), */ /* //'type' => 'LEFT', */ /* ), */ /* ); */ } function gridDataTables(&$params, &$model) { $link = $this->gridDataCountTables($params, $model); $link['link']['CurrentLease']['StatementEntry'] = array('fields' => array()); return $link; } /* function gridDataTables(&$params, &$model) { */ /* return array */ /* ('link' => array('Unit' => array('fields' => array('Unit.id', 'Unit.name')), */ /* 'Customer' => array('fields' => array('Customer.id', 'Customer.name')))); */ /* } */ function gridDataFields(&$params, &$model) { $fields = parent::gridDataFields($params, $model); $fields[] = 'ROUND(UnitSize.width/12 * UnitSize.depth/12, 0) AS sqft'; return array_merge($fields, $this->Unit->Lease->StatementEntry->chargeDisbursementFields(true)); } function gridDataConditions(&$params, &$model) { $conditions = parent::gridDataConditions($params, $model); if ($params['action'] === 'unavailable') { $conditions[] = $this->Unit->conditionUnavailable(); } elseif ($params['action'] === 'vacant') { $conditions[] = $this->Unit->conditionVacant(); } elseif ($params['action'] === 'occupied') { $conditions[] = $this->Unit->conditionOccupied(); } elseif ($params['action'] === 'unoccupied') { $conditions[] = array('NOT' => array($this->Unit->conditionOccupied())); } elseif ($params['action'] === 'locked') { $conditions[] = array('Unit.status' => 'LOCKED'); } return $conditions; } function gridDataOrder(&$params, &$model, $index, $direction) { // Instead of sorting by name, sort by defined order if ($index === 'Unit.name') $index = 'Unit.sort_order'; $order = array(); $order[] = parent::gridDataOrder($params, $model, $index, $direction); // If sorting by anything other than name (defined order) // add the sort-order as a secondary condition if ($index !== 'Unit.name') $order[] = parent::gridDataOrder($params, $model, 'Unit.sort_order', $direction); return $order; } function gridDataPostProcessLinks(&$params, &$model, &$records, $links) { $links['Unit'] = array('name'); //$links['UnitSize'] = array('name'); return parent::gridDataPostProcessLinks($params, $model, $records, $links); } /************************************************************************** ************************************************************************** ************************************************************************** * action: move_in * - Sets up the move-in page for the given unit. */ function move_in($id = null) { $customer = array(); $unit = array(); if (!empty($id)) { $this->Unit->recursive = -1; $unit = current($this->Unit->read(null, $id)); } $title = 'Unit Move-In'; $this->set(compact('customer', 'unit', 'title')); $this->render('/leases/move'); } /************************************************************************** ************************************************************************** ************************************************************************** * 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), )); $this->set('customer', $unit['CurrentLease']['Customer']); $this->set('unit', $unit['Unit']); $this->set('lease', $unit['CurrentLease']); $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', 'redirect')); $this->render('/leases/move'); } /************************************************************************** ************************************************************************** ************************************************************************** * action: view * - Displays information about a specific unit */ function view($id = null) { if (!$id) { $this->Session->setFlash(__('Invalid Item.', true)); $this->redirect(array('action'=>'')); } $unit = $this->Unit->find ('first', array('contain' => array(// Models 'UnitSize', 'Lease' => array('Customer'), 'CurrentLease' => array('Customer') ), 'conditions' => array('Unit.id' => $id), )); // Get the balance on each lease. foreach ($unit['Lease'] AS &$lease) { $stats = $this->Unit->Lease->stats($lease['id']); $lease['balance'] = $stats['balance']; } $outstanding_balance = 0; $outstanding_deposit = 0; if (isset($unit['CurrentLease']['id'])) { // Figure out the outstanding balance of the current lease. $stats = $this->Unit->stats($id); $outstanding_balance = $stats['CurrentLease']['balance']; // Figure out the total security deposit for the current lease. $deposits = $this->Unit->Lease->securityDeposits($unit['CurrentLease']['id']); $outstanding_deposit = $this->Unit->Lease->securityDepositBalance($unit['CurrentLease']['id']); } // Set up dynamic menu items if (isset($unit['CurrentLease']['id']) && !isset($unit['CurrentLease']['moveout_date'])) { $this->addSideMenuLink('Move-Out', array('action' => 'move_out', $id), null, 'ACTION'); } elseif ($this->Unit->available($unit['Unit']['status'])) { $this->addSideMenuLink('Move-In', array('action' => 'move_in', $id), null, 'ACTION'); } else { // Unit is unavailable (dirty, damaged, reserved, business-use, etc) } if (isset($unit['CurrentLease']['id']) && !isset($unit['CurrentLease']['close_date'])) { $this->addSideMenuLink('New Invoice', array('controller' => 'leases', 'action' => 'invoice', $unit['CurrentLease']['id']), null, 'ACTION'); $this->addSideMenuLink('New Receipt', array('controller' => 'customers', 'action' => 'receipt', $unit['CurrentLease']['customer_id']), null, 'ACTION'); } $this->addSideMenuLink('Edit', array('action' => 'edit', $id), null, 'ACTION'); // Prepare to render. $title = 'Unit ' . $unit['Unit']['name']; $this->set(compact('unit', 'title', '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)); } 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')); } }