'Units', 'header' => true), array('name' => 'Occupied', 'url' => array('controller' => 'units', 'action' => 'occupied')), array('name' => 'Vacant', 'url' => array('controller' => 'units', 'action' => 'vacant')), array('name' => 'Unavailable', 'url' => array('controller' => 'units', 'action' => 'unavailable')), array('name' => 'All', 'url' => array('controller' => 'units', 'action' => 'all')), ); /************************************************************************** ************************************************************************** ************************************************************************** * override: sideMenuLinks * - Generates controller specific links for the side menu */ function sideMenuLinks() { return array_merge(parent::sideMenuLinks(), $this->sidemenu_links); } /************************************************************************** ************************************************************************** ************************************************************************** * action: index / unavailable / vacant / occupied / all * - Generate a listing of units */ function index() { $this->all(); } function unavailable() { $this->gridView('Unavailable Units'); } function vacant() { $this->gridView('Vacant Units'); } function occupied() { $this->gridView('Occupied Units'); } function all() { $this->gridView('All Units', 'all'); } /************************************************************************** ************************************************************************** ************************************************************************** * 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) { $link = array ('link' => array(// Models 'UnitSize' => array('fields' => array('id', 'name')), ), ); if ($params['action'] === 'occupied') $link['Lease'] = array('fields' => array(), // Models 'Contact' => array('fields' => array('display_name'), //'type' => 'LEFT', ), ); return $link; } function gridDataTables(&$params, &$model) { $link = $this->gridDataCountTables($params, $model); $link['link']['CurrentLease']['LedgerEntry'] = array('fields' => array()); $link['link']['CurrentLease']['LedgerEntry']['Ledger'] = array('fields' => array()); $link['link']['CurrentLease']['LedgerEntry']['Ledger']['Account'] = array('fields' => array()); // INNER JOIN would be great, as it would ensure we're only looking // at the ledger entries that we truly want. However, this also // removes from the query any leases that do not yet have a ledger // entry in A/R. A solution would be to INNER JOIN these tables, // and LEFT JOIN it to the rest. Grouping of JOINs, however, is // implemented with the 'joins' tag, and is not available through // the Linkable behavior interface. //$link['link']['CurrentLease']['LedgerEntry']['Ledger']['Account']['type'] = 'INNER'; $link['link']['CurrentLease']['LedgerEntry']['Ledger']['Account']['conditions'] = array('Account.id' => $this->Unit->CurrentLease->LedgerEntry->Ledger->Account->accountReceivableAccountID()); return $link; } function gridDataFields(&$params, &$model) { $db = &$model->getDataSource(); $fields = $db->fields($model, $model->alias); $fields[] = ("SUM(IF(Account.id IS NULL, 0," . " IF(LedgerEntry.debit_ledger_id = Account.id," . " 1, -1))" . " * LedgerEntry.amount) AS 'balance'"); return $fields; } 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())); } 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 (isset($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->findSecurityDeposits($unit['CurrentLease']['id']); $outstanding_deposit = $deposits['summary']['balance']; } // Set up dynamic menu items $this->sidemenu_links[] = array('name' => 'Operations', 'header' => true); if (isset($unit['CurrentLease']['id']) && !isset($unit['CurrentLease']['moveout_date'])) { $this->sidemenu_links[] = array('name' => 'Move-Out', 'url' => array('action' => 'move_out', $id)); } else { $this->sidemenu_links[] = array('name' => 'Move-In', 'url' => array('action' => 'move_in', $id)); } if (isset($unit['CurrentLease']['id']) && !isset($unit['CurrentLease']['close_date'])) { $this->sidemenu_links[] = array('name' => 'Payment', 'url' => array('controller' => 'customers', 'action' => 'receipt', $unit['CurrentLease']['customer_id'])); } // Prepare to render. $title = 'Unit ' . $unit['Unit']['name']; $this->set(compact('unit', 'title', 'outstanding_balance', 'outstanding_deposit')); } }