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', ); var $cache; /************************************************************************** ************************************************************************** ************************************************************************** * 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'])) { return $this->cache[$id]['type']; } $account = $this->find('first', array ('recursive' => -1, 'fields' => array('type'), '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'])); */ 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])) { 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']); } // Save the ledgers in our cache for future reference $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); } $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: 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. $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'); $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; } } ?>