Significant changes to work with the new account/ledger structure. Removed much of the auto-generated model association code. Added helper functions into the models to perform model related work, such as model 'stats' (a bad name for a function to return a summary of pertinent financial information from a given model instance). There is a ton of cleanup to do, but first I want to get it all captured.

git-svn-id: file:///svn-source/pmgr/branches/ledger_transactions_20090605@81 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
abijah
2009-06-10 02:41:23 +00:00
parent 4988d2717f
commit 9f8d4fa9b2
41 changed files with 1441 additions and 916 deletions

View File

@@ -25,8 +25,6 @@ class LedgersController extends AppController {
'group' => 'Ledger.id',
'order' => array('Ledger.name' => 'ASC'));
var $uses = array('Ledger', 'LedgerEntry');
var $sidemenu_links =
array(array('name' => 'Ledgers', 'header' => true),
array('name' => 'Current', 'url' => array('controller' => 'ledgers', 'action' => 'current')),
@@ -115,73 +113,37 @@ class LedgersController extends AppController {
$this->redirect(array('action'=>'index'));
}
/* $this->Ledger->bindModel(array('hasMany' => array */
/* ('LedgerEntry' => array */
/* ('className' => 'LedgerEntry', */
/* 'foreignKey' => false, */
/* 'finderQuery' => 'SELECT `LedgerEntry`.* */
/* FROM pmgr_entries AS `LedgerEntry` */
/* LEFT JOIN pmgr_transactions AS `Transaction` */
/* ON `Transaction`.id = `LedgerEntry`.transaction_id */
/* WHERE LedgerEntry.debit_ledger_id = ({$__cakeID__$}) */
/* OR LedgerEntry.credit_ledger_id = ({$__cakeID__$}) */
/* ORDER BY Transaction.stamp', */
/* )))); */
/* $this->Ledger->Behaviors->attach('Containable'); */
/* $this->Ledger->Contain(array('LedgerEntry' => array */
/* ( */
/* // Models */
/* 'Transaction' => array */
/* ( */
/* // Models */
/* 'Customer', */
/* )), */
/* )); */
/* } */
/* $ledger = $this->Ledger->read(null, $id); */
// Get details about the ledger itself (no entries yet)
$this->Ledger->Behaviors->attach('Containable');
$this->Ledger->Contain(array('Account'));
$ledger = $this->Ledger->read(null, $id);
//pr($ledger);
$ledger = $this->Ledger->find
('first',
array('contain' =>
array(// Models
'Account',
),
'conditions' => array(array('Ledger.id' => $id)),
)
);
$this->Ledger->Behaviors->detach('Containable');
if (in_array($ledger['Account']['type'], array('ASSET', 'EXPENSE')))
$ledger_type = 'debit';
else
$ledger_type = 'credit';
// Get all ledger entries of this ledger
$ledger['LedgerEntry'] = $this->Ledger->findLedgerEntries
($id, $ledger['Account']['type']);
$ledger['LedgerEntry'] = $this->LedgerEntry->find
('all',
array('link' => array
(// Models
'Transaction' =>
array(// Models
'Customer',
)),
'fields' =>
array('id', 'name', 'comment',
"IF(LedgerEntry.debit_ledger_id = $id, LedgerEntry.amount, NULL) AS debit",
"IF(LedgerEntry.credit_ledger_id = $id, LedgerEntry.amount, NULL) AS credit",
"(IF(LedgerEntry.{$ledger_type}_ledger_id = $id, 1, -1) * LedgerEntry.amount) AS balance"),
'conditions' => array('OR' =>
array("LedgerEntry.debit_ledger_id = $id",
"LedgerEntry.credit_ledger_id = $id")),
'order' => array('Transaction.stamp'),
//'limit' => 15,
//'limit' => 2,
));
//pr($ledger);
/* $ledger['LedgerEntry'] = $this->Ledger->LedgerEntry->findInLedgerContext */
/* ($id,
/* $ledger['LedgerEntry'] = $this->Ledger->findLedgerEntries */
/* ($id); */
//($id, $ledger['Account']['type']);
$balance = 0;
foreach($ledger['LedgerEntry'] AS &$entry) {
if (isset($entry[0]))
$entry = array_merge($entry[0], array_diff_key($entry, array(0)));
$balance += $entry['balance'];
}
// Summarize the entries, and obtain the ledger balance
$ledger['Ledger'] =
array_merge($ledger['Ledger'],
array('stats' => $this->Ledger->stats($id)));
$balance = $ledger['Ledger']['stats']['balance'];
// OK, set our view variables and render!
$title = 'Ledger: ' . $ledger['Ledger']['name'];
$this->set(compact('ledger', 'title', 'balance'));
}