git-svn-id: file:///svn-source/pmgr/branches/ledger_transactions_20090605@132 97e9348a-65ac-dc4b-aefc-98561f571b83
156 lines
5.9 KiB
PHP
156 lines
5.9 KiB
PHP
<?php
|
|
|
|
class AccountsController extends AppController {
|
|
|
|
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 / asset / liability / equity / income / expense / all
|
|
* - Generate a chart of accounts
|
|
*/
|
|
|
|
function index() { $this->all(); }
|
|
function all() { $this->jqGridView('All Accounts', 'all'); }
|
|
function asset() { $this->jqGridView('Asset Accounts', 'asset'); }
|
|
function liability() { $this->jqGridView('Liability Accounts', 'liability'); }
|
|
function equity() { $this->jqGridView('Equity Accounts', 'equity'); }
|
|
function income() { $this->jqGridView('Income Accounts', 'income'); }
|
|
function expense() { $this->jqGridView('Expense Accounts', 'expense'); }
|
|
|
|
|
|
/**************************************************************************
|
|
**************************************************************************
|
|
**************************************************************************
|
|
* 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 jqGridDataExtraTables(&$params, &$model, &$query) {
|
|
unset($query['contain']);
|
|
$query['link'] =
|
|
array(// Models
|
|
'CurrentLedger' => array
|
|
(// Models
|
|
'LedgerEntry' => array
|
|
('conditions' =>
|
|
array('OR' =>
|
|
array('LedgerEntry.debit_ledger_id = CurrentLedger.id',
|
|
'LedgerEntry.credit_ledger_id = CurrentLedger.id'),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
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: 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 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'));
|
|
}
|
|
}
|