diff --git a/models/customer.php b/models/customer.php index dd9bd91..10fcd41 100644 --- a/models/customer.php +++ b/models/customer.php @@ -101,12 +101,35 @@ class Customer extends AppModel { $this->prEnter(compact('id', 'query')); $this->queryInit($query); - $query['conditions'][] = array('StatementEntry.customer_id' => $id); - $query['conditions'][] = array('StatementEntry.account_id' => - $this->StatementEntry->Account->securityDepositAccountID()); + $sd_account_id = + $this->StatementEntry->Account->securityDepositAccountID(); - $stats = $this->StatementEntry->stats(null, $query); - return $this->prReturn($stats['account_balance']); + $squery = $query; + $squery['conditions'][] = array('StatementEntry.customer_id' => $id); + $squery['conditions'][] = array('StatementEntry.account_id' => $sd_account_id); + $stats = $this->StatementEntry->stats(null, $squery); + $this->pr(26, compact('squery', 'stats')); + + // OK, we know now how much we charged for a security + // deposit, as well as how much we received to pay for it. + // Now we need to know if any has been released. + // Yes... this sucks. + $lquery = $query; + $lquery['link'] = array('Transaction' => + array('fields' => array(), + 'Customer' => + (empty($query['link']) + ? array('fields' => array()) + : $query['link']))); + $lquery['conditions'][] = array('Transaction.customer_id' => $id); + $lquery['conditions'][] = array('LedgerEntry.account_id' => $sd_account_id); + $lquery['conditions'][] = array('LedgerEntry.crdr' => 'DEBIT'); + $lquery['fields'][] = 'SUM(LedgerEntry.amount) AS total'; + $released = $this->StatementEntry->Transaction->LedgerEntry->find + ('first', $lquery); + $this->pr(26, compact('lquery', 'released')); + + return $this->prReturn($stats['Charge']['disbursement'] - $released[0]['total']); } diff --git a/models/ledger_entry.php b/models/ledger_entry.php index cb29f3a..096ca19 100644 --- a/models/ledger_entry.php +++ b/models/ledger_entry.php @@ -165,53 +165,13 @@ class LedgerEntry extends AppModel { */ function stats($id = null, $query = null, $set = null) { $this->queryInit($query); - unset($query['group']); - if (!isset($query['link']['DoubleEntry'])) - $query['link']['DoubleEntry'] = array(); -/* if (!isset($query['link']['DoubleEntry']['fields'])) */ -/* $query['link']['DoubleEntry']['fields'] = array(); */ - - if (isset($id)) - $query['conditions'][] = array('Entry.id' => $id); - - if (isset($set)) - $set = strtoupper($set); - - //pr(array('stats()', compact('id', 'query', 'set'))); - - $rtypes = array('charge', 'disbursement', - // 'debit', 'credit', - ); - - $stats = array(); - foreach($rtypes AS $rtype) { - $Rtype = ucfirst($rtype); - - if (($rtype == 'charge' && (!isset($set) || $set == 'DISBURSEMENT')) || - ($rtype == 'disbursement' && (!isset($set) || $set == 'CHARGE')) - ) { - - $rquery = $query; - $rquery['link'][$Rtype] = - array('fields' => array("SUM(COALESCE(Applied{$Rtype}.amount,0)) AS reconciled")); - - $rquery['fields'] = array(); - //$rquery['fields'][] = "SUM(DoubleEntry.amount) AS total"; - $rquery['fields'][] = "SUM(DoubleEntry.amount) - SUM(COALESCE(Applied{$Rtype}.amount,0)) AS balance"; - $rquery['conditions'][] = array("Applied{$Rtype}.id !=" => null); - - $result = $this->find('first', $rquery); - //pr(compact('Rtype', 'rquery', 'result')); - - $sumfld = $Rtype; - $stats[$sumfld] = $result[0]; -/* if (!isset($stats[$sumfld]['applied'])) */ -/* $stats[$sumfld]['applied'] = 0; */ - } - } - - return $stats; + // REVISIT : 20090816 + // This function appeared to be dramatically broken, + // a throwback to an earlier time. I deleted its + // contents and added this error to ensure it does + // not get used. + $this->INTERNAL_ERROR('This function should not be used'); } }