Preliminary work on NSF. It's far from working, but we're headed in the right direction.
git-svn-id: file:///svn-source/pmgr/branches/yafr_20090716@426 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
@@ -86,7 +86,7 @@ class Transaction extends AppModel {
|
||||
// Go through the statement entries and flag as payments
|
||||
foreach ($data['Entry'] AS &$entry) {
|
||||
$entry['crdr'] = 'DEBIT';
|
||||
if (isset($entry['Tender']['tender_type_id'])) {
|
||||
if (empty($entry['account_id']) && isset($entry['Tender']['tender_type_id'])) {
|
||||
$entry['account_id'] = $this->LedgerEntry->Tender->TenderType->
|
||||
accountID($entry['Tender']['tender_type_id']);
|
||||
}
|
||||
@@ -464,6 +464,158 @@ class Transaction extends AppModel {
|
||||
}
|
||||
|
||||
|
||||
/**************************************************************************
|
||||
**************************************************************************
|
||||
**************************************************************************
|
||||
* function: addNsf
|
||||
* - Adds NSF transaction
|
||||
*/
|
||||
|
||||
function addNsf($data) {
|
||||
pr(array("Transaction::addNsf()"
|
||||
=> compact('data')));
|
||||
|
||||
// Verify that we have a transaction and entries
|
||||
if (empty($data['Transaction']) || empty($data['Entry']))
|
||||
return array('error' => true);
|
||||
|
||||
// Extract the transaction to a local variable
|
||||
$transaction = $data['Transaction'];
|
||||
|
||||
// set ledger ID as the current ledger of the specified account
|
||||
$transaction['ledger_id'] =
|
||||
$this->Account->currentLedgerID($transaction['account_id']);
|
||||
|
||||
// Automatically figure out the customer if we have the lease
|
||||
if (!empty($transaction['lease_id']) && empty($transaction['customer_id'])) {
|
||||
$L = new Lease();
|
||||
$L->recursive = -1;
|
||||
$lease = $L->read(null, $transaction['lease_id']);
|
||||
$transaction['customer_id'] = $lease['Lease']['customer_id'];
|
||||
}
|
||||
|
||||
|
||||
// Account.id
|
||||
// - Bank account that NSF occurred on
|
||||
// amount
|
||||
// - Amount of NSF
|
||||
// StatementEntry list
|
||||
// - payments to reverse
|
||||
|
||||
|
||||
|
||||
// Break each entry out of the combined statement/ledger entry
|
||||
// and into individual entries appropriate for saving. While
|
||||
// we're at it, calculate the transaction total as well.
|
||||
$entry_total = 0;
|
||||
foreach ($data['Entry'] AS &$entry) {
|
||||
// Add entry amount into the transaction total
|
||||
$entry_total += $entry['amount'];
|
||||
|
||||
// We won't be using ledger entries
|
||||
$le1 = null;
|
||||
$le1_tender = null;
|
||||
$le2 = null;
|
||||
|
||||
// Create a statement entry
|
||||
$se =
|
||||
array_intersect_key($entry,
|
||||
array_flip(array('type', 'account_id', 'amount',
|
||||
'customer_id', 'lease_id',
|
||||
'charge_entry_id')));
|
||||
|
||||
$se['ledger_id'] =
|
||||
$this->Account->currentLedgerID($se['account_id']);
|
||||
|
||||
// Replace combined entry with our new individual entries
|
||||
$entry = compact('le1', 'le1_tender', 'le2', 'se');
|
||||
}
|
||||
|
||||
array_unshift($data['Entry'],
|
||||
array('le1' => array('account_id' => $transaction['account_id'],
|
||||
'crdr' => 'DEBIT',
|
||||
'amount' => $transaction['amount']),
|
||||
'le2' => array('account_id' => $this->Account->accountReceivableAccountID(),
|
||||
'crdr' => 'CREDIT',
|
||||
'amount' => $transaction['amount'])
|
||||
));
|
||||
|
||||
// Move forward, verifying and saving everything.
|
||||
$ret = array();
|
||||
/* if (!$this->verifyTransaction($transaction, $data['Entry'])) */
|
||||
/* return array('error' => true) + $ret; */
|
||||
|
||||
pr(array('Transaction::addNsf' =>
|
||||
array('checkpoint' => 'Pre Transaction Save')
|
||||
+ compact('transaction')));
|
||||
|
||||
pr(compact('data'));
|
||||
|
||||
// Save transaction to the database
|
||||
$this->create();
|
||||
if (!$this->save($transaction))
|
||||
return array('error' => true) + $ret;
|
||||
|
||||
// Set up our return ids array
|
||||
$ret['transaction_id'] = $this->id;
|
||||
$ret['entries'] = array();
|
||||
$ret['error'] = false;
|
||||
|
||||
// Go through the entries
|
||||
foreach ($data['Entry'] AS $e_index => &$entry) {
|
||||
$le1 = null;
|
||||
$le1_tender = null;
|
||||
$le2 = null;
|
||||
$se = null;
|
||||
pr(compact('entry'));
|
||||
extract($entry);
|
||||
|
||||
if (!empty($le1) && !empty($le2)) {
|
||||
$le1['transaction_id'] = $le2['transaction_id'] = $ret['transaction_id'];
|
||||
if (isset($le1_tender))
|
||||
$le1_tender['customer_id'] = $transaction['customer_id'];
|
||||
$result = $this->LedgerEntry->DoubleEntry->addDoubleEntry($le1, $le2, $le1_tender);
|
||||
$ret['entries'][$e_index]['DoubleEntry'] = $result;
|
||||
if ($result['error']) {
|
||||
$ret['error'] = true;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($se)) {
|
||||
$se['transaction_id'] = $ret['transaction_id'];
|
||||
$result = $this->StatementEntry->addStatementEntry($se);
|
||||
$ret['entries'][$e_index]['StatementEntry'] = $result;
|
||||
if ($result['error']) {
|
||||
$ret['error'] = true;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
pr(compact('ret'));
|
||||
}
|
||||
|
||||
/* if (($transaction['type'] == 'INVOICE' || */
|
||||
/* $transaction['type'] == 'RECEIPT') && */
|
||||
/* !$ret['error']) { */
|
||||
/* $result = $this->StatementEntry->assignCredits */
|
||||
/* (array('link' => array('Customer'), */
|
||||
/* 'conditions' => array('Customer.id' => $transaction['customer_id'])), */
|
||||
/* ($transaction['type'] == 'RECEIPT' */
|
||||
/* ? $ret['transaction_id'] */
|
||||
/* : null)); */
|
||||
|
||||
/* $ret['assigned'] = $result; */
|
||||
/* if ($result['error']) */
|
||||
/* $ret['error'] = true; */
|
||||
/* } */
|
||||
|
||||
pr(array('Transaction::addNsf' =>
|
||||
array('return' => $ret)));
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
|
||||
/**************************************************************************
|
||||
**************************************************************************
|
||||
**************************************************************************
|
||||
|
||||
Reference in New Issue
Block a user