diff --git a/site/models/customer.php b/site/models/customer.php index d3458de..d3568b4 100644 --- a/site/models/customer.php +++ b/site/models/customer.php @@ -345,28 +345,58 @@ class Customer extends AppModel { * - Returns summary data from the requested customer. */ - function stats($id = null) { + function stats($id = null, $query = null) { if (!$id) return null; - $stats = $this->StatementEntry->find + $this->queryInit($query); + + // REVISIT : 20090725 + // We'll need to go directly to the statement entries if + // transactions are not always associated with the customer. + // This could happen if either we remove the customer_id + // field from Transaction, or we allow multiple customers + // to be part of the same transaction (essentially making + // the Transaction.customer_id meaningless). + +/* $stats = $this->StatementEntry->find */ +/* ('first', array */ +/* ('contain' => false, */ +/* 'fields' => $this->StatementEntry->chargePaymentFields(true), */ +/* 'conditions' => array('StatementEntry.customer_id' => $id), */ +/* )); */ + + $find_stats = $this->StatementEntry->find ('first', array ('contain' => false, 'fields' => $this->StatementEntry->chargePaymentFields(true), 'conditions' => array('StatementEntry.customer_id' => $id), )); + pr(compact('find_stats')); - pr(compact('stats')); + $tquery = $query; + $tquery['conditions'][] = array('StatementEntry.customer_id' => $id); + $statement_stats = $this->StatementEntry->stats(null, $tquery); + $statement_stats['balance'] = $statement_stats['Charge']['balance']; + pr(compact('statement_stats')); - $stat2 = - $this->Transaction->stats(null, - array('conditions' => - array('Transaction.customer_id' => $id), - )); + $tquery = $query; + //$tquery['conditions'][] = array('StatementEntry.customer_id' => $id); + $tquery['conditions'][] = array('Transaction.customer_id' => $id); + $transaction_stats = $this->Transaction->stats(null, $tquery); + $transaction_stats += $transaction_stats['StatementEntry']; + pr(compact('transaction_stats')); - pr(compact('stat2')); + $tquery = $query; + //$tquery['conditions'][] = array('StatementEntry.customer_id' => $id); + $tquery['conditions'][] = array('Transaction.customer_id' => $id); + $ar_transaction_stats = $this->Transaction->stats(null, $tquery, + $this->Transaction->Account->accountReceivableAccountID()); + $ar_transaction_stats += $ar_transaction_stats['LedgerEntry']; + pr(compact('ar_transaction_stats')); - return $stats[0]; + $stats = $statement_stats; + return $stats; } } diff --git a/site/models/transaction.php b/site/models/transaction.php index d887125..ba312fd 100644 --- a/site/models/transaction.php +++ b/site/models/transaction.php @@ -367,7 +367,7 @@ class Transaction extends AppModel { * function: stats * - Returns summary data from the requested transaction */ - function stats($id = null, $query = null) { + function stats($id = null, $query = null, $balance_account_id = null) { //pr(array('Transaction::stats' => compact('id', 'query'))); $this->queryInit($query); @@ -392,8 +392,13 @@ class Transaction extends AppModel { $squery['link'][$table] = array('fields' => array()); if ($table == 'LedgerEntry') { + if (isset($balance_account_id)) { + $squery['link']['LedgerEntry']['Account'] = array('fields' => array()); + $squery['conditions'][] = array("Account.id" => $balance_account_id); + } + $squery['fields'] = array_merge($squery['fields'], - $this->LedgerEntry->debitCreditFields(true, false)); + $this->LedgerEntry->debitCreditFields(true, $balance_account_id != null)); } elseif ($table == 'StatementEntry') { $squery['fields'] = array_merge($squery['fields'],