Combined the addNsf and addTransaction functions, which was always intended. There is more that could be done, but not all bugs are flushed out yet. Its close enough though, that I want to capture it as is to help work through the subtle issues.

git-svn-id: file:///svn-source/pmgr/branches/yafr_20090716@464 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
abijah
2009-08-01 01:47:11 +00:00
parent 15a4528e75
commit 8818ad7a80
2 changed files with 210 additions and 281 deletions

View File

@@ -103,7 +103,7 @@ class Tender extends AppModel {
*/
function nsf($id, $stamp = null) {
//$this->prFunctionLevel(30);
$this->prFunctionLevel(30);
$this->prEnter(compact('id'));
// Get information about this NSF item.
@@ -115,7 +115,6 @@ class Tender extends AppModel {
'DepositTransaction',
'NsfTransaction'),
));
//'conditions' => array(array('Tender.id' => $id)),
$this->pr(20, compact('tender'));
if (!empty($tender['NsfTransaction']['id']))
@@ -124,108 +123,19 @@ class Tender extends AppModel {
if (empty($tender['DepositTransaction']['id']))
die("Item has not been deposited yet");
// Enter the NSF
// This is the transaction pulling money from the bank account
// and recording it in the NSF account. It has nothing to do
// with the customer statement (charges, payments, credits, etc).
$nsf_result = $this->DepositTransaction->addDeposit
(array('Transaction' => array(),
'Entry' => array(array('tender_id' => null,
'account_id' => $this->LedgerEntry->Account->nsfAccountID(),
'amount' => -1 * $tender['LedgerEntry']['amount'],
))),
$tender['DepositTransaction']['account_id']);
$this->pr(20, compact('result'));
$tender['Transaction'] = $tender['DepositTransaction'];
unset($tender['DepositTransaction']);
unset($tender['NsfTransaction']);
if ($nsf_result['error'])
die("Unable to save NSF transaction");
$T = new Transaction();
$result = $T->addNsf($tender, $stamp);
if ($result['error'])
return $this->prReturn(false);
// Since we may have saved the nsf transaction with a null
// timestamp, query it back out of the database to find out
// what timestamp was _really_ specified, for later use.
$nsf_deposit = $this->DepositTransaction->find
('first', array('contain' => false, 'id' => $nsf_result['transaction_id']));
$nsf_deposit = $nsf_deposit['DepositTransaction'];
// OK, now move into customer realm, finding all statement
// entries that were affected by the bad payment (tender).
$nsf_ledger_entry = $this->LedgerEntry->find
('first', array
('contain' => array('Transaction' =>
array(//'fields' => array(),
'StatementEntry' =>
array(//'fields' => array(),
),
),
),
'conditions' => array('LedgerEntry.id' => $tender['LedgerEntry']['id']),
));
$this->pr(20, compact('nsf_ledger_entry'));
// Build a transaction to adjust all of the statement entries
$bounce = array('Transaction' => array(), 'Entry' => array());
$bounce['Transaction']['stamp'] = $nsf_deposit['stamp'];
$bounce['Transaction']['account_id'] = $this->LedgerEntry->Account->nsfAccountID();
$bounce['Transaction']['customer_id'] = $tender['Tender']['customer_id'];
$bounce['Transaction']['amount'] = -1 * $tender['LedgerEntry']['amount'];
foreach ($nsf_ledger_entry['Transaction']['StatementEntry'] AS $payment) {
if ($payment['type'] === 'SURPLUS') {
$payment['type'] = 'VOID';
$this->NsfTransaction->StatementEntry->id = $payment['id'];
$this->NsfTransaction->StatementEntry->saveField('type', $payment['type']);
}
else {
$bounce['Entry'][] =
array('type' => $payment['type'],
'amount' => -1 * $payment['amount'],
'account_id' => $this->LedgerEntry->Account->nsfAccountID(),
'customer_id' => $payment['customer_id'],
'lease_id' => $payment['lease_id'],
'charge_entry_id' => $payment['charge_entry_id'],
'effective_date' => $nsf_deposit['stamp'],
);
}
}
// Record the transaction, which will un-pay previously paid
// charges, void any credits, and other similar work.
$this->pr(20, compact('bounce'));
$bounce_result = $this->NsfTransaction->addNsf($bounce);
$this->pr(20, compact('bounce_result'));
if ($bounce_result['error'])
die("Unable to save Bounce transaction");
// Flag the tender as NSF, using the items created above.
// Flag the tender as NSF, using the items created from addNsf
$this->id = $id;
$this->saveField('nsf_transaction_id', $nsf_result['transaction_id']);
$this->saveField('nsf_ledger_entry_id', $bounce_result['entries'][0]['DoubleEntry']['Entry1']['ledger_entry_id']);
// Add NSF Charge
$result = $this->NsfTransaction->addInvoice
(array('Transaction' =>
array('stamp' => $nsf_deposit['stamp'],
),
'Entry' =>
array
(array('account_id' => $this->LedgerEntry->Account->nsfChargeAccountID(),
'effective_date' => $nsf_deposit['stamp'],
// REVISIT <AP>: 20090730
// BAD, BAD, BAD... who would actually
// hardcode a value like this???? ;-)
'amount' => 35,
'comment' => "NSF: " . $tender['Tender']['name'],
),
),
),
$tender['Tender']['customer_id']);
$this->pr(20, compact('result'));
$this->saveField('nsf_transaction_id', $result['nsf_transaction_id']);
$this->saveField('nsf_ledger_entry_id', $result['nsf_ledger_entry_id']);
return $this->prReturn(true);
}