'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')), array('name' => 'Bank Deposit', 'url' => array('controller' => 'accounts', 'action' => 'deposit')), ); /************************************************************************** ************************************************************************** ************************************************************************** * override: sideMenuLinks * - Generates controller specific links for the side menu */ function sideMenuLinks() { return array_merge(parent::sideMenuLinks(), $this->sidemenu_links); } /************************************************************************** ************************************************************************** ************************************************************************** * action: index / asset / liability / equity / income / expense / all * - Generate a chart of accounts */ function index() { $this->all(); } function asset() { $this->jqGridView('Asset Accounts'); } function liability() { $this->jqGridView('Liability Accounts'); } function equity() { $this->jqGridView('Equity Accounts'); } function income() { $this->jqGridView('Income Accounts'); } function expense() { $this->jqGridView('Expense Accounts'); } function all() { $this->jqGridView('All Accounts', 'all'); } /************************************************************************** ************************************************************************** ************************************************************************** * virtuals: jqGridData * - With the application controller handling the jqGridData action, * these virtual functions ensure that the correct data is passed * to jqGrid. */ function jqGridDataSetup(&$params) { parent::jqGridDataSetup($params); if (!isset($params['action'])) $params['action'] = 'all'; } function jqGridDataCountTables(&$params, &$model) { return parent::jqGridDataTables($params, $model); } function jqGridDataTables(&$params, &$model) { return array ('link' => array(// Models 'CurrentLedger' => array (// Models 'LedgerEntry' /* REVISIT 20090615: * I'll remove this 'conditions' section on a future checkin, * after I've proven out the %{MODEL_ALIAS} feature will be * sticking around. => array ('conditions' => array('OR' => array('LedgerEntry.debit_ledger_id = CurrentLedger.id', 'LedgerEntry.credit_ledger_id = CurrentLedger.id'), ), ), * END REVISIT */ ), ), ); } function jqGridDataFields(&$params, &$model) { return array ('Account.*', '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'); } function jqGridDataConditions(&$params, &$model) { $conditions = parent::jqGridDataConditions($params, $model); if (in_array($params['action'], array('asset', 'liability', 'equity', 'income', 'expense'))) { $conditions[] = array('Account.type' => strtoupper($params['action'])); } return $conditions; } function jqGridRecordLinks(&$params, &$model, &$records, $links) { $links['Account'] = array('name'); return parent::jqGridRecordLinks($params, $model, $records, $links); } /************************************************************************** ************************************************************************** ************************************************************************** * action: newledger * - Close the current account ledger and create a new one, * carrying forward any balance if necessary. */ function newledger($id = null) { if (!$this->Account->closeCurrentLedger($id)) { $this->Session->setFlash(__('Unable to create new Ledger.', true)); } if ($id) $this->redirect(array('action'=>'view', $id)); else $this->redirect(array('action'=>'index')); } /************************************************************************** ************************************************************************** ************************************************************************** * action: deposit * - Prepares the books for a bank deposit */ function deposit() { if ($this->data) { pr($this->data); $deposit = array('total' => 0, 'ledgers' => array()); $transaction = array(); foreach ($this->data['Tillable']['Ledger'] AS $ledger_id => $ledger) { if (!$ledger['checked']) continue; /* pr(array('checkpoint' => 'save data', */ /* compact('ledger'))); */ $ledger_entries = $this->Account->Ledger->find ('all', array('link' => array ('Account' => array('fields' => array('name')), 'LedgerEntry' => array('fields' => array('amount'), 'MonetarySource' => array('fields' => array('name')), 'Customer' => array('fields' => array('name')), //'Transaction' => //array('fields' => array('stamp')), ), ), 'fields' => false, 'conditions' => array(array('Ledger.id' => $ledger_id), array('LedgerEntry.id IS NOT NULL'), ), )); //pr($ledger_entries); $deposit['total'] += $ledger['amount']; $deposit['ledgers'][] = array('name' => $ledger['account_name'], 'total' => $ledger['amount'], 'entries' => $ledger_entries); if ($ledger['amount'] != 0) { $ids = $this->Account->postLedgerEntry ($transaction, null, array('debit_account_id' => $this->data['Deposit']['Account']['id'], 'credit_ledger_id' => $ledger_id, 'amount' => $ledger['amount'])); $transaction = array_intersect_key($ids, array('transaction_id'=>1)); $this->Account->closeCurrentLedger($ledger['account_id']); } } $this->set(compact('deposit')); $this->render('deposit_slip'); return; } $tillable_account = $this->Account->relatedAccounts('tillable'); $depositable_account = $this->Account->relatedAccounts('depositable'); foreach ($tillable_account AS &$acct) { $acct['Account']['stats'] = $this->Account->stats($acct['Account']['id']); } $this->set(compact('tillable_account', 'depositable_account')); } /************************************************************************** ************************************************************************** ************************************************************************** * 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) $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)), ) ); // Get all ledger entries of the CURRENT ledger $entries = $this->Account->findLedgerEntries($id); $account['CurrentLedger']['LedgerEntry'] = $entries['Entries']; // Summarize each ledger foreach($account['Ledger'] AS &$ledger) $ledger = array_merge($ledger, $this->Account->Ledger->stats($ledger['id'])); // Obtain stats across ALL ledgers for the summary infobox $stats = $this->Account->stats($id, true); $stats = $stats['Ledger']; $this->sidemenu_links[] = array('name' => 'Operations', 'header' => true); $this->sidemenu_links[] = array('name' => 'New Ledger', 'url' => array('action' => 'newledger', $id)); // Prepare to render $title = 'Account: ' . $account['Account']['name']; $this->set(compact('account', 'title', 'stats')); } }