git-svn-id: file:///svn-source/pmgr/branches/single_AR_20090622@181 97e9348a-65ac-dc4b-aefc-98561f571b83
358 lines
12 KiB
PHP
358 lines
12 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',
|
|
'conditions' => array('NOT' => array('CurrentLedger.closed'))
|
|
),
|
|
);
|
|
|
|
var $hasMany = array(
|
|
'Ledger',
|
|
);
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* function: type
|
|
* - Returns the type of this account
|
|
*/
|
|
function type($id) {
|
|
$this->cacheQueries = true;
|
|
$account = $this->find('first', array
|
|
('recursive' => -1,
|
|
'fields' => array('type'),
|
|
'conditions' => array(array('Account.id' => $id)),
|
|
));
|
|
$this->cacheQueries = false;
|
|
|
|
return $account['Account']['type'];
|
|
}
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* function: fundamentalType
|
|
* - Returns the fundmental type of the account, credit or debit
|
|
*/
|
|
function fundamentalType($id_or_type) {
|
|
if (is_numeric($id_or_type))
|
|
$type = $this->type($id_or_type);
|
|
else
|
|
$type = $id_or_type;
|
|
|
|
// Asset and Expense accounts are debit accounts
|
|
if (in_array(strtoupper($type), array('ASSET', 'EXPENSE')))
|
|
return 'debit';
|
|
|
|
// Otherwise, it's a credit account
|
|
return 'credit';
|
|
}
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* function: fundamentalOpposite
|
|
* - Returns the opposite fundmental type of the account, credit or debit
|
|
*/
|
|
function fundamentalOpposite($id_or_type) {
|
|
if (in_array(strtolower($id_or_type), array('credit', 'debit')))
|
|
$fund = $id_or_type;
|
|
else
|
|
$fund = $this->fundamentalType($id_or_type);
|
|
|
|
if ($fund == 'debit')
|
|
return 'credit';
|
|
|
|
return 'debit';
|
|
}
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* function: accountNameToID
|
|
* - Returns the ID of the named account
|
|
*/
|
|
function accountNameToID($name) {
|
|
$this->cacheQueries = true;
|
|
$account = $this->find('first', array
|
|
('recursive' => -1,
|
|
'conditions' => compact('name'),
|
|
));
|
|
$this->cacheQueries = false;
|
|
return $account['Account']['id'];
|
|
}
|
|
|
|
function securityDepositAccountID() { return $this->accountNameToID('Security Deposit'); }
|
|
function rentAccountID() { return $this->accountNameToID('Rent'); }
|
|
function accountReceivableAccountID() { return $this->accountNameToID('A/R'); }
|
|
function invoiceAccountID() { return $this->accountReceivableAccountID(); }
|
|
function receiptAccountID() { return $this->accountReceivableAccountID(); }
|
|
//function invoiceAccountID() { return $this->accountNameToID('Invoice'); }
|
|
//function receiptAccountID() { return $this->accountNameToID('Receipt'); }
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* function: ledgers
|
|
* - Returns an array of ledger ids from the given account
|
|
*/
|
|
function ledgers($id, $all = false) {
|
|
if ($all) {
|
|
$contain = array('Ledger' => array('fields' => array('Ledger.id')));
|
|
} else {
|
|
$contain = array('CurrentLedger' => array('fields' => array('CurrentLedger.id')));
|
|
}
|
|
|
|
$this->cacheQueries = true;
|
|
$account = $this->find('first', array
|
|
('contain' => $contain,
|
|
'fields' => array(),
|
|
'conditions' => array(array('Account.id' => $id)),
|
|
));
|
|
$this->cacheQueries = false;
|
|
|
|
if ($all) {
|
|
$ledger_ids = array();
|
|
foreach ($account['Ledger'] AS $ledger)
|
|
array_push($ledger_ids, $ledger['id']);
|
|
}
|
|
else {
|
|
$ledger_ids = array($account['CurrentLedger']['id']);
|
|
}
|
|
|
|
/* pr(array('function' => 'Account::ledgers', */
|
|
/* 'args' => compact('id', 'all'), */
|
|
/* 'return' => $ledger_ids)); */
|
|
|
|
return $ledger_ids;
|
|
}
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* 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);
|
|
}
|
|
|
|
$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'), */
|
|
/* )); */
|
|
|
|
return $entries;
|
|
}
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* 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: findUnreconciledLedgerEntries
|
|
* - Returns ledger entries that are not yet reconciled
|
|
* (such as charges not paid).
|
|
*/
|
|
|
|
function findUnreconciledLedgerEntries($id = null, $fundamental_type = null, $cond = null) {
|
|
if (!isset($cond))
|
|
$cond = array();
|
|
$cond[] = array('Account.id' => $id);
|
|
|
|
foreach (($fundamental_type
|
|
? array($fundamental_type)
|
|
: array('debit', 'credit')) AS $fund) {
|
|
$ucfund = ucfirst($fund);
|
|
$unreconciled[$fund]['entry'] = $this->find
|
|
('all', array
|
|
('link' => array
|
|
('Ledger' => array
|
|
('fields' => array(),
|
|
"LedgerEntry" => array
|
|
('class' => "{$ucfund}LedgerEntry",
|
|
'fields' => array('id', 'amount'),
|
|
"ReconciliationLedgerEntry" => array
|
|
('class' => "{$ucfund}ReconciliationLedgerEntry",
|
|
'fields' => array
|
|
("COALESCE(SUM(Reconciliation.amount),0) AS 'reconciled'",
|
|
"LedgerEntry.amount - COALESCE(SUM(Reconciliation.amount),0) AS 'balance'",
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
'group' => ("LedgerEntry.id" .
|
|
" HAVING LedgerEntry.amount" .
|
|
" <> COALESCE(SUM(Reconciliation.amount),0)"),
|
|
'conditions' => $cond,
|
|
'fields' => array(),
|
|
));
|
|
$balance = 0;
|
|
foreach ($unreconciled[$fund]['entry'] AS &$entry) {
|
|
$entry = array_merge(array_diff_key($entry["LedgerEntry"], array(0=>true)),
|
|
$entry[0]);
|
|
$balance += $entry['balance'];
|
|
}
|
|
$unreconciled[$fund]['balance'] = $balance;
|
|
}
|
|
|
|
return $unreconciled;
|
|
}
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* 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, $cond = null) {
|
|
$ofund = $this->fundamentalOpposite($fundamental_type);
|
|
$unreconciled = array($ofund => array('entry'=>array(), 'balance'=>0));
|
|
$applied = 0;
|
|
|
|
// if there is no money in the entry, it can reconcile nothing
|
|
// don't bother wasting time sifting ledger entries.
|
|
if ($amount > 0) {
|
|
$unreconciled = $this->findUnreconciledLedgerEntries($id, $ofund, $cond);
|
|
|
|
foreach ($unreconciled[$ofund]['entry'] AS $i => &$entry) {
|
|
// Determine if amount is sufficient to cover the entry
|
|
if ($amount > $entry['balance'])
|
|
$apply = $entry['balance'];
|
|
elseif ($amount > 0)
|
|
$apply = $amount;
|
|
else {
|
|
unset($unreconciled[$ofund]['entry'][$i]);
|
|
continue;
|
|
}
|
|
|
|
$entry['applied'] = $apply;
|
|
$entry['reconciled'] += $apply;
|
|
$entry['balance'] -= $apply;
|
|
$applied += $apply;
|
|
$amount -= $apply;
|
|
}
|
|
}
|
|
|
|
$unreconciled[$ofund]['unapplied'] = $amount;
|
|
$unreconciled[$ofund]['applied'] = $applied;
|
|
$unreconciled[$ofund]['balance'] -= $applied;
|
|
return $unreconciled;
|
|
}
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* 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,
|
|
// (not just the balance), so we may have to query all
|
|
// ledgers, as dictated by the $all parameter.
|
|
|
|
$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))
|
|
));
|
|
|
|
$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;
|
|
}
|
|
|
|
}
|
|
?>
|