diff --git a/site/app_controller.php b/site/app_controller.php index ab0dc81..86da138 100644 --- a/site/app_controller.php +++ b/site/app_controller.php @@ -112,6 +112,9 @@ class AppController extends Controller { array('link'=>1, 'contain'=>1)); $query['conditions'] = $this->jqGridDataConditions($params, $model); + // DEBUG PURPOSES ONLY! + $params['count_query'] = $query; + // Get the number of records prior to pagination $count = $this->jqGridDataRecordCount($params, $model, $query); @@ -136,15 +139,15 @@ class AppController extends Controller { $query['fields'] = $this->jqGridDataFields($params, $model); $results = $this->jqGridDataRecords($params, $model, $query); + // DEBUG PURPOSES ONLY! + $params['query'] = $query; + // Post process the records $this->jqGridRecordsPostProcess($params, $model, $results); // Add in any needed hyperlinks $this->jqGridRecordLinks($params, $model, $results, array()); - // DEBUG PURPOSES ONLY! - $params['query'] = $query; - // Finally, dump out the data $this->jqGridDataOutputHeader($params, $model); echo "\n"; diff --git a/site/controllers/leases_controller.php b/site/controllers/leases_controller.php index e877c66..a6b46fd 100644 --- a/site/controllers/leases_controller.php +++ b/site/controllers/leases_controller.php @@ -49,12 +49,39 @@ class LeasesController extends AppController { $params['action'] = 'all'; } - function jqGridDataTables(&$params, &$model) { + function jqGridDataCountTables(&$params, &$model) { return array ('link' => array('Unit' => array('fields' => array('Unit.id', 'Unit.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) { $conditions = parent::jqGridDataConditions($params, $model); @@ -75,18 +102,6 @@ class LeasesController extends AppController { 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; - } - /************************************************************************** ************************************************************************** diff --git a/site/models/ledger_entry.php b/site/models/ledger_entry.php index b51b203..8578197 100644 --- a/site/models/ledger_entry.php +++ b/site/models/ledger_entry.php @@ -32,6 +32,23 @@ class LedgerEntry extends AppModel { 'className' => 'Ledger', '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( diff --git a/site/views/elements/leases.ctp b/site/views/elements/leases.ctp index 9171cca..26c095e 100644 --- a/site/views/elements/leases.ctp +++ b/site/views/elements/leases.ctp @@ -6,12 +6,12 @@ $cols['LeaseID'] = array('index' => 'Lease.id', 'hidden' => true); $cols['Lease'] = array('index' => 'Lease.number', 'formatter' => 'id'); $cols['Unit'] = array('index' => 'Unit.name', 'width' => '50', 'align' => 'center'); $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['Signed'] = array('index' => 'Lease.lease_date', 'formatter' => 'date'); $cols['Move-In'] = array('index' => 'Lease.movein_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'); $custom_post_data = compact('nothing');