Moved ledgers over the jqGrid. Working reasonably well at the moment.

git-svn-id: file:///svn-source/pmgr/branches/ledger_transactions_20090605/site@131 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
abijah
2009-06-15 06:22:38 +00:00
parent 5fd7483ac1
commit e3b838d62a
6 changed files with 132 additions and 168 deletions

View File

@@ -91,13 +91,16 @@ class AppController extends Controller {
$model = $this->jqGridDataModel($params);
// Establish the basic query and conditions
$query = array_intersect_key($this->jqGridDataTables($params, $model),
$query = array_intersect_key($this->jqGridDataCountTables($params, $model),
array('link'=>1, 'contain'=>1));
$query['conditions'] = $this->jqGridDataConditions($params, $model);
// Get the number of records prior to pagination
$count = $this->jqGridDataRecordCount($params, $model, $query);
// Get the rest of the tables.
$this->jqGridDataExtraTables($params, $model, $query);
// Verify a few parameters and determine our starting row
$limit = $params['rows'];
$total = ($count < 0) ? 0 : ceil($count/$limit);
@@ -113,6 +116,9 @@ class AppController extends Controller {
$query['fields'] = $this->jqGridDataFields($params, $model);
$results = $this->jqGridDataRecords($params, $model, $query);
// Post process the records
$this->jqGridRecordsPostProcess($params, $model, $results);
// Add in any needed hyperlinks
$this->jqGridRecordLinks($params, $model, $results, array());
@@ -181,6 +187,14 @@ class AppController extends Controller {
return array('contain' => false);
}
function jqGridDataCountTables(&$params, &$model) {
// For backwards compatibility
return $this->jqGridDataTables($params, $model);
}
function jqGridDataExtraTables(&$params, &$model, &$query) {
}
function jqGridDataConditions(&$params, &$model) {
$searches = array();
@@ -249,6 +263,16 @@ class AppController extends Controller {
return $model->find('all', $query);
}
function jqGridRecordsPostProcess(&$params, &$model, &$records) {
$model_alias = $model->alias;
foreach ($records AS &$record) {
// Add the calculated fields (if any), to the model fields
if (isset($record[0]))
$record[$model_alias] += $record[0];
}
}
function jqGridRecordLinks(&$params, &$model, &$records, $links) {
foreach ($links AS $table => $fields) {
$controller = Inflector::pluralize(strtolower($table));
@@ -291,10 +315,6 @@ class AppController extends Controller {
$model_alias = $model->alias;
foreach ($records AS $record) {
// Add the calculated fields (if any), to the model fields
if (isset($record[0]))
$record[$model_alias] += $record[0];
echo " <row id='{$record[$model_alias]['id']}'>\n";
foreach ($params['fields'] AS $field) {
if (preg_match("/\./", $field)) {

View File

@@ -1,33 +1,6 @@
<?php
class LedgersController extends AppController {
var $paginate = array
('limit' => 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),
@@ -51,56 +24,95 @@ class LedgersController extends AppController {
/**************************************************************************
**************************************************************************
**************************************************************************
* action: index
* action: index / current / closed / all
* - Generate a list of ledgers
*/
function index() {
$this->current();
}
function index() { $this->all(); }
function current() { $this->jqGridView('Current Ledgers'); }
function closed() { $this->jqGridView('Closed Ledgers'); }
function all() { $this->jqGridView('All Ledgers', 'all'); }
/**************************************************************************
**************************************************************************
**************************************************************************
* action: current
* - Lists all current ledgers
* virtuals: jqGridData
* - With the application controller handling the jqGridData action,
* these virtual functions ensure that the correct data is passed
* to jqGrid.
*/
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');
function jqGridDataSetup(&$params) {
parent::jqGridDataSetup($params);
if (!isset($params['action']))
$params['action'] = 'all';
}
/**************************************************************************
**************************************************************************
**************************************************************************
* 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');
function jqGridDataExtraTables(&$params, &$model, &$query) {
unset($query['contain']);
$query['link'] =
array(// Models
'Account',
'LedgerEntry' =>
array('conditions' =>
array('OR' =>
array('LedgerEntry.debit_ledger_id = Ledger.id',
'LedgerEntry.credit_ledger_id = Ledger.id'),
),
),
);
}
function jqGridDataFields(&$params, &$model) {
return array
('Ledger.*',
'CONCAT(Account.id, "-", Ledger.sequence) AS id_sequence',
'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');
}
/**************************************************************************
**************************************************************************
**************************************************************************
* action: all
* - Lists all ledgers
*/
function jqGridDataConditions(&$params, &$model) {
$conditions = parent::jqGridDataConditions($params, $model);
function all() {
$title = 'All Ledgers';
$this->set('title', $title); $this->set('heading', $title);
$this->set('ledgers', $this->paginate());
$this->render('index');
if ($params['action'] === 'current') {
$conditions[] = array('NOT' => array('Ledger.closed'));
}
elseif ($params['action'] === 'closed') {
$conditions[] = 'Ledger.closed';
}
return $conditions;
}
function jqGridDataOrder(&$params, &$model, $index, $direction) {
$id_sequence = false;
if ($index === 'id_sequence') {
$id_sequence = true;
$index = 'Ledger.account_id';
}
$order = parent::jqGridDataOrder($params, $model, $index, $direction);
if ($id_sequence) {
$order[] = 'Ledger.sequence ' . $direction;
}
return $order;
}
function jqGridRecordLinks(&$params, &$model, &$records, $links) {
$links['Ledger'] = array('id_sequence');
$links['Account'] = array('name');
return parent::jqGridRecordLinks($params, $model, $records, $links);
}

View File

@@ -11,9 +11,6 @@ $cols['Credits'] = array('index' => 'Account.credits', 'formatter' => 'currenc
$cols['Balance'] = array('index' => 'Account.balance', 'formatter' => 'currency');
$cols['Comment'] = array('index' => 'Account.comment', 'formatter' => 'comment');
$cols['Name']['formatoptions'] = array('showAction' => 'view', 'idName' => 'myid');
$jqGrid_options = array('jqGridColumns' => $cols,
'controller' => 'accounts',
'caption' => isset($caption) ? $caption : null);

View File

@@ -7,16 +7,18 @@ if (!isset($limit))
$limit = 20;
if (!isset($limitOptions)) {
$limitOptions = array();
$limitOptions = array($limit);
if ($limit < 10)
array_push($limitOptions, 2, 5);
if ($limit < 30)
array_push($limitOptions, 10, 25);
if ($limit > 10)
array_push($limitOptions, 50, 200);
array_push($limitOptions, 25, 50, 200);
if ($limit > 20)
array_push($limitOptions, 500);
}
sort($limitOptions, SORT_NUMERIC);
$limitOptions = array_unique($limitOptions, SORT_NUMERIC);
if (!isset($height))
$height = 'auto';
@@ -130,6 +132,10 @@ foreach ($jqGridColumns AS &$col) {
$col = array_merge($default, $col);
}
reset($jqGridColumns);
$sortname = current($jqGridColumns);
$sortname = $sortname['index'];
// OK, now that everything is in place, get out of PHP mode,
// and add the javascript code (along with a touch of HTML)
@@ -163,12 +169,14 @@ jQuery(document).ready(function(){
'height' => $height,
'rowNum' => $limit,
'rowList' => $limitOptions,
'sortname' => $sortname,
'caption' => $caption,
'imgpath' => $imgpath,
'viewrecords' => true,
'pager' => $grid_id.'-pager',
'loadComplete' => array('--special' => "function() {url=jQuery('#{$grid_id}').getGridParam('url');url=url.replace(/\/debug.*$/,'?'); pd=jQuery('#{$grid_id}').getPostData();$.each(pd,function(i){ url+=i+'='+escape(pd[i])+'&'; }); jQuery('#{$grid_id}-query').html('<A HREF=\"'+url+'\">Grid Query</A><BR>'+url);}"),
'loadError' => array('--special' => "function(xhr,st,err) {url=jQuery('#{$grid_id}').getGridParam('url');url=url.replace(/\/debug.*$/,'?'); pd=jQuery('#{$grid_id}').getPostData();$.each(pd,function(i){ url+=i+'='+escape(pd[i])+'&'; }); jQuery('#{$grid_id}-query').html('<A HREF=\"'+url+'\">Grid Error Query</A><BR>'+url);}"),
//'toolbar' => array(true,"bottom"),
)); ?>
);

View File

@@ -1,88 +1,29 @@
<?php /* -*- mode:PHP -*- */
if (isset($heading))
echo $heading;
elseif (!isset($caption))
echo '<h2>'.__('Ledgers',true).'</h2>';
// Define the table columns
$cols = array();
$cols['ID'] = array('index' => 'id_sequence', 'formatter' => 'id');
$cols['Account'] = array('index' => 'Account.name', 'formatter' => 'name', 'width' => '250');
$cols['Entries'] = array('index' => 'entries', 'width' => '60', 'align' => 'right');
$cols['Debits'] = array('index' => 'debits', 'formatter' => 'currency');
$cols['Credits'] = array('index' => 'credits', 'formatter' => 'currency');
$cols['Balance'] = array('index' => 'balance', 'formatter' => 'currency');
$cols['Close Date'] = array('index' => 'Ledger.close_stamp', 'formatter' => 'date');
$cols['Comment'] = array('index' => 'Ledger.comment', 'formatter' => 'comment');
$headers = array_merge(array('Sequence'), //array('Name'),
(isset($ledgers[0]['Account'])
? array('Account')
: array()),
array('Entries', 'Debits', 'Credits', 'Balance', 'Close Date', 'Comment'));
$column_class = array();
foreach (array_intersect($headers, array('ID', 'Sequence')) AS $k => $v) {
$column_class[$k] = 'id';
$jqGrid_options = array('jqGridColumns' => $cols,
'controller' => 'ledgers',
'caption' => isset($caption) ? $caption : null);
if (isset($ledgers)) {
$jqGrid_options += array('custom_ids' =>
array_map(create_function('$data',
'return $data["id"];'),
$ledgers),
'limit' => 5);
}
foreach (array_intersect($headers, array('Debits', 'Credits', 'Balance')) AS $k => $v) {
$column_class[$k] = 'currency';
}
foreach (array_intersect($headers, array('Comment')) AS $k => $v) {
$column_class[$k] = 'slack';
else {
$jqGrid_options += array('search_fields' => array('Account'));
}
if (isset($paginator)) {
echo $paginator->counter(array(
'format' => __('Page %page% of %pages%, showing %current% records (%start% - %end%) of %count% total', true)));
$headers = array_merge(array($paginator->sort('sequence')), //array($paginator->sort('name')),
(isset($ledgers[0]['Account'])
? array($paginator->sort('Account', 'Account.name'))
: array()),
array($paginator->sort('entries'),
$paginator->sort('debits'),
$paginator->sort('credits'),
$paginator->sort('balance'),
$paginator->sort('Close Date', 'close_stamp'),
$paginator->sort('comment')));
}
$rows = array();
foreach ($ledgers as $ledger) {
$account = null;
$stats = null;
if (isset($ledger[0]))
$stats = $ledger[0];
else
$stats = $ledger;
if (isset($ledger['Account']))
$account = $ledger['Account'];
if (isset($ledger['Ledger']))
$ledger = $ledger['Ledger'];
$rows[] = array_merge(array($html->link('#'.$ledger['account_id'].'-'.$ledger['sequence'],
array('controller' => 'ledgers',
'action' => 'view',
$ledger['id']))),
(isset($account)
? array($html->link($account['name'],
array('controller' => 'accounts',
'action' => 'view',
$account['id'])))
: array()),
array($stats['entries'],
FormatHelper::currency($stats['debits']),
FormatHelper::currency($stats['credits']),
FormatHelper::currency($stats['balance']),
FormatHelper::datetime($ledger['close_stamp']),
$ledger['comment']));
}
echo $this->element('table',
array('class' => 'item ledger 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");
}
echo $this->element('jqGrid', $jqGrid_options);

View File

@@ -1,14 +0,0 @@
<div class="ledgers index">
<?php
function currency($number) {
if (!isset($number))
return null;
if ($number < 0)
return "($ " . number_format(-1*$number, 2) . ")";
else
return "$ " . number_format($number, 2);
}
?>
<?php echo $this->element('ledgers', array('heading' => '<h2>'.$heading.'</h2>')) ?>
</div>