Added back in the logic to simply update an existing explicit credit instead of creating an additional one. At the moment, I can't think of a scenario for this other than when reversing charges, which is still broken. So, until I make progress on that, it's not even clear this changes is needed, let alone whether it works.

git-svn-id: file:///svn-source/pmgr/branches/surplus_account_20090815@575 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
abijah
2009-08-16 02:22:45 +00:00
parent a1bdecfcaa
commit 14805190fc

View File

@@ -507,31 +507,12 @@ class StatementEntry extends AppModel {
),
array
(array('type' => $disbursement_type,
//$credit['StatementEntry']['effective_date'];
'effective_date' => $credit['StatementEntry']['effective_date'],
'account_id' => $credit['StatementEntry']['account_id'],
'amount' => $disbursement_amount,
/* 'customer_id' => $charge['StatementEntry']['customer_id'], */
/* 'lease_id' => $charge['StatementEntry']['lease_id'], */
'charge_entry_id' => $charge['StatementEntry']['id'],
),
));
/* $result = $this->Transaction->addReceipt */
/* (array('control' => */
/* array('include_ledger_entry' => true, */
/* 'include_statement_entry' => false), */
/* 'Transaction' => */
/* array('id' => $credit['StatementEntry']['transaction_id'], */
/* 'account_id' => $credit['StatementEntry']['account_id'], */
/* 'crdr' => 'DEBIT'), */
/* array(array('type' => $disbursement_type, */
/* //$credit['StatementEntry']['effective_date']; */
/* 'effective_date' => $charge['StatementEntry']['effective_date'], */
/* 'account_id' => $charge['StatementEntry']['account_id'], */
/* 'amount' => $disbursement_amount, */
/* ), */
/* )); */
$ret['Disbursement'][] = $result;
if ($result['error'])
@@ -576,51 +557,68 @@ class StatementEntry extends AppModel {
// Clean up any explicit credits that have been used
foreach ($credits AS $credit) {
if (empty($credit['receipt'])) {
// Explicit Credit
if (empty($credit['applied']))
continue;
if (!empty($credit['receipt']))
continue;
if ($credit['balance'] > 0) {
$this->pr(20, compact('credit'),
'Update Credit Entry');
if (empty($credit['applied']))
continue;
$this->id = $credit['StatementEntry']['id'];
$this->saveField('amount', $credit['balance']);
}
else {
$this->pr(20, compact('credit'),
'Delete Exhausted Credit Entry');
if ($credit['balance'] > 0) {
$this->pr(20, compact('credit'),
'Update Credit Entry');
$this->delete($credit['StatementEntry']['id'], false);
}
$this->id = $credit['StatementEntry']['id'];
$this->saveField('amount', $credit['balance']);
}
else {
// Receipt Credit
if (empty($credit['balance']))
continue;
$this->pr(20, compact('credit'),
'Delete Exhausted Credit Entry');
$ret['receipt_balance'] = $credit['balance'];
// Convert non-exhausted receipt credit to an explicit one
$this->pr(18, compact('credit'),
'Create Explicit Credit');
/* //$result = $this->Transaction->LedgerEntry->DoubleEntry->addDoubleEntry */
/* $result = $this->addStatementEntry */
/* (array('type' => 'SURPLUS', */
/* 'account_id' => $credit['LedgerEntry']['account_id'], */
/* 'amount' => $credit['balance'], */
/* 'effective_date' => $credit['Transaction']['stamp'], */
/* 'transaction_id' => $credit['Transaction']['id'], */
/* 'customer_id' => $customer_id, */
/* 'lease_id' => $lease_id, */
/* )); */
/* $ret['Credit'] = $result; */
/* if ($result['error']) */
/* $ret['error'] = true; */
$this->delete($credit['StatementEntry']['id'], false);
}
}
// Check for any implicit receipt credits, converting
// into explicit credits if there is a remaining balance.
foreach ($credits AS $credit) {
if (empty($credit['receipt']))
continue;
if (empty($credit['balance']))
continue;
// See if there is an existing explicit credit
// for this transaction.
$explicit_credit = $this->find
('first', array('contain' => false,
'conditions' =>
array(array('transaction_id' => $credit['Transaction']['id']),
array('type' => 'SURPLUS')),
));
if (!empty($explicit_credit)) {
// Since there IS an existing explicit credit, we must update
// its balance instead of creating a new one, since it has
// already been incorporated in the overall credit balance.
// If we were to create a new one, we would erroneously create
// an excess of credit available.
$this->pr(18, compact('explicit_credit', 'credit'),
'Update existing explicit credit');
$EC = new StatementEntry();
$EC->id = $explicit_credit['StatementEntry']['id'];
$EC->saveField('amount', $credit['balance']);
continue;
}
if (!empty($ret['receipt_balance']))
$this->INTERNAL_ERROR('Only one receipt expected in assignCredits');
// Give caller the information necessary to create an explicit
// credit from the passed receipt, which we've not exhausted.
$this->pr(18, compact('credit'), 'Convert to explicit credit');
$ret['receipt_balance'] = $credit['balance'];
}
return $this->prReturn($ret + array('error' => false));
}