diff --git a/site/models/statement_entry.php b/site/models/statement_entry.php index 1a234ed..d64bd0b 100644 --- a/site/models/statement_entry.php +++ b/site/models/statement_entry.php @@ -24,6 +24,7 @@ class StatementEntry extends AppModel { ); var $default_log_level = array('log' => 30, 'show' => 15); + var $max_log_level = 10; /************************************************************************** ************************************************************************** @@ -413,8 +414,7 @@ class StatementEntry extends AppModel { $this->INTERNAL_ERROR("Unable to locate receipt."); $stats = $this->Transaction->stats($receipt_id); - $receipt_credit['balance'] = - $receipt_credit['Transaction']['amount'] - $stats['StatementEntry']['disbursements']; + $receipt_credit['balance'] = $stats['undisbursed']; $receipt_credit['receipt'] = true; $credits = array($receipt_credit); @@ -610,6 +610,10 @@ class StatementEntry extends AppModel { )); if (!empty($explicit_credit)) { + // REVISIT : 20090815 + // Testing whether or not this case occurs + $this->INTERNAL_ERROR('Existing explicit credit unexpected'); + // 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. diff --git a/site/models/transaction.php b/site/models/transaction.php index b604cdb..5e0d796 100644 --- a/site/models/transaction.php +++ b/site/models/transaction.php @@ -44,6 +44,7 @@ class Transaction extends AppModel { var $default_log_level = array('log' => 30, 'show' => 15); + //var $max_log_level = 10; /************************************************************************** ************************************************************************** @@ -454,6 +455,11 @@ 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)); @@ -491,32 +497,36 @@ class Transaction extends AppModel { ); $ret['assigned'] = $result; - if ($result['error']) + if (!empty($result['error'])) $ret['error'] = true; + } - if (!empty($control['assign_receipt']) && - !empty($result['receipt_balance'])) { + 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'); + if (!empty($stats['undisbursed'])) { $result = $this->addTransactionEntries (array('include_ledger_entry' => true, 'include_statement_entry' => true), array('crdr' => 'DEBIT') + $transaction, array(array('type' => 'SURPLUS', 'account_id' => $this->Account->accountPayableAccountID(), - 'amount' => $result['receipt_balance'], + 'amount' => $stats['undisbursed'], ), )); + $ret['credit'] = $result; - if ($result['error']) + if (!empty($result['error'])) $ret['error'] = true; - // Verify the explicit credit worked. Use either Debit or Credit - // do determine the receipt total (they BETTER be the same value!) + // Verify the explicit credit worked. $stats = $this->stats($transaction['id']); - if ($transaction['amount'] != $stats['StatementEntry']['disbursements']) + if (!empty($stats['undisbursed'])) $this->INTERNAL_ERROR('Explicit credit did not resolve receipt balance'); } - } $this->Customer->update($transaction['customer_id']); @@ -1128,6 +1138,13 @@ class Transaction extends AppModel { unset($stats[$table][0]); } + if (!empty($id)) { + $this->id = $id; + $stats['total'] = $this->field('amount'); + $stats['uncharged'] = $stats['total'] - $stats['StatementEntry']['charges']; + $stats['undisbursed'] = $stats['total'] - $stats['StatementEntry']['disbursements']; + } + return $this->prReturn($stats); } }