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

@@ -905,23 +905,26 @@ DROP TABLE IF EXISTS `pmgr_ledgers`;
CREATE TABLE `pmgr_ledgers` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
-- REVISIT <AP>: 20090605
-- If ledgers do need to be closed, then we'll need
-- to add several parameters indicating the period
-- this particular ledger is valid and so on.
`account_id` INT(10) UNSIGNED NOT NULL,
`sequence` INT(10) UNSIGNED DEFAULT 1,
`closed` INT UNSIGNED DEFAULT 0,
-- REVISIT <AP>: 20090607
-- Probably, a single close should have the ability to close
-- many different account ledgers. For now, just timestamping.
-- `close_id` INT(10) UNSIGNED NOT NULL,
`prior_ledger_id` INT(10) UNSIGNED DEFAULT NULL,
`close_id` INT(10) UNSIGNED DEFAULT NULL,
`open_stamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`close_stamp` DATETIME DEFAULT NULL,
`comment` VARCHAR(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
-- ----------------------------------------------------------------------
-- ----------------------------------------------------------------------
-- TABLE pmgr_closes
DROP TABLE IF EXISTS `pmgr_closes`;
CREATE TABLE `pmgr_closes` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`stamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`comment` VARCHAR(255) DEFAULT NULL,
PRIMARY KEY (`id`)

View File

@@ -467,7 +467,6 @@ $result = query($db_handle, $query);
foreach $row (@$result) {
addRow('ledgers',
{ 'account_id' => $row->{'id'},
'open_stamp' => '2009-01-01',
'comment' => undef });
$newdb{'lookup'}{'account'}{$row->{'name'}}

View File

@@ -147,17 +147,15 @@ class AccountsController extends AppController {
*/
function deposit() {
if ($this->data) {
pr($this->data);
// Action the close based on provided data
//pr($this->data);
// Get data about each closed ledger.
$deposit = array('total' => 0, 'ledgers' => array());
$transaction = array();
foreach ($this->data['Tillable']['Ledger'] AS $ledger_id => $ledger) {
if (!$ledger['checked'])
continue;
/* pr(array('checkpoint' => 'save data', */
/* compact('ledger'))); */
$ledger_entries =
$this->Account->Ledger->find
('all',
@@ -184,29 +182,29 @@ class AccountsController extends AppController {
),
));
//pr($ledger_entries);
$deposit['total'] += $ledger['amount'];
$deposit['ledgers'][] = array('name' => $ledger['account_name'],
'total' => $ledger['amount'],
'entries' => $ledger_entries);
if ($ledger['amount'] != 0) {
$ids = $this->Account->postLedgerEntry
($transaction,
null,
array('debit_account_id' => $this->data['Deposit']['Account']['id'],
'credit_ledger_id' => $ledger_id,
'amount' => $ledger['amount']));
$transaction = array_intersect_key($ids, array('transaction_id'=>1));
$this->Account->closeCurrentLedger($ledger['account_id']);
}
}
// Estabish a set of ledgers to close
$set = array();
foreach ($this->data['Tillable']['Ledger'] AS $ledger_id => $ledger) {
if (!$ledger['checked'])
continue;
$set[] = array('ledger_id' => $ledger_id, 'amount' => $ledger['amount']);
}
// Pull the trigger
$this->Account->closeAndDeposit($set, $this->data['Deposit']['Account']['id']);
$this->set(compact('deposit'));
$this->render('deposit_slip');
return;
}
// Prepare a close page...
$tillable_account = $this->Account->relatedAccounts('tillable');
$depositable_account = $this->Account->relatedAccounts('depositable');
@@ -240,7 +238,8 @@ class AccountsController extends AppController {
array('fields' => array('id', 'sequence')),
'Ledger' =>
array('order' => array('Ledger.open_stamp' => 'DESC')),
array('Close' => array
('order' => array('Close.stamp' => 'DESC'))),
),
'conditions' => array(array('Account.id' => $id)),
)

View File

@@ -59,6 +59,7 @@ class LedgersController extends AppController {
array(// Models
'Account',
'LedgerEntry',
'Close',
),
);
}

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;
}

View File

@@ -4,17 +4,20 @@
$cols = array();
$cols['ID'] = array('index' => 'id_sequence', 'formatter' => 'id');
$cols['Account'] = array('index' => 'Account.name', 'formatter' => 'longname');
$cols['Open Date'] = array('index' => 'Ledger.open_stamp', 'formatter' => 'date');
$cols['Close Date'] = array('index' => 'Ledger.close_stamp', 'formatter' => 'date');
//$cols['Open Date'] = array('index' => 'PriorClose.stamp', 'formatter' => 'date');
$cols['Close Date'] = array('index' => 'Close.stamp', 'formatter' => 'date');
$cols['Comment'] = array('index' => 'Ledger.comment', 'formatter' => 'comment');
$cols['Entries'] = array('index' => 'entries', 'width' => '60', 'align' => 'right');
$cols['Debits'] = array('index' => 'debits', 'formatter' => 'currency');
$cols['Credits'] = array('index' => 'credits', 'formatter' => 'currency');
$cols['Balance'] = array('index' => 'balance', 'formatter' => 'currency');
$custom_post_data = compact('nothing');
$jqGrid_options = array('jqGridColumns' => $cols,
'controller' => 'ledgers',
'caption' => isset($caption) ? $caption : null);
);
$jqGrid_options += compact('grid_div_id', 'grid_id', 'caption', 'grid_setup', 'limit');
if (isset($ledgers)) {
$jqGrid_options += array('custom_ids' =>
@@ -27,4 +30,7 @@ else {
$jqGrid_options += array('search_fields' => array('Account'));
}
$jqGrid_options += compact('custom_post_data');
$jqGrid_options['sort_column'] = 'ID';
$jqGrid_options['sort_order'] = 'DESC';
echo $this->element('jqGrid', $jqGrid_options);