From a968d7abe6b2c734f4d476606cc4e1059994fbd8 Mon Sep 17 00:00:00 2001 From: abijah Date: Sun, 16 Aug 2009 04:43:34 +0000 Subject: [PATCH] Changed the explicit receipt amount to be driven from stats instead of the assignCredits return value. This is in anticipation of creating an explicit credit directly, without even calling assignCredits. I'll do that for reversal on the next checkin. git-svn-id: file:///svn-source/pmgr/branches/surplus_account_20090815@577 97e9348a-65ac-dc4b-aefc-98561f571b83 --- site/models/statement_entry.php | 8 ++++++-- site/models/transaction.php | 35 ++++++++++++++++++++++++--------- 2 files changed, 32 insertions(+), 11 deletions(-) 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); } }