diff --git a/site/models/statement_entry.php b/site/models/statement_entry.php index ad99ae2..b8f8f46 100644 --- a/site/models/statement_entry.php +++ b/site/models/statement_entry.php @@ -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)); }