diff --git a/controllers/ledger_entries_controller.php b/controllers/ledger_entries_controller.php index 4a21335..05a6fd4 100644 --- a/controllers/ledger_entries_controller.php +++ b/controllers/ledger_entries_controller.php @@ -314,6 +314,9 @@ class LedgerEntriesController extends AppController { 'fields' => array('Account.id', 'Account.name', 'Account.type', 'Account.trackable'), 'conditions' => array('Account.id' => $entry['DebitLedger']['account_id']), )); + $entry['DebitLedger']['Account']['ftype'] = + $this->LedgerEntry->DebitLedger->Account + ->fundamentalType($entry['DebitLedger']['Account']['type']); // Get the Account from CreditLedger $entry['CreditLedger'] += $this->LedgerEntry->CreditLedger->Account->find @@ -322,13 +325,18 @@ class LedgerEntriesController extends AppController { 'fields' => array('Account.id', 'Account.name', 'Account.type', 'Account.trackable'), 'conditions' => array('Account.id' => $entry['CreditLedger']['account_id']), )); + $entry['CreditLedger']['Account']['ftype'] = + $this->LedgerEntry->CreditLedger->Account + ->fundamentalType($entry['CreditLedger']['Account']['type']); // Get the reconciliation balances for this ledger entry $stats = $this->LedgerEntry->stats($id); + $stats['debit']['amount_reconciled'] = $stats['debit_amount_reconciled']; + $stats['credit']['amount_reconciled'] = $stats['credit_amount_reconciled']; if ($entry['DebitLedger']['Account']['trackable']) - $stats['debit_amount_remaining'] = $entry['LedgerEntry']['amount'] - $stats['debit_amount_reconciled']; + $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']; + $stats['credit']['amount_remaining'] = $entry['LedgerEntry']['amount'] - $stats['credit']['amount_reconciled']; //pr($stats); $reconciled = $this->LedgerEntry->findReconciledLedgerEntries($id); diff --git a/views/ledger_entries/view.ctp b/views/ledger_entries/view.ctp index 5e437b6..efc8926 100644 --- a/views/ledger_entries/view.ctp +++ b/views/ledger_entries/view.ctp @@ -12,61 +12,56 @@ echo '
' . "\n"; $transaction = $entry['Transaction']; $debit_ledger = $entry['DebitLedger']; $credit_ledger = $entry['CreditLedger']; +$ledgers['debit'] = $entry['DebitLedger']; +$ledgers['credit'] = $entry['CreditLedger']; $source = $entry['MonetarySource']; $customer = $entry['Customer']; $lease = $entry['Lease']; $entry = $entry['LedgerEntry']; -$rows = array(array('ID', $entry['id']), - array('Transaction', $html->link('#'.$transaction['id'], - array('controller' => 'transactions', - 'action' => 'view', - $transaction['id']))), - array('Timestamp', FormatHelper::datetime($transaction['stamp'])), - array('Effective', FormatHelper::date($entry['effective_date'])), - array('Through', FormatHelper::date($entry['through_date'])), - array('Customer', (isset($customer['name']) - ? $html->link($customer['name'], - array('controller' => 'customers', - 'action' => 'view', - $customer['id'])) - : null)), - array('Lease', (isset($lease['id']) - ? $html->link('#'.$lease['id'], - array('controller' => 'leases', - 'action' => 'view', - $lease['id'])) - : null)), - array('Monetary Source', (isset($source['name']) - ? $html->link($source['name'], - array('controller' => 'monetary_sources', - 'action' => 'view', - $source['id'])) - : null)), - array('Amount', FormatHelper::currency($entry['amount'])), - array('Debit', ($html->link($debit_ledger['Account']['name'], - array('controller' => 'accounts', - 'action' => 'view', - $debit_ledger['Account']['id'])) - . ' (' - . $html->link('#' . $debit_ledger['Account']['id'] - . '-' . $debit_ledger['sequence'], - array('controller' => 'ledgers', - 'action' => 'view', - $debit_ledger['id'])) - . ')')), - array('Credit', ($html->link($credit_ledger['Account']['name'], - array('controller' => 'accounts', - 'action' => 'view', - $credit_ledger['Account']['id'])) - . ' (' - . $html->link('#' . $credit_ledger['Account']['id'] - . '-' . $credit_ledger['sequence'], - array('controller' => 'ledgers', - 'action' => 'view', - $credit_ledger['id'])) - . ')')), - array('Comment', $entry['comment'])); +$rows = array(); +$rows[] = array('ID', $entry['id']); +$rows[] = array('Transaction', $html->link('#'.$transaction['id'], + array('controller' => 'transactions', + 'action' => 'view', + $transaction['id']))); +$rows[] = array('Timestamp', FormatHelper::datetime($transaction['stamp'])); +$rows[] = array('Effective', FormatHelper::date($entry['effective_date'])); +$rows[] = array('Through', FormatHelper::date($entry['through_date'])); +$rows[] = array('Customer', (isset($customer['name']) + ? $html->link($customer['name'], + array('controller' => 'customers', + 'action' => 'view', + $customer['id'])) + : null)); +$rows[] = array('Lease', (isset($lease['id']) + ? $html->link('#'.$lease['id'], + array('controller' => 'leases', + 'action' => 'view', + $lease['id'])) + : null)); +$rows[] = array('Monetary Source', (isset($source['name']) + ? $html->link($source['name'], + array('controller' => 'monetary_sources', + 'action' => 'view', + $source['id'])) + : null)); +$rows[] = array('Amount', FormatHelper::currency($entry['amount'])); + +foreach ($ledgers AS $type => $ledger) + $rows[] = array(ucfirst($type), ($html->link($ledger['Account']['name'], + array('controller' => 'accounts', + 'action' => 'view', + $ledger['Account']['id'])) + . ' (' + . $html->link('#' . $ledger['Account']['id'] + . '-' . $ledger['sequence'], + array('controller' => 'ledgers', + 'action' => 'view', + $ledger['id'])) + . ')')); + +$rows[] = array('Comment', $entry['comment']); echo $this->element('table', @@ -82,18 +77,60 @@ echo $this->element('table', echo '
' . "\n"; $rows = array(); -if ($debit_ledger['Account']['trackable']) { - $rows[] = array("Applied from {$debit_ledger['Account']['name']}:", - FormatHelper::currency($stats['debit_amount_reconciled'])); +foreach ($ledgers AS $type => $ledger) { + pr($ledger); + if (!$ledger['Account']['trackable']) + continue; + + // The two accounts, debit and credit, are actually individual + // entries in each of those account (each make up one of the two + // entries required for "double entry"). This, when we provide + // reconcile information, we're really providing reconcile info + // for two independent accounts. The reconciling entries, + // therefore, are those on the opposite side of the ledger in + // each account. For example, assume this "double" entry is + // + // debit: A/R credit: Cash amount: 55 + // + // Then, our accounts might look like: + // + // RENT TAX A/R CASH BANK + // ------- ------- ------- ------- ------- + // |20 | 20| | | <-- Unrelated + // | | |20 20| | <-- Unrelated + // | | | | | + // |50 | 50| | | <-- Rent paid by this entry + // | |5 5| | | <-- Tax paid by this entry + // | | |55 55| | <-- THIS ENTRY + // | | | | | + // | | | |75 75| <-- Deposit includes this entry + // | | | | | + // + // In this case, we're looking to provide reconcile information + // of A/R for (the credit side of) this entry, and also of Cash + // (for the debit side). Taking the accounts as individual + // entries, instead of the "double entry" representation in the + // database, we're actually providing information on the two + // A/R entries, 50 & 5, which are both debits, i.e. opposite + // entries to the credit of A/R. The cash account entry + // reconciles against the credit of 75. Again, this is the + // opposite entry to the debit of Cash. + // + // Thus, for our debit_ledger_id, we're reconciling against + // credits, and for our credit_ledger_id, against debits. + + // Since we're looking at items opposite to $type (see above), + // our verbage needs to reflect those entry types. + + $verb = ($ledger['Account']['ftype'] == $type) ? 'REDUCTION' : 'INCREASE'; + + $rows[] = array("Applied $verb {$ledger['Account']['name']}:", + FormatHelper::currency($stats[$type]['amount_reconciled'])); $rows[] = array("{$debit_ledger['Account']['name']} Amount Remaining:", - FormatHelper::currency($stats['debit_amount_remaining'])); -} -if ($credit_ledger['Account']['trackable']) { - $rows[] = array("Applied to {$credit_ledger['Account']['name']}:", - FormatHelper::currency($stats['credit_amount_reconciled'])); - $rows[] = array("{$credit_ledger['Account']['name']} Amount Remaining:", - FormatHelper::currency($stats['credit_amount_remaining'])); + FormatHelper::currency($stats[$type]['amount_remaining'])); + } + echo $this->element('table', array('class' => 'summary', 'rows' => $rows, @@ -117,30 +154,25 @@ echo '
' . "\n"; * Reconciliation Ledger Entries */ -if ($debit_ledger['Account']['trackable']) { +foreach ($ledgers AS $type => $ledger) { + if (!$ledger['Account']['trackable']) + continue; + + //$verb = ($ledger['Account']['ftype'] == $type) ? 'REDUCTION' : 'INCREASE'; + $caption = ('Matching ' . (($type == 'credit') ? 'DEBITS' : 'CREDITS') . + ' from Account ' . $ledger['Account']['name']); + //$caption = ('Reconciled Entries ' . ($ledger['Account']['ftype'] == $type ? 'OUT OF' : 'INTO') . + $caption = ('Contribution to entries ' . ($ledger['Account']['ftype'] == $type ? 'OUT OF' : 'INTO') . + ' Account ' . $ledger['Account']['name']); echo $this->element('ledger_entries', array (// Element configuration - 'account_ftype' => 'debit', + 'account_ftype' => $type, 'reconcile_id' => $entry['id'], // Grid configuration 'config' => array - ('caption' => "Applied to " . $debit_ledger['Account']['name'], - 'grid_div_id' => 'debit_reconciliation_ledger_entries', - ), - )); -} - -if ($credit_ledger['Account']['trackable']) { - echo $this->element('ledger_entries', array - (// Element configuration - 'account_ftype' => 'credit', - 'reconcile_id' => $entry['id'], - - // Grid configuration - 'config' => array - ('caption' => "Applied to " . $credit_ledger['Account']['name'], - 'grid_div_id' => 'credit_reconciliation_ledger_entries', + ('caption' => $caption, + 'grid_div_id' => $type.'_reconciliation_ledger_entries', ), )); }