'Customers', 'header' => true), array('name' => 'Current', 'url' => array('controller' => 'customers', 'action' => 'current')), array('name' => 'Past', 'url' => array('controller' => 'customers', 'action' => 'past')), array('name' => 'All', 'url' => array('controller' => 'customers', 'action' => 'all')), array('name' => 'Add Customer', 'url' => array('controller' => 'customers', 'action' => 'add')), ); //var $components = array('RequestHandler'); /************************************************************************** ************************************************************************** ************************************************************************** * 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 customers */ function index() { $this->current(); } function current() { $this->jqGridView('Current Tenants', 'current'); } function past() { $this->jqGridView('Past Tenants'); } function all() { $this->jqGridView('All Customers'); } /************************************************************************** ************************************************************************** ************************************************************************** * virtuals: jqGridData * - With the application controller handling the jqGridData action, * these virtual functions ensure that the correct data is passed * to jqGrid. */ function jqGridDataSetup(&$params) { parent::jqGridDataSetup($params); if (!isset($params['action'])) $params['action'] = 'all'; } function jqGridDataCountTables(&$params, &$model) { return array ('link' => array(// Models 'PrimaryContact', 'CurrentLease' => array('fields' => array()), ), ); } function jqGridDataTables(&$params, &$model) { $link = $this->jqGridDataCountTables($params, $model); $link['link']['DoubleEntry'] = array('fields' => array()); $link['link']['DoubleEntry']['Ledger'] = array('fields' => array()); $link['link']['DoubleEntry']['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 units 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']['DoubleEntry']['Ledger']['Account']['type'] = 'INNER'; $link['link']['DoubleEntry']['Ledger']['Account']['conditions'] = array('Account.id' => $this->Customer->DoubleEntry->Ledger->Account->accountReceivableAccountID()); return $link; } function jqGridDataFields(&$params, &$model) { $db = &$model->getDataSource(); $fields = $db->fields($model, $model->alias); $fields[] = ('COUNT(DISTINCT CurrentLease.id) AS lease_count'); $fields[] = ("SUM(IF(Account.id IS NULL, 0," . " IF(DoubleEntry.debit_ledger_id = Account.id," . " 1, -1))" . " * IF(DoubleEntry.amount IS NULL, 0, DoubleEntry.amount))" . " AS 'balance'"); return $fields; } function jqGridDataConditions(&$params, &$model) { $conditions = parent::jqGridDataConditions($params, $model); if ($params['action'] === 'current') { $conditions[] = 'CurrentLease.id IS NOT NULL'; } elseif ($params['action'] === 'past') { $conditions[] = 'CurrentLease.id IS NULL'; } return $conditions; } function jqGridDataOrder(&$params, &$model, $index, $direction) { $order = array(); $order[] = parent::jqGridDataOrder($params, $model, $index, $direction); if ($index !== 'PrimaryContact.last_name') $order[] = parent::jqGridDataOrder($params, $model, 'PrimaryContact.last_name', $direction); if ($index !== 'PrimaryContact.first_name') $order[] = parent::jqGridDataOrder($params, $model, 'PrimaryContact.first_name', $direction); if ($index !== 'Customer.id') $order[] = parent::jqGridDataOrder($params, $model, 'Customer.id', $direction); return $order; } function jqGridDataRecordCount(&$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, $model); $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; } function jqGridDataRecords(&$params, &$model, $query) { $customers = parent::jqGridDataRecords($params, $model, $query); // Get the balance on each customer. foreach ($customers AS &$customer) { $stats = $this->Customer->stats($customer['Customer']['id']); $customer['Customer']['balance'] = $stats['balance']; } return $customers; } function jqGridRecordLinks(&$params, &$model, &$records, $links) { $links['Customer'] = array('name'); return parent::jqGridRecordLinks($params, $model, $records, $links); } /************************************************************************** ************************************************************************** ************************************************************************** * action: move_in * - Sets up the move-in page for the given customer. */ function move_in($id = null) { $customer = array(); $unit = array(); if (isset($id)) { $this->Customer->recursive = -1; $customer = current($this->Customer->read(null, $id)); } $this->set(compact('customer', 'unit')); $title = 'Customer Move-In'; $this->set(compact('title')); $this->render('/leases/move'); } /************************************************************************** ************************************************************************** ************************************************************************** * 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), )); $this->set('customer', $lease['Customer']); $this->set('unit', array()); $redirect = array('controller' => 'customers', 'action' => 'view', $id); $title = $customer['Customer']['name'] . ': Prepare Move-Out'; $this->set(compact('title', 'customer', 'redirect')); $this->render('/leases/move'); } /************************************************************************** ************************************************************************** ************************************************************************** * action: view * - Displays information about a specific customer */ function view($id = null) { if (!$id) { $this->Session->setFlash(__('Invalid Item.', true)); $this->redirect(array('action'=>'index')); } /* //$result = $this->Customer->securityDeposits($id); */ /* $result = $this->Customer->excessPayments($id); */ /* //$result = $this->Customer->unreconciledCharges($id); */ /* echo('