git-svn-id: file:///svn-source/pmgr/branches/v0.3_work@1038 97e9348a-65ac-dc4b-aefc-98561f571b83
288 lines
8.3 KiB
PHP
288 lines
8.3 KiB
PHP
<?php
|
|
class Unit extends AppModel {
|
|
|
|
var $name = 'Unit';
|
|
var $validate = array(
|
|
'id' => array('numeric'),
|
|
'unit_size_id' => array('numeric'),
|
|
'name' => array('notempty'),
|
|
'sort_order' => array('numeric'),
|
|
'walk_order' => array('numeric'),
|
|
'deposit' => array('money'),
|
|
'amount' => array('money')
|
|
);
|
|
|
|
var $belongsTo = array(
|
|
'UnitSize',
|
|
);
|
|
|
|
var $hasOne = array(
|
|
'CurrentLease' => array(
|
|
'className' => 'Lease',
|
|
'conditions' => 'CurrentLease.moveout_date IS NULL',
|
|
),
|
|
);
|
|
|
|
var $hasMany = array(
|
|
'Lease',
|
|
'LocksUnit'
|
|
);
|
|
|
|
var $hasAndBelongsToMany = array(
|
|
'Lock'
|
|
);
|
|
|
|
//var $default_log_level = array('log' => 30, 'show' => 15);
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* helpers: status enumerations
|
|
*/
|
|
|
|
function statusEnums() {
|
|
static $status_enums;
|
|
if (!isset($status_enums))
|
|
$status_enums = $this->getEnumValues('status');
|
|
return $status_enums;
|
|
}
|
|
|
|
function activeStatusEnums() {
|
|
return array_diff_key($this->statusEnums(), array(''=>1, 'DELETED'=>1));
|
|
}
|
|
|
|
function statusValue($enum) {
|
|
$enums = $this->statusEnums();
|
|
return $enums[$enum];
|
|
}
|
|
|
|
function occupiedEnumValue() {
|
|
return $this->statusValue('OCCUPIED');
|
|
}
|
|
|
|
function statusCheck($id_or_enum,
|
|
$min = null, $min_strict = false,
|
|
$max = null, $max_strict = false)
|
|
{
|
|
$this->prEnter(compact('id_or_enum', 'min', 'min_strict', 'max', 'max_strict'));
|
|
|
|
if (is_int($id_or_enum)) {
|
|
$this->id = $id_or_enum;
|
|
$id_or_enum = $this->field('status');
|
|
}
|
|
|
|
$enum_val = $this->statusValue($id_or_enum);
|
|
if (isset($min) && is_string($min))
|
|
$min = $this->statusValue($min);
|
|
if (isset($max) && is_string($max))
|
|
$max = $this->statusValue($max);
|
|
|
|
$this->pr(17, compact('enum_val', 'min', 'min_strict', 'max', 'max_strict'));
|
|
|
|
if (isset($min) &&
|
|
($enum_val < $min ||
|
|
($min_strict && $enum_val == $min)))
|
|
return $this->prReturn(false);
|
|
|
|
if (isset($max) &&
|
|
($enum_val > $max ||
|
|
($max_strict && $enum_val == $max)))
|
|
return $this->prReturn(false);
|
|
|
|
return $this->prReturn(true);
|
|
}
|
|
|
|
function locked($enum) {
|
|
return $this->statusCheck($enum, 'LOCKED', false, null, false);
|
|
}
|
|
|
|
function conditionLocked() {
|
|
//return array('Unit.status' => 'LOCKED');
|
|
return ('Unit.status >= ' . $this->statusValue('LOCKED'));
|
|
}
|
|
|
|
function liened($enum) {
|
|
return $this->statusCheck($enum, 'LIENED', false, null, false);
|
|
}
|
|
|
|
function conditionLiened() {
|
|
return ('Unit.status >= ' . $this->statusValue('LIENED'));
|
|
}
|
|
|
|
function occupied($enum) {
|
|
return $this->statusCheck($enum, 'OCCUPIED', false, null, false);
|
|
}
|
|
|
|
function conditionOccupied() {
|
|
return ('Unit.status >= ' . $this->statusValue('OCCUPIED'));
|
|
}
|
|
|
|
function vacant($enum) {
|
|
return $this->statusCheck($enum, 'UNAVAILABLE', true, 'OCCUPIED', true);
|
|
}
|
|
|
|
function conditionVacant() {
|
|
return ('Unit.status BETWEEN ' .
|
|
($this->statusValue('UNAVAILABLE')+1) .
|
|
' AND ' .
|
|
($this->statusValue('OCCUPIED')-1));
|
|
}
|
|
|
|
function unavailable($enum) {
|
|
return $this->statusCheck($enum, null, false, 'UNAVAILABLE', false);
|
|
}
|
|
|
|
function conditionUnavailable() {
|
|
return ('Unit.status <= ' . $this->statusValue('UNAVAILABLE'));
|
|
}
|
|
|
|
function available($enum) { return $this->vacant($enum); }
|
|
function conditionAvailable() { return $this->conditionVacant($enum); }
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* function: allowedStatusSet
|
|
* - Returns the status set allowed for the given unit
|
|
*/
|
|
function allowedStatusSet($id) {
|
|
$this->prEnter(compact('id'));
|
|
$this->id = $id;
|
|
$old_status = $this->field('status');
|
|
$old_val = $this->statusValue($old_status);
|
|
$this->pr(17, compact('old_status', 'old_val'));
|
|
|
|
$enums = $this->activeStatusEnums();
|
|
$this->pr(21, compact('enums'));
|
|
|
|
foreach ($enums AS $enum => $val) {
|
|
if (($old_val < $this->occupiedEnumValue()) !=
|
|
($val < $this->occupiedEnumValue())) {
|
|
unset($enums[$enum]);
|
|
}
|
|
}
|
|
|
|
return $this->prReturn($enums);
|
|
}
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* function: updateStatus
|
|
* - Update the given unit to the given status
|
|
*/
|
|
function updateStatus($id, $status, $check = false) {
|
|
$this->prEnter(compact('id', 'status', 'check'));
|
|
|
|
/* if ($check) { */
|
|
/* $old_status = $this->field('status'); */
|
|
/* $this->pr(17, compact('old_status')); */
|
|
/* if ($this->statusValue($old_status) < $this->occupiedEnumValue() && */
|
|
/* $this->statusValue($status) >= $this->occupiedEnumValue()) */
|
|
/* { */
|
|
/* die("Can't transition a unit from vacant to occupied"); */
|
|
/* return $this->prReturn(false); */
|
|
/* } */
|
|
/* if ($this->statusValue($old_status) >= $this->occupiedEnumValue() && */
|
|
/* $this->statusValue($status) < $this->occupiedEnumValue()) */
|
|
/* { */
|
|
/* die("Can't transition a unit from occupied to vacant"); */
|
|
/* return $this->prReturn(false); */
|
|
/* } */
|
|
/* } */
|
|
|
|
if ($check) {
|
|
if (!array_key_exists($status, $this->allowedStatusSet($id)))
|
|
return $this->prReturn(false);
|
|
}
|
|
|
|
$this->id = $id;
|
|
$this->saveField('status', $status);
|
|
return $this->prReturn(true);
|
|
}
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* function: update
|
|
* - Update any cached or calculated fields
|
|
*/
|
|
function update($id) {
|
|
}
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* function: lockUnit
|
|
* - Put lock on unit
|
|
*/
|
|
function lockUnit($id, $lock_ids) {
|
|
$this->prEnter(compact('id', 'lock_ids'));
|
|
$this->id = $id;
|
|
|
|
// Remove any exising locks for this unit
|
|
$this->LocksUnit->deleteAll
|
|
(array('unit_id' => $id), false);
|
|
|
|
// We'll proceed forward as much as possible, even
|
|
// if we encounter an error. For now, we'll assume
|
|
// the operation will succeed.
|
|
$ret = true;
|
|
|
|
// Go through each lock, and put them on the unit
|
|
foreach ($lock_ids AS $lock_id) {
|
|
$pair['unit_id'] = $id;
|
|
$pair['lock_id'] = $lock_id;
|
|
|
|
// Save the relationship between lock and unit
|
|
$LU = new LocksUnit();
|
|
if (!$LU->save($pair, false))
|
|
$ret = false;
|
|
}
|
|
|
|
return $this->prReturn($ret);
|
|
}
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* function: stats
|
|
* - Returns summary data from the requested customer.
|
|
*/
|
|
|
|
function stats($id = null) {
|
|
if (!$id)
|
|
return null;
|
|
|
|
// Get the basic information necessary
|
|
$unit = $this->find('first',
|
|
array('contain' => array
|
|
('Lease' => array
|
|
('fields' => array('Lease.id')),
|
|
|
|
'CurrentLease' => array
|
|
('fields' => array('CurrentLease.id'))),
|
|
|
|
'conditions' => array
|
|
(array('Unit.id' => $id)),
|
|
));
|
|
|
|
// Get the stats for the current lease
|
|
$stats['CurrentLease'] = $this->Lease->stats($unit['CurrentLease']['id']);
|
|
|
|
// Sum the stats for all leases together
|
|
foreach ($unit['Lease'] AS $lease) {
|
|
$this->statsMerge($stats['Lease'], $this->Lease->stats($lease['id']));
|
|
}
|
|
|
|
// Return the collection
|
|
return $stats;
|
|
}
|
|
|
|
}
|