From dc79667dcade44c870c971b76f3c8479777c41be Mon Sep 17 00:00:00 2001 From: abijah Date: Sun, 12 Jul 2009 20:15:56 +0000 Subject: [PATCH] New generic mechanism for subtotaling columns. This is NOT an ideal solution, as it only subtotals for the current grid page. Hopefully we can get something better into the app controller at some point git-svn-id: file:///svn-source/pmgr/branches/invoice_receipt_20090629/site@321 97e9348a-65ac-dc4b-aefc-98561f571b83 --- app_controller.php | 21 +++++++++++++++++++++ views/elements/ledger_entries.ctp | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/app_controller.php b/app_controller.php index 0d962dd..050d8ae 100644 --- a/app_controller.php +++ b/app_controller.php @@ -322,6 +322,14 @@ class AppController extends Controller { $model_alias = $model->alias; $id = $model->primaryKey; + $subtotals = array(); + foreach ($params['fields'] AS $field) { + if (preg_match('/subtotal-(.*)$/', $field, $matches)) + $subtotals[] = array('field' => $matches[1], + 'name' => $field, + 'amount' => 0); + } + foreach ($records AS &$record) { $record['jqGrid_id'] = $record[$model_alias][$id]; // Add the calculated fields (if any), to the model fields @@ -329,6 +337,19 @@ class AppController extends Controller { $record[$model_alias] += $record[0]; unset($record[0]); } + + foreach ($subtotals AS &$subtotal) { + $field = $subtotal['field']; + if (preg_match("/\./", $field)) { + list($tbl, $col) = explode(".", $field); + $record['subtotal-'.$tbl][$col] = + ($subtotal['amount'] += $record[$tbl][$col]); + } + else { + $record[$model->alias]['subtotal-'.$field] = + ($subtotal['amount'] += $record[$model->alias][$field]); + } + } } // DEBUG PURPOSES ONLY! diff --git a/views/elements/ledger_entries.ctp b/views/elements/ledger_entries.ctp index 2dca8c6..ededadc 100644 --- a/views/elements/ledger_entries.ctp +++ b/views/elements/ledger_entries.ctp @@ -25,7 +25,7 @@ $cols['Debit'] = array('index' => 'debit', 'formatter' => $cols['Credit'] = array('index' => 'credit', 'formatter' => 'currency'); $cols['Applied'] = array('index' => "Reconciliation.amount", 'formatter' => 'currency'); -$cols['Sub-Total'] = array('index' => 'subtotal', 'formatter' => 'currency', 'sortable' => false); +$cols['Sub-Total'] = array('index' => 'subtotal-LedgerEntry.amount', 'formatter' => 'currency', 'sortable' => false); // Since group_by_tx is a boolean, let's just get it