git-svn-id: file:///svn-source/pmgr/branches/ledger_transactions_20090605@81 97e9348a-65ac-dc4b-aefc-98561f571b83
438 lines
16 KiB
PHP
438 lines
16 KiB
PHP
<?php
|
|
class Account extends AppModel {
|
|
|
|
var $name = 'Account';
|
|
var $validate = array(
|
|
'id' => array('numeric'),
|
|
'name' => array('notempty'),
|
|
'external_name' => array('notempty')
|
|
);
|
|
|
|
var $hasOne = array(
|
|
'CurrentLedger' => array(
|
|
'className' => 'Ledger',
|
|
//'foreignKey' => 'account_id',
|
|
'conditions' => array('NOT' => array('CurrentLedger.closed'))
|
|
),
|
|
);
|
|
|
|
var $hasMany = array(
|
|
'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;
|
|
}
|
|
|
|
}
|
|
?>
|