git-svn-id: file:///svn-source/pmgr/branches/pre_0.1_work_20090819@837 97e9348a-65ac-dc4b-aefc-98561f571b83
529 lines
18 KiB
PHP
529 lines
18 KiB
PHP
<?php
|
|
|
|
class LeasesController extends AppController {
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* override: addGridViewSideMenuLinks
|
|
* - Adds grid view navigation side menu links
|
|
*/
|
|
|
|
function addGridViewSideMenuLinks() {
|
|
parent::addGridViewSideMenuLinks();
|
|
|
|
$this->addSideMenuLink('Active',
|
|
array('controller' => 'leases', 'action' => 'active'), null,
|
|
'CONTROLLER');
|
|
$this->addSideMenuLink('Closed',
|
|
array('controller' => 'leases', 'action' => 'closed'), null,
|
|
'CONTROLLER');
|
|
$this->addSideMenuLink('Delinquent',
|
|
array('controller' => 'leases', 'action' => 'delinquent'), null,
|
|
'CONTROLLER');
|
|
$this->addSideMenuLink('All',
|
|
array('controller' => 'leases', 'action' => 'all'), null,
|
|
'CONTROLLER');
|
|
}
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* action: index / active / closed / all
|
|
* - Generate a listing of leases
|
|
*/
|
|
|
|
function index() { $this->active(); }
|
|
function active() { $this->gridView('Active Leases', 'active'); }
|
|
function delinquent() { $this->gridView('Delinquent Leases'); }
|
|
function closed() { $this->gridView('Closed Leases'); }
|
|
function all() { $this->gridView('All Leases'); }
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* 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('Unit' => array('fields' => array('id', 'name')),
|
|
'Customer' => array('fields' => array('id', 'name'))));
|
|
}
|
|
|
|
function gridDataTables(&$params, &$model) {
|
|
$link = $this->gridDataCountTables($params, $model);
|
|
$link['link']['StatementEntry'] = array('fields' => array());
|
|
return $link;
|
|
}
|
|
|
|
function gridDataFields(&$params, &$model) {
|
|
$fields = parent::gridDataFields($params, $model);
|
|
$fields[] = ("IF(" . $this->Lease->conditionDelinquent() . "," .
|
|
" 'DELINQUENT', 'CURRENT') AS 'status'");
|
|
return array_merge($fields,
|
|
$this->Lease->StatementEntry->chargeDisbursementFields(true));
|
|
}
|
|
|
|
function gridDataConditions(&$params, &$model) {
|
|
$conditions = parent::gridDataConditions($params, $model);
|
|
|
|
if ($params['action'] === 'active') {
|
|
$conditions[] = 'Lease.close_date IS NULL';
|
|
}
|
|
elseif ($params['action'] === 'delinquent') {
|
|
$conditions[] = $this->Lease->conditionDelinquent();
|
|
}
|
|
elseif ($params['action'] === 'closed') {
|
|
$conditions[] = 'Lease.close_date IS NOT NULL';
|
|
}
|
|
|
|
if (isset($customer_id))
|
|
$conditions[] = array('Lease.customer_id' => $customer_id);
|
|
|
|
return $conditions;
|
|
}
|
|
|
|
function gridDataOrder(&$params, &$model, $index, $direction) {
|
|
// Do not sort by number, which is type varchar and
|
|
// sorts on an ascii basis. Sort by ID instead.
|
|
if ($index === 'Lease.number')
|
|
$index = 'Lease.id';
|
|
|
|
// 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 id/number
|
|
// add sorting by id as a secondary condition.
|
|
if ($index !== 'Lease.id' && $index !== 'Lease.number')
|
|
$order[] = parent::gridDataOrder($params, $model,
|
|
'Lease.id', $direction);
|
|
|
|
return $order;
|
|
}
|
|
|
|
/* function gridDataPostProcess(&$params, &$model, &$records) { */
|
|
/* foreach ($records AS &$record) { */
|
|
/* $record['Lease']['through_date'] */
|
|
/* = $this->Lease->rentChargeThrough($record['Lease']['id']); */
|
|
/* } */
|
|
|
|
/* parent::gridDataPostProcess($params, $model, $records); */
|
|
/* } */
|
|
|
|
function gridDataPostProcessLinks(&$params, &$model, &$records, $links) {
|
|
$links['Lease'] = array('number');
|
|
$links['Unit'] = array('name');
|
|
$links['Customer'] = array('name');
|
|
return parent::gridDataPostProcessLinks($params, $model, $records, $links);
|
|
}
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* action: move_in
|
|
* - execute a move in on a new lease
|
|
*/
|
|
|
|
function move_in() {
|
|
if (!$this->data)
|
|
die("Should have some data");
|
|
|
|
// Handle the move in based on the data given
|
|
//pr(array('Move-in data', $this->data));
|
|
foreach (array('deposit', 'rent') AS $currency) {
|
|
$this->data['Lease'][$currency]
|
|
= str_replace('$', '', $this->data['Lease'][$currency]);
|
|
}
|
|
|
|
$lid = $this->Lease->moveIn($this->data['Lease']['customer_id'],
|
|
$this->data['Lease']['unit_id'],
|
|
$this->data['Lease']['deposit'],
|
|
$this->data['Lease']['rent'],
|
|
$this->data['Lease']['movein_date'],
|
|
$this->data['Lease']['comment']
|
|
);
|
|
|
|
// Since this is a new lease, go to the invoice
|
|
// screen so we can start assessing charges.
|
|
$this->redirect(array('action'=>'invoice', $lid, 'move-in'));
|
|
}
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* action: move_out
|
|
* - prepare or execute a move out on a specific lease
|
|
*/
|
|
|
|
function move_out($id = null) {
|
|
if ($this->data) {
|
|
// Handle the move out based on the data given
|
|
//pr($this->data);
|
|
|
|
$this->Lease->moveOut($this->data['Lease']['id'],
|
|
'VACANT',
|
|
$this->data['Lease']['moveout_date']
|
|
);
|
|
|
|
$this->redirect(array('controller' => 'leases',
|
|
'action' => 'view',
|
|
$this->data['Lease']['id']));
|
|
}
|
|
|
|
if (isset($id)) {
|
|
$lease = $this->Lease->find
|
|
('first', array
|
|
('contain' => array
|
|
(// Models
|
|
'Unit' =>
|
|
array('order' => array('sort_order'),
|
|
'fields' => array('id', 'name'),
|
|
),
|
|
|
|
'Customer' =>
|
|
array('fields' => array('id', 'name'),
|
|
),
|
|
),
|
|
|
|
'conditions' => array(array('Lease.id' => $id),
|
|
array('Lease.close_date' => null),
|
|
),
|
|
));
|
|
$this->set('customer', $lease['Customer']);
|
|
$this->set('unit', $lease['Unit']);
|
|
$this->set('lease', $lease['Lease']);
|
|
|
|
$title = ('Lease #' . $lease['Lease']['number'] . ': ' .
|
|
$lease['Unit']['name'] . ': ' .
|
|
$lease['Customer']['name'] . ': Move-Out');
|
|
}
|
|
else {
|
|
$title = 'Move-Out';
|
|
}
|
|
|
|
$this->set(compact('title'));
|
|
$this->render('/leases/move');
|
|
}
|
|
|
|
|
|
/* /\************************************************************************** */
|
|
/* ************************************************************************** */
|
|
/* ************************************************************************** */
|
|
/* * action: promote_credit */
|
|
/* * - Moves any lease credit up to the customer level, so that */
|
|
/* * it may be used for charges other than those on this lease. */
|
|
/* *\/ */
|
|
|
|
/* function promote_surplus($id) { */
|
|
/* $this->Lease->promoteSurplus($id); */
|
|
/* $this->redirect(array('controller' => 'leases', */
|
|
/* 'action' => 'view', */
|
|
/* $id)); */
|
|
/* } */
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* action: refund
|
|
* - Provides lease customer with a refund
|
|
*/
|
|
|
|
function refund($id) {
|
|
$lease = $this->Lease->find
|
|
('first', array
|
|
('contain' => array
|
|
(// Models
|
|
'Unit' => array('fields' => array('id', 'name')),
|
|
'Customer' => array('fields' => array('id', 'name')),
|
|
),
|
|
|
|
'conditions' => array(array('Lease.id' => $id),
|
|
// Make sure lease is not closed...
|
|
array('Lease.close_date' => null),
|
|
),
|
|
));
|
|
if (empty($lease)) {
|
|
$this->redirect(array('action'=>'view', $id));
|
|
}
|
|
|
|
// Determine the lease balance, bailing if the customer owes money
|
|
$balance = $this->Lease->balance($id);
|
|
if ($balance >= 0) {
|
|
$this->redirect(array('action'=>'view', $id));
|
|
}
|
|
|
|
// The refund will be for a positive amount
|
|
$balance *= -1;
|
|
|
|
// Get the accounts capable of paying the refund
|
|
$refundAccounts = $this->Lease->StatementEntry->Account->refundAccounts();
|
|
$defaultAccount = current($refundAccounts);
|
|
$this->set(compact('refundAccounts', 'defaultAccount'));
|
|
|
|
// Prepare to render
|
|
$title = ('Lease #' . $lease['Lease']['number'] . ': ' .
|
|
$lease['Unit']['name'] . ': ' .
|
|
$lease['Customer']['name'] . ': Refund');
|
|
$this->set(compact('title', 'lease', 'balance'));
|
|
$this->render('/transactions/refund');
|
|
}
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* action: bad_debt
|
|
* - Sets up the write-off entry page, so that the
|
|
* user can write off remaining charges on a lease.
|
|
*/
|
|
|
|
function bad_debt($id) {
|
|
$this->Lease->id = $id;
|
|
$lease = $this->Lease->find
|
|
('first', array
|
|
('contain' => array
|
|
(// Models
|
|
'Unit' => array('fields' => array('id', 'name')),
|
|
'Customer' => array('fields' => array('id', 'name')),
|
|
),
|
|
));
|
|
|
|
// Make sure we have a valid lease to write off
|
|
if (empty($lease))
|
|
$this->redirect(array('action' => 'view', $id));
|
|
|
|
// Get the lease balance
|
|
$balance = $this->Lease->balance($id);
|
|
|
|
// Prepare to render
|
|
$title = ('Lease #' . $lease['Lease']['number'] . ': ' .
|
|
$lease['Unit']['name'] . ': ' .
|
|
$lease['Customer']['name'] . ': Write Off Bad Debt');
|
|
$this->set(compact('title', 'lease', 'balance'));
|
|
$this->render('/transactions/bad_debt');
|
|
}
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* action: close
|
|
* - Closes a lease to any further action
|
|
*/
|
|
|
|
// REVISIT <AP>: 20090809
|
|
// While cleaning up the sitelink data, then delete reldep()
|
|
function reldep($id) {
|
|
$this->Lease->id = $id;
|
|
$stamp = $this->Lease->field('moveout_date');
|
|
$this->Lease->releaseSecurityDeposits($id, $stamp);
|
|
$this->redirect(array('action'=>'view', $id));
|
|
}
|
|
|
|
function close($id) {
|
|
// REVISIT <AP>: 20090708
|
|
// We should probably seek confirmation first...
|
|
if (!$this->Lease->closeable($id)) {
|
|
$this->INTERNAL_ERROR("This lease is not ready to close");
|
|
$this->redirect(array('action'=>'view', $id));
|
|
}
|
|
|
|
$this->Lease->close($id);
|
|
$this->redirect(array('action'=>'view', $id));
|
|
}
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* action: invoice
|
|
* - Sets up the invoice entry page for the given customer.
|
|
*/
|
|
|
|
function invoice($id = null, $type = null) {
|
|
|
|
$lease = $this->Lease->find
|
|
('first', array
|
|
('contain' => array
|
|
(// Models
|
|
'Unit' =>
|
|
array('order' => array('sort_order'),
|
|
'fields' => array('id', 'name'),
|
|
),
|
|
|
|
'Customer' =>
|
|
array('fields' => array('id', 'name'),
|
|
),
|
|
),
|
|
|
|
'conditions' => array(array('Lease.id' => $id),
|
|
array('Lease.close_date' => null),
|
|
),
|
|
));
|
|
|
|
$A = new Account();
|
|
$charge_accounts = $A->invoiceAccounts();
|
|
$default_account = $A->rentAccountID();
|
|
$rent_account = $A->rentAccountID();
|
|
$security_deposit_account = $A->securityDepositAccountID();
|
|
$this->set(compact('charge_accounts', 'default_account',
|
|
'rent_account', 'security_deposit_account'));
|
|
|
|
// REVISIT <AP> 20090705:
|
|
// Of course, the late charge should come from the late_schedule
|
|
$default_late = 10;
|
|
$this->set(compact('default_late'));
|
|
|
|
if ($type === 'move-in') {
|
|
// Make sure we have a valid lease that we're moving in
|
|
if (empty($lease))
|
|
$this->redirect(array('action' => 'index'));
|
|
|
|
$movein = array();
|
|
$movein['time'] = strtotime($lease['Lease']['movein_date']);
|
|
$movein['effective_time'] = strtotime($lease['Lease']['movein_date']);
|
|
$movein_date = getdate($movein['effective_time']);
|
|
$movein['through_time'] = mktime(0, 0, 0, $movein_date['mon'] + 1, 0, $movein_date['year']);
|
|
$days_in_month = idate('d', $movein['through_time']);
|
|
$movein['prorated_days'] = $days_in_month - $movein_date['mday'] + 1;
|
|
$movein['prorated_rent'] = $lease['Lease']['rent'] * $movein['prorated_days'] / $days_in_month;
|
|
$movein['prorated'] = $movein['prorated_days'] != $days_in_month;
|
|
$movein['deposit'] = $lease['Lease']['deposit'];
|
|
$this->set(compact('movein'));
|
|
}
|
|
|
|
|
|
$title = ('Lease #' . $lease['Lease']['number'] . ': ' .
|
|
$lease['Unit']['name'] . ': ' .
|
|
$lease['Customer']['name'] . ': Charge Entry');
|
|
$this->set(compact('title', 'lease', 'charge'));
|
|
}
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* action: assess_rent/late
|
|
* - Assesses the new monthly rent/late charge, if need be
|
|
*/
|
|
|
|
function assess_rent($date = null) {
|
|
$this->Lease->assessMonthlyRentAll($date);
|
|
$this->redirect(array('action'=>'index'));
|
|
}
|
|
function assess_late($date = null) {
|
|
$this->Lease->assessMonthlyLateAll($date);
|
|
$this->redirect(array('action'=>'index'));
|
|
}
|
|
function assess_all($date = null) {
|
|
$this->Lease->assessMonthlyRentAll($date);
|
|
$this->Lease->assessMonthlyLateAll($date);
|
|
$this->redirect(array('action'=>'index'));
|
|
}
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* action: view
|
|
* - Displays information about a specific lease
|
|
*/
|
|
|
|
function view($id = null) {
|
|
if (!$id) {
|
|
$this->Session->setFlash(__('Invalid Item.', true));
|
|
$this->redirect(array('action'=>'index'));
|
|
}
|
|
|
|
// Get details about the lease and its ledgers (no ledger entries yet)
|
|
$lease = $this->Lease->find
|
|
('first',
|
|
array('contain' =>
|
|
array(// Models
|
|
'LeaseType(id,name)',
|
|
'Unit(id,name)',
|
|
'Customer(id,name)',
|
|
),
|
|
'fields' => array('Lease.*', $this->Lease->delinquentField()),
|
|
'conditions' => array(array('Lease.id' => $id)),
|
|
)
|
|
);
|
|
$lease['Lease'] += $lease[0];
|
|
unset($lease[0]);
|
|
|
|
// Figure out the outstanding balances for this lease
|
|
$outstanding_balance = $this->Lease->balance($id);
|
|
$outstanding_deposit = $this->Lease->securityDepositBalance($id);
|
|
|
|
// Set up dynamic menu items. Normally, these will only be present
|
|
// on an open lease, but it's possible for a lease to be closed, and
|
|
// yet still have an outstanding balance. This can happen if someone
|
|
// were to reverse charges, or if a payment should come back NSF.
|
|
if (!isset($lease['Lease']['close_date']) || $outstanding_balance > 0) {
|
|
if (!isset($lease['Lease']['moveout_date']))
|
|
$this->addSideMenuLink('Move-Out',
|
|
array('action' => 'move_out', $id), null,
|
|
'ACTION');
|
|
|
|
if (!isset($lease['Lease']['close_date']))
|
|
$this->addSideMenuLink('New Invoice',
|
|
array('action' => 'invoice', $id), null,
|
|
'ACTION');
|
|
|
|
$this->addSideMenuLink('New Receipt',
|
|
array('controller' => 'customers',
|
|
'action' => 'receipt',
|
|
$lease['Customer']['id']), null,
|
|
'ACTION');
|
|
|
|
// REVISIT <AP>:
|
|
// Not allowing refund to be issued from the lease, as
|
|
// in fact, we should never have a positive lease balance.
|
|
// I'll flag this at the moment, since we might get one
|
|
// when a charge is reimbursed; a bug that we'll either
|
|
// need to fix, or we'll have to revisit this assumption.
|
|
if ($outstanding_balance < 0)
|
|
$this->INTERNAL_ERROR("Should not have a customer lease credit.");
|
|
|
|
/* if ($outstanding_balance < 0) */
|
|
/* $this->addSideMenuLink('Issue Refund', */
|
|
/* array('action' => 'refund', $id), null, */
|
|
/* 'ACTION'); */
|
|
|
|
if (isset($lease['Lease']['moveout_date']) && $outstanding_balance > 0)
|
|
$this->addSideMenuLink('Write-Off',
|
|
array('action' => 'bad_debt', $id), null,
|
|
'ACTION');
|
|
|
|
if ($this->Lease->closeable($id))
|
|
$this->addSideMenuLink('Close',
|
|
array('action' => 'close', $id), null,
|
|
'ACTION');
|
|
}
|
|
|
|
// Prepare to render
|
|
$title = 'Lease: #' . $lease['Lease']['id'];
|
|
$this->set(compact('lease', 'title',
|
|
'outstanding_deposit',
|
|
'outstanding_balance'));
|
|
}
|
|
}
|