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:
abijah
2009-07-02 09:19:33 +00:00
parent 021c0626a3
commit 1ae1b6a4f3
8 changed files with 114 additions and 54 deletions

View File

@@ -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);
}
}
/**************************************************************************
**************************************************************************
**************************************************************************

12
site/models/close.php Normal file
View File

@@ -0,0 +1,12 @@
<?php
class Close extends AppModel {
var $belongsTo = array(
);
var $hasMany = array(
'Ledger',
);
}
?>

View File

@@ -9,6 +9,8 @@ class Ledger extends AppModel {
var $belongsTo = array(
'Account',
'PriorLedger' => array('className' => 'Ledger'),
'Close',
);
var $hasMany = array(
@@ -50,23 +52,21 @@ class Ledger extends AppModel {
* function: closeLedger
* - Closes the current ledger, and returns a fresh one
*/
function closeLedger($id) {
function closeLedger($id, $close_id) {
$this->recursive = -1;
$stamp = date('Y-m-d G:i:s');
$this->id = $id;
$this->read();
$this->data['Ledger']['close_stamp'] = $stamp;
$this->data['Ledger']['closed'] = 1;
$this->data['Ledger']['close_id'] = $close_id;
$this->save($this->data, false);
$stats = $this->stats($id);
$this->read();
$this->data['Ledger']['id'] = null;
$this->data['Ledger']['closed'] = 0;
$this->data['Ledger']['open_stamp'] = $stamp;
$this->data['Ledger']['close_stamp'] = null;
$this->data['Ledger']['close_id'] = null;
$this->data['Ledger']['prior_ledger_id'] = $id;
$this->data['Ledger']['comment'] = null;
++$this->data['Ledger']['sequence'];
$this->id = null;
@@ -83,9 +83,7 @@ class Ledger extends AppModel {
// Create a transaction for balance transfer
$transaction = new Transaction();
$transaction->create();
if (!$transaction->save(array(),
array('validate' => false,
))) {
if (!$transaction->save(array(), false)) {
return null;
}
@@ -100,9 +98,7 @@ class Ledger extends AppModel {
$carry_entry = new LedgerEntry();
$carry_entry->create();
if (!$carry_entry->save($carry_entry_data,
array('validate' => false,
))) {
if (!$carry_entry->save($carry_entry_data, false)) {
return null;
}