Implemented a bank deposit routine, to transfer funds out of the till and into the bank.

git-svn-id: file:///svn-source/pmgr/branches/invoice_receipt_20090629@196 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
abijah
2009-07-01 11:10:57 +00:00
parent 44df78e69f
commit 8d87a0698c
6 changed files with 316 additions and 19 deletions

View File

@@ -92,6 +92,26 @@ class Account extends AppModel {
function invoiceAccountID() { return $this->nameToID('Invoice'); }
function receiptAccountID() { return $this->nameToID('Receipt'); }
/**************************************************************************
**************************************************************************
**************************************************************************
* function: relatedAccounts
* - Returns an array of accounts related by similar attributes
*/
function relatedAccounts($attribute) {
$this->cacheQueries = true;
$account = $this->find('all', array
('contain' => array('CurrentLedger'),
'fields' => array('Account.id', 'Account.type', 'Account.name', 'CurrentLedger.id'),
'conditions' => array('Account.'.$attribute => true)
));
$this->cacheQueries = false;
return $account;
}
/**************************************************************************
**************************************************************************
**************************************************************************
@@ -346,6 +366,165 @@ class Account extends AppModel {
return $unreconciled;
}
/**************************************************************************
**************************************************************************
**************************************************************************
* function: postLedgerEntry
* -
* transaction_data
* - transaction_id (optional... if set all else is ignored)
* - Transaction
* - stamp (optional... otherwise NOW is used)
* - comment
*
* monetary_source_data
* - monetary_source_id (optional... if set all else is ignored)
* - monetary_type_name
* - MonetarySource
* - name
* - monetary_type_id
*/
function postLedgerEntry($transaction_data,
$monetary_data,
$entry_data) {
/* if (!isset($entry_data) || */
/* !isset($entry_data['amount']) || */
/* !$entry_data['amount']) */
/* return false; */
$A = new Account();
/* // Create a transaction if necessary */
/* if (!isset($transaction_data['id'])) { */
/* $transaction = new Transaction(); */
/* $transaction->create(); */
/* if (!$transaction->save($transaction_data, false)) { */
/* return false; */
/* } */
/* $transaction_data['id'] = $transaction->id; */
/* } */
// Get the Transaction squared away
if (isset($transaction_data['transaction_id'])) {
$transaction_data
= array_intersect_key($transaction_data,
array('transaction_id'=>1));
}
elseif (isset($transaction_data['Transaction'])) {
$transaction_data
= array_intersect_key($transaction_data,
array('Transaction'=>1));
}
else {
$transaction_data = array('Transaction'=>array('stamp' => null));
}
// Get the Monetary Source squared away
if (isset($monetary_data['monetary_source_id'])) {
$monetary_data
= array_intersect_key($monetary_data,
array('monetary_source_id'=>1));
}
elseif (isset($monetary_data['monetary_type_name'])) {
if ($monetary_data['monetary_type_name'] === 'Cash') {
// No distinguishing features of Cash, just
// use the shared monetary source
$monetary_data['monetary_source_id'] =
$this->Ledger->LedgerEntry->MonetarySource->nameToID('Cash');
$monetary_data
= array_intersect_key($monetary_data,
array('monetary_source_id'=>1));
}
else {
// The monetary source needs to be unique
// Create a new one dedicated to this entry
$monetary_data['MonetarySource']['monetary_type_id'] =
$this->Ledger->LedgerEntry->MonetarySource->MonetaryType
->nameToID($monetary_data['monetary_type_name']);
$monetary_data['MonetarySource']['name'] =
$this->Ledger->LedgerEntry->MonetarySource->MonetaryType
->nameToID($monetary_data['monetary_type_name']);
// Give it a fancy name based on the check number
$monetary_data['MonetarySource']['name'] = $monetary_data['monetary_type_name'];
if ($monetary_data['monetary_type_name'] === 'Check' ||
$monetary_data['monetary_type_name'] === 'Money Order') {
$monetary_data['MonetarySource']['name'] .=
' #' . $monetary_data['MonetarySource']['data1'];
}
$monetary_data
= array_intersect_key($monetary_data,
array('MonetarySource'=>1));
}
}
elseif (isset($monetary_data)) {
$monetary_data
= array_intersect_key($monetary_data,
array('MonetarySource'=>1));
}
else {
$monetary_data = array();
}
// Make sure to clean out any unwanted data from the entry
$entry_data
= array_diff_key($entry_data,
array('transaction_id'=>1, 'Transaction'=>1,
'monetary_source_id'=>1, 'MonetarySource'=>1));
// Then add in the transaction and monetary source data
//pr(compact('transaction_data', 'monetary_data', 'entry_data'));
if (isset($transaction_data))
$entry_data += $transaction_data;
if (isset($monetary_data))
$entry_data += $monetary_data;
// Set up the debit ledger id
if (!isset($entry_data['debit_ledger_id'])) {
$entry_data['debit_ledger_id'] =
($entry_data['debit_account_id']
? $A->currentLedgerID($entry_data['debit_account_id'])
: ($entry_data['debit_account_name']
? $A->currentLedgerID($A->nameToID($entry_data['debit_account_name']))
: null
)
);
}
// Set up the credit ledger id
if (!isset($entry_data['credit_ledger_id'])) {
$entry_data['credit_ledger_id'] =
($entry_data['credit_account_id']
? $A->currentLedgerID($entry_data['credit_account_id'])
: ($entry_data['credit_account_name']
? $A->currentLedgerID($A->nameToID($entry_data['credit_account_name']))
: null
)
);
}
//pr(array('pre-save', compact('entry_data')));
// Create it!
$entry = new LedgerEntry();
$entry->create();
if (!$entry->saveAll($entry_data, array('validate'=>false))) {
return false;
}
$entry->read();
//pr(array('post-save', $entry->data));
return array('transaction_id' => $entry->data['LedgerEntry']['transaction_id'],
'monetary_source_id' => $entry->data['LedgerEntry']['monetary_source_id'],
'id' => $entry->data['LedgerEntry']['id']);
}
/**************************************************************************
**************************************************************************
**************************************************************************