Files
pmgr/controllers/units_controller.php

343 lines
12 KiB
PHP

<?php
class UnitsController extends AppController {
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 / 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) {
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);
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()));
}
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),
));
$unit['Unit'] = $unit[0] + $unit['Unit'];
// 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
$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[] =
array('name' => 'Move-Out', 'url' => array('action' => 'move_out',
$id));
} elseif ($this->Unit->available($unit['Unit']['status'])) {
$this->sidemenu_links[] =
array('name' => 'Move-In', 'url' => array('action' => 'move_in',
$id));
} else {
// Unit is unavailable (dirty, damaged, reserved, business-use, etc)
}
if (isset($unit['CurrentLease']['id']) &&
!isset($unit['CurrentLease']['close_date'])) {
$this->sidemenu_links[] =
array('name' => 'New Invoice', 'url' => array('controller' => 'leases',
'action' => 'invoice',
$unit['CurrentLease']['id']));
$this->sidemenu_links[] =
array('name' => 'New Receipt', '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'));
}
/**************************************************************************
**************************************************************************
**************************************************************************
* 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'));
}
}