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/site@81 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
@@ -9,12 +9,13 @@ class AccountsController extends AppController {
|
||||
(// Models
|
||||
'LedgerEntry' =>
|
||||
array('fields' =>
|
||||
array('SUM(IF(LedgerEntry.debit_ledger_id = CurrentLedger.id, LedgerEntry.amount, 0)) AS debits',
|
||||
'SUM(IF(LedgerEntry.credit_ledger_id = CurrentLedger.id, LedgerEntry.amount, 0)) AS credits',
|
||||
array('SUM(IF(LedgerEntry.debit_ledger_id = CurrentLedger.id, LedgerEntry.amount, NULL)) AS debits',
|
||||
'SUM(IF(LedgerEntry.credit_ledger_id = CurrentLedger.id, LedgerEntry.amount, NULL)) AS credits',
|
||||
"SUM(IF(Account.type IN ('ASSET', 'EXPENSE'),
|
||||
IF(LedgerEntry.debit_ledger_id = CurrentLedger.id, 1, -1),
|
||||
IF(LedgerEntry.credit_ledger_id = CurrentLedger.id, 1, -1)
|
||||
)* LedgerEntry.amount) AS balance",
|
||||
) * IF(LedgerEntry.amount, LedgerEntry.amount, 0)
|
||||
) AS balance",
|
||||
'COUNT(LedgerEntry.id) AS entries'),
|
||||
'conditions' =>
|
||||
array('OR' =>
|
||||
@@ -166,99 +167,51 @@ class AccountsController extends AppController {
|
||||
$this->redirect(array('action'=>'index'));
|
||||
}
|
||||
|
||||
/* $this->Account->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', */
|
||||
/* )))); */
|
||||
// Get details about the account and its ledgers (no ledger entries yet)
|
||||
$this->Account->Behaviors->attach('Containable');
|
||||
$account = $this->Account->find
|
||||
('first',
|
||||
array('contain' =>
|
||||
array(// Models
|
||||
'CurrentLedger' =>
|
||||
array('fields' => array('id', 'sequence')),
|
||||
|
||||
/* $this->Account->Behaviors->attach('Containable'); */
|
||||
/* $this->Account->Contain(array('LedgerEntry' => array */
|
||||
/* ( */
|
||||
/* // Models */
|
||||
/* 'Transaction' => array */
|
||||
/* ( */
|
||||
/* // Models */
|
||||
/* 'Customer', */
|
||||
/* )), */
|
||||
|
||||
/* )); */
|
||||
/* } */
|
||||
/* $account = $this->Account->read(null, $id); */
|
||||
'Ledger' =>
|
||||
array('order' => array('Ledger.open_stamp' => 'DESC')),
|
||||
),
|
||||
'conditions' => array(array('Account.id' => $id)),
|
||||
)
|
||||
);
|
||||
$this->Account->Behaviors->detach('Containable');
|
||||
|
||||
// Simply debug stuff... testing.
|
||||
$cond = null;
|
||||
//$cond = array('Transaction.stamp >' => '2009-05-16');
|
||||
|
||||
$this->Account->recursive = -1;
|
||||
$account = $this->Account->read(null, $id);
|
||||
// Get all ledger entries of the CURRENT ledger
|
||||
//$crap = $this->Account->findSecurityDeposits($id);
|
||||
//$crap = $this->Account->findAccountRelatedEntries($id, 8);
|
||||
//pr(array('crap', $crap));
|
||||
/* $this->autoRender = false; */
|
||||
/* return; */
|
||||
|
||||
$account['Ledger'] = $this->Account->find
|
||||
('all', array
|
||||
('link' => array
|
||||
(// Models
|
||||
'Ledger' => array
|
||||
(// Models
|
||||
'LedgerEntry' =>
|
||||
array('fields' =>
|
||||
array('SUM(IF(LedgerEntry.debit_ledger_id = Ledger.id, LedgerEntry.amount, 0)) AS debits',
|
||||
'SUM(IF(LedgerEntry.credit_ledger_id = Ledger.id, LedgerEntry.amount, 0)) AS credits',
|
||||
"SUM(IF(Account.type IN ('ASSET', 'EXPENSE'),
|
||||
IF(LedgerEntry.debit_ledger_id = Ledger.id, 1, -1),
|
||||
IF(LedgerEntry.credit_ledger_id = Ledger.id, 1, -1)
|
||||
) * LedgerEntry.amount) AS balance",
|
||||
'COUNT(LedgerEntry.id) AS entries'),
|
||||
'conditions' =>
|
||||
array('OR' =>
|
||||
array('LedgerEntry.debit_ledger_id = Ledger.id',
|
||||
'LedgerEntry.credit_ledger_id = Ledger.id'),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
'fields' => array(),
|
||||
'conditions' => array('Account.id' => $id),
|
||||
'order' => 'Ledger.name',
|
||||
'group' => 'Ledger.id',
|
||||
'limit' => 4,
|
||||
));
|
||||
// Get all ledger entries of the CURRENT ledger
|
||||
$account['CurrentLedger']['LedgerEntry']
|
||||
= $this->Account->findCurrentLedgerEntries($id, $cond);
|
||||
//pr(array('Account summary', $account));
|
||||
|
||||
|
||||
$account['CurrentLedger'] = array_shift($this->Account->Ledger->find
|
||||
('first', array
|
||||
('link' => array
|
||||
(// Models
|
||||
'Account' => array('fields' => array()),
|
||||
'LedgerEntry' =>
|
||||
array('fields' =>
|
||||
array("SUM(IF(LedgerEntry.debit_ledger_id = Ledger.id, LedgerEntry.amount, 0)) AS debits",
|
||||
"SUM(IF(LedgerEntry.credit_ledger_id = Ledger.id, LedgerEntry.amount, 0)) AS credits",
|
||||
"SUM(IF(Account.type IN ('ASSET', 'EXPENSE'),
|
||||
IF(LedgerEntry.debit_ledger_id = Ledger.id, 1, -1),
|
||||
IF(LedgerEntry.credit_ledger_id = Ledger.id, 1, -1)
|
||||
) * LedgerEntry.amount) AS balance",
|
||||
'COUNT(LedgerEntry.id) AS entries'),
|
||||
'conditions' =>
|
||||
array('OR' =>
|
||||
array("LedgerEntry.debit_ledger_id = Ledger.id",
|
||||
"LedgerEntry.credit_ledger_id = Ledger.id"),
|
||||
),
|
||||
),
|
||||
),
|
||||
'fields' => array(),
|
||||
'conditions' => array(array('Ledger.account_id' => $id),
|
||||
'NOT' => array('Ledger.closed')),
|
||||
//'order' => 'Ledger.name',
|
||||
'group' => 'Ledger.id',
|
||||
//'limit' => 4,
|
||||
)));
|
||||
//pr($account);
|
||||
// Summarize each ledger
|
||||
foreach($account['Ledger'] AS &$ledger)
|
||||
$ledger = array_merge($ledger,
|
||||
$this->Account->Ledger->stats($ledger['id']));
|
||||
|
||||
$balance = $account['CurrentLedger']['balance'];
|
||||
// Obtain the overall account balance
|
||||
$account['Account'] =
|
||||
array_merge($account['Account'],
|
||||
array('stats' => $this->Account->stats($id)));
|
||||
$balance = $account['Account']['stats']['Ledger']['balance'];
|
||||
|
||||
// Prepare to render
|
||||
$title = 'Account: ' . $account['Account']['name'];
|
||||
$this->set(compact('account', 'title', 'balance'));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user