100, 'link' => array(// Models 'CurrentLedger' => array (// Models 'LedgerEntry' => array('fields' => array('SUM(IF(LedgerEntry.debit_ledger_id = CurrentLedger.id, LedgerEntry.amount, NULL)) AS debits', 'SUM(IF(LedgerEntry.credit_ledger_id = CurrentLedger.id, LedgerEntry.amount, NULL)) AS credits', "SUM(IF(Account.type IN ('ASSET', 'EXPENSE'), IF(LedgerEntry.debit_ledger_id = CurrentLedger.id, 1, -1), IF(LedgerEntry.credit_ledger_id = CurrentLedger.id, 1, -1) ) * IF(LedgerEntry.amount, LedgerEntry.amount, 0) ) AS balance", 'COUNT(LedgerEntry.id) AS entries'), 'conditions' => array('OR' => array('LedgerEntry.debit_ledger_id = CurrentLedger.id', 'LedgerEntry.credit_ledger_id = CurrentLedger.id'), ), ), ), ), 'group' => 'Account.id', 'order' => array('Account.name' => 'ASC')); var $uses = array('Account', 'LedgerEntry'); var $sidemenu_links = array(array('name' => 'Accounts', 'header' => true), array('name' => 'All', 'url' => array('controller' => 'accounts', 'action' => 'all')), array('name' => 'Asset', 'url' => array('controller' => 'accounts', 'action' => 'asset')), array('name' => 'Liability', 'url' => array('controller' => 'accounts', 'action' => 'liability')), array('name' => 'Equity', 'url' => array('controller' => 'accounts', 'action' => 'equity')), array('name' => 'Income', 'url' => array('controller' => 'accounts', 'action' => 'income')), array('name' => 'Expense', 'url' => array('controller' => 'accounts', 'action' => 'expense')), ); /************************************************************************** ************************************************************************** ************************************************************************** * override: sideMenuLinks * - Generates controller specific links for the side menu */ function sideMenuLinks() { return array_merge(parent::sideMenuLinks(), $this->sidemenu_links); } /************************************************************************** ************************************************************************** ************************************************************************** * action: index * - Lists all accounts */ function index() { $this->all(); } /************************************************************************** ************************************************************************** ************************************************************************** * action: asset * - Lists all asset accounts */ function asset() { $title = 'Asset Accounts'; $this->set('title', $title); $this->set('heading', $title); $this->set('accounts', $this->paginate(array('Account.type' => 'ASSET'))); $this->render('index'); } /************************************************************************** ************************************************************************** ************************************************************************** * action: liability * - Lists all liability accounts */ function liability() { $title = 'Liability Accounts'; $this->set('title', $title); $this->set('heading', $title); $this->set('accounts', $this->paginate(array('Account.type' => 'LIABILITY'))); $this->render('index'); } /************************************************************************** ************************************************************************** ************************************************************************** * action: equity * - Lists all equity accounts */ function equity() { $title = 'Equity Accounts'; $this->set('title', $title); $this->set('heading', $title); $this->set('accounts', $this->paginate(array('Account.type' => 'EQUITY'))); $this->render('index'); } /************************************************************************** ************************************************************************** ************************************************************************** * action: income * - Lists all income accounts */ function income() { $title = 'Income Accounts'; $this->set('title', $title); $this->set('heading', $title); $this->set('accounts', $this->paginate(array('Account.type' => 'INCOME'))); $this->render('index'); } /************************************************************************** ************************************************************************** ************************************************************************** * action: expense * - Lists all expense accounts */ function expense() { $title = 'Expense Accounts'; $this->set('title', $title); $this->set('heading', $title); $this->set('accounts', $this->paginate(array('Account.type' => 'EXPENSE'))); $this->render('index'); } /************************************************************************** ************************************************************************** ************************************************************************** * action: all * - Lists all accounts */ function all() { $title = 'All Accounts'; $this->set('title', $title); $this->set('heading', $title); $this->set('accounts', $this->paginate()); $this->render('index'); } /************************************************************************** ************************************************************************** ************************************************************************** * action: view * - Displays information about a specific account */ function view($id = null) { if (!$id) { $this->Session->setFlash(__('Invalid Item.', true)); $this->redirect(array('action'=>'index')); } // Get details about the account and its ledgers (no ledger entries yet) $this->Account->Behaviors->attach('Containable'); $account = $this->Account->find ('first', array('contain' => array(// Models 'CurrentLedger' => array('fields' => array('id', 'sequence')), 'Ledger' => array('order' => array('Ledger.open_stamp' => 'DESC')), ), 'conditions' => array(array('Account.id' => $id)), ) ); $this->Account->Behaviors->detach('Containable'); // Simply debug stuff... testing. $cond = null; //$cond = array('Transaction.stamp >' => '2009-05-16'); // Get all ledger entries of the CURRENT ledger //$crap = $this->Account->findSecurityDeposits($id); //$crap = $this->Account->findAccountRelatedEntries($id, 8); //pr(array('crap', $crap)); /* $this->autoRender = false; */ /* return; */ // Get all ledger entries of the CURRENT ledger $account['CurrentLedger']['LedgerEntry'] = $this->Account->findCurrentLedgerEntries($id, $cond); //pr(array('Account summary', $account)); // Summarize each ledger foreach($account['Ledger'] AS &$ledger) $ledger = array_merge($ledger, $this->Account->Ledger->stats($ledger['id'])); // Obtain the overall account balance $account['Account'] = array_merge($account['Account'], array('stats' => $this->Account->stats($id))); $balance = $account['Account']['stats']['Ledger']['balance']; // Prepare to render $title = 'Account: ' . $account['Account']['name']; $this->set(compact('account', 'title', 'balance')); } }