git-svn-id: file:///svn-source/pmgr/branches/v0.3_work@1038 97e9348a-65ac-dc4b-aefc-98561f571b83
532 lines
18 KiB
PHP
532 lines
18 KiB
PHP
<?php
|
|
|
|
class UnitsController extends AppController {
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* override: addGridViewSideMenuLinks
|
|
* - Adds grid view navigation side menu links
|
|
*/
|
|
|
|
function addGridViewSideMenuLinks() {
|
|
parent::addGridViewSideMenuLinks();
|
|
|
|
$this->addSideMenuLink('Unavailable',
|
|
array('controller' => 'units', 'action' => 'unavailable'), null,
|
|
'CONTROLLER');
|
|
$this->addSideMenuLink('Vacant',
|
|
array('controller' => 'units', 'action' => 'vacant'), null,
|
|
'CONTROLLER');
|
|
$this->addSideMenuLink('Occupied',
|
|
array('controller' => 'units', 'action' => 'occupied'), null,
|
|
'CONTROLLER');
|
|
$this->addSideMenuLink('Overlocked',
|
|
array('controller' => 'units', 'action' => 'locked'), null,
|
|
'CONTROLLER');
|
|
$this->addSideMenuLink('Liened',
|
|
array('controller' => 'units', 'action' => 'liened'), null,
|
|
'CONTROLLER');
|
|
$this->addSideMenuLink('All',
|
|
array('controller' => 'units', 'action' => 'all'), null,
|
|
'CONTROLLER');
|
|
}
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* 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 locked() { $this->gridView('Overlocked Units'); }
|
|
function liened() { $this->gridView('Liened 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);
|
|
|
|
$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[] = $this->Unit->conditionLocked();
|
|
}
|
|
elseif ($params['action'] === 'liened') {
|
|
$conditions[] = $this->Unit->conditionLiened();
|
|
}
|
|
|
|
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: lock/unlock/lien
|
|
* - Transitions the unit into / out of the LOCKED state
|
|
*/
|
|
|
|
function status($id, $status) {
|
|
$this->Unit->updateStatus($id, $status, true);
|
|
$this->redirect(array('action' => 'view', $id));
|
|
}
|
|
|
|
function lock($id) {
|
|
if (isset($this->data)) {
|
|
$id = $this->id = $this->data['Unit']['id'];
|
|
|
|
// Check to see if the operation was cancelled.
|
|
if (isset($this->params['form']['cancel'])) {
|
|
if (isset($id))
|
|
$this->redirect(array('action'=>'view', $id));
|
|
|
|
$this->redirect(array('action'=>'index'));
|
|
}
|
|
|
|
// Figure out which locks the user put on
|
|
$locks = array();
|
|
if (isset($this->data['Lock']) && is_array($this->data['Lock'])) {
|
|
foreach ($this->data['Lock'] AS $lock) {
|
|
$locks[] = $lock['id'];
|
|
}
|
|
}
|
|
|
|
// Save the lock and all associated data
|
|
if (!$this->Unit->lockUnit($id, $locks)) {
|
|
$this->Session->setFlash("UNIT LOCK FAILED", true);
|
|
pr("UNIT LOCK FAILED");
|
|
}
|
|
|
|
// If it's no longer locked, change status to OCCUPIED
|
|
// Could still be liened... but that would be odd.
|
|
if (count($locks) == 0)
|
|
$this->status($id, 'OCCUPIED');
|
|
|
|
// If we're not liened, we must now just be locked
|
|
if (!$this->Unit->liened($id))
|
|
$this->status($id, 'LOCKED');
|
|
|
|
// Otherwise, don't change anything.
|
|
$this->redirect(array('action' => 'view', $id));
|
|
}
|
|
|
|
if (!$id)
|
|
$this->INTERNAL_ERROR("$id cannot be NULL");
|
|
|
|
// Get all locks on this unit
|
|
$this->data = $this->Unit->find
|
|
('first',
|
|
array('contain' => array('Lock' => array('id')),
|
|
'fields' => array('id'),
|
|
'conditions' => array('Unit.id' => $id)
|
|
));
|
|
|
|
$locks = $this->Unit->Lock->lockList();
|
|
/* $locksold = $locks; */
|
|
/* foreach ($locksold AS $name) { */
|
|
/* $locks[$name] = $name; */
|
|
/* } */
|
|
$this->set(compact('locks'));
|
|
|
|
// Prepare to render.
|
|
//pr($this->data);
|
|
$this->set(compact('title'));
|
|
// $this->render('lock');
|
|
}
|
|
|
|
|
|
function unlock($id) { $this->lock($id); }
|
|
function lien($id) { $this->status($id, 'LIENED'); }
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* action: overview
|
|
* - Displays overview information for all units
|
|
*/
|
|
function overview() {
|
|
$result = $this->Unit->find
|
|
('all',
|
|
array('link' => array('UnitSize' => array('fields' => array(), 'UnitType' => array('fields' => array('name')))),
|
|
'fields' => array('status', 'COUNT(Unit.id) AS cnt', 'SUM(Unit.rent) AS rents'),
|
|
//'conditions' => array('
|
|
'group' => array('UnitType.id', 'Unit.status'),
|
|
'order' => array('UnitType.name', 'Unit.status')
|
|
));
|
|
|
|
$overview = array('types' => array(), 'count' => 0, 'rents' => 0);
|
|
foreach ($result AS $row) {
|
|
$utname = $row['UnitType']['name'];
|
|
if (empty($overview['types'][$utname]))
|
|
$overview['types'][$utname] = array('name' => $utname,
|
|
'subs' => array(),
|
|
'count' => 0,
|
|
'rents' => 0,
|
|
'phys_pct' => 0,
|
|
'econ_pct' => 0);
|
|
$type = &$overview['types'][$utname];
|
|
$type['subs'][] = array('name' => $row['Unit']['status'],
|
|
'count' => $row[0]['cnt'],
|
|
'rents' => $row[0]['rents'],
|
|
'phys_subpct' => 0,
|
|
'phys_totpct' => 0,
|
|
'econ_subpct' => 0,
|
|
'econ_totpct' => 0);
|
|
|
|
|
|
$type['count'] += $row[0]['cnt'];
|
|
$type['rents'] += $row[0]['rents'];
|
|
|
|
$overview['count'] += $row[0]['cnt'];
|
|
$overview['rents'] += $row[0]['rents'];
|
|
}
|
|
|
|
foreach ($overview['types'] AS &$type) {
|
|
foreach ($type['subs'] AS &$sub) {
|
|
$sub['phys_subpct'] = $sub['count'] / $type['count'];
|
|
$sub['econ_subpct'] = $sub['rents'] / $type['rents'];
|
|
$sub['phys_totpct'] = $sub['count'] / $overview['count'];
|
|
$sub['econ_totpct'] = $sub['rents'] / $overview['rents'];
|
|
}
|
|
$type['phys_pct'] = $type['count'] / $overview['count'];
|
|
$type['econ_pct'] = $type['rents'] / $overview['rents'];
|
|
}
|
|
|
|
// Enable the Reports menu section
|
|
$this->sideMenuAreaActivate('REPORT');
|
|
|
|
// Prepare to render.
|
|
$this->set('title', 'Unit Overview');
|
|
$this->set(compact('overview'));
|
|
}
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* 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']);
|
|
}
|
|
|
|
// If the unit is occupied, but not locked, provide a
|
|
// mechanism to do so. This doesn't have to be restricted
|
|
// to past due customers. There are times we need to
|
|
// overlock customers in good standing, such as if their
|
|
// lock breaks, is cut, or missing for any reason.
|
|
if ($this->Unit->occupied($unit['Unit']['status']) &&
|
|
!$this->Unit->locked($unit['Unit']['status']))
|
|
$this->addSideMenuLink('Lock',
|
|
array('action' => 'lock', $id), null,
|
|
'ACTION');
|
|
|
|
// If the unit is locked, provide an option to unlock it,
|
|
if ($this->Unit->locked($unit['Unit']['status']))
|
|
$this->addSideMenuLink('Relock/Unlock',
|
|
array('action' => 'lock', $id), null,
|
|
'ACTION');
|
|
|
|
// If the unit is locked, but not liened, give option to lien.
|
|
if ($this->Unit->locked($unit['Unit']['status']) &&
|
|
!$this->Unit->liened($unit['Unit']['status']))
|
|
$this->addSideMenuLink('Lien',
|
|
array('action' => 'lien', $id), null,
|
|
'ACTION');
|
|
|
|
// If there is a current lease on this unit, then provide
|
|
// a link to move the tenant out. Current lease for a unit
|
|
// has a bit different definition than a current lease for
|
|
// a customer, since a lease stays with a customer until it
|
|
// is finally closed. A lease, however, only stays with a
|
|
// unit while occupied (since a unit is not responsible for
|
|
// any lingering financial obligations, like a customer is).
|
|
// Of course, if there is no current lease, provide a link
|
|
// to move a new tenant in (if the unit is available).
|
|
if (isset($unit['CurrentLease']['id'])) {
|
|
$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 there is a current lease, allow new charges to
|
|
// be added, and payments to be made.
|
|
if (isset($unit['CurrentLease']['id'])) {
|
|
$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');
|
|
}
|
|
|
|
// Always allow the unit to be edited.
|
|
$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.
|
|
$this->set(compact('title'));
|
|
}
|
|
|
|
|
|
}
|