100, 'link' => array(// Models 'Account', 'LedgerEntry' => array('fields' => array('SUM(IF(LedgerEntry.debit_ledger_id = Ledger.id, LedgerEntry.amount, NULL)) AS debits', 'SUM(IF(LedgerEntry.credit_ledger_id = Ledger.id, LedgerEntry.amount, NULL)) 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) ) * IF(LedgerEntry.amount, LedgerEntry.amount, 0) ) AS balance", 'COUNT(LedgerEntry.id) AS entries'), 'conditions' => array('OR' => array('LedgerEntry.debit_ledger_id = Ledger.id', 'LedgerEntry.credit_ledger_id = Ledger.id'), ), ), ), 'group' => 'Ledger.id', 'order' => array('Ledger.account_id', 'Ledger.sequence')); var $sidemenu_links = array(array('name' => 'Ledgers', 'header' => true), array('name' => 'Current', 'url' => array('controller' => 'ledgers', 'action' => 'current')), array('name' => 'Closed', 'url' => array('controller' => 'ledgers', 'action' => 'closed')), array('name' => 'All', 'url' => array('controller' => 'ledgers', 'action' => 'all')), ); /************************************************************************** ************************************************************************** ************************************************************************** * override: sideMenuLinks * - Generates controller specific links for the side menu */ function sideMenuLinks() { return array_merge(parent::sideMenuLinks(), $this->sidemenu_links); } /************************************************************************** ************************************************************************** ************************************************************************** * action: index */ function index() { $this->current(); } /************************************************************************** ************************************************************************** ************************************************************************** * action: current * - Lists all current ledgers */ function current() { $title = 'Current Ledgers'; $this->set('title', $title); $this->set('heading', $title); $this->set('ledgers', $this->paginate(null, array('NOT' => array('Ledger.closed')))); $this->render('index'); } /************************************************************************** ************************************************************************** ************************************************************************** * action: closed * - Lists all closed ledgers */ function closed() { $title = 'Closed Ledgers'; $this->set('title', $title); $this->set('heading', $title); $this->set('ledgers', $this->paginate(null, array('Ledger.closed'))); $this->render('index'); } /************************************************************************** ************************************************************************** ************************************************************************** * action: all * - Lists all ledgers */ function all() { $title = 'All Ledgers'; $this->set('title', $title); $this->set('heading', $title); $this->set('ledgers', $this->paginate()); $this->render('index'); } /************************************************************************** ************************************************************************** ************************************************************************** * action: view * - Displays information about a specific ledger */ function view($id = null) { if (!$id) { $this->Session->setFlash(__('Invalid Item.', true)); $this->redirect(array('action'=>'index')); } // Get details about the ledger itself (no entries yet) $this->Ledger->Behaviors->attach('Containable'); $ledger = $this->Ledger->find ('first', array('contain' => array(// Models 'Account', ), 'conditions' => array(array('Ledger.id' => $id)), ) ); $this->Ledger->Behaviors->detach('Containable'); // Get all ledger entries of this ledger $ledger['LedgerEntry'] = $this->Ledger->findLedgerEntries ($id, $ledger['Account']['type']); // Summarize the entries, and obtain the ledger balance $ledger['Ledger'] = array_merge($ledger['Ledger'], array('stats' => $this->Ledger->stats($id))); $balance = $ledger['Ledger']['stats']['balance']; // OK, set our view variables and render! $title = 'Ledger: ' . $ledger['Ledger']['name']; $this->set(compact('ledger', 'title', 'balance')); } }