Changed reversals to create an explicit credit BEFORE assigning credits to outstanding charges. This ensures that charges are paid from the customer surplus account and we don't end up with bizarre disbursements, like having Rent paid from Damage or whatever the reversed charge was.

git-svn-id: file:///svn-source/pmgr/branches/surplus_account_20090815@578 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
abijah
2009-08-16 05:17:09 +00:00
parent a968d7abe6
commit 945221d565

View File

@@ -455,11 +455,6 @@ class Transaction extends AppModel {
function addTransaction($control, $transaction, $entries) {
$this->prEnter(compact('control', 'transaction', 'entries'));
// By default, let's create an explicit credit if there
// is any balance remaining after assigning credits.
if (!empty($control['assign']) && !empty($control['assign_receipt']))
$control += array('create_explicit_credit' => true);
$result = $this->_splitEntries($control, $transaction, $entries);
if (!empty($result['error']))
return $this->prReturn(array('error' => true));
@@ -485,29 +480,45 @@ class Transaction extends AppModel {
// Add the entries
$ret = $this->addTransactionEntries($control, $transaction, $entries, false);
if (!empty($control['assign']) && !$ret['error']) {
$result = $this->StatementEntry->assignCredits
(null,
(empty($control['assign_receipt']) ? null
: $ret['transaction_id']),
null,
$assign_disbursement_type,
$transaction['customer_id'],
$transaction['lease_id']
);
// If the caller requests 'assign'=>true, they really
// want to do a credit assignment, and _then_ create
// an explicit credit with any leftover. If an array
// is specified, they get full control of the order.
if (empty($control['assign']))
$assign_ops = array();
elseif (is_array($control['assign']))
$assign_ops = $control['assign'];
elseif (is_boolean($control['assign']))
$assign_ops = array('assign', 'create');
else
$this->INTERNAL_ERROR('Invalid control[assign] parameter');
$ret['assigned'] = $result;
if (!empty($result['error']))
$ret['error'] = true;
}
// Go through the requested assignment mechanisms
foreach ($assign_ops AS $method) {
if (!empty($ret['error']))
break;
if (!empty($control['create_explicit_credit']) &&
empty($ret['error'])) {
$stats = $this->stats($transaction['id']);
if ($stats['undisbursed'] < 0)
$this->INTERNAL_ERROR('Receipt has negative undisbursed balance');
$this->pr(17, compact('method'), 'Assigning credits');
if ($method === 'assign') {
$result = $this->StatementEntry->assignCredits
(null,
(empty($control['assign_receipt']) ? null
: $ret['transaction_id']),
null,
$assign_disbursement_type,
$transaction['customer_id'],
$transaction['lease_id']
);
}
elseif ($method === 'create') {
$stats = $this->stats($transaction['id']);
if ($stats['undisbursed'] < 0)
$this->INTERNAL_ERROR('Receipt has negative undisbursed balance');
if (empty($stats['undisbursed']))
continue;
if (!empty($stats['undisbursed'])) {
$result = $this->addTransactionEntries
(array('include_ledger_entry' => true,
'include_statement_entry' => true),
@@ -518,15 +529,17 @@ class Transaction extends AppModel {
),
));
$ret['credit'] = $result;
if (!empty($result['error']))
$ret['error'] = true;
// Verify the explicit credit worked.
$stats = $this->stats($transaction['id']);
if (!empty($stats['undisbursed']))
$this->INTERNAL_ERROR('Explicit credit did not resolve receipt balance');
}
else
$this->INTERNAL_ERROR('Invalid assign method');
$ret[$method] = $result;
if (!empty($result['error']))
$ret['error'] = true;
}
$this->Customer->update($transaction['customer_id']);
@@ -952,9 +965,7 @@ class Transaction extends AppModel {
// These are all disbursements against the charge we're reversing
$rollback =
array('control' =>
array('assign' => true,
'assign_receipt' => true,
//'assign_receipt' => true,
array('assign' => array('create', 'assign'),
'include_ledger_entry' => false,
'include_statement_entry' => true,
),