100, 'group' => 'Unit.id', 'order' => array('Unit.sort_order' => 'ASC')); var $sidemenu_links = array(array('name' => '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 / current / past / all * - Creates a list of tenants */ function index() { $this->all(); } function unavailable() { $this->jqGridView('unavailable', 'Unavailable Units'); } function vacant() { $this->jqGridView('vacant', 'Vacant Units'); } function occupied() { $this->jqGridView('occupied', 'Occupied Units'); } function all() { $this->jqGridView('all', 'All Units'); } /************************************************************************** ************************************************************************** ************************************************************************** * virtuals: jqGridData * - With the application controller handling the jqGridData action, * these virutal functions ensure that the correct data is passed * to jqGrid. */ function jqGridDataSetup(&$params) { parent::jqGridDataSetup($params); if (!isset($params['action'])) $params['action'] = 'all'; } function jqGridDataTables(&$params) { $link = array ('link' => array(// Models 'UnitSize' => array('fields' => array('name')), ), ); if ($params['action'] === 'occupied') $link['Lease'] = array('fields' => array(), // Models 'Contact' => array('fields' => array('display_name'), //'type' => 'LEFT', ), ); return $link; } function jqGridDataConditions(&$params) { $conditions = parent::jqGridDataConditions($params); if ($params['action'] === 'unavailable') { $conditions[] = $this->Unit->conditionUnavailable(); } elseif ($params['action'] === 'vacant') { $conditions[] = $this->Unit->conditionVacant(); } elseif ($params['action'] === 'occupied') { $conditions[] = $this->Unit->conditionOccupied(); } return $conditions; } function zzjqGridDataRecordCount(&$params, $model, $query) { // We don't have a good way to use the query to obtain // our count. The problem is that we're relying on the // group by for the query, which will destroy the count, // whether we omit the group by or leave it in. // So, build a fresh query for counting. $query['conditions'] = parent::jqGridDataConditions($params); $count = $model->find('count', array_merge(array('link' => array_diff_key($query['link'], array('CurrentLease'=>1))), array_diff_key($query, array('link'=>1)))); if ($params['action'] === 'all') return $count; $query['conditions'][] = 'CurrentLease.id IS NULL'; $count_past = $model->find('count', $query); // Since we can't easily count 'current' directly, we // can quickly derive it since 'current' customers // are mutually exclusive to 'past' customers. if ($params['action'] == 'current') $count = $count - $count_past; elseif ($params['action'] == 'past') { $count = $count_past; } return $count; } /************************************************************************** ************************************************************************** ************************************************************************** * 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['Account']['Ledger']['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']['Account']['Ledger']['balance']; // Figure out the total security deposit for the current lease. $deposits = $this->Unit->Lease->findSecurityDeposits($unit['CurrentLease']['id']); $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')); // Prepare to render. $title = 'Unit ' . $unit['Unit']['name']; $this->set(compact('unit', 'title', 'outstanding_balance', 'outstanding_deposit')); } }