Added initial support for ledger entry reconciliation (e.g. matching payments to charges).

git-svn-id: file:///svn-source/pmgr/branches/ledger_transactions_20090605@152 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
abijah
2009-06-16 21:40:46 +00:00
parent a83edef049
commit fae90c3fa9
5 changed files with 141 additions and 22 deletions

View File

@@ -26,8 +26,7 @@ class LedgerEntriesController extends AppController {
*/
function jqGridDataTables(&$params, &$model) {
return array
('link' =>
$link =
array(// Models
'Transaction' =>
array('fields' => array('id', 'stamp'),
@@ -52,8 +51,16 @@ class LedgerEntriesController extends AppController {
'MonetarySource' =>
array('fields' => array('id', 'name'),
),
),
);
if ($params['action'] === 'reconcile') {
$type = $params['custom']['reconcile'];
$link[$type.'ReconciliationLedgerEntry'] =
array('fields' => array('Reconciliation.amount'));
}
return array('link' => $link);
}
function jqGridDataFields(&$params, &$model) {
@@ -63,8 +70,17 @@ class LedgerEntriesController extends AppController {
function jqGridDataConditions(&$params, &$model) {
$conditions = parent::jqGridDataConditions($params, $model);
if ($params['action'] === 'ledger') {
$conditions[] = $model->ledgerContextConditions($params['custom']['ledger_id'],
$params['custom']['account_type']);
}
if ($params['action'] === 'reconcile') {
$type = $params['custom']['reconcile'];
$other = ($type === 'Credit' ? 'debit' : 'credit');
$conditions[] = array('Reconciliation.'.$other.'_ledger_entry_id' => $params['custom']['reconcile_id']);
}
return $conditions;
}
@@ -118,25 +134,31 @@ class LedgerEntriesController extends AppController {
// seems). We'll have to break out each Account separately.
// Get the Account from DebitLedger
$account = $this->LedgerEntry->DebitLedger->Account->find
$entry['DebitLedger'] += $this->LedgerEntry->DebitLedger->Account->find
('first',
array('contain' => true,
'fields' => array('Account.id', 'Account.name', 'Account.type'),
'fields' => array('Account.id', 'Account.name', 'Account.type', 'Account.trackable'),
'conditions' => array('Account.id' => $entry['DebitLedger']['account_id']),
));
$entry['DebitLedger'] = array_merge($entry['DebitLedger'], $account);
// Get the Account from CreditLedger
$account = $this->LedgerEntry->CreditLedger->Account->find
$entry['CreditLedger'] += $this->LedgerEntry->CreditLedger->Account->find
('first',
array('contain' => true,
'fields' => array('Account.id', 'Account.name', 'Account.type'),
'fields' => array('Account.id', 'Account.name', 'Account.type', 'Account.trackable'),
'conditions' => array('Account.id' => $entry['CreditLedger']['account_id']),
));
$entry['CreditLedger'] = array_merge($entry['CreditLedger'], $account);
// Get the reconciliation balances for this ledger entry
$stats = $this->LedgerEntry->stats($id);
if ($entry['DebitLedger']['Account']['trackable'])
$stats['debit_amount_remaining'] = $entry['LedgerEntry']['amount'] - $stats['debit_amount_reconciled'];
if ($entry['CreditLedger']['Account']['trackable'])
$stats['credit_amount_remaining'] = $entry['LedgerEntry']['amount'] - $stats['credit_amount_reconciled'];
//pr($stats);
// Prepare to render.
$title = "Ledger Entry #{$entry['LedgerEntry']['id']}";
$this->set(compact('entry', 'title'));
$this->set(compact('entry', 'title', 'stats'));
}
}

View File

@@ -22,6 +22,20 @@ class LedgerEntry extends AppModel {
),
);
var $hasAndBelongsToMany = array(
'DebitReconciliationLedgerEntry' => array(
'className' => 'LedgerEntry',
'joinTable' => 'reconciliations',
'foreignKey' => 'credit_ledger_entry_id',
'associationForeignKey' => 'debit_ledger_entry_id',
),
'CreditReconciliationLedgerEntry' => array(
'className' => 'LedgerEntry',
'joinTable' => 'reconciliations',
'foreignKey' => 'debit_ledger_entry_id',
'associationForeignKey' => 'credit_ledger_entry_id',
),
);
/**************************************************************************
**************************************************************************
@@ -104,6 +118,39 @@ class LedgerEntry extends AppModel {
return $entries;
}
}
?>
/**************************************************************************
**************************************************************************
**************************************************************************
* function: stats
* - Returns summary data from the requested ledger entry
*/
function stats($id) {
$query = array
(
'fields' => array("SUM(Reconciliation.amount) AS 'reconciled'"),
'conditions' => array(isset($cond) ? $cond : array(),
array('LedgerEntry.id' => $id)),
'group' => 'LedgerEntry.id',
);
// Get the applied amounts on the debit side
$query['link'] =
array('DebitReconciliationLedgerEntry' => array('alias' => 'DRLE', 'DRLETransaction' => array('class' => 'Transaction')));
$tmpstats = $this->find('first', $query);
$stats['debit_amount_reconciled'] = $tmpstats[0]['reconciled'];
// Get the applied amounts on the credit side
$query['link'] =
array('CreditReconciliationLedgerEntry' => array('alias' => 'CRLE', 'CRLETransaction' => array('class' => 'Transaction')));
$tmpstats = $this->find('first', $query);
$stats['credit_amount_reconciled'] = $tmpstats[0]['reconciled'];
return $stats;
}
}

View File

@@ -8,19 +8,38 @@ $cols['Date'] = array('index' => 'Transaction.stamp', 'formatter' =>
$cols['Debit Account'] = array('index' => 'DebitAccount.name', 'formatter' => 'longname');
$cols['Credit Account'] = array('index' => 'CreditAccount.name', 'formatter' => 'longname');
$cols['Source'] = array('index' => 'MonetarySource.name', 'formatter' => 'name');
$cols['Comment'] = array('index' => 'LedgerEntry.comment', 'formatter' => 'comment');
$cols['Comment'] = array('index' => 'LedgerEntry.comment', 'formatter' => 'comment', 'width'=>150);
$cols['Debit'] = array('index' => 'debit', 'formatter' => 'currency');
$cols['Credit'] = array('index' => 'credit', 'formatter' => 'currency');
if (isset($reconcile))
$cols['Applied'] = array('index' => "Reconciliation.amount", 'formatter' => 'currency');
$custom_post_data = array('ledger_id' => $ledger_id,
'account_type' => $account_type);
$jqGrid_options = array('jqGridColumns' => $cols,
'grid_id' => isset($grid_id) ? $grid_id : null,
'caption' => isset($caption) ? $caption : null,
'controller' => 'ledger_entries',
'caption' => isset($caption) ? $caption : null);
);
if (isset($ledger_id)) {
$jqGrid_options += array('custom_post_data' => array('ledger_id' => $ledger_id,
'account_type' => $account_type),
if (isset($ledger_entries)) {
$jqGrid_options += array('custom_ids' =>
array_map(create_function('$data',
'return $data["id"];'),
$ledger_entries),
'limit' => 10);
}
elseif (isset($reconcile)) {
$custom_post_data += compact('reconcile', 'reconcile_id');
$jqGrid_options += array('action' => 'reconcile',
'limit' => 5);
}
else {
$jqGrid_options += array('action' => 'ledger',
'limit' => 50);
}
$jqGrid_options += compact('custom_post_data');
echo $this->element('jqGrid', $jqGrid_options);

View File

@@ -66,6 +66,14 @@ echo $this->element('table',
echo '<div class="infobox">' . "\n";
$rows = array();
if ($debit_ledger['Account']['trackable']) {
$rows[] = array('Debit Amount Reconciled:', FormatHelper::currency($stats['debit_amount_reconciled']));
$rows[] = array('Debit Amount Remaining:', FormatHelper::currency($stats['debit_amount_remaining']));
}
if ($credit_ledger['Account']['trackable']) {
$rows[] = array('Credit Amount Reconciled:', FormatHelper::currency($stats['credit_amount_reconciled']));
$rows[] = array('Credit Amount Remaining:', FormatHelper::currency($stats['credit_amount_remaining']));
}
echo $this->element('table',
array('class' => 'summary',
'rows' => $rows,
@@ -85,6 +93,29 @@ echo '</div>' . "\n";
echo '<div CLASS="detail supporting">' . "\n";
/**********************************************************************
* Reconciliation Ledger Entries
*/
echo $this->element('ledger_entries',
array('caption' => "Debit Applications",
'grid_id' => 'debit_reconciliation_ledger_entries',
'ledger_id' => $debit_ledger['id'],
'account_type' => $debit_ledger['Account']['type'],
'reconcile' => 'Credit', // Looking for credits to match this debit
'reconcile_id' => $entry['id'],
));
echo $this->element('ledger_entries',
array('caption' => "Credit Applications",
'grid_id' => 'credit_reconciliation_ledger_entries',
'ledger_id' => $credit_ledger['id'],
'account_type' => $credit_ledger['Account']['type'],
'reconcile' => 'Debit', // Looking for debits to match this credit
'reconcile_id' => $entry['id'],
));
/* End "detail supporting" div */
echo '</div>' . "\n";

View File

@@ -59,7 +59,7 @@ echo '<div CLASS="detail supporting">' . "\n";
/**********************************************************************
* Ledger
* Ledger Entries
*/
echo $this->element('ledger_entries',