From 1f95e835f8a480222a403fa11cce6004b3d974af Mon Sep 17 00:00:00 2001 From: abijah Date: Wed, 8 Jul 2009 00:34:03 +0000 Subject: [PATCH] Updated the balance algorithm for units and customers as was done for leases. git-svn-id: file:///svn-source/pmgr/branches/invoice_receipt_20090629@253 97e9348a-65ac-dc4b-aefc-98561f571b83 --- site/controllers/customers_controller.php | 56 ++++++++++++++++++----- site/controllers/units_controller.php | 47 +++++++++++++++++-- site/views/elements/customers.ctp | 2 +- site/views/elements/units.ctp | 1 + 4 files changed, 89 insertions(+), 17 deletions(-) diff --git a/site/controllers/customers_controller.php b/site/controllers/customers_controller.php index 0b7e45a..2e48793 100644 --- a/site/controllers/customers_controller.php +++ b/site/controllers/customers_controller.php @@ -51,7 +51,7 @@ class CustomersController extends AppController { $params['action'] = 'all'; } - function jqGridDataTables(&$params, &$model) { + function jqGridDataCountTables(&$params, &$model) { return array ('link' => array(// Models @@ -61,9 +61,35 @@ class CustomersController extends AppController { ); } + 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 units 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->Customer->LedgerEntry->Ledger->Account->accountReceivableAccountID()); + return $link; + } + function jqGridDataFields(&$params, &$model) { - return array('Customer.*', - 'COUNT(CurrentLease.id) AS lease_count'); + $db = &$model->getDataSource(); + $fields = $db->fields($model, $model->alias); + $fields[] = ('COUNT(DISTINCT CurrentLease.id) AS lease_count'); + $fields[] = ("SUM(IF(Account.id IS NULL, 0," . + " IF(LedgerEntry.debit_ledger_id = Account.id," . + " 1, -1))" . + " * IF(LedgerEntry.amount IS NULL, 0, LedgerEntry.amount))" . + " AS 'balance'"); + return $fields; } function jqGridDataConditions(&$params, &$model) { @@ -80,13 +106,19 @@ class CustomersController extends AppController { } function jqGridDataOrder(&$params, &$model, $index, $direction) { - $order = parent::jqGridDataOrder($params, $model, $index, $direction); - if ($index === 'PrimaryContact.last_name') { - $order[] = 'PrimaryContact.first_name ' . $direction; - } - if ($index === 'PrimaryContact.first_name') { - $order[] = 'PrimaryContact.last_name ' . $direction; - } + $order = array(); + $order[] = parent::jqGridDataOrder($params, $model, $index, $direction); + + if ($index !== 'PrimaryContact.last_name') + $order[] = parent::jqGridDataOrder($params, $model, + 'PrimaryContact.last_name', $direction); + if ($index !== 'PrimaryContact.first_name') + $order[] = parent::jqGridDataOrder($params, $model, + 'PrimaryContact.first_name', $direction); + if ($index !== 'Customer.id') + $order[] = parent::jqGridDataOrder($params, $model, + 'Customer.id', $direction); + return $order; } @@ -309,8 +341,8 @@ class CustomersController extends AppController { pr("CUSTOMER SAVE FAILED"); } - // Exisisting Customer, then view it, else if this - // is a new customer, go to the move in screen. + // If existing customer, then view it. Otherwise, since + // this is a new customer, go to the move in screen. if ($this->data['Customer']['id']) $this->redirect(array('action'=>'view', $this->Customer->id)); else diff --git a/site/controllers/units_controller.php b/site/controllers/units_controller.php index 0d570b7..aaaad60 100644 --- a/site/controllers/units_controller.php +++ b/site/controllers/units_controller.php @@ -51,7 +51,7 @@ class UnitsController extends AppController { $params['action'] = 'all'; } - function jqGridDataTables(&$params, &$model) { + function jqGridDataCountTables(&$params, &$model) { $link = array ('link' => array(// Models @@ -70,6 +70,35 @@ class UnitsController extends AppController { return $link; } + function jqGridDataTables(&$params, &$model) { + $link = $this->jqGridDataCountTables($params, $model); + $link['link']['CurrentLease']['LedgerEntry'] = array('fields' => array()); + $link['link']['CurrentLease']['LedgerEntry']['Ledger'] = array('fields' => array()); + $link['link']['CurrentLease']['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']['CurrentLease']['LedgerEntry']['Ledger']['Account']['type'] = 'INNER'; + $link['link']['CurrentLease']['LedgerEntry']['Ledger']['Account']['conditions'] + = array('Account.id' => + $this->Unit->CurrentLease->LedgerEntry->Ledger->Account->accountReceivableAccountID()); + return $link; + } + + function jqGridDataFields(&$params, &$model) { + $db = &$model->getDataSource(); + $fields = $db->fields($model, $model->alias); + $fields[] = ("SUM(IF(Account.id IS NULL, 0," . + " IF(LedgerEntry.debit_ledger_id = Account.id," . + " 1, -1))" . + " * LedgerEntry.amount) AS 'balance'"); + return $fields; + } + function jqGridDataConditions(&$params, &$model) { $conditions = parent::jqGridDataConditions($params, $model); @@ -90,10 +119,20 @@ class UnitsController extends AppController { } function jqGridDataOrder(&$params, &$model, $index, $direction) { - if ($index === 'Unit.name') { + // Instead of sorting by name, sort by defined order + if ($index === 'Unit.name') $index = 'Unit.sort_order'; - } - return parent::jqGridDataOrder($params, $model, $index, $direction); + + $order = array(); + $order[] = parent::jqGridDataOrder($params, $model, $index, $direction); + + // If sorting by anything other than name (defined order) + // add the sort-order as a secondary condition + if ($index !== 'Unit.name') + $order[] = parent::jqGridDataOrder($params, $model, + 'Unit.sort_order', $direction); + + return $order; } function jqGridRecordLinks(&$params, &$model, &$records, $links) { diff --git a/site/views/elements/customers.ctp b/site/views/elements/customers.ctp index 46cd28f..0dab029 100644 --- a/site/views/elements/customers.ctp +++ b/site/views/elements/customers.ctp @@ -9,7 +9,7 @@ $cols['Name'] = array('index' => 'Customer.name', 'formatter $cols['Last Name'] = array('index' => 'PrimaryContact.last_name', 'formatter' => 'name'); $cols['First Name'] = array('index' => 'PrimaryContact.first_name', 'formatter' => 'name'); $cols['Leases'] = array('index' => 'lease_count', 'width' => '60'); -$cols['Balance'] = array('index' => 'balance', 'formatter' => 'currency', 'sortable' => false); +$cols['Balance'] = array('index' => 'balance', 'formatter' => 'currency'); $cols['Comment'] = array('index' => 'Customer.comment', 'formatter' => 'comment'); $custom_post_data = compact('nothing'); diff --git a/site/views/elements/units.ctp b/site/views/elements/units.ctp index 6f2eed7..95c17e6 100644 --- a/site/views/elements/units.ctp +++ b/site/views/elements/units.ctp @@ -9,6 +9,7 @@ $cols['ID'] = array('index' => 'Unit.id', 'formatter' => 'id'); $cols['Unit'] = array('index' => 'Unit.name', 'width' => '50'); $cols['Size'] = array('index' => 'UnitSize.name', 'width' => '75'); $cols['Status'] = array('index' => 'Unit.status', 'width' => '75'); +$cols['Balance'] = array('index' => 'balance', 'formatter' => 'currency'); $cols['Comment'] = array('index' => 'Unit.comment', 'formatter' => 'comment');