Moved the ledger_entries controller/view to jqGrid.

Changed the app controller jqGrid virtual functions for getting the tables for both count and the full query.  The ExtraTables bit was too confusing, and was only intended to allow the user to specify a subset for count and augment it for full query.  This is easily accomplished by having DataTables just call DataCountTables first... one line solution ends the confusion.

Modified linkable behavior to support a %{MODEL_ALIAS} tag, which is replaced in the relationship conditions at runtime with the actual model alias.  This is experimental at best, and certainly will create a problem for any model that ends up using the conditions in 'contain' instead of 'link'.

Broke the LedgerEntry->findInLedgerContext function out into multiple pieces, so those same pieces could be used in the LedgerEntries controller to populate jqGrid.

Changed the ledger_entries element, as a special case, to also allow listing control from ledger_id and account_type, instead of idlist as a mechanism for populating the grid.

Changed the infobox summary css, which will break several pages until I rework them.

Some next steps include fixing the broken infoboxes, and changing the transactions view to use the ledger_entries element.  This also means the ledger_entries element will need to add support for idlist, or if we have any credit/debit issues, perhaps another custom transaction_id instead.



git-svn-id: file:///svn-source/pmgr/branches/ledger_transactions_20090605@134 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
abijah
2009-06-15 19:41:20 +00:00
parent 3911b3303f
commit 543e2daa43
13 changed files with 282 additions and 170 deletions

View File

@@ -41,6 +41,28 @@ class LinkableBehavior extends ModelBehavior {
protected $_defaults = array('type' => 'LEFT');
/*
* This is a function for made recursive str_replaces in an array
* NOTE: The palacement of this function is terrible, but I don't
* know if I really want to go down this path or not.
*/
function recursive_array_replace($find, $replace, &$data) {
if (!isset($data))
return;
if (is_array($data)) {
foreach ($data as $key => $value) {
if (is_array($value)) {
$this->recursive_array_replace($find, $replace, $data[$key]);
} else {
$data[$key] = str_replace($find, $replace, $value);
}
}
} else {
$data = str_replace($find, $replace, $data);
}
}
public function beforeFind(&$Model, $query) {
/* pr("Linkable::beforeFind() begin"); pr($query); */
if (isset($query[$this->_key])) {
@@ -111,6 +133,15 @@ class LinkableBehavior extends ModelBehavior {
$_Model->unbindModel(array('belongsTo' => array($Reference->alias)));
}
if ($associatedThroughReference)
$this->recursive_array_replace("%{MODEL_ALIAS}",
$Reference->alias,
$association['conditions']);
else
$this->recursive_array_replace("%{MODEL_ALIAS}",
$_Model->alias,
$association['conditions']);
// Determine which model holds the foreign key
if (($type === 'hasMany' || $type === 'hasOne') ^ $associatedThroughReference) {
$primaryModel = $Reference;

View File

@@ -19,8 +19,8 @@ class Ledger extends AppModel {
// conditions will be used when JOINing tables
// (such as find with LinkableBehavior)
'conditions' => array('OR' =>
array('LedgerEntry.debit_ledger_id = Ledger.id',
'LedgerEntry.credit_ledger_id = Ledger.id')),
array('LedgerEntry.debit_ledger_id = %{MODEL_ALIAS}.id',
'LedgerEntry.credit_ledger_id = %{MODEL_ALIAS}.id')),
// finderQuery will be used when tables are put
// together across several querys, not with JOIN.

View File

@@ -37,6 +37,41 @@ class LedgerEntry extends AppModel {
}
/**************************************************************************
**************************************************************************
**************************************************************************
* function: ledgerContext query helpers
* - Returns parameters necessary to generate a query which
* puts ledger entries into the context of a ledger. Since
* debit/credit depends on the account type, it is required
* as an argument for each function to avoid having to
* query the ledger/account to find it out.
*/
function ledgerContextFields($ledger_id, $account_type) {
if (in_array($account_type, array('ASSET', 'EXPENSE')))
$ledger_type = 'debit';
else
$ledger_type = 'credit';
return array
('id', 'name', 'comment',
"IF(LedgerEntry.debit_ledger_id = $ledger_id," .
" LedgerEntry.amount, NULL) AS debit",
"IF(LedgerEntry.credit_ledger_id = $ledger_id," .
" LedgerEntry.amount, NULL) AS credit",
"(IF(LedgerEntry.{$ledger_type}_ledger_id = $ledger_id, 1, -1)" .
" * LedgerEntry.amount) AS balance",
);
}
function ledgerContextConditions($ledger_id, $account_type) {
return array
('OR' =>
array(array('LedgerEntry.debit_ledger_id' => $ledger_id),
array('LedgerEntry.credit_ledger_id' => $ledger_id)),
);
}
/**************************************************************************
**************************************************************************
**************************************************************************
@@ -52,32 +87,19 @@ class LedgerEntry extends AppModel {
if (!isset($link))
$link = array('Transaction');
if (in_array($account_type, array('ASSET', 'EXPENSE')))
$ledger_type = 'debit';
else
$ledger_type = 'credit';
if (!isset($cond))
$cond = array();
$fields = $this->ledgerContextFields($ledger_id, $account_type);
$cond[] = $this->ledgerContextConditions($ledger_id, $account_type);
$order = array('Transaction.stamp');
$entries = $this->find
('all',
array('link' => $link,
'fields' =>
array('id', 'name', 'comment',
"IF(LedgerEntry.debit_ledger_id = $ledger_id," .
" LedgerEntry.amount, NULL) AS debit",
"IF(LedgerEntry.credit_ledger_id = $ledger_id," .
" LedgerEntry.amount, NULL) AS credit",
"(IF(LedgerEntry.{$ledger_type}_ledger_id = $ledger_id, 1, -1)" .
" * LedgerEntry.amount) AS balance"),
'conditions' =>
array(isset($cond) ? $cond : array(),
'OR' =>
array(array('LedgerEntry.debit_ledger_id' => $ledger_id),
array('LedgerEntry.credit_ledger_id' => $ledger_id))),
'order' =>
array('Transaction.stamp'),
array('link' => $link,
'fields' => $fields,
'conditions' => $cond,
'order' => $order,
));
return $entries;