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/site@200 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
@@ -147,17 +147,15 @@ class AccountsController extends AppController {
|
|||||||
*/
|
*/
|
||||||
function deposit() {
|
function deposit() {
|
||||||
if ($this->data) {
|
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());
|
$deposit = array('total' => 0, 'ledgers' => array());
|
||||||
$transaction = array();
|
|
||||||
foreach ($this->data['Tillable']['Ledger'] AS $ledger_id => $ledger) {
|
foreach ($this->data['Tillable']['Ledger'] AS $ledger_id => $ledger) {
|
||||||
if (!$ledger['checked'])
|
if (!$ledger['checked'])
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
/* pr(array('checkpoint' => 'save data', */
|
|
||||||
/* compact('ledger'))); */
|
|
||||||
|
|
||||||
$ledger_entries =
|
$ledger_entries =
|
||||||
$this->Account->Ledger->find
|
$this->Account->Ledger->find
|
||||||
('all',
|
('all',
|
||||||
@@ -184,29 +182,29 @@ class AccountsController extends AppController {
|
|||||||
),
|
),
|
||||||
));
|
));
|
||||||
|
|
||||||
//pr($ledger_entries);
|
|
||||||
$deposit['total'] += $ledger['amount'];
|
$deposit['total'] += $ledger['amount'];
|
||||||
$deposit['ledgers'][] = array('name' => $ledger['account_name'],
|
$deposit['ledgers'][] = array('name' => $ledger['account_name'],
|
||||||
'total' => $ledger['amount'],
|
'total' => $ledger['amount'],
|
||||||
'entries' => $ledger_entries);
|
'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->set(compact('deposit'));
|
||||||
$this->render('deposit_slip');
|
$this->render('deposit_slip');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Prepare a close page...
|
||||||
$tillable_account = $this->Account->relatedAccounts('tillable');
|
$tillable_account = $this->Account->relatedAccounts('tillable');
|
||||||
$depositable_account = $this->Account->relatedAccounts('depositable');
|
$depositable_account = $this->Account->relatedAccounts('depositable');
|
||||||
|
|
||||||
@@ -240,7 +238,8 @@ class AccountsController extends AppController {
|
|||||||
array('fields' => array('id', 'sequence')),
|
array('fields' => array('id', 'sequence')),
|
||||||
|
|
||||||
'Ledger' =>
|
'Ledger' =>
|
||||||
array('order' => array('Ledger.open_stamp' => 'DESC')),
|
array('Close' => array
|
||||||
|
('order' => array('Close.stamp' => 'DESC'))),
|
||||||
),
|
),
|
||||||
'conditions' => array(array('Account.id' => $id)),
|
'conditions' => array(array('Account.id' => $id)),
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -59,6 +59,7 @@ class LedgersController extends AppController {
|
|||||||
array(// Models
|
array(// Models
|
||||||
'Account',
|
'Account',
|
||||||
'LedgerEntry',
|
'LedgerEntry',
|
||||||
|
'Close',
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,13 @@ class Account extends AppModel {
|
|||||||
var $hasOne = array(
|
var $hasOne = array(
|
||||||
'CurrentLedger' => array(
|
'CurrentLedger' => array(
|
||||||
'className' => 'Ledger',
|
'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
|
* - Closes the current account ledger, and opens a new one
|
||||||
* with the old balance carried forward.
|
* 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')));
|
$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;
|
$this->cacheQueries = true;
|
||||||
$account = $this->find('all', array
|
$account = $this->find('all', array
|
||||||
('contain' => $contain,
|
('contain' => $contain,
|
||||||
@@ -189,7 +204,7 @@ class Account extends AppModel {
|
|||||||
//pr(compact('id', 'account'));
|
//pr(compact('id', 'account'));
|
||||||
|
|
||||||
foreach ($account AS $acct) {
|
foreach ($account AS $acct) {
|
||||||
if (!$this->Ledger->closeLedger($acct['CurrentLedger']['id']))
|
if (!$this->Ledger->closeLedger($acct['CurrentLedger']['id'], $close_id))
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@@ -488,9 +503,9 @@ class Account extends AppModel {
|
|||||||
// Set up the debit ledger id
|
// Set up the debit ledger id
|
||||||
if (!isset($entry_data['debit_ledger_id'])) {
|
if (!isset($entry_data['debit_ledger_id'])) {
|
||||||
$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'])
|
? $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']))
|
? $A->currentLedgerID($A->nameToID($entry_data['debit_account_name']))
|
||||||
: null
|
: null
|
||||||
)
|
)
|
||||||
@@ -500,9 +515,9 @@ class Account extends AppModel {
|
|||||||
// Set up the credit ledger id
|
// Set up the credit ledger id
|
||||||
if (!isset($entry_data['credit_ledger_id'])) {
|
if (!isset($entry_data['credit_ledger_id'])) {
|
||||||
$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'])
|
? $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']))
|
? $A->currentLedgerID($A->nameToID($entry_data['credit_account_name']))
|
||||||
: null
|
: 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
models/close.php
Normal file
12
models/close.php
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
<?php
|
||||||
|
class Close extends AppModel {
|
||||||
|
|
||||||
|
var $belongsTo = array(
|
||||||
|
);
|
||||||
|
|
||||||
|
var $hasMany = array(
|
||||||
|
'Ledger',
|
||||||
|
);
|
||||||
|
|
||||||
|
}
|
||||||
|
?>
|
||||||
@@ -9,6 +9,8 @@ class Ledger extends AppModel {
|
|||||||
|
|
||||||
var $belongsTo = array(
|
var $belongsTo = array(
|
||||||
'Account',
|
'Account',
|
||||||
|
'PriorLedger' => array('className' => 'Ledger'),
|
||||||
|
'Close',
|
||||||
);
|
);
|
||||||
|
|
||||||
var $hasMany = array(
|
var $hasMany = array(
|
||||||
@@ -50,23 +52,21 @@ class Ledger extends AppModel {
|
|||||||
* function: closeLedger
|
* function: closeLedger
|
||||||
* - Closes the current ledger, and returns a fresh one
|
* - Closes the current ledger, and returns a fresh one
|
||||||
*/
|
*/
|
||||||
function closeLedger($id) {
|
function closeLedger($id, $close_id) {
|
||||||
$this->recursive = -1;
|
$this->recursive = -1;
|
||||||
|
|
||||||
$stamp = date('Y-m-d G:i:s');
|
$stamp = date('Y-m-d G:i:s');
|
||||||
$this->id = $id;
|
$this->id = $id;
|
||||||
$this->read();
|
$this->read();
|
||||||
$this->data['Ledger']['close_stamp'] = $stamp;
|
$this->data['Ledger']['close_id'] = $close_id;
|
||||||
$this->data['Ledger']['closed'] = 1;
|
|
||||||
$this->save($this->data, false);
|
$this->save($this->data, false);
|
||||||
|
|
||||||
$stats = $this->stats($id);
|
$stats = $this->stats($id);
|
||||||
|
|
||||||
$this->read();
|
$this->read();
|
||||||
$this->data['Ledger']['id'] = null;
|
$this->data['Ledger']['id'] = null;
|
||||||
$this->data['Ledger']['closed'] = 0;
|
$this->data['Ledger']['close_id'] = null;
|
||||||
$this->data['Ledger']['open_stamp'] = $stamp;
|
$this->data['Ledger']['prior_ledger_id'] = $id;
|
||||||
$this->data['Ledger']['close_stamp'] = null;
|
|
||||||
$this->data['Ledger']['comment'] = null;
|
$this->data['Ledger']['comment'] = null;
|
||||||
++$this->data['Ledger']['sequence'];
|
++$this->data['Ledger']['sequence'];
|
||||||
$this->id = null;
|
$this->id = null;
|
||||||
@@ -83,9 +83,7 @@ class Ledger extends AppModel {
|
|||||||
// Create a transaction for balance transfer
|
// Create a transaction for balance transfer
|
||||||
$transaction = new Transaction();
|
$transaction = new Transaction();
|
||||||
$transaction->create();
|
$transaction->create();
|
||||||
if (!$transaction->save(array(),
|
if (!$transaction->save(array(), false)) {
|
||||||
array('validate' => false,
|
|
||||||
))) {
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,9 +98,7 @@ class Ledger extends AppModel {
|
|||||||
|
|
||||||
$carry_entry = new LedgerEntry();
|
$carry_entry = new LedgerEntry();
|
||||||
$carry_entry->create();
|
$carry_entry->create();
|
||||||
if (!$carry_entry->save($carry_entry_data,
|
if (!$carry_entry->save($carry_entry_data, false)) {
|
||||||
array('validate' => false,
|
|
||||||
))) {
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,17 +4,20 @@
|
|||||||
$cols = array();
|
$cols = array();
|
||||||
$cols['ID'] = array('index' => 'id_sequence', 'formatter' => 'id');
|
$cols['ID'] = array('index' => 'id_sequence', 'formatter' => 'id');
|
||||||
$cols['Account'] = array('index' => 'Account.name', 'formatter' => 'longname');
|
$cols['Account'] = array('index' => 'Account.name', 'formatter' => 'longname');
|
||||||
$cols['Open Date'] = array('index' => 'Ledger.open_stamp', 'formatter' => 'date');
|
//$cols['Open Date'] = array('index' => 'PriorClose.stamp', 'formatter' => 'date');
|
||||||
$cols['Close Date'] = array('index' => 'Ledger.close_stamp', 'formatter' => 'date');
|
$cols['Close Date'] = array('index' => 'Close.stamp', 'formatter' => 'date');
|
||||||
$cols['Comment'] = array('index' => 'Ledger.comment', 'formatter' => 'comment');
|
$cols['Comment'] = array('index' => 'Ledger.comment', 'formatter' => 'comment');
|
||||||
$cols['Entries'] = array('index' => 'entries', 'width' => '60', 'align' => 'right');
|
$cols['Entries'] = array('index' => 'entries', 'width' => '60', 'align' => 'right');
|
||||||
$cols['Debits'] = array('index' => 'debits', 'formatter' => 'currency');
|
$cols['Debits'] = array('index' => 'debits', 'formatter' => 'currency');
|
||||||
$cols['Credits'] = array('index' => 'credits', 'formatter' => 'currency');
|
$cols['Credits'] = array('index' => 'credits', 'formatter' => 'currency');
|
||||||
$cols['Balance'] = array('index' => 'balance', 'formatter' => 'currency');
|
$cols['Balance'] = array('index' => 'balance', 'formatter' => 'currency');
|
||||||
|
|
||||||
|
$custom_post_data = compact('nothing');
|
||||||
$jqGrid_options = array('jqGridColumns' => $cols,
|
$jqGrid_options = array('jqGridColumns' => $cols,
|
||||||
'controller' => 'ledgers',
|
'controller' => 'ledgers',
|
||||||
'caption' => isset($caption) ? $caption : null);
|
);
|
||||||
|
$jqGrid_options += compact('grid_div_id', 'grid_id', 'caption', 'grid_setup', 'limit');
|
||||||
|
|
||||||
|
|
||||||
if (isset($ledgers)) {
|
if (isset($ledgers)) {
|
||||||
$jqGrid_options += array('custom_ids' =>
|
$jqGrid_options += array('custom_ids' =>
|
||||||
@@ -27,4 +30,7 @@ else {
|
|||||||
$jqGrid_options += array('search_fields' => array('Account'));
|
$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);
|
echo $this->element('jqGrid', $jqGrid_options);
|
||||||
|
|||||||
Reference in New Issue
Block a user