Files
pmgr/controllers/units_controller.php

238 lines
7.4 KiB
PHP

<?php
class UnitsController extends AppController {
var $paginate = array('limit' => 100,
'group' => 'Unit.id',
'order' => array('Unit.sort_order' => 'ASC'));
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
* - Lists all units
*/
function index() {
$this->all();
}
/**************************************************************************
**************************************************************************
**************************************************************************
* action: unavailable
* - Lists unavailable units
*/
function unavailable() {
$this->paginate = array_merge
($this->paginate,
array('link' =>
array(// Models
'UnitSize' => array('fields' => array('name')),
),
'conditions' => $this->Unit->conditionUnavailable()
));
$title = 'Unavailable Units';
$this->set('title', $title); $this->set('heading', $title);
$this->set('units', $this->paginate());
$this->render('index');
}
/**************************************************************************
**************************************************************************
**************************************************************************
* action: vacant
* - Lists vacant units
*/
function vacant() {
$this->paginate = array_merge
($this->paginate,
array('link' =>
array(// Models
'UnitSize' => array('fields' => array('name')),
),
'conditions' => $this->Unit->conditionVacant()
));
$title = 'Vacant Units';
$this->set('title', $title); $this->set('heading', $title);
$this->set('units', $this->paginate());
$this->render('index');
}
/**************************************************************************
**************************************************************************
**************************************************************************
* action: occupied
* - Lists occupied units
*/
function occupied() {
$this->paginate = array_merge
($this->paginate,
array('link' =>
array(// Models
'UnitSize' => array('fields' => array('name')),
'Lease' => array('fields' => array(),
// Models
'Contact' => array('fields' => array('display_name'),
//'type' => 'LEFT',
),
),
),
'conditions' => $this->Unit->conditionOccupied()
));
$title = 'Occupied Units';
$this->set('title', $title); $this->set('heading', $title);
$this->set('units', $this->paginate());
$this->render('index');
}
/**************************************************************************
**************************************************************************
**************************************************************************
* action: all
* - Lists all units
*/
function all() {
$this->paginate = array_merge
($this->paginate,
array('link' =>
array(// Models
'UnitSize' => array('fields' => array('name')),
),
));
$title = 'All Units';
$this->set('title', $title); $this->set('heading', $title);
$this->set('units', $this->paginate());
$this->render('index');
}
/**************************************************************************
**************************************************************************
**************************************************************************
* action: view
* - Displays information about a specific unit
*/
function view($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid Item.', true));
$this->redirect(array('action'=>''));
}
$this->Unit->Behaviors->attach('Containable');
$this->Unit->contain
(array(// Models
'UnitSize',
//'CurrentLease' =>
'Lease' =>
array(//'order' => 'movein_date',
//'conditions' => array('Lease.lease_date IS NOT NULL',
//),
// Models
//'Contact' => array('alias' => 'PrimaryContact'),
// 'PrimaryContact',
'Customer' => array('fields' => array('id', 'name'),
// Models
'Transaction' =>
array('order' => array('stamp'),
// Models
'LedgerEntry' => array('DebitLedger' => array('Account'),
'CreditLedger' => array('Account'),
),
),
),
/* array(//'order' => array('sort_order'), */
/* 'fields' => array('id', 'display_name'), */
/* ), */
)
)
);
//$unit = $this->Unit->read(null, $id);
//pr($unit);
$this->Unit->Lease->Customer->Transaction->LedgerEntry->Behaviors->attach('Containable');
$entries = $this->Unit->Lease->Customer->Transaction->LedgerEntry->find
('all',
array
(
'contain' =>
array('Transaction' =>
array('Customer' =>
array('Lease' =>
array('Unit' =>
array('conditions' => array('Unit.id' => $id)),
),
),
),
),
//'conditions' => 'xyz',
'limit' => 2,
));
pr($entries);
$this->autoRender = false;
$outstanding_deposit = 0;
$outstanding_balance = 0;
foreach($unit['Lease'] AS $lease) {
foreach($lease['Customer']['Transaction'] AS $transaction) {
foreach($transaction['LedgerEntry'] AS $entry) {
if ($entry['DebitLedger']['Account']['name'] === 'A/R')
$outstanding_balance += $entry['amount'];
if ($entry['CreditLedger']['Account']['name'] === 'A/R')
$outstanding_balance -= $entry['amount'];
if ($entry['DebitLedger']['Account']['name'] === 'Security Deposit')
$outstanding_deposit -= $entry['amount'];
if ($entry['CreditLedger']['Account']['name'] === 'Security Deposit')
$outstanding_deposit += $entry['amount'];
}
}
}
$this->sidemenu_links[] =
array('name' => 'Operations', 'header' => true);
$this->sidemenu_links[] =
array('name' => 'Move-Out', 'url' => array('controller' => 'units', 'action' => 'move-out'));
$title = 'Unit ' . $unit['Unit']['name'];
$this->set(compact('unit', 'title',
'outstanding_balance',
'outstanding_deposit'));
}
}