git-svn-id: file:///svn-source/pmgr/branches/invoice_receipt_20090629@231 97e9348a-65ac-dc4b-aefc-98561f571b83
303 lines
9.4 KiB
PHP
303 lines
9.4 KiB
PHP
<?php
|
|
class Lease extends AppModel {
|
|
|
|
var $name = 'Lease';
|
|
var $validate = array(
|
|
'id' => array('numeric'),
|
|
'number' => array('alphanumeric'),
|
|
'lease_type_id' => array('numeric'),
|
|
'unit_id' => array('numeric'),
|
|
'late_schedule_id' => array('numeric'),
|
|
'lease_date' => array('date'),
|
|
'movein_planned_date' => array('date'),
|
|
'movein_date' => array('date'),
|
|
'moveout_date' => array('date'),
|
|
'moveout_planned_date' => array('date'),
|
|
'notice_given_date' => array('date'),
|
|
'notice_received_date' => array('date'),
|
|
'close_date' => array('date'),
|
|
'deposit' => array('money'),
|
|
'rent' => array('money'),
|
|
'next_rent' => array('money'),
|
|
'next_rent_date' => array('date')
|
|
);
|
|
|
|
var $belongsTo = array(
|
|
'LeaseType',
|
|
'Unit',
|
|
'Customer',
|
|
'LateSchedule',
|
|
);
|
|
|
|
var $hasMany = array(
|
|
'LedgerEntry',
|
|
);
|
|
|
|
|
|
function beforeSave() {
|
|
foreach (array('lease',
|
|
'movein', 'movein_planned',
|
|
'moveout', 'moveout_planned',
|
|
'notice_givein', 'notice_received',
|
|
'close', 'next_rent') AS $dtfield) {
|
|
$dtfield .= '_date';
|
|
if(isset($this->data['Lease'][$dtfield]))
|
|
$this->data['Lease'][$dtfield] =
|
|
$this->dateFormatBeforeSave($this->data['Lease'][$dtfield]);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* function: accountId
|
|
* - Returns the accountId of the given lease
|
|
*/
|
|
function accountId($id) {
|
|
$A = new Account();
|
|
return $A->invoiceAccountID();
|
|
}
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* function: findAccountEntries
|
|
* - Returns an array of ledger entries from the account of the given
|
|
* lease.
|
|
*/
|
|
function findAccountEntries($id, $all = false, $cond = null, $link = null) {
|
|
/* pr(array('function' => 'Lease::findAccountEntries', */
|
|
/* 'args' => compact('id', 'all', 'cond', 'link'), */
|
|
/* )); */
|
|
|
|
if (!isset($cond))
|
|
$cond = array();
|
|
$cond[] = array('LedgerEntry.lease_id' => $id);
|
|
|
|
$A = new Account();
|
|
$entries = $A->findLedgerEntries($this->accountId($id),
|
|
$all, $cond, $link);
|
|
|
|
/* pr(array('function' => 'Lease::findAccountEntries', */
|
|
/* 'args' => compact('id', 'all', 'cond', 'link'), */
|
|
/* 'vars' => compact('lease'), */
|
|
/* 'return' => compact('entries'), */
|
|
/* )); */
|
|
return $entries;
|
|
}
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* function: findSecurityDeposits
|
|
* - Returns an array of security deposit entries
|
|
*/
|
|
function findSecurityDeposits($id, $link = null) {
|
|
/* pr(array('function' => 'Lease::findSecurityDeposits', */
|
|
/* 'args' => compact('id', 'link'), */
|
|
/* )); */
|
|
|
|
$A = new Account();
|
|
$entries = $A->findLedgerEntriesRelatedToAccount
|
|
($this->accountId($id),
|
|
$A->securityDepositAccountID(),
|
|
true, array('LedgerEntry.lease_id' => $id), $link);
|
|
|
|
/* pr(array('function' => 'Lease::findSecurityDeposits', */
|
|
/* 'args' => compact('id', 'link'), */
|
|
/* 'vars' => compact('lease'), */
|
|
/* 'return' => compact('entries'), */
|
|
/* )); */
|
|
return $entries;
|
|
}
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* function: findUnreconciledLedgerEntries
|
|
* - Returns ledger entries that are not yet reconciled
|
|
* (such as charges not paid).
|
|
*/
|
|
|
|
function findUnreconciledLedgerEntries($id = null, $fundamental_type = null) {
|
|
$A = new Account();
|
|
return $A->findUnreconciledLedgerEntries
|
|
($this->accountId($id), $fundamental_type, array('LedgerEntry.lease_id' => $id));
|
|
}
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* function: reconcileNewLedgerEntry
|
|
* - Returns which ledger entries a new credit/debit would
|
|
* reconcile, and how much.
|
|
*
|
|
* - REVISIT <AP> 20090617
|
|
* This should be subject to different algorithms, such
|
|
* as apply to oldest charges first, newest first, to fees
|
|
* before rent, etc. Until we get there, I'll hardcode
|
|
* whatever algorithm is simplest.
|
|
*/
|
|
|
|
function reconcileNewLedgerEntry($id, $fundamental_type, $amount) {
|
|
$A = new Account();
|
|
return $A->reconcileNewLedgerEntry
|
|
($this->accountId($id), $fundamental_type, $amount, array('LedgerEntry.lease_id' => $id));
|
|
}
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* function: moveIn
|
|
* - Moves the specified customer into the specified lease
|
|
*/
|
|
|
|
function moveIn($customer_id, $unit_id,
|
|
$deposit = null, $rent = null,
|
|
$stamp = null, $comment = null) {
|
|
|
|
$lt = $this->LeaseType->find('first',
|
|
array('conditions' =>
|
|
array('code' => 'SL')));
|
|
|
|
// Use NOW if not given a movein date
|
|
if (!isset($stamp))
|
|
$stamp = date('Y-m-d G:i:s');
|
|
|
|
if (!$comment)
|
|
$comment = null;
|
|
|
|
if (!isset($deposit) || !isset($rent)) {
|
|
$rates = $this->Unit->find
|
|
('first',
|
|
array('contain' =>
|
|
array('UnitSize' =>
|
|
array('deposit', 'rent'),
|
|
),
|
|
'fields' => array('deposit', 'rent'),
|
|
'conditions' => array('Unit.id' => $unit_id),
|
|
));
|
|
|
|
$deposit =
|
|
(isset($deposit)
|
|
? $deposit
|
|
: (isset($rates['Unit']['deposit'])
|
|
? $rates['Unit']['deposit']
|
|
: (isset($rates['UnitSize']['deposit'])
|
|
? $rates['UnitSize']['deposit']
|
|
: 0)));
|
|
|
|
$rent =
|
|
(isset($rent)
|
|
? $rent
|
|
: (isset($rates['Unit']['rent'])
|
|
? $rates['Unit']['rent']
|
|
: (isset($rates['UnitSize']['rent'])
|
|
? $rates['UnitSize']['rent']
|
|
: 0)));
|
|
}
|
|
|
|
|
|
// Save this new lease.
|
|
$this->create();
|
|
if (!$this->save(array('lease_type_id' => $lt['LeaseType']['id'],
|
|
'unit_id' => $unit_id,
|
|
'customer_id' => $customer_id,
|
|
'lease_date' => $stamp,
|
|
'movein_date' => $stamp,
|
|
'deposit' => $deposit,
|
|
'rent' => $rent,
|
|
'comment' => $comment), false)) {
|
|
return null;
|
|
}
|
|
|
|
// Set the lease number to be the same as the lease ID
|
|
$this->id;
|
|
$this->saveField('number', $this->id);
|
|
|
|
// Update the unit status
|
|
$this->Unit->updateStatus($unit_id, 'OCCUPIED');
|
|
|
|
// REVISIT <AP>: 20090702
|
|
// We need to assess the security deposit charge,
|
|
// and probably rent as well. Rent, however, will
|
|
// require user parameters to indicate whether it
|
|
// was waived, pro-rated, etc.
|
|
|
|
// Return the new lease ID
|
|
return $this->id;
|
|
}
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* function: moveOut
|
|
* - Moves the customer out of the specified lease
|
|
*/
|
|
|
|
function moveOut($id = null, $status = 'VACANT',
|
|
$stamp = null, $close = false) {
|
|
$this->create(false);
|
|
$this->id = $id;
|
|
|
|
// Use NOW if not given a moveout date
|
|
if (!isset($stamp))
|
|
$stamp = date('Y-m-d G:i:s');
|
|
|
|
// Move customer out of the lease, and possibly close it
|
|
$this->data['Lease']['moveout_date'] = $stamp;
|
|
if ($close)
|
|
$this->data['Lease']['close_date'] = $stamp;
|
|
|
|
// Save it!
|
|
$this->save($this->data, false);
|
|
|
|
// Finally, update the unit status
|
|
$this->recursive = -1;
|
|
$this->read();
|
|
$this->Unit->updateStatus($this->data['Lease']['unit_id'], $status);
|
|
}
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* function: addCharge
|
|
* - Adds an additional charge to the lease
|
|
*/
|
|
|
|
function addCharge($id, $charge) {
|
|
|
|
}
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* function: stats
|
|
* - Returns summary data from the requested lease.
|
|
*/
|
|
|
|
function stats($id = null) {
|
|
if (!$id)
|
|
return null;
|
|
|
|
$A = new Account();
|
|
$stats = $A->stats($A->accountReceivableAccountID(), true,
|
|
array('LedgerEntry.lease_id' => $id));
|
|
|
|
// Pull to the top level and return
|
|
$stats = $stats['Ledger'];
|
|
return $stats;
|
|
}
|
|
|
|
}
|
|
?>
|