This rework is nowhere near complete, but there are certain things that are falling in place, and worth capturing. I started a branch for just this purpose of being able to check in intermediate work.

git-svn-id: file:///svn-source/pmgr/branches/yafr_20090716@352 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
abijah
2009-07-19 23:35:25 +00:00
parent e303898a95
commit 6ac0204baf
26 changed files with 3058 additions and 1767 deletions

View File

@@ -1,12 +1,6 @@
<?php
class Ledger extends AppModel {
var $name = 'Ledger';
var $validate = array(
'id' => array('numeric'),
'name' => array('notempty'),
);
var $belongsTo = array(
'Account',
'PriorLedger' => array('className' => 'Ledger'),
@@ -14,32 +8,33 @@ class Ledger extends AppModel {
);
var $hasMany = array(
'LedgerEntry' => array(
'DoubleEntry' => array(
'foreignKey' => false,
// conditions will be used when JOINing tables
// (such as find with LinkableBehavior)
'conditions' => array('OR' =>
array('LedgerEntry.debit_ledger_id = %{MODEL_ALIAS}.id',
'LedgerEntry.credit_ledger_id = %{MODEL_ALIAS}.id')),
array('DoubleEntry.debit_ledger_id = %{MODEL_ALIAS}.id',
'DoubleEntry.credit_ledger_id = %{MODEL_ALIAS}.id')),
// finderQuery will be used when tables are put
// together across several querys, not with JOIN.
// (such as find with ContainableBehavior)
'finderQuery' => 'SELECT `LedgerEntry`.*
FROM pmgr_ledger_entries AS `LedgerEntry`
WHERE LedgerEntry.debit_ledger_id = ({$__cakeID__$})
OR LedgerEntry.credit_ledger_id = ({$__cakeID__$})',
'finderQuery' => 'SELECT `DoubleEntry`.*
FROM pmgr_double_entries AS `DoubleEntry`
WHERE DoubleEntry.debit_ledger_id = ({$__cakeID__$})
OR DoubleEntry.credit_ledger_id = ({$__cakeID__$})',
'counterQuery' => ''
),
'DebitLedgerEntry' => array(
'className' => 'LedgerEntry',
'className' => 'DoubleEntry',
'foreignKey' => 'debit_ledger_id',
'dependent' => false,
),
'CreditLedgerEntry' => array(
'className' => 'LedgerEntry',
'className' => 'DoubleEntry',
'foreignKey' => 'credit_ledger_id',
'dependent' => false,
),
@@ -124,7 +119,7 @@ class Ledger extends AppModel {
'comment' => "Ledger Balance Forward",
);
$carry_entry = new LedgerEntry();
$carry_entry = new DoubleEntry();
$carry_entry->create();
if (!$carry_entry->save($carry_entry_data, false)) {
return null;
@@ -137,11 +132,11 @@ class Ledger extends AppModel {
/**************************************************************************
**************************************************************************
**************************************************************************
* function: findLedgerEntries
* function: ledgerEntries
* - Returns an array of ledger entries that belong to a given
* ledger. There is extra work done... see the LedgerEntry model.
* ledger. There is extra work done... see the DoubleEntry model.
*/
function findLedgerEntries($id, $account_type = null, $cond = null, $link = null) {
function ledgerEntries($id, $account_type = null, $cond = null, $link = null) {
/* pr(array('function' => 'Ledger::findLedgerEntries', */
/* 'args' => compact('id', 'account_type', 'cond', 'link'), */
/* )); */
@@ -177,7 +172,7 @@ class Ledger extends AppModel {
'credit' => $stats['credits'],
'balance' => $stats['balance']),
'LedgerEntry' => array('id' => null,
'DoubleEntry' => array('id' => null,
//'comment' => "Balance Forward from $date"),
'comment' => "-- SUMMARY OF EXCLUDED ENTRIES --"),
@@ -188,7 +183,7 @@ class Ledger extends AppModel {
));
}
$entries = $this->LedgerEntry->findInLedgerContext($id, $account_type, $cond, $link);
$entries = $this->DoubleEntry->findInLedgerContext($id, $account_type, $cond, $link);
/* pr(array('function' => 'Ledger::findLedgerEntries', */
/* 'args' => compact('id', 'account_type', 'cond', 'link'), */
/* 'vars' => compact('ledger'), */
@@ -214,23 +209,23 @@ class Ledger extends AppModel {
('link' =>
array(// Models
'Account' => array('fields' => array()),
//'LedgerEntry' => array('fields' => array()),
'LedgerEntry' =>
//'DoubleEntry' => array('fields' => array()),
'DoubleEntry' =>
array('fields' => array(),
'Transaction' => array('fields' => array('stamp')),
),
),
'fields' =>
array("SUM(IF(LedgerEntry.debit_ledger_id = Ledger.id,
LedgerEntry.amount, NULL)) AS debits",
"SUM(IF(LedgerEntry.credit_ledger_id = Ledger.id,
LedgerEntry.amount, NULL)) AS credits",
array("SUM(IF(DoubleEntry.debit_ledger_id = Ledger.id,
DoubleEntry.amount, NULL)) AS debits",
"SUM(IF(DoubleEntry.credit_ledger_id = Ledger.id,
DoubleEntry.amount, NULL)) 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)
) * IF(LedgerEntry.amount, LedgerEntry.amount, 0)
IF(DoubleEntry.debit_ledger_id = Ledger.id, 1, -1),
IF(DoubleEntry.credit_ledger_id = Ledger.id, 1, -1)
) * IF(DoubleEntry.amount, DoubleEntry.amount, 0)
) AS balance",
"COUNT(LedgerEntry.id) AS entries"),
"COUNT(DoubleEntry.id) AS entries"),
'conditions' => $cond,
'group' => 'Ledger.id',
));