Added a closes table to the schema, so that we can keep track of daily closes, deposits, etc. Moved into the model an operation to close a set of ledgers and make a deposit.
git-svn-id: file:///svn-source/pmgr/branches/invoice_receipt_20090629@200 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
@@ -11,7 +11,13 @@ class Account extends AppModel {
|
||||
var $hasOne = array(
|
||||
'CurrentLedger' => array(
|
||||
'className' => 'Ledger',
|
||||
'conditions' => array('NOT' => array('CurrentLedger.closed'))
|
||||
// REVISIT <AP> 20090702:
|
||||
// I would prefer this statement, which has no
|
||||
// engine specific code. However, it doesn't
|
||||
// work with the Linkable behavior. I need to
|
||||
// look into that, just not right now.
|
||||
//'conditions' => array('CurrentLedger.close_id' => null),
|
||||
'conditions' => array('CurrentLedger.close_id IS NULL'),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -175,9 +181,18 @@ class Account extends AppModel {
|
||||
* - Closes the current account ledger, and opens a new one
|
||||
* with the old balance carried forward.
|
||||
*/
|
||||
function closeCurrentLedger($id = null) {
|
||||
function closeCurrentLedger($id = null, $close_id = null) {
|
||||
$contain = array('CurrentLedger' => array('fields' => array('CurrentLedger.id')));
|
||||
|
||||
if (!$close_id) {
|
||||
$close = new Close();
|
||||
$close->create();
|
||||
if (!$close->save(array('stamp' => null), false)) {
|
||||
return false;
|
||||
}
|
||||
$close_id = $close->id;
|
||||
}
|
||||
|
||||
$this->cacheQueries = true;
|
||||
$account = $this->find('all', array
|
||||
('contain' => $contain,
|
||||
@@ -189,7 +204,7 @@ class Account extends AppModel {
|
||||
//pr(compact('id', 'account'));
|
||||
|
||||
foreach ($account AS $acct) {
|
||||
if (!$this->Ledger->closeLedger($acct['CurrentLedger']['id']))
|
||||
if (!$this->Ledger->closeLedger($acct['CurrentLedger']['id'], $close_id))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@@ -488,9 +503,9 @@ class Account extends AppModel {
|
||||
// Set up the debit ledger id
|
||||
if (!isset($entry_data['debit_ledger_id'])) {
|
||||
$entry_data['debit_ledger_id'] =
|
||||
($entry_data['debit_account_id']
|
||||
(isset($entry_data['debit_account_id'])
|
||||
? $A->currentLedgerID($entry_data['debit_account_id'])
|
||||
: ($entry_data['debit_account_name']
|
||||
: (isset($entry_data['debit_account_name'])
|
||||
? $A->currentLedgerID($A->nameToID($entry_data['debit_account_name']))
|
||||
: null
|
||||
)
|
||||
@@ -500,9 +515,9 @@ class Account extends AppModel {
|
||||
// Set up the credit ledger id
|
||||
if (!isset($entry_data['credit_ledger_id'])) {
|
||||
$entry_data['credit_ledger_id'] =
|
||||
($entry_data['credit_account_id']
|
||||
(isset($entry_data['credit_account_id'])
|
||||
? $A->currentLedgerID($entry_data['credit_account_id'])
|
||||
: ($entry_data['credit_account_name']
|
||||
: (isset($entry_data['credit_account_name'])
|
||||
? $A->currentLedgerID($A->nameToID($entry_data['credit_account_name']))
|
||||
: null
|
||||
)
|
||||
@@ -525,6 +540,35 @@ class Account extends AppModel {
|
||||
}
|
||||
|
||||
|
||||
/**************************************************************************
|
||||
**************************************************************************
|
||||
**************************************************************************
|
||||
* function: closeAndDeposit
|
||||
* - Closes the current set of ledgers, transferring
|
||||
* their balances to specified ledger.
|
||||
*/
|
||||
function closeAndDeposit($set, $deposit_account_id) {
|
||||
|
||||
$close = new Close();
|
||||
$close->create();
|
||||
if (!$close->save(array('stamp' => null, 'comment' => 'Deposit'), false)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$transaction = array();
|
||||
foreach ($set AS $ledger) {
|
||||
$ids = $this->postLedgerEntry
|
||||
($transaction,
|
||||
null,
|
||||
array('debit_account_id' => $deposit_account_id,
|
||||
'credit_ledger_id' => $ledger['ledger_id'],
|
||||
'amount' => $ledger['amount']));
|
||||
$transaction = array_intersect_key($ids, array('transaction_id'=>1));
|
||||
$this->Ledger->closeLedger($ledger['ledger_id'], $close->id);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**************************************************************************
|
||||
**************************************************************************
|
||||
**************************************************************************
|
||||
|
||||
Reference in New Issue
Block a user