100, 'link' => array(// Models 'CurrentLedger' => array (// Models 'LedgerEntry' => array('fields' => array('SUM(IF(LedgerEntry.debit_ledger_id = CurrentLedger.id, LedgerEntry.amount, 0)) AS debits', 'SUM(IF(LedgerEntry.credit_ledger_id = CurrentLedger.id, LedgerEntry.amount, 0)) 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) )* LedgerEntry.amount) 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')); } /* $this->Account->bindModel(array('hasMany' => array */ /* ('LedgerEntry' => array */ /* ('className' => 'LedgerEntry', */ /* 'foreignKey' => false, */ /* 'finderQuery' => 'SELECT `LedgerEntry`.* */ /* FROM pmgr_entries AS `LedgerEntry` */ /* LEFT JOIN pmgr_transactions AS `Transaction` */ /* ON `Transaction`.id = `LedgerEntry`.transaction_id */ /* WHERE LedgerEntry.debit_ledger_id = ({$__cakeID__$}) */ /* OR LedgerEntry.credit_ledger_id = ({$__cakeID__$}) */ /* ORDER BY Transaction.stamp', */ /* )))); */ /* $this->Account->Behaviors->attach('Containable'); */ /* $this->Account->Contain(array('LedgerEntry' => array */ /* ( */ /* // Models */ /* 'Transaction' => array */ /* ( */ /* // Models */ /* 'Customer', */ /* )), */ /* )); */ /* } */ /* $account = $this->Account->read(null, $id); */ $this->Account->recursive = -1; $account = $this->Account->read(null, $id); $account['Ledger'] = $this->Account->find ('all', array ('link' => array (// Models 'Ledger' => array (// Models 'LedgerEntry' => array('fields' => array('SUM(IF(LedgerEntry.debit_ledger_id = Ledger.id, LedgerEntry.amount, 0)) AS debits', 'SUM(IF(LedgerEntry.credit_ledger_id = Ledger.id, LedgerEntry.amount, 0)) 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) ) * LedgerEntry.amount) AS balance", 'COUNT(LedgerEntry.id) AS entries'), 'conditions' => array('OR' => array('LedgerEntry.debit_ledger_id = Ledger.id', 'LedgerEntry.credit_ledger_id = Ledger.id'), ), ), ), ), 'fields' => array(), 'conditions' => array('Account.id' => $id), 'order' => 'Ledger.name', 'group' => 'Ledger.id', 'limit' => 4, )); $account['CurrentLedger'] = array_shift($this->Account->Ledger->find ('first', array ('link' => array (// Models 'Account' => array('fields' => array()), 'LedgerEntry' => array('fields' => array("SUM(IF(LedgerEntry.debit_ledger_id = Ledger.id, LedgerEntry.amount, 0)) AS debits", "SUM(IF(LedgerEntry.credit_ledger_id = Ledger.id, LedgerEntry.amount, 0)) 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) ) * LedgerEntry.amount) AS balance", 'COUNT(LedgerEntry.id) AS entries'), 'conditions' => array('OR' => array("LedgerEntry.debit_ledger_id = Ledger.id", "LedgerEntry.credit_ledger_id = Ledger.id"), ), ), ), 'fields' => array(), 'conditions' => array(array('Ledger.account_id' => $id), 'NOT' => array('Ledger.closed')), //'order' => 'Ledger.name', 'group' => 'Ledger.id', //'limit' => 4, ))); //pr($account); $balance = $account['CurrentLedger']['balance']; $title = 'Account: ' . $account['Account']['name']; $this->set(compact('account', 'title', 'balance')); } }