Primarly a cleanup checkin, although a couple minor bugfixes were included as well.

git-svn-id: file:///svn-source/pmgr/branches/ledger_transactions_20090605/site@82 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
abijah
2009-06-10 05:14:08 +00:00
parent ffd1b64580
commit e0d9edc4a8
15 changed files with 151 additions and 519 deletions

View File

@@ -11,7 +11,6 @@ class Account extends AppModel {
var $hasOne = array(
'CurrentLedger' => array(
'className' => 'Ledger',
//'foreignKey' => 'account_id',
'conditions' => array('NOT' => array('CurrentLedger.closed'))
),
);
@@ -21,19 +20,6 @@ class Account extends AppModel {
);
var $cache;
/* var $instantiated; */
/* /\************************************************************************** */
/* ************************************************************************** */
/* ************************************************************************** */
/* * function: constructor */
/* *\/ */
/* function __constructor($id = null) { */
/* parent::__contstructor(); */
/* $this->id = $id; */
/* $this->instantiated = true; */
/* } */
/**************************************************************************
@@ -45,7 +31,8 @@ class Account extends AppModel {
function securityDepositAccountID() {
$account = $this->find('first', array
('recursive' => -1,
'conditions' => array(array('name' => 'Security Deposit')),
'conditions' => array
(array('name' => 'Security Deposit')),
));
return $account['Account']['id'];
}
@@ -60,7 +47,8 @@ class Account extends AppModel {
function rentAccountID() {
$account = $this->find('first', array
('recursive' => -1,
'conditions' => array(array('name' => 'Rent')),
'conditions' => array
(array('name' => 'Rent')),
));
return $account['Account']['id'];
}
@@ -74,10 +62,6 @@ class Account extends AppModel {
*/
function type($id) {
if (isset($this->cache[$id]['type'])) {
/* pr(array('function' => 'Account::type', */
/* 'args' => compact('id'), */
/* 'cache_hit' => true, */
/* 'return' => $this->cache[$id]['type'])); */
return $this->cache[$id]['type'];
}
@@ -87,7 +71,9 @@ class Account extends AppModel {
'conditions' => array(array('Account.id' => $id)),
));
// Save the account type in our cache for future reference
$this->cache[$id]['type'] = $account['Account']['type'];
/* pr(array('function' => 'Account::type', */
/* 'args' => compact('id'), */
/* 'return' => $this->cache[$id]['type'])); */
@@ -105,10 +91,6 @@ class Account extends AppModel {
function ledgers($id, $all = false) {
$cachekey = $all ? 'all' : 'current';
if (isset($this->cache[$id]['ledgers'][$cachekey])) {
/* pr(array('function' => 'Account::ledgers', */
/* 'args' => compact('id', 'all'), */
/* 'cache_hit' => true, */
/* 'return' => $this->cache[$id]['ledgers'][$cachekey])); */
return $this->cache[$id]['ledgers'][$cachekey];
}
@@ -135,6 +117,7 @@ class Account extends AppModel {
$ledger_ids = array($account['CurrentLedger']['id']);
}
// Save the ledgers in our cache for future reference
$this->cache[$id]['ledgers'][$cachekey] = $ledger_ids;
/* pr(array('function' => 'Account::ledgers', */
@@ -159,19 +142,22 @@ class Account extends AppModel {
$entries = array();
foreach ($this->ledgers($id, $all) AS $ledger_id) {
$ledger_entries = $this->Ledger->findLedgerEntries($ledger_id, $this->type($id), $cond, $link);
$ledger_entries = $this->Ledger->findLedgerEntries
($ledger_id, $this->type($id), $cond, $link);
$entries = array_merge($entries, $ledger_entries);
//$entries = array_merge($entries, array_diff_key($ledger_entries, array('summary'=>1)));
//$this->statsMerge($entries['summary'], $ledger_entries['summary']);
}
$stats = $this->stats($id, $all, $cond);
$entries = array('Entries' => $entries,
'summary' => $stats['Ledger']);
/* pr(array('function' => 'Account::findLedgerEntries', */
/* 'args' => compact('id', 'all', 'cond', 'link'), */
/* 'vars' => compact('stats'), */
/* 'return' => compact('entries'), */
/* )); */
$stats = $this->stats($id, $all, $cond);
return array('Entries' => $entries, 'summary' => $stats['Ledger']);
return $entries;
}
@@ -187,9 +173,9 @@ class Account extends AppModel {
/* pr(array('function' => 'Account::findLedgerEntriesRelatedToAccount', */
/* 'args' => compact('id', 'rel_ids', 'all', 'cond', 'link'), */
/* )); */
if (!isset($cond))
$cond = array();
if (!is_array($rel_ids))
$rel_ids = array($rel_ids);
@@ -209,196 +195,6 @@ class Account extends AppModel {
}
/**************************************************************************
**************************************************************************
**************************************************************************
* function: findCurrentLedgerEntries
* - Returns an array of ledger entries that belong to the current
* ledger of the given account. There is extra work done... see the
* LedgerEntry model.
*/
function findCurrentLedgerEntries($id, $cond = null, $link = null) {
$this->Behaviors->attach('Containable');
$account = $this->find('first', array
('contain' => array('CurrentLedger'),
'fields' => array('Account.type', 'CurrentLedger.id'),
'conditions' => array(array('Account.id' => $id)),
));
$this->Behaviors->detach('Containable');
$ledger_id = $account['CurrentLedger']['id'];
$account_type = $account['Account']['type'];
return $this->Ledger->findLedgerEntries($ledger_id, $account_type, $cond, $link);
}
/**************************************************************************
**************************************************************************
**************************************************************************
* function: findLedgerRelatedEntries
* - Returns an array of ledger entries from this account that are
* related to one of the given ledgers.
*/
function findLedgerRelatedEntries($id, $ledgers, $link = null) {
pr(array_merge(array('function' => 'Account::findLedgerRelatedEntries',
'checkpoint' => 'begin'),
compact('id', 'ledgers', 'link')));
$this->Behaviors->attach('Containable');
$account = $this->find('first', array
('contain' => array
('Ledger' => array('fields' => array('Ledger.id')),
),
'fields' => array('Account.type'),
'conditions' => array(array('Account.id' => $id)),
));
$this->Behaviors->detach('Containable');
$cond = array('OR' =>
array(array('debit_ledger_id' => $ledgers),
array('credit_ledger_id' => $ledgers)));
pr(array_merge(array('function' => 'Account::findLedgerRelatedEntries',
'checkpoint' => 'get-account-ledgers'),
compact('account', 'cond')));
$entries = array();
foreach($account['Ledger'] AS $ledger) {
//pr(array('find', $ledger, $account, $cond, $link));
$entries = array_merge
($entries,
$this->Ledger->findLedgerEntries($ledger['id'],
$account['Account']['type'],
$cond, $link));
}
foreach($entries AS $entry)
$this->statsMerge($entries['summary'], $entry[0]);
//$entries['summary']
pr(array_merge(array('function' => 'Account::findLedgerRelatedEntries',
'checkpoint' => 'return'),
compact('entries')));
return $entries;
}
/**************************************************************************
**************************************************************************
**************************************************************************
* function: findAccountRelatedEntries
* - Returns an array of ledger entries from this account that are
* related to the given account.
*/
function findAccountRelatedEntries($id, $relid, $link = null) {
pr(array_merge(array('function' => 'Account::findAccountRelatedEntries',
'checkpoint' => 'begin'),
compact('id', 'relid', 'link')));
$this->Behaviors->attach('Containable');
$related = $this->find('first', array
('contain' => array
('Ledger' => array('fields' => array('Ledger.id')),
),
'fields' => array(),
'conditions' => array(array('Account.id' => $relid)),
));
$this->Behaviors->detach('Containable');
$ledger_ids = array();
foreach ($related['Ledger'] AS $ledger)
array_push($ledger_ids, $ledger['id']);
pr(array_merge(array('function' => 'Account::findAccountRelatedEntries',
'checkpoint' => 'get-related'),
compact('related', 'ledger_ids')));
$entries = $this->findLedgerRelatedEntries($id, $ledger_ids, $link);
pr(array_merge(array('function' => 'Account::findAccountRelatedEntries',
'checkpoint' => 'return'),
compact('entries')));
return $entries;
//return $this->findLedgerRelatedEntries($id, $ledger_ids, $link);
}
/* /\************************************************************************** */
/* ************************************************************************** */
/* ************************************************************************** */
/* * function: findSecurityDeposits */
/* * - Returns an array of security deposit entries */
/* *\/ */
/* function findSecurityDeposits($id, $link = null) { */
/* pr(array_merge(array('function' => 'Account::findSecurityDeposits', */
/* 'checkpoint' => 'begin'), */
/* compact('id', 'link'))); */
/* $sd_account = $this->find('first', array */
/* ('recursive' => -1, */
/* 'conditions' => array(array('name' => 'Security Deposit')), */
/* )); */
/* pr(array_merge(array('function' => 'Account::findSecurityDeposits', */
/* 'checkpoint' => 'get-security-deposit-account'), */
/* compact('sd_account'))); */
/* $deposits = $this->findAccountRelatedEntries($id, $sd_account['Account']['id'], $link); */
/* pr(array_merge(array('function' => 'Account::findSecurityDeposits', */
/* 'checkpoint' => 'return'), */
/* compact('deposits'))); */
/* return $deposits; */
/* //return $this->findAccountRelatedEntries($id, $sd_account['Account']['id'], $link); */
/* } */
/**************************************************************************
**************************************************************************
**************************************************************************
* function: findSecurityDeposits
* - Returns an array of security deposit entries
*/
function zzfindSecurityDeposits($id, $cond = array(), $link = null) {
$this->Behaviors->attach('Containable');
$account = $this->find('first', array
('contain' => array
('Ledger' => array('fields' => array('Ledger.id')),
),
'fields' => array('Account.type'),
'conditions' => array(array('Account.id' => $id)),
));
$sd_account = $this->find('first', array
('contain' => array
('Ledger' => array('fields' => array('Ledger.id')),
),
'conditions' => array(array('name' => 'Security Deposit')),
));
$this->Behaviors->detach('Containable');
pr(array('sd_account', $sd_account));
$sd_ledger_ids = array();
foreach ($sd_account['Ledger'] AS $ledger)
array_push($sd_ledger_ids, $ledger['id']);
pr(array('sd_ledger_ids', $sd_ledger_ids));
array_push($cond,
array('OR' =>
array(array('debit_ledger_id' => $sd_ledger_ids),
array('credit_ledger_id' => $sd_ledger_ids))));
$sd_entries = array();
foreach($account['Ledger'] AS $ledger) {
pr(array('find', $ledger, $account, $cond, $link));
$add = $this->Ledger->findLedgerEntries($ledger['id'],
$account['Account']['type'],
$cond, $link);
$sd_entries = array_merge($sd_entries, $add);
}
pr(array('sd_entries', $sd_entries));
//return $this->Ledger->findLedgerEntries($ledger_id, $account_type, $cond, $link);
}
/**************************************************************************
**************************************************************************
**************************************************************************
@@ -411,24 +207,33 @@ class Account extends AppModel {
return null;
// All old, closed ledgers MUST balance to 0.
// However, the user may want the ENTIRE running totals.
// However, the user may want the ENTIRE running totals,
// (not just the balance), so we may have to query all
// ledgers, as dictated by the $all parameter.
$this->Behaviors->attach('Containable');
$account = $this->find('first',
array('contain' =>
($all
? array('Ledger' => array('fields' => array('id')))
: array('CurrentLedger' => array('fields' => array('id')))
? array('Ledger' => array
('fields' => array('id')))
: array('CurrentLedger' => array
('fields' => array('id')))
),
'conditions' => array(array('Account.id' => $id))));
'conditions' => array
(array('Account.id' => $id))
));
$this->Behaviors->detach('Containable');
if ($all)
$ledgers = $account['Ledger'];
else
$ledgers = array($account['CurrentLedger']);
foreach ($ledgers AS $ledger) {
$this->statsMerge($stats['Ledger'], $this->Ledger->stats($ledger['id'], $cond));
$stats = array();
if ($all) {
foreach ($account['Ledger'] AS $ledger)
$this->statsMerge($stats['Ledger'],
$this->Ledger->stats($ledger['id'], $cond));
}
else {
$stats['Ledger'] =
$this->Ledger->stats($account['CurrentLedger']['id'], $cond);
}
return $stats;

View File

@@ -25,77 +25,6 @@ class Customer extends AppModel {
);
/**************************************************************************
**************************************************************************
**************************************************************************
* function: findAccountEntries
* - Returns an array of ledger entries that belong to the current
* ledger of the account for the given customer. There is extra work
* done... see the LedgerEntry model.
*/
/* function findAccountEntries($id, $date = null, $link = null) { */
/* $result = $this->find('first', array */
/* ('recursive' => -1, */
/* 'fields' => array('account_id'), */
/* 'conditions' => array(array('id' => $id)), */
/* )); */
/* return $this->Account->findCurrentLedgerEntries($result['account_id'], */
/* $date, $link); */
/* } */
/**************************************************************************
**************************************************************************
**************************************************************************
* function: findSecurityDeposits
* - Returns an array of security deposit entries
*/
function zfindSecurityDeposits($id, $link = null) {
pr(array('function' => 'Customer::findSecurityDeposits',
'args' => compact('id', 'link'),
));
$this->Behaviors->attach('Containable');
$customer = $this->find('first',
array('contain' =>
array('Lease' => array
('fields' => array('Lease.account_id')),
),
'fields' => array('Customer.account_id'),
'conditions' => array(array('Customer.id' => $id))));
$this->Behaviors->detach('Containable');
$account_ids = array($customer['Customer']['account_id']);
foreach ($customer['Lease'] AS $lease)
array_push($account_ids, $lease['account_id']);
$acct = new Account();
$entries = $acct->findLedgerEntriesRelatedToAccount
($acct->securityDepositAccountID(),
$account_ids,
//$acct->rentAccountID(),
//6,
//array_merge(array(1), $account_ids),
true, null, $link);
// OK, we cheated by finding the entries of the security deposit account,
// and not by finding the security deposit entries of the customer
// account(s). Therefore, we have to invert the credit/debit business.
$entries['summary']['debits'] = $entries['summary']['credit'];
$entries['summary']['credits'] = $entries['summary']['debit'];
unset($entries['summary']['credit']);
unset($entries['summary']['debit']);
pr(array('function' => 'Customer::findSecurityDeposits',
'args' => compact('id', 'link'),
'vars' => compact('customer', 'account_ids'),
'return' => compact('entries'),
));
return $entries;
}
/**************************************************************************
**************************************************************************
**************************************************************************
@@ -103,9 +32,9 @@ class Customer extends AppModel {
* - Returns an array of security deposit entries
*/
function findSecurityDeposits($id, $link = null) {
/* pr(array_merge(array('function' => 'Customer::findSecurityDeposits', */
/* 'checkpoint' => 'begin'), */
/* compact('id', 'link'))); */
/* pr(array('function' => 'Customer::findSecurityDeposits', */
/* 'args' => compact('id', 'link'), */
/* )); */
$this->Behaviors->attach('Containable');
$customer = $this->find('first',
@@ -122,16 +51,16 @@ class Customer extends AppModel {
foreach ($customer['Lease'] AS $lease) {
$ledger_entries = $this->Lease->findSecurityDeposits($lease['id'], $link);
//$this->statsMerge($ledger_entries['summary'], $entries['summary']);
//unset($entries['summary']);
$this->statsMerge($entries['summary'], $ledger_entries['summary']);
$entries['Entries'] = array_merge($entries['Entries'], $ledger_entries['Entries']);
$entries['Entries'] = array_merge($entries['Entries'],
$ledger_entries['Entries']);
}
/* pr(array('function' => 'Customer::findSecurityDeposits', */
/* 'args' => compact('id', 'link'), */
/* 'vars' => compact('customer'), */
/* 'return' => compact('entries'))); */
/* 'return' => compact('entries'), */
/* )); */
return $entries;
}
@@ -148,48 +77,28 @@ class Customer extends AppModel {
if (!$id)
return null;
// Get the basic information necessary
$this->Behaviors->attach('Containable');
$customer = $this->find('first',
array('contain' =>
array('Account' => array('fields' => array('Account.id')),
'Lease' => array('fields' => array('Lease.id'))
array('Account' => array
('fields' => array('Account.id')),
'Lease' => array
('fields' => array('Lease.id'))
),
/* array('Account' => array */
/* ('fields' => array('id'), */
/* /\* 'CurrentLedger' => array *\/ */
/* /\* ('fields => *\/ */
/* /\* 'Lease' => array('fields' => array('id') *\/ */
/* ), */
/* ), */
'conditions' => array(array('Customer.id' => $id))));
'conditions' => array
(array('Customer.id' => $id))));
$this->Behaviors->detach('Containable');
// Get stats from the customer account, and each lease
$stats['Account'] = $this->Account->stats($customer['Account']['id']);
foreach ($customer['Lease'] AS $lease) {
$this->statsMerge($stats['Lease'], $this->Lease->stats($lease['id']));
}
/* 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']; */
/* } */
/* } */
/* } */
/* if ($entry['DebitLedger']['Account']['name'] === 'Security Deposit') */
/* $outstanding_deposit -= $entry['amount']; */
/* if ($entry['CreditLedger']['Account']['name'] === 'Security Deposit') */
/* $outstanding_deposit += $entry['amount']; */
// Merge the stats from both the customer specific account, as
// well as the lease. This will provide current customer standing.
// well as the leases. This will provide current customer standing.
$this->statsMerge($stats, $stats['Account']['Ledger']);
$this->statsMerge($stats, $stats['Lease']['Account']['Ledger']);

View File

@@ -77,10 +77,6 @@ class Lease extends AppModel {
'conditions' => array(array('id' => $id)),
));
/* $sd_account_id = $this->Account->securityDepositAccountID(); */
/* $sd_ledger_ids = $this->Account->ledgers($sd_account_id); */
/* $cond = conditionEntryAsCreditOrDebit($sd_ledger_ids); */
$entries = $this->Account->findLedgerEntriesRelatedToAccount
($lease['Lease']['account_id'],
$this->Account->securityDepositAccountID(),
@@ -95,63 +91,6 @@ class Lease extends AppModel {
}
/**************************************************************************
**************************************************************************
**************************************************************************
* function: findSecurityDeposits
* - Returns an array of security deposit entries
*/
function findAccountDeposits($id, $link = null) {
pr(array_merge(array('function' => 'Lease::findSecurityDeposits',
'checkpoint' => 'begin'),
compact('id', 'link')));
$lease = $this->find('first', array
('recursive' => -1,
'fields' => array('account_id'),
'conditions' => array(array('id' => $id)),
));
pr(array_merge(array('function' => 'Lease::findSecurityDeposits',
'checkpoint' => 'get-lease'),
compact('lease')));
$deposits = $this->Account->findSecurityDeposits($lease['Lease']['account_id'], $link);
pr(array_merge(array('function' => 'Lease::findSecurityDeposits',
'checkpoint' => 'return'),
compact('deposits')));
return $deposits;
//return $this->Account->findSecurityDeposits($lease['Lease']['account_id'], $link);
}
/**************************************************************************
**************************************************************************
**************************************************************************
* function: findSecurityDeposits
* - Returns an array of security deposit entries
*/
function qqfindSecurityDeposits($id, $link = null) {
pr(array_merge(array('function' => 'Lease::findSecurityDeposits',
'checkpoint' => 'begin'),
compact('id', 'link')));
$lease = $this->find('first', array
('recursive' => -1,
'fields' => array('account_id'),
'conditions' => array(array('id' => $id)),
));
pr(array_merge(array('function' => 'Lease::findSecurityDeposits',
'checkpoint' => 'get-lease'),
compact('lease')));
return $this->Account->findAccountRelatedEntries($id, $relaccount, $link);
$deposits = $this->Account->findSecurityDeposits($lease['Lease']['account_id'], $link);
pr(array_merge(array('function' => 'Lease::findSecurityDeposits',
'checkpoint' => 'return'),
compact('deposits')));
return $deposits;
//return $this->Account->findSecurityDeposits($lease['Lease']['account_id'], $link);
}
/**************************************************************************
**************************************************************************
**************************************************************************

View File

@@ -15,18 +15,21 @@ class Ledger extends AppModel {
'LedgerEntry' => array(
'className' => 'LedgerEntry',
'foreignKey' => false,
// conditions will be used when JOINing tables
// (such as find with LinkableBehavior)
'conditions' => array('OR' =>
array('LedgerEntry.debit_ledger_id = Ledger.id',
'LedgerEntry.credit_ledger_id = Ledger.id')),
// finderQuery will be used when tables are put
// together across several querys, not with JOIN.
// (such as find with ContainableBehavior)
// finderQuery will be used when tables are put
// together across several querys, not with JOIN.
// (such as find with ContainableBehavior)
'finderQuery' => 'SELECT `LedgerEntry`.*
FROM pmgr_ledger_entries AS `LedgerEntry`
WHERE LedgerEntry.debit_ledger_id = ({$__cakeID__$})
OR LedgerEntry.credit_ledger_id = ({$__cakeID__$})',
'counterQuery' => ''
),
'DebitLedgerEntry' => array(
@@ -71,7 +74,7 @@ class Ledger extends AppModel {
// If the requested entries are limited by date, we must calculate
// a balance forward, or the resulting balance will be thrown off.
//
// REVISIT <AP>: This obviously is more general than date.
// As such, it will not work (or, only work if the
// condition only manages to exclude the first parts
@@ -105,10 +108,6 @@ class Ledger extends AppModel {
/* 'return' => compact('entries'), */
/* )); */
return $entries;
// Return entries from the ledger, along with our balance forward.
/* return array_merge($bf, */
/* $this->LedgerEntry->findInLedgerContext($id, $account_type, $cond, $link)); */
}
@@ -133,14 +132,14 @@ class Ledger extends AppModel {
),
'fields' =>
array("SUM(IF(LedgerEntry.debit_ledger_id = Ledger.id,
LedgerEntry.amount, NULL)) AS debits",
LedgerEntry.amount, NULL)) AS debits",
"SUM(IF(LedgerEntry.credit_ledger_id = Ledger.id,
LedgerEntry.amount, NULL)) AS credits",
LedgerEntry.amount, NULL)) AS credits",
"SUM(IF(Account.type IN ('ASSET', 'EXPENSE'),
IF(LedgerEntry.debit_ledger_id = Ledger.id, 1, -1),
IF(LedgerEntry.credit_ledger_id = Ledger.id, 1, -1)
) * IF(LedgerEntry.amount, LedgerEntry.amount, 0)
) AS balance",
IF(LedgerEntry.debit_ledger_id = Ledger.id, 1, -1),
IF(LedgerEntry.credit_ledger_id = Ledger.id, 1, -1)
) * IF(LedgerEntry.amount, LedgerEntry.amount, 0)
) AS balance",
"COUNT(LedgerEntry.id) AS entries"),
'conditions' => array(isset($cond) ? $cond : array(),
array('Ledger.id' => $id)),

View File

@@ -26,14 +26,16 @@ class LedgerEntry extends AppModel {
/**************************************************************************
**************************************************************************
**************************************************************************
* function:
* -
* function: conditionEntryAsCreditOrDebit
* - returns the condition necessary to match a set of
* Ledgers to all related LedgerEntries
*/
function conditionEntryAsCreditOrDebit($ledger_ids) {
return array('OR' =>
array(array('debit_ledger_id' => $ledger_ids),
array('credit_ledger_id' => $ledger_ids)));
}
/**************************************************************************
**************************************************************************
@@ -78,18 +80,6 @@ class LedgerEntry extends AppModel {
array('Transaction.stamp'),
));
/* $entries['summary'] = array('balance' => null, 'debit' => null, 'credit' => null); */
/* foreach($entries AS $entry) */
/* $this->statsMerge($entries['summary'], $entry[0]); */
/* //if (isset($entries['summary']['debit']) || isset($entries['summary']['credit'])) { */
/* $entries['summary']['debits'] = $entries['summary']['debit']; */
/* $entries['summary']['credits'] = $entries['summary']['credit']; */
/* unset($entries['summary']['debit']); */
/* unset($entries['summary']['credit']); */
/* //} */
return $entries;
}
}

View File

@@ -19,17 +19,12 @@ class Unit extends AppModel {
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,
)
'Lease',
);
function statusEnums() {
@@ -74,20 +69,30 @@ class Unit extends AppModel {
if (!$id)
return null;
// Get the basic information necessary
$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))));
('Lease' => array
('fields' => array('Lease.id')),
'CurrentLease' => array
('fields' => array('CurrentLease.id'))),
'conditions' => array
(array('Unit.id' => $id)),
));
$this->Behaviors->detach('Containable');
// Get the stats for the current lease
$stats['CurrentLease'] = $this->Lease->stats($unit['CurrentLease']['id']);
// Sum the stats for all leases together
foreach ($unit['Lease'] AS $lease) {
$this->statsMerge($stats['Lease'], $this->Lease->stats($lease['id']));
}
// Return the collection
return $stats;
}