Files
pmgr/models/unit.php

95 lines
2.4 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',
//'foreignKey' => 'unit_id',
'conditions' => 'CurrentLease.close_date IS NULL',
),
);
var $hasMany = array(
'Lease' => array(
'className' => 'Lease',
'foreignKey' => 'unit_id',
'dependent' => false,
)
);
function statusEnums() {
static $status_enums;
if (!isset($status_enums))
$status_enums = $this->getEnumValues('status');
return $status_enums;
}
function statusValue($enum) {
$enums = $this->statusEnums();
return $enums[$enum];
}
function occupiedEnumValue() {
return statusValue('OCCUPIED');
}
function conditionOccupied() {
return ('Unit.status >= ' . $this->statusValue('OCCUPIED'));
}
function conditionVacant() {
return ('Unit.status BETWEEN ' .
($this->statusValue('UNAVAILABLE')+1) .
' AND ' .
($this->statusValue('OCCUPIED')-1));
}
function conditionUnavailable() {
return ('Unit.status <= ' . $this->statusValue('UNAVAILABLE'));
}
/**************************************************************************
**************************************************************************
**************************************************************************
* function: stats
* - Returns summary data from the requested customer.
*/
function stats($id = null) {
if (!$id)
return null;
$this->Behaviors->attach('Containable');
$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))));
$this->Behaviors->detach('Containable');
$stats['CurrentLease'] = $this->Lease->stats($unit['CurrentLease']['id']);
foreach ($unit['Lease'] AS $lease) {
$this->statsMerge($stats['Lease'], $this->Lease->stats($lease['id']));
}
return $stats;
}
}