Updated accounts to use jqGrid.

git-svn-id: file:///svn-source/pmgr/branches/ledger_transactions_20090605@130 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
abijah
2009-06-15 04:15:55 +00:00
parent 41636f5c75
commit 3dcdcb03eb
3 changed files with 81 additions and 176 deletions

View File

@@ -1,34 +1,6 @@
<?php <?php
class AccountsController extends AppController { class AccountsController extends AppController {
var $paginate = array
('limit' => 100,
'link' =>
array(// Models
'CurrentLedger' => array
(// Models
'LedgerEntry' =>
array('fields' =>
array('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'),
'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 $uses = array('Account', 'LedgerEntry');
@@ -57,102 +29,80 @@ class AccountsController extends AppController {
/************************************************************************** /**************************************************************************
************************************************************************** **************************************************************************
************************************************************************** **************************************************************************
* action: index * action: index / asset / liability / equity / income / expense / all
* - Lists all accounts * - Generate a chart of accounts
*/ */
function index() { function index() { $this->all(); }
$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'); }
/************************************************************************** /**************************************************************************
************************************************************************** **************************************************************************
************************************************************************** **************************************************************************
* action: asset * virtuals: jqGridData
* - Lists all asset accounts * - With the application controller handling the jqGridData action,
* these virtual functions ensure that the correct data is passed
* to jqGrid.
*/ */
function asset() { function jqGridDataSetup(&$params) {
$title = 'Asset Accounts'; parent::jqGridDataSetup($params);
$this->set('title', $title); $this->set('heading', $title); if (!isset($params['action']))
$this->set('accounts', $this->paginate(array('Account.type' => 'ASSET'))); $params['action'] = 'all';
$this->render('index');
} }
function jqGridDataTables(&$params, &$model) {
/************************************************************************** return array
************************************************************************** ('link' =>
************************************************************************** array(// Models
* action: liability 'CurrentLedger' => array
* - Lists all liability accounts (// Models
*/ 'LedgerEntry' => array
('conditions' =>
function liability() { array('OR' =>
$title = 'Liability Accounts'; array('LedgerEntry.debit_ledger_id = CurrentLedger.id',
$this->set('title', $title); $this->set('heading', $title); 'LedgerEntry.credit_ledger_id = CurrentLedger.id'),
$this->set('accounts', $this->paginate(array('Account.type' => 'LIABILITY'))); ),
$this->render('index'); ),
),
),
);
} }
function jqGridDataFields(&$params, &$model) {
/************************************************************************** return array
************************************************************************** ('Account.*',
************************************************************************** 'SUM(IF(LedgerEntry.debit_ledger_id = CurrentLedger.id,
* action: equity LedgerEntry.amount, NULL)) AS debits',
* - Lists all equity accounts 'SUM(IF(LedgerEntry.credit_ledger_id = CurrentLedger.id,
*/ LedgerEntry.amount, NULL)) AS credits',
"SUM(IF(Account.type IN ('ASSET', 'EXPENSE'),
function equity() { IF(LedgerEntry.debit_ledger_id = CurrentLedger.id, 1, -1),
$title = 'Equity Accounts'; IF(LedgerEntry.credit_ledger_id = CurrentLedger.id, 1, -1)
$this->set('title', $title); $this->set('heading', $title); ) * IF(LedgerEntry.amount, LedgerEntry.amount, 0)
$this->set('accounts', $this->paginate(array('Account.type' => 'EQUITY'))); ) AS balance",
$this->render('index'); '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']));
************************************************************************** }
* action: income
* - Lists all income accounts
*/
function income() { return $conditions;
$title = 'Income Accounts';
$this->set('title', $title); $this->set('heading', $title);
$this->set('accounts', $this->paginate(array('Account.type' => 'INCOME')));
$this->render('index');
} }
function jqGridRecordLinks(&$params, &$model, &$records, $links) {
/************************************************************************** $links['Account'] = array('name');
************************************************************************** return parent::jqGridRecordLinks($params, $model, $records, $links);
**************************************************************************
* 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');
} }

View File

@@ -1,3 +0,0 @@
<div class="accounts index">
<?php echo $this->element('accounts', array('heading' => '<h2>'.$heading.'</h2>')) ?>
</div>

View File

@@ -1,74 +1,32 @@
<?php /* -*- mode:PHP -*- */ <?php /* -*- mode:PHP -*- */
if (isset($heading)) // Define the table columns
echo $heading; $cols = array();
elseif (!isset($caption)) $cols['ID'] = array('index' => 'Account.id', 'formatter' => 'id');
echo '<h2>'.__('Accounts',true).'</h2>'; $cols['Name'] = array('index' => 'Account.name', 'formatter' => 'name', 'width' => '250');
$cols['Type'] = array('index' => 'Account.type', 'width' => '60');
$cols['Entries'] = array('index' => 'Account.entries', 'width' => '60', 'align' => 'right');
$cols['Debits'] = array('index' => 'Account.debits', 'formatter' => 'currency');
$cols['Credits'] = array('index' => 'Account.credits', 'formatter' => 'currency');
$cols['Balance'] = array('index' => 'Account.balance', 'formatter' => 'currency');
$cols['Comment'] = array('index' => 'Account.comment', 'formatter' => 'comment');
$headers = array('Name', 'Type', /*'Ext. Name', 'Ext. Account',*/ 'Entries', 'Debits', 'Credits', 'Balance', 'Comment');
$column_class = array(); $cols['Name']['formatoptions'] = array('showAction' => 'view', 'idName' => 'myid');
foreach (array_intersect($headers, array('Debits', 'Credits', 'Balance')) AS $k => $v) {
$column_class[$k] = 'currency'; $jqGrid_options = array('jqGridColumns' => $cols,
'controller' => 'accounts',
'caption' => isset($caption) ? $caption : null);
if (isset($accounts)) {
$jqGrid_options += array('custom_ids' =>
array_map(create_function('$data',
'return $data["id"];'),
$accounts),
'limit' => 5);
} }
foreach (array_intersect($headers, array('Comment')) AS $k => $v) { else {
$column_class[$k] = 'slack'; $jqGrid_options += array('search_fields' => array('Name'));
} }
if (isset($paginator)) { echo $this->element('jqGrid', $jqGrid_options);
echo $paginator->counter(array(
'format' => __('Page %page% of %pages%, showing %current% records (%start% - %end%) of %count% total', true)));
$headers = array($paginator->sort('name'),
$paginator->sort('type'),
/* $paginator->sort('Ext. Name', 'external_name'), */
/* $paginator->sort('Ext. Account', 'external_account'), */
$paginator->sort('entries'),
$paginator->sort('debits'),
$paginator->sort('credits'),
$paginator->sort('balance'),
$paginator->sort('comment'));
}
$rows = array();
foreach ($accounts as $account) {
//pr($account);
if (isset($account[0]))
$stats = $account[0];
else
$stats = $account;
// Move our pointer to the meat
if (isset($account['Account']))
$account = $account['Account'];
$rows[] = array($html->link($account['name'],
array('controller' => 'accounts',
'action' => 'view',
$account['id'])),
$account['type'],
/* $account['external_name'], */
/* $account['external_account'], */
$stats['entries'],
FormatHelper::currency($stats['debits']),
FormatHelper::currency($stats['credits']),
FormatHelper::currency($stats['balance']),
$account['comment'],
);
}
echo $this->element('table',
array('class' => 'item account list',
'caption' => isset($caption) ? $caption : null,
'headers' => $headers,
'rows' => $rows,
'column_class' => $column_class));
if (isset($paginator)) {
echo('<div class="paging">' . "\n");
echo $paginator->prev('<< '.__('previous', true), array(), null, array('class'=>'disabled'));
echo(' | ');
echo $paginator->numbers();
echo(' | ');
echo $paginator->next(__('next', true).' >>', array(), null, array('class'=>'disabled'));
echo('</div>' . "\n");
}