Implemented the balance field of Leases as part of a single query. This not only reduces the number of queries required, it also allows balance to be a sortable column, so that we can determine which customers are overdue.

git-svn-id: file:///svn-source/pmgr/branches/invoice_receipt_20090629/site@251 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
abijah
2009-07-07 16:08:56 +00:00
parent aa2e647199
commit 011c05d098
4 changed files with 53 additions and 18 deletions

View File

@@ -112,6 +112,9 @@ class AppController extends Controller {
array('link'=>1, 'contain'=>1)); array('link'=>1, 'contain'=>1));
$query['conditions'] = $this->jqGridDataConditions($params, $model); $query['conditions'] = $this->jqGridDataConditions($params, $model);
// DEBUG PURPOSES ONLY!
$params['count_query'] = $query;
// Get the number of records prior to pagination // Get the number of records prior to pagination
$count = $this->jqGridDataRecordCount($params, $model, $query); $count = $this->jqGridDataRecordCount($params, $model, $query);
@@ -136,15 +139,15 @@ class AppController extends Controller {
$query['fields'] = $this->jqGridDataFields($params, $model); $query['fields'] = $this->jqGridDataFields($params, $model);
$results = $this->jqGridDataRecords($params, $model, $query); $results = $this->jqGridDataRecords($params, $model, $query);
// DEBUG PURPOSES ONLY!
$params['query'] = $query;
// Post process the records // Post process the records
$this->jqGridRecordsPostProcess($params, $model, $results); $this->jqGridRecordsPostProcess($params, $model, $results);
// Add in any needed hyperlinks // Add in any needed hyperlinks
$this->jqGridRecordLinks($params, $model, $results, array()); $this->jqGridRecordLinks($params, $model, $results, array());
// DEBUG PURPOSES ONLY!
$params['query'] = $query;
// Finally, dump out the data // Finally, dump out the data
$this->jqGridDataOutputHeader($params, $model); $this->jqGridDataOutputHeader($params, $model);
echo "<?xml version='1.0' encoding='utf-8'?>\n"; echo "<?xml version='1.0' encoding='utf-8'?>\n";

View File

@@ -49,12 +49,39 @@ class LeasesController extends AppController {
$params['action'] = 'all'; $params['action'] = 'all';
} }
function jqGridDataTables(&$params, &$model) { function jqGridDataCountTables(&$params, &$model) {
return array return array
('link' => array('Unit' => array('fields' => array('Unit.id', 'Unit.name')), ('link' => array('Unit' => array('fields' => array('Unit.id', 'Unit.name')),
'Customer' => array('fields' => array('Customer.id', 'Customer.name')))); 'Customer' => array('fields' => array('Customer.id', 'Customer.name'))));
} }
function jqGridDataTables(&$params, &$model) {
$link = $this->jqGridDataCountTables($params, $model);
$link['link']['LedgerEntry'] = array('fields' => array());
$link['link']['LedgerEntry']['Ledger'] = array('fields' => array());
$link['link']['LedgerEntry']['Ledger']['Account'] = array('fields' => array());
// INNER JOIN would be great, as it would ensure we're only looking
// at the ledger entries that we truly want. However, this also
// removes from the query any leases that do not yet have a ledger
// entry in A/R. A solution would be to INNER JOIN these tables,
// and LEFT JOIN it to the rest. Grouping of JOINs, however, is
// implemented with the 'joins' tag, and is not available through
// the Linkable behavior interface.
//$link['link']['LedgerEntry']['Ledger']['Account']['type'] = 'INNER';
$link['link']['LedgerEntry']['Ledger']['Account']['conditions']
= array('Account.id' =>
$this->Lease->LedgerEntry->Ledger->Account->accountReceivableAccountID());
return $link;
}
function jqGridDataFields(&$params, &$model) {
return array("Lease.*",
"SUM(IF(Account.id IS NULL, 0," .
" IF(LedgerEntry.debit_ledger_id = Account.id," .
" 1, -1))" .
" * LedgerEntry.amount) AS 'balance'");
}
function jqGridDataConditions(&$params, &$model) { function jqGridDataConditions(&$params, &$model) {
$conditions = parent::jqGridDataConditions($params, $model); $conditions = parent::jqGridDataConditions($params, $model);
@@ -75,18 +102,6 @@ class LeasesController extends AppController {
return parent::jqGridRecordLinks($params, $model, $records, $links); return parent::jqGridRecordLinks($params, $model, $records, $links);
} }
function jqGridDataRecords(&$params, &$model, $query) {
$leases = parent::jqGridDataRecords($params, $model, $query);
// Get the balance on each lease.
foreach ($leases AS &$lease) {
$stats = $this->Lease->stats($lease['Lease']['id']);
$lease['Lease']['balance'] = $stats['balance'];
}
return $leases;
}
/************************************************************************** /**************************************************************************
************************************************************************** **************************************************************************

View File

@@ -32,6 +32,23 @@ class LedgerEntry extends AppModel {
'className' => 'Ledger', 'className' => 'Ledger',
'foreignKey' => 'credit_ledger_id', 'foreignKey' => 'credit_ledger_id',
), ),
'Ledger' => array(
'foreignKey' => false,
// conditions will be used when JOINing tables
// (such as find with LinkableBehavior)
'conditions' => array('OR' =>
array('%{MODEL_ALIAS}.debit_ledger_id = Ledger.id',
'%{MODEL_ALIAS}.credit_ledger_id = Ledger.id')),
// finderQuery will be used when tables are put
// together across several querys, not with JOIN.
// (such as find with ContainableBehavior)
'finderQuery' => 'NOT-IMPLEMENTED',
'counterQuery' => ''
),
); );
var $hasAndBelongsToMany = array( var $hasAndBelongsToMany = array(

View File

@@ -6,12 +6,12 @@ $cols['LeaseID'] = array('index' => 'Lease.id', 'hidden' => true);
$cols['Lease'] = array('index' => 'Lease.number', 'formatter' => 'id'); $cols['Lease'] = array('index' => 'Lease.number', 'formatter' => 'id');
$cols['Unit'] = array('index' => 'Unit.name', 'width' => '50', 'align' => 'center'); $cols['Unit'] = array('index' => 'Unit.name', 'width' => '50', 'align' => 'center');
$cols['Customer'] = array('index' => 'Customer.name', 'formatter' => 'longname'); $cols['Customer'] = array('index' => 'Customer.name', 'formatter' => 'longname');
$cols['Rent'] = array('index' => 'Lease.rent', 'formatter' => 'currency', 'hidden' => true); $cols['Rent'] = array('index' => 'Lease.rent', 'formatter' => 'currency', 'hiddenz' => true);
$cols['Deposit'] = array('index' => 'Lease.deposit', 'formatter' => 'currency', 'hiddenz' => true); $cols['Deposit'] = array('index' => 'Lease.deposit', 'formatter' => 'currency', 'hiddenz' => true);
$cols['Signed'] = array('index' => 'Lease.lease_date', 'formatter' => 'date'); $cols['Signed'] = array('index' => 'Lease.lease_date', 'formatter' => 'date');
$cols['Move-In'] = array('index' => 'Lease.movein_date', 'formatter' => 'date'); $cols['Move-In'] = array('index' => 'Lease.movein_date', 'formatter' => 'date');
$cols['Move-Out'] = array('index' => 'Lease.moveout_date', 'formatter' => 'date'); $cols['Move-Out'] = array('index' => 'Lease.moveout_date', 'formatter' => 'date');
$cols['Balance'] = array('index' => 'Lease.balance', 'formatter' => 'currency', 'sortable'=>false); $cols['Balance'] = array('index' => 'balance', 'formatter' => 'currency');
$cols['Comment'] = array('index' => 'Lease.comment', 'formatter' => 'comment'); $cols['Comment'] = array('index' => 'Lease.comment', 'formatter' => 'comment');
$custom_post_data = compact('nothing'); $custom_post_data = compact('nothing');