This rework is nowhere near complete, but there are certain things that are falling in place, and worth capturing. I started a branch for just this purpose of being able to check in intermediate work.

git-svn-id: file:///svn-source/pmgr/branches/yafr_20090716@352 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
abijah
2009-07-19 23:35:25 +00:00
parent e303898a95
commit 6ac0204baf
26 changed files with 3058 additions and 1767 deletions

View File

@@ -1,27 +1,6 @@
<?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',
@@ -30,7 +9,7 @@ class Lease extends AppModel {
);
var $hasMany = array(
'LedgerEntry',
'DoubleEntry',
);
@@ -60,7 +39,7 @@ class Lease extends AppModel {
if (!isset($cond))
$cond = array();
$cond[] = array('LedgerEntry.lease_id' => $id);
$cond[] = array('DoubleEntry.lease_id' => $id);
$A = new Account();
$entries = $A->findLedgerEntries($this->accountId($id),
@@ -89,7 +68,7 @@ class Lease extends AppModel {
$A = new Account();
$entries = $A->findLedgerEntries
($A->securityDepositAccountID(),
true, array('LedgerEntry.lease_id' => $id), $link);
true, array('DoubleEntry.lease_id' => $id), $link);
/* pr(array('function' => 'Lease::findSecurityDeposits', */
/* 'args' => compact('id', 'link'), */
@@ -100,42 +79,6 @@ class Lease extends AppModel {
}
/**************************************************************************
**************************************************************************
**************************************************************************
* 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));
}
/**************************************************************************
**************************************************************************
**************************************************************************
@@ -153,7 +96,7 @@ class Lease extends AppModel {
('all',
array('link' =>
array(// Models
'LedgerEntry' => array
'DoubleEntry' => array
('Ledger' => array
('fields' => array(),
'Account' => array
@@ -161,12 +104,12 @@ class Lease extends AppModel {
'Ledger' => array
('alias' => 'Lx',
'fields' => array(),
'LedgerEntry' => array
'DoubleEntry' => array
('alias' => 'LEx',
'fields' => array(),
'conditions' => array
('LEx.effective_date = DATE_ADD(LedgerEntry.through_date, INTERVAL 1 day)',
'LEx.lease_id = LedgerEntry.lease_id',
('LEx.effective_date = DATE_ADD(DoubleEntry.through_date, INTERVAL 1 day)',
'LEx.lease_id = DoubleEntry.lease_id',
)
),
),
@@ -219,7 +162,7 @@ class Lease extends AppModel {
return false;
if (count($entries) != 1)
return null;
return $entries[0]['LedgerEntry']['through_date'];
return $entries[0]['DoubleEntry']['through_date'];
}
@@ -234,8 +177,8 @@ class Lease extends AppModel {
// Income / Receipt / Money
// debit: A/R credit: Income <-- this entry
// debit: Receipt credit: A/R <-- ReceiptLedgerEntry, below
// debit: Money credit: Receipt <-- MoneyLedgerEntry, below
// debit: Receipt credit: A/R <-- ReceiptDoubleEntry, below
// debit: Money credit: Receipt <-- MoneyDoubleEntry, below
$query = array
('link' => array
@@ -250,43 +193,43 @@ class Lease extends AppModel {
// We're searching for the Receipt<->A/R entries,
// which are debits on the A/R account. Find the
// reconciling entries to that A/R debit.
'DebitReconciliationLedgerEntry' =>
array('alias' => 'ReceiptLedgerEntry',
'DebitReconciliationDoubleEntry' =>
array('alias' => 'ReceiptDoubleEntry',
'fields' => array(),
// Finally, the Money (Cash/Check/etc) Entry is the one
// which reconciles our ReceiptLedgerEntry debit
'DebitReconciliationLedgerEntry' =>
array('alias' => 'MoneyLedgerEntry',
'linkalias' => 'MoneyLedgerEntryR',
'fields' => array('SUM(COALESCE(MoneyLedgerEntryR.amount,0)) AS paid'),
// which reconciles our ReceiptDoubleEntry debit
'DebitReconciliationDoubleEntry' =>
array('alias' => 'MoneyDoubleEntry',
'linkalias' => 'MoneyDoubleEntryR',
'fields' => array('SUM(COALESCE(MoneyDoubleEntryR.amount,0)) AS paid'),
),
),
),
'fields' => array('LedgerEntry.amount',
'DATE_SUB(LedgerEntry.effective_date, INTERVAL 1 DAY) AS paid_through',
'fields' => array('DoubleEntry.amount',
'DATE_SUB(DoubleEntry.effective_date, INTERVAL 1 DAY) AS paid_through',
),
'group' => 'LedgerEntry.id HAVING paid <> LedgerEntry.amount',
'group' => 'DoubleEntry.id HAVING paid <> DoubleEntry.amount',
'conditions' => array(array('LedgerEntry.lease_id' => $id),
array('Account.id' => $this->LedgerEntry->Ledger->Account->rentAccountID()),
'conditions' => array(array('DoubleEntry.lease_id' => $id),
array('Account.id' => $this->DoubleEntry->Ledger->Account->rentAccountID()),
),
'order' => array('LedgerEntry.effective_date',
'order' => array('DoubleEntry.effective_date',
),
);
$rent = $this->LedgerEntry->find('first', $query);
$rent = $this->DoubleEntry->find('first', $query);
if ($rent)
return $rent[0]['paid_through'];
$query['fields'] = 'LedgerEntry.through_date';
$query['order'] = 'LedgerEntry.through_date DESC';
$query['group'] = 'LedgerEntry.id';
$rent = $this->LedgerEntry->find('first', $query);
$query['fields'] = 'DoubleEntry.through_date';
$query['order'] = 'DoubleEntry.through_date DESC';
$query['group'] = 'DoubleEntry.id';
$rent = $this->DoubleEntry->find('first', $query);
if ($rent)
return $rent['LedgerEntry']['through_date'];
return $rent['DoubleEntry']['through_date'];
return null;
}
@@ -495,7 +438,7 @@ class Lease extends AppModel {
$A = new Account();
$stats = $A->stats($A->accountReceivableAccountID(), true,
array('LedgerEntry.lease_id' => $id));
array('DoubleEntry.lease_id' => $id));
// Pull to the top level and return
$stats = $stats['Ledger'];