Significant changes to work with the new account/ledger structure. Removed much of the auto-generated model association code. Added helper functions into the models to perform model related work, such as model 'stats' (a bad name for a function to return a summary of pertinent financial information from a given model instance). There is a ton of cleanup to do, but first I want to get it all captured.

git-svn-id: file:///svn-source/pmgr/branches/ledger_transactions_20090605@81 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
abijah
2009-06-10 02:41:23 +00:00
parent 4988d2717f
commit 9f8d4fa9b2
41 changed files with 1441 additions and 916 deletions

View File

@@ -11,34 +11,428 @@ class Account extends AppModel {
var $hasOne = array(
'CurrentLedger' => array(
'className' => 'Ledger',
'foreignKey' => 'account_id',
'dependent' => false,
'conditions' => array(array('CurrentLedger.closed' => 0)),
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
//'foreignKey' => 'account_id',
'conditions' => array('NOT' => array('CurrentLedger.closed'))
),
);
var $hasMany = array(
'Ledger' => array(
'className' => 'Ledger',
'foreignKey' => 'account_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
'Ledger',
);
var $cache;
/* var $instantiated; */
/* /\************************************************************************** */
/* ************************************************************************** */
/* ************************************************************************** */
/* * function: constructor */
/* *\/ */
/* function __constructor($id = null) { */
/* parent::__contstructor(); */
/* $this->id = $id; */
/* $this->instantiated = true; */
/* } */
/**************************************************************************
**************************************************************************
**************************************************************************
* function: securityDepositAccountID
* - Returns the ID of the Security Deposit Account
*/
function securityDepositAccountID() {
$account = $this->find('first', array
('recursive' => -1,
'conditions' => array(array('name' => 'Security Deposit')),
));
return $account['Account']['id'];
}
/**************************************************************************
**************************************************************************
**************************************************************************
* function:rentAccountID
* - Returns the ID of the Rent Account
*/
function rentAccountID() {
$account = $this->find('first', array
('recursive' => -1,
'conditions' => array(array('name' => 'Rent')),
));
return $account['Account']['id'];
}
/**************************************************************************
**************************************************************************
**************************************************************************
* function: type
* - Returns the type of this array of ledger ids from the given account
*/
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'];
}
$account = $this->find('first', array
('recursive' => -1,
'fields' => array('type'),
'conditions' => array(array('Account.id' => $id)),
));
$this->cache[$id]['type'] = $account['Account']['type'];
/* pr(array('function' => 'Account::type', */
/* 'args' => compact('id'), */
/* 'return' => $this->cache[$id]['type'])); */
return $this->cache[$id]['type'];
}
/**************************************************************************
**************************************************************************
**************************************************************************
* function: ledgers
* - Returns an array of ledger ids from the given account
*/
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];
}
if ($all) {
$contain = array('Ledger' => array('fields' => array('Ledger.id')));
} else {
$contain = array('CurrentLedger' => array('fields' => array('CurrentLedger.id')));
}
$this->Behaviors->attach('Containable');
$account = $this->find('first', array
('contain' => $contain,
'fields' => array(),
'conditions' => array(array('Account.id' => $id)),
));
$this->Behaviors->detach('Containable');
if ($all) {
$ledger_ids = array();
foreach ($account['Ledger'] AS $ledger)
array_push($ledger_ids, $ledger['id']);
}
else {
$ledger_ids = array($account['CurrentLedger']['id']);
}
$this->cache[$id]['ledgers'][$cachekey] = $ledger_ids;
/* pr(array('function' => 'Account::ledgers', */
/* 'args' => compact('id', 'all'), */
/* 'return' => $this->cache[$id]['ledgers'][$cachekey])); */
return $this->cache[$id]['ledgers'][$cachekey];
}
/**************************************************************************
**************************************************************************
**************************************************************************
* function: findLedgerEntries
* - Returns an array of ledger entries that belong to the given
* account, either just from the current ledger, or from all ledgers.
*/
function findLedgerEntries($id, $all = false, $cond = null, $link = null) {
/* pr(array('function' => 'Account::findLedgerEntries', */
/* 'args' => compact('id', 'all', 'cond', 'link'), */
/* )); */
$entries = array();
foreach ($this->ledgers($id, $all) AS $ledger_id) {
$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']);
}
/* pr(array('function' => 'Account::findLedgerEntries', */
/* 'args' => compact('id', 'all', 'cond', 'link'), */
/* 'return' => compact('entries'), */
/* )); */
$stats = $this->stats($id, $all, $cond);
return array('Entries' => $entries, 'summary' => $stats['Ledger']);
}
/**************************************************************************
**************************************************************************
**************************************************************************
* function: findLedgerEntriesRelatedToAccount
* - Returns an array of ledger entries that belong to the given
* account, and are related to a specific account, either just from
* the current ledger, or from all ledgers.
*/
function findLedgerEntriesRelatedToAccount($id, $rel_ids, $all = false, $cond = null, $link = null) {
/* 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);
$ledger_ids = array();
foreach ($rel_ids AS $rel_id)
$ledger_ids = array_merge($ledger_ids, $this->ledgers($rel_id));
array_push($cond, $this->Ledger->LedgerEntry->conditionEntryAsCreditOrDebit($ledger_ids));
$entries = $this->findLedgerEntries($id, $all, $cond, $link);
/* pr(array('function' => 'Account::findLedgerEntriesRelatedToAccount', */
/* 'args' => compact('id', 'relid', 'all', 'cond', 'link'), */
/* 'vars' => compact('ledger_ids'), */
/* 'return' => compact('entries'), */
/* )); */
return $entries;
}
/**************************************************************************
**************************************************************************
**************************************************************************
* 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);
}
/**************************************************************************
**************************************************************************
**************************************************************************
* function: stats
* - Returns summary data from the requested account.
*/
function stats($id = null, $all = false, $cond = null) {
if (!$id)
return null;
// All old, closed ledgers MUST balance to 0.
// However, the user may want the ENTIRE running totals.
$this->Behaviors->attach('Containable');
$account = $this->find('first',
array('contain' =>
($all
? array('Ledger' => array('fields' => array('id')))
: array('CurrentLedger' => array('fields' => array('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));
}
return $stats;
}
}
?>

View File

@@ -10,21 +10,7 @@ class Contact extends AppModel {
);
var $hasAndBelongsToMany = array(
'Customer' => array(
'className' => 'Customer',
'joinTable' => 'contacts_customers',
'foreignKey' => 'contact_id',
'associationForeignKey' => 'customer_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
),
'Customer',
'ContactAddress' => array(
'className' => 'ContactAddress',
'joinTable' => 'contacts_methods',
@@ -32,13 +18,6 @@ class Contact extends AppModel {
'associationForeignKey' => 'method_id',
'unique' => true,
'conditions' => "method = 'POST'",
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
),
'ContactPhone' => array(
'className' => 'ContactPhone',
@@ -47,13 +26,6 @@ class Contact extends AppModel {
'associationForeignKey' => 'method_id',
'unique' => true,
'conditions' => "method = 'PHONE'",
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
),
'ContactEmail' => array(
'className' => 'ContactEmail',
@@ -62,13 +34,6 @@ class Contact extends AppModel {
'associationForeignKey' => 'method_id',
'unique' => true,
'conditions' => "method = 'EMAIL'",
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
),
);

View File

@@ -15,13 +15,6 @@ class ContactAddress extends AppModel {
'associationForeignKey' => 'contact_id',
'unique' => true,
'conditions' => "method = 'POST'",
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)
);
}

View File

@@ -15,13 +15,6 @@ class ContactEmail extends AppModel {
'associationForeignKey' => 'contact_id',
'unique' => true,
'conditions' => "method = 'EMAIL'",
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)
);

View File

@@ -17,13 +17,6 @@ class ContactPhone extends AppModel {
'associationForeignKey' => 'contact_id',
'unique' => true,
'conditions' => "method = 'PHONE'",
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)
);

View File

@@ -7,52 +7,194 @@ class Customer extends AppModel {
'name' => array('notempty'),
);
var $belongsTo = array(
'Account',
);
var $hasMany = array(
'Lease' => array(
'CurrentLease' => array(
'className' => 'Lease',
'foreignKey' => 'customer_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
'conditions' => 'CurrentLease.close_date IS NULL',
),
'Transaction' => array(
'className' => 'Transaction',
'foreignKey' => 'customer_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
'Lease',
'Transaction',
);
var $hasAndBelongsToMany = array(
'Contact' => array(
'className' => 'Contact',
'joinTable' => 'contacts_customers',
'foreignKey' => 'customer_id',
'associationForeignKey' => 'contact_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
),
'Contact',
);
/**************************************************************************
**************************************************************************
**************************************************************************
* 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;
}
/**************************************************************************
**************************************************************************
**************************************************************************
* function: findSecurityDeposits
* - Returns an array of security deposit entries
*/
function findSecurityDeposits($id, $link = null) {
/* pr(array_merge(array('function' => 'Customer::findSecurityDeposits', */
/* 'checkpoint' => 'begin'), */
/* compact('id', 'link'))); */
$this->Behaviors->attach('Containable');
$customer = $this->find('first',
array('contain' =>
array('Lease' => array('fields' => array('id'))),
'fields' => array('account_id'),
'conditions' => array(array('Customer.id' => $id))));
$this->Behaviors->detach('Containable');
$entries = $this->Account->findLedgerEntriesRelatedToAccount
($customer['Customer']['account_id'],
$this->Account->securityDepositAccountID(),
true, null, $link);
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']);
}
/* pr(array('function' => 'Customer::findSecurityDeposits', */
/* 'args' => compact('id', 'link'), */
/* 'vars' => compact('customer'), */
/* 'return' => compact('entries'))); */
return $entries;
}
/**************************************************************************
**************************************************************************
**************************************************************************
* function: stats
* - Returns summary data from the requested customer.
*/
function stats($id = null) {
if (!$id)
return null;
$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('id'), */
/* /\* 'CurrentLedger' => array *\/ */
/* /\* ('fields => *\/ */
/* /\* 'Lease' => array('fields' => array('id') *\/ */
/* ), */
/* ), */
'conditions' => array(array('Customer.id' => $id))));
$this->Behaviors->detach('Containable');
$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.
$this->statsMerge($stats, $stats['Account']['Ledger']);
$this->statsMerge($stats, $stats['Lease']['Account']['Ledger']);
return $stats;
}
}
?>

View File

@@ -23,36 +23,155 @@ class Lease extends AppModel {
);
var $belongsTo = array(
'LeaseType' => array(
'className' => 'LeaseType',
'foreignKey' => 'lease_type_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Unit' => array(
'className' => 'Unit',
'foreignKey' => 'unit_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'LateSchedule' => array(
'className' => 'LateSchedule',
'foreignKey' => 'late_schedule_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Customer' => array(
'className' => 'Customer',
'foreignKey' => 'customer_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => ''
),
'LeaseType',
'Unit',
'Account',
'Customer',
'LateSchedule',
);
/**************************************************************************
**************************************************************************
**************************************************************************
* 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'), */
/* )); */
$lease = $this->find('first', array
('recursive' => -1,
'fields' => array('account_id'),
'conditions' => array(array('id' => $id)),
));
$entries = $this->Account->findLedgerEntries($lease['Lease']['account_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'), */
/* )); */
$lease = $this->find('first', array
('recursive' => -1,
'fields' => array('account_id'),
'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(),
true, null, $link);
/* pr(array('function' => 'Lease::findSecurityDeposits', */
/* 'args' => compact('id', 'link'), */
/* 'vars' => compact('lease'), */
/* 'return' => compact('entries'), */
/* )); */
return $entries;
}
/**************************************************************************
**************************************************************************
**************************************************************************
* 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);
}
/**************************************************************************
**************************************************************************
**************************************************************************
* function: stats
* - Returns summary data from the requested lease.
*/
function stats($id = null) {
if (!$id)
return null;
// Find the associated account.
$lease = $this->find('first',
array('recursive' => -1,
'conditions' => array(array('Lease.id' => $id))));
// Pull the stats from the account.
$stats['Account'] = $this->Account->stats($lease['Lease']['account_id']);
return $stats;
}
}
?>

View File

@@ -7,21 +7,8 @@ class LeaseType extends AppModel {
'name' => array('notempty')
);
//The Associations below have been created with all possible keys, those that are not needed can be removed
var $hasMany = array(
'Lease' => array(
'className' => 'Lease',
'foreignKey' => 'lease_type_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
'Lease',
);
}

View File

@@ -1,65 +1,156 @@
<?php
class Ledger extends AppModel {
var $name = 'Ledger';
var $validate = array(
'id' => array('numeric'),
'name' => array('notempty'),
);
var $name = 'Ledger';
var $validate = array(
'id' => array('numeric'),
'name' => array('notempty'),
);
var $belongsTo = array(
'Account' => array(
'className' => 'Account',
'foreignKey' => 'account_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
);
var $belongsTo = array(
'Account',
);
var $hasMany = array(
/* 'LedgerEntry' => array( */
/* 'className' => 'LedgerEntry', */
/* 'foreignKey' => false, */
/* 'dependent' => false, */
/* 'conditions' => '', */
/* 'fields' => '', */
/* 'order' => '', */
/* 'limit' => '', */
/* 'offset' => '', */
/* 'exclusive' => '', */
/* 'finderQuery' => 'SELECT `Entry`.* FROM pmgr_entries AS `Entry` */
/* WHERE Entry.debit_ledger_id = ({$__cakeID__$}) */
/* OR Entry.credit_ledger_id = ({$__cakeID__$})', */
/* 'counterQuery' => '' */
/* ), */
'DebitLedgerEntry' => array(
'className' => 'LedgerEntry',
'foreignKey' => 'debit_ledger_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
'CreditLedgerEntry' => array(
'className' => 'LedgerEntry',
'foreignKey' => 'credit_ledger_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
);
var $hasMany = array(
'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' => 'SELECT `LedgerEntry`.*
FROM pmgr_ledger_entries AS `LedgerEntry`
WHERE LedgerEntry.debit_ledger_id = ({$__cakeID__$})
OR LedgerEntry.credit_ledger_id = ({$__cakeID__$})',
'counterQuery' => ''
),
'DebitLedgerEntry' => array(
'className' => 'LedgerEntry',
'foreignKey' => 'debit_ledger_id',
'dependent' => false,
),
'CreditLedgerEntry' => array(
'className' => 'LedgerEntry',
'foreignKey' => 'credit_ledger_id',
'dependent' => false,
),
);
/**************************************************************************
**************************************************************************
**************************************************************************
* function: findLedgerEntries
* - Returns an array of ledger entries that belong to a given
* ledger. There is extra work done... see the LedgerEntry model.
*/
function findLedgerEntries($id, $account_type = null, $cond = null, $link = null) {
/* pr(array('function' => 'Ledger::findLedgerEntries', */
/* 'args' => compact('id', 'account_type', 'cond', 'link'), */
/* )); */
if (!isset($account_type)) {
$this->Behaviors->attach('Containable');
$ledger = $this->find('first', array
('contain' => array
('Account' => array
('fields' => array('type'),
),
),
'fields' => array(),
'conditions' => array(array('Ledger.id' => $id)),
));
$this->Behaviors->detach('Containable');
$account_type = $ledger['Account']['type'];
}
// 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
// of the ledger, nothing in the middle or at the
// end. For now, I'll just create an 'other' entry,
// not necessarily a balance forward.
$bf = array();
if (0 && isset($cond)) {
//$date = '<NOT IMPLEMENTED>';
$stats = $this->stats($id, array('NOT' => array($cond)));
$bf = array(array(array('debit' => $stats['debits'],
'credit' => $stats['credits'],
'balance' => $stats['balance']),
'LedgerEntry' => array('id' => null,
//'comment' => "Balance Forward from $date"),
'comment' => "-- SUMMARY OF EXCLUDED ENTRIES --"),
'Transaction' => array('id' => null,
//'stamp' => $date,
'stamp' => null,
'comment' => null),
));
}
$entries = $this->LedgerEntry->findInLedgerContext($id, $account_type, $cond, $link);
/* pr(array('function' => 'Ledger::findLedgerEntries', */
/* 'args' => compact('id', 'account_type', 'cond', 'link'), */
/* 'vars' => compact('ledger'), */
/* '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)); */
}
/**************************************************************************
**************************************************************************
**************************************************************************
* function: stats
* - Returns summary data from the requested ledger.
*/
function stats($id, $cond = null) {
$stats = $this->find
('first', array
('link' =>
array(// Models
'Account' => array('fields' => array()),
//'LedgerEntry' => array('fields' => array()),
'LedgerEntry' =>
array('fields' => array(),
'Transaction' => array('fields' => array('stamp')),
),
),
'fields' =>
array("SUM(IF(LedgerEntry.debit_ledger_id = Ledger.id,
LedgerEntry.amount, NULL)) AS debits",
"SUM(IF(LedgerEntry.credit_ledger_id = Ledger.id,
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",
"COUNT(LedgerEntry.id) AS entries"),
'conditions' => array(isset($cond) ? $cond : array(),
array('Ledger.id' => $id)),
'group' => 'Ledger.id',
));
// The fields are all tucked into the [0] index,
// and the rest of the array is useless (empty).
return $stats[0];
}
}
?>

View File

@@ -9,35 +9,89 @@ class LedgerEntry extends AppModel {
);
var $belongsTo = array(
'MonetarySource' => array(
'className' => 'MonetarySource',
'foreignKey' => 'monetary_source_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Transaction' => array(
'className' => 'Transaction',
'foreignKey' => 'transaction_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'MonetarySource',
'Transaction',
'DebitLedger' => array(
'className' => 'Ledger',
'foreignKey' => 'debit_ledger_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'CreditLedger' => array(
'className' => 'Ledger',
'foreignKey' => 'credit_ledger_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
);
/**************************************************************************
**************************************************************************
**************************************************************************
* function:
* -
*/
function conditionEntryAsCreditOrDebit($ledger_ids) {
return array('OR' =>
array(array('debit_ledger_id' => $ledger_ids),
array('credit_ledger_id' => $ledger_ids)));
}
/**************************************************************************
**************************************************************************
**************************************************************************
* function: findInLedgerContext
* - Returns an array of ledger entries that belong to a given ledger.
* There is extra logic to also figure out whether the ledger_entry
* amount is either a credit, or a debit, depending on how it was
* written into the ledger, as well as whether the amount increases or
* decreases the balance depending on the particular account type of
* the ledger.
*/
function findInLedgerContext($ledger_id, $account_type, $cond = null, $link = null) {
if (!isset($link))
$link = array('Transaction');
if (in_array($account_type, array('ASSET', 'EXPENSE')))
$ledger_type = 'debit';
else
$ledger_type = 'credit';
$entries = $this->find
('all',
array('link' => $link,
'fields' =>
array('id', 'name', 'comment',
"IF(LedgerEntry.debit_ledger_id = $ledger_id," .
" LedgerEntry.amount, NULL) AS debit",
"IF(LedgerEntry.credit_ledger_id = $ledger_id," .
" LedgerEntry.amount, NULL) AS credit",
"(IF(LedgerEntry.{$ledger_type}_ledger_id = $ledger_id, 1, -1)" .
" * LedgerEntry.amount) AS balance"),
'conditions' =>
array(isset($cond) ? $cond : array(),
'OR' =>
array(array('LedgerEntry.debit_ledger_id' => $ledger_id),
array('LedgerEntry.credit_ledger_id' => $ledger_id))),
'order' =>
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

@@ -11,33 +11,12 @@ class Map extends AppModel {
'depth' => array('numeric')
);
//The Associations below have been created with all possible keys, those that are not needed can be removed
var $belongsTo = array(
'SiteArea' => array(
'className' => 'SiteArea',
'foreignKey' => 'site_area_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
'SiteArea',
);
var $hasAndBelongsToMany = array(
'Unit' => array(
'className' => 'Unit',
'joinTable' => 'maps_units',
'foreignKey' => 'map_id',
'associationForeignKey' => 'unit_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)
'Unit',
);
}

View File

@@ -9,19 +9,7 @@ class MonetarySource extends AppModel {
);
var $belongsTo = array(
'MonetaryType' => array(
'className' => 'MonetaryType',
'foreignKey' => 'monetary_type_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
'MonetaryType',
);
}

View File

@@ -9,19 +9,7 @@ class MonetaryType extends AppModel {
);
var $hasMany = array(
'MonetarySource' => array(
'className' => 'MonetarySource',
'foreignKey' => 'monetary_type_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
'MonetarySource',
);
}

View File

@@ -7,34 +7,9 @@ class Site extends AppModel {
'name' => array('notempty')
);
//The Associations below have been created with all possible keys, those that are not needed can be removed
var $hasMany = array(
'SiteArea' => array(
'className' => 'SiteArea',
'foreignKey' => 'site_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
'SiteOption' => array(
'className' => 'SiteOption',
'foreignKey' => 'site_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
'SiteArea',
'SiteOption',
);
}

View File

@@ -8,26 +8,12 @@ class SiteArea extends AppModel {
'name' => array('notempty')
);
//The Associations below have been created with all possible keys, those that are not needed can be removed
var $belongsTo = array(
'Site' => array(
'className' => 'Site',
'foreignKey' => 'site_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
'Site',
);
var $hasOne = array(
'Map' => array(
'className' => 'Map',
'foreignKey' => 'site_area_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => ''
)
'Map',
);
}

View File

@@ -8,29 +8,11 @@ class Transaction extends AppModel {
);
var $belongsTo = array(
'Customer' => array(
'className' => 'Customer',
'foreignKey' => 'customer_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Customer',
);
var $hasMany = array(
'LedgerEntry' => array(
'className' => 'LedgerEntry',
'foreignKey' => 'transaction_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
'LedgerEntry',
);
}

View File

@@ -13,27 +13,15 @@ class Unit extends AppModel {
);
var $belongsTo = array(
'UnitSize' => array(
'className' => 'UnitSize',
'foreignKey' => 'unit_size_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'UnitSize',
);
var $hasOne = array(
'CurrentLease' => array(
'className' => 'Lease',
'foreignKey' => 'current_lease_id',
'conditions' => '',
'fields' => '',
'order' => ''
//'foreignKey' => 'unit_id',
'conditions' => 'CurrentLease.close_date IS NULL',
),
/* 'Map' => array( */
/* 'className' => 'MapsUnit', */
/* 'foreignKey' => 'unit_id', */
/* 'conditions' => '', */
/* 'fields' => '', */
/* 'order' => '' */
/* ) */
);
var $hasMany = array(
@@ -41,14 +29,6 @@ class Unit extends AppModel {
'className' => 'Lease',
'foreignKey' => 'unit_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
@@ -83,4 +63,32 @@ class Unit extends AppModel {
return ('Unit.status <= ' . $this->statusValue('UNAVAILABLE'));
}
/**************************************************************************
**************************************************************************
**************************************************************************
* function: stats
* - Returns summary data from the requested customer.
*/
function stats($id = null) {
if (!$id)
return null;
$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))));
$this->Behaviors->detach('Containable');
$stats['CurrentLease'] = $this->Lease->stats($unit['CurrentLease']['id']);
foreach ($unit['Lease'] AS $lease) {
$this->statsMerge($stats['Lease'], $this->Lease->stats($lease['id']));
}
return $stats;
}
}

View File

@@ -14,29 +14,11 @@ class UnitSize extends AppModel {
);
var $belongsTo = array(
'UnitType' => array(
'className' => 'UnitType',
'foreignKey' => 'unit_type_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
'UnitType',
);
var $hasMany = array(
'Unit' => array(
'className' => 'Unit',
'foreignKey' => 'unit_size_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
'Unit',
);
}

View File

@@ -9,19 +9,7 @@ class UnitType extends AppModel {
);
var $hasMany = array(
'UnitSize' => array(
'className' => 'UnitSize',
'foreignKey' => 'unit_type_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
'UnitSize',
);
}