Added transaction stats. Added (possibly unwanted) columns.
git-svn-id: file:///svn-source/pmgr/branches/yafr_20090716/site@389 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
@@ -309,15 +309,37 @@ class AppController extends Controller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$table = $this->gridDataFilterTablesTable($params, $model, $tbl);
|
$table = $this->gridDataFilterTablesTable($params, $model, $tbl);
|
||||||
if (!$table || $table == $model->alias)
|
if (!$table)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// If the table is already part of the query, don't replace it
|
$config = $this->gridDataFilterTablesConfig($params, $model, $tbl);
|
||||||
if (isset($query[$link][$table]) || in_array($table, $query[$link]))
|
|
||||||
continue;
|
/* pr(array('pre-filter-table-config' => */
|
||||||
|
/* array('query[link]' => $query[$link], */
|
||||||
|
/* 'config' => $config))); */
|
||||||
|
|
||||||
|
// If the table is already part of the query, append to it
|
||||||
|
if ($table == $model->alias) {
|
||||||
|
$query[$link] =
|
||||||
|
array_merge_recursive($query[$link], $config);
|
||||||
|
}
|
||||||
|
elseif (isset($query[$link][$table])) {
|
||||||
|
$query[$link][$table] =
|
||||||
|
array_merge_recursive($query[$link][$table], $config);
|
||||||
|
}
|
||||||
|
elseif (in_array($table, $query[$link])) {
|
||||||
|
$key = array_search($table, $query[$link]);
|
||||||
|
$query[$link][$key] =
|
||||||
|
array_merge_recursive($query[$link][$key], $config);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// Table is NOT already part of the query... set it now
|
||||||
|
$query[$link][$table] = $config;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* pr(array('post-filter-table-config' => */
|
||||||
|
/* array('query[link]' => $query[$link]))); */
|
||||||
|
|
||||||
$query[$link][$table]
|
|
||||||
= $this->gridDataFilterTablesConfig($params, $model, $table);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $query;
|
return $query;
|
||||||
@@ -351,12 +373,12 @@ class AppController extends Controller {
|
|||||||
if (!$table)
|
if (!$table)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
$key = $this->gridDataFilterConditionsKey($params, $model, $table, $id);
|
$key = $this->gridDataFilterConditionsKey($params, $model, $tbl, $id);
|
||||||
if (!$key)
|
if (!$key)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
$conditions[]
|
$conditions[]
|
||||||
= $this->gridDataFilterConditionsStatement($params, $model, $table, $key, $value);
|
= $this->gridDataFilterConditionsStatement($params, $model, $tbl, $key, $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $conditions;
|
return $conditions;
|
||||||
|
|||||||
@@ -62,9 +62,34 @@ class LedgerEntriesController extends AppController {
|
|||||||
// Ensure we don't add it in again as part of filtering.
|
// Ensure we don't add it in again as part of filtering.
|
||||||
if ($table == 'Account')
|
if ($table == 'Account')
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
|
// Customer needs to be added beneath Transaction
|
||||||
|
if ($table == 'Customer')
|
||||||
|
return 'Transaction';
|
||||||
|
|
||||||
return $table;
|
return $table;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function gridDataFilterTablesConfig(&$params, &$model, $table) {
|
||||||
|
$config = parent::gridDataFilterTablesConfig($params, $model, $table);
|
||||||
|
|
||||||
|
// Customer is special in that its linked in by Transaction
|
||||||
|
// Therefore, the actual table used for the join is 'Transaction',
|
||||||
|
// not 'Customer', and so we need to specify Customer here.
|
||||||
|
if ($table == 'Customer')
|
||||||
|
$config = array('Customer' => $config);
|
||||||
|
|
||||||
|
return $config;
|
||||||
|
}
|
||||||
|
|
||||||
|
function gridDataFilterConditionsStatement(&$params, &$model, $table, $key, $value) {
|
||||||
|
pr(compact('table', 'key', 'value'));
|
||||||
|
if ($table == 'Account' && $value == '-AR-')
|
||||||
|
$value = $this->LedgerEntry->Ledger->Account->accountReceivableAccountID();
|
||||||
|
return parent::gridDataFilterConditionsStatement($params, $model, $table, $key, $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
function gridDataOrder(&$params, &$model, $index, $direction) {
|
function gridDataOrder(&$params, &$model, $index, $direction) {
|
||||||
/* if ($index === 'balance') */
|
/* if ($index === 'balance') */
|
||||||
/* return ($index .' '. $direction); */
|
/* return ($index .' '. $direction); */
|
||||||
|
|||||||
@@ -41,6 +41,22 @@ class TransactionsController extends AppController {
|
|||||||
* to jqGrid.
|
* to jqGrid.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
function gridDataCountTables(&$params, &$model) {
|
||||||
|
return parent::gridDataTables($params, $model);
|
||||||
|
}
|
||||||
|
|
||||||
|
function gridDataTables(&$params, &$model) {
|
||||||
|
$link = $this->gridDataCountTables($params, $model);
|
||||||
|
$link['link']['StatementEntry'] = array('fields' => array());
|
||||||
|
return $link;
|
||||||
|
}
|
||||||
|
|
||||||
|
function gridDataFields(&$params, &$model) {
|
||||||
|
$fields = parent::gridDataFields($params, $model);
|
||||||
|
$fields[] = 'COUNT(StatementEntry.id) AS entries';
|
||||||
|
return $fields;
|
||||||
|
}
|
||||||
|
|
||||||
function gridDataPostProcessLinks(&$params, &$model, &$records, $links) {
|
function gridDataPostProcessLinks(&$params, &$model, &$records, $links) {
|
||||||
$links['Transaction'] = array('id');
|
$links['Transaction'] = array('id');
|
||||||
return parent::gridDataPostProcessLinks($params, $model, $records, $links);
|
return parent::gridDataPostProcessLinks($params, $model, $records, $links);
|
||||||
|
|||||||
@@ -107,9 +107,10 @@ class Account extends AppModel {
|
|||||||
* on the overall balance of the account.
|
* on the overall balance of the account.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function debitCreditFields($sum = false, $entry_name = 'LedgerEntry', $account_name = 'Account') {
|
function debitCreditFields($sum = false, $balance = true,
|
||||||
|
$entry_name = 'LedgerEntry', $account_name = 'Account') {
|
||||||
return $this->LedgerEntry->debitCreditFields
|
return $this->LedgerEntry->debitCreditFields
|
||||||
($sum, $entry_name, $account_name);
|
($sum, $balance, $entry_name, $account_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -110,9 +110,10 @@ class Ledger extends AppModel {
|
|||||||
* entries are a debit, or a credit, and also the effect each have
|
* entries are a debit, or a credit, and also the effect each have
|
||||||
* on the overall balance of the ledger.
|
* on the overall balance of the ledger.
|
||||||
*/
|
*/
|
||||||
function debitCreditFields($sum = false, $entry_name = 'LedgerEntry', $account_name = 'Account') {
|
function debitCreditFields($sum = false, $balance = true,
|
||||||
|
$entry_name = 'LedgerEntry', $account_name = 'Account') {
|
||||||
return $this->LedgerEntry->debitCreditFields
|
return $this->LedgerEntry->debitCreditFields
|
||||||
($sum, $entry_name, $account_name);
|
($sum, $balance, $entry_name, $account_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**************************************************************************
|
/**************************************************************************
|
||||||
|
|||||||
@@ -44,7 +44,8 @@ class LedgerEntry extends AppModel {
|
|||||||
* on the overall balance of the account/ledger.
|
* on the overall balance of the account/ledger.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function debitCreditFields($sum = false, $entry_name = 'LedgerEntry', $account_name = 'Account') {
|
function debitCreditFields($sum = false, $balance = true,
|
||||||
|
$entry_name = 'LedgerEntry', $account_name = 'Account') {
|
||||||
$fields = array
|
$fields = array
|
||||||
(
|
(
|
||||||
($sum ? 'SUM(' : '') .
|
($sum ? 'SUM(' : '') .
|
||||||
@@ -56,15 +57,17 @@ class LedgerEntry extends AppModel {
|
|||||||
"IF({$entry_name}.crdr = 'CREDIT'," .
|
"IF({$entry_name}.crdr = 'CREDIT'," .
|
||||||
" {$entry_name}.amount, NULL)" .
|
" {$entry_name}.amount, NULL)" .
|
||||||
($sum ? ')' : '') . ' AS credit' . ($sum ? 's' : ''),
|
($sum ? ')' : '') . ' AS credit' . ($sum ? 's' : ''),
|
||||||
|
|
||||||
($sum ? 'SUM(' : '') .
|
|
||||||
"IF(${account_name}.type IN ('ASSET', 'EXPENSE')," .
|
|
||||||
" IF({$entry_name}.crdr = 'DEBIT', 1, -1)," .
|
|
||||||
" IF({$entry_name}.crdr = 'CREDIT', 1, -1))" .
|
|
||||||
" * IF({$entry_name}.amount, {$entry_name}.amount, 0)" .
|
|
||||||
($sum ? ')' : '') . ' AS balance',
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if ($balance)
|
||||||
|
$fields[] =
|
||||||
|
($sum ? 'SUM(' : '') .
|
||||||
|
"IF(${account_name}.type IN ('ASSET', 'EXPENSE')," .
|
||||||
|
" IF({$entry_name}.crdr = 'DEBIT', 1, -1)," .
|
||||||
|
" IF({$entry_name}.crdr = 'CREDIT', 1, -1))" .
|
||||||
|
" * IF({$entry_name}.amount, {$entry_name}.amount, 0)" .
|
||||||
|
($sum ? ')' : '') . ' AS balance';
|
||||||
|
|
||||||
if ($sum)
|
if ($sum)
|
||||||
$fields[] = "COUNT({$entry_name}.id) AS entries";
|
$fields[] = "COUNT({$entry_name}.id) AS entries";
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,27 @@ class Transaction extends AppModel {
|
|||||||
var $hasMany = array(
|
var $hasMany = array(
|
||||||
'LedgerEntry',
|
'LedgerEntry',
|
||||||
'StatementEntry',
|
'StatementEntry',
|
||||||
|
|
||||||
|
'Charge' => array(
|
||||||
|
'className' => 'StatementEntry',
|
||||||
|
'conditions' => array('Charge.type' => 'CHARGE')
|
||||||
|
),
|
||||||
|
|
||||||
|
'Payment' => array(
|
||||||
|
'className' => 'StatementEntry',
|
||||||
|
'conditions' => array('Payment.type' => 'PAYMENT')
|
||||||
|
),
|
||||||
|
|
||||||
|
'Debit' => array(
|
||||||
|
'className' => 'LedgerEntry',
|
||||||
|
'conditions' => array('Debit.crdr' => 'DEBIT')
|
||||||
|
),
|
||||||
|
|
||||||
|
'Credit' => array(
|
||||||
|
'className' => 'LedgerEntry',
|
||||||
|
'conditions' => array('Credit.crdr' => 'CREDIT')
|
||||||
|
),
|
||||||
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
@@ -340,5 +361,62 @@ class Transaction extends AppModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**************************************************************************
|
||||||
|
**************************************************************************
|
||||||
|
**************************************************************************
|
||||||
|
* function: stats
|
||||||
|
* - Returns summary data from the requested transaction
|
||||||
|
*/
|
||||||
|
function stats($id = null, $query = null) {
|
||||||
|
//pr(array('Transaction::stats' => compact('id', 'query')));
|
||||||
|
|
||||||
|
$this->queryInit($query);
|
||||||
|
unset($query['group']);
|
||||||
|
|
||||||
|
if (isset($id)) {
|
||||||
|
$query['conditions'][] = array('Transaction.id' => $id);
|
||||||
|
$query['group'] = 'Transaction.id';
|
||||||
|
}
|
||||||
|
else
|
||||||
|
// CakePHP seems to automagically add in our ID as a part
|
||||||
|
// of the query conditions, but only on a 'first' query,
|
||||||
|
// not an 'all'. I suppose this is helpful :-/
|
||||||
|
unset($this->id);
|
||||||
|
|
||||||
|
if (empty($query['fields']))
|
||||||
|
$query['fields'] = array();
|
||||||
|
|
||||||
|
$stats = array();
|
||||||
|
foreach (array_keys($this->hasMany) AS $table) {
|
||||||
|
$squery = $query;
|
||||||
|
$squery['link'][$table] = array('fields' => array());
|
||||||
|
|
||||||
|
if ($table == 'LedgerEntry') {
|
||||||
|
$squery['fields'] = array_merge($squery['fields'],
|
||||||
|
$this->LedgerEntry->debitCreditFields(true, false));
|
||||||
|
}
|
||||||
|
elseif ($table == 'StatementEntry') {
|
||||||
|
$squery['fields'] = array_merge($squery['fields'],
|
||||||
|
$this->StatementEntry->chargePaymentFields(true));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$squery['fields'][] = "SUM({$table}.amount) AS total";
|
||||||
|
$squery['fields'][] = "COUNT({$table}.id) AS entries";
|
||||||
|
}
|
||||||
|
$stats[$table] = $this->find('first', $squery);
|
||||||
|
// REVISIT <AP>: 20090724
|
||||||
|
// [0][0] is for when we do an 'all' query. This can
|
||||||
|
// be removed at some point, but I'm keeping it while
|
||||||
|
// toggling between 'all' and 'first' (testing).
|
||||||
|
if (isset($stats[$table][0][0]))
|
||||||
|
$stats[$table] += $stats[$table][0][0];
|
||||||
|
else
|
||||||
|
$stats[$table] += $stats[$table][0];
|
||||||
|
unset($stats[$table][0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
//pr(array('Transaction::stats' => array('return' => compact('stats'))));
|
||||||
|
return $stats;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
@@ -86,6 +86,31 @@ echo $this->element('statement_entries', array
|
|||||||
)));
|
)));
|
||||||
|
|
||||||
|
|
||||||
|
/**********************************************************************
|
||||||
|
* Customer Ledger History
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* REVISIT <AP>: 20090724
|
||||||
|
* It's not my intention to really include this, as I believe it
|
||||||
|
* just will confuse folks. However, I've added it at the moment
|
||||||
|
* to help me see the picture of what's happening. It may prove
|
||||||
|
* useful with respect to identifying pre-payments, so after using
|
||||||
|
* it for a while, maybe we can get a feeling for that. I suspect
|
||||||
|
* it will be MUCH more useful just to add the pre-pay amount to
|
||||||
|
* the info box, or provide a list of ledger entries that are JUST
|
||||||
|
* pre-payments. We'll see...
|
||||||
|
*/
|
||||||
|
echo $this->element('ledger_entries', array
|
||||||
|
(// Grid configuration
|
||||||
|
'config' => array
|
||||||
|
('caption' => 'Ledger Entries',
|
||||||
|
'filter' => array('Customer.id' => $customer['Customer']['id'],
|
||||||
|
'Account.id !=' => '-AR-'),
|
||||||
|
'exclude' => array('Customer'),
|
||||||
|
)));
|
||||||
|
|
||||||
|
|
||||||
/* End "detail supporting" div */
|
/* End "detail supporting" div */
|
||||||
echo '</div>' . "\n";
|
echo '</div>' . "\n";
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ $cols['Type'] = array('index' => 'Transaction.type', 'formatter' =
|
|||||||
//$cols['Customer'] = array('index' => 'Customer.name', 'formatter' => 'longname');
|
//$cols['Customer'] = array('index' => 'Customer.name', 'formatter' => 'longname');
|
||||||
$cols['Timestamp'] = array('index' => 'Transaction.stamp', 'formatter' => 'date');
|
$cols['Timestamp'] = array('index' => 'Transaction.stamp', 'formatter' => 'date');
|
||||||
$cols['Amount'] = array('index' => 'Transaction.amount', 'formatter' => 'currency');
|
$cols['Amount'] = array('index' => 'Transaction.amount', 'formatter' => 'currency');
|
||||||
|
$cols['entries'] = array('index' => 'entries', 'formatter' => 'number');
|
||||||
$cols['Comment'] = array('index' => 'Transaction.comment', 'formatter' => 'comment');
|
$cols['Comment'] = array('index' => 'Transaction.comment', 'formatter' => 'comment');
|
||||||
|
|
||||||
// Render the grid
|
// Render the grid
|
||||||
|
|||||||
Reference in New Issue
Block a user