100, 'link' => array(// Models 'Account', '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'), ), ), ), 'group' => 'Ledger.id', 'order' => array('Ledger.name' => 'ASC')); var $uses = array('Ledger', 'LedgerEntry'); 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')); } /* $this->Ledger->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->Ledger->Behaviors->attach('Containable'); */ /* $this->Ledger->Contain(array('LedgerEntry' => array */ /* ( */ /* // Models */ /* 'Transaction' => array */ /* ( */ /* // Models */ /* 'Customer', */ /* )), */ /* )); */ /* } */ /* $ledger = $this->Ledger->read(null, $id); */ $this->Ledger->Behaviors->attach('Containable'); $this->Ledger->Contain(array('Account')); $ledger = $this->Ledger->read(null, $id); //pr($ledger); if (in_array($ledger['Account']['type'], array('ASSET', 'EXPENSE'))) $ledger_type = 'debit'; else $ledger_type = 'credit'; $ledger['LedgerEntry'] = $this->LedgerEntry->find ('all', array('link' => array (// Models 'Transaction' => array(// Models 'Customer', )), 'fields' => array('id', 'name', 'comment', "IF(LedgerEntry.debit_ledger_id = $id, LedgerEntry.amount, NULL) AS debit", "IF(LedgerEntry.credit_ledger_id = $id, LedgerEntry.amount, NULL) AS credit", "(IF(LedgerEntry.{$ledger_type}_ledger_id = $id, 1, -1) * LedgerEntry.amount) AS balance"), 'conditions' => array('OR' => array("LedgerEntry.debit_ledger_id = $id", "LedgerEntry.credit_ledger_id = $id")), 'order' => array('Transaction.stamp'), //'limit' => 15, //'limit' => 2, )); //pr($ledger); $balance = 0; foreach($ledger['LedgerEntry'] AS &$entry) { if (isset($entry[0])) $entry = array_merge($entry[0], array_diff_key($entry, array(0))); $balance += $entry['balance']; } $title = 'Ledger: ' . $ledger['Ledger']['name']; $this->set(compact('ledger', 'title', 'balance')); } }