Even with all the effort put into getting the counts right on the customers grid, it still didn't work right. The root of the problem is the join to CurrentLease, which can result in multiple rows for the same customer. I can revisit this in the future to put some clever solution back in, but in the meantime, it was easiest just to add fields to the customers table, and simply update it whenever the customer lease situation changes. I don't like it... but it just made life much simpler.

git-svn-id: file:///svn-source/pmgr/branches/yafr_20090716/site@481 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
abijah
2009-08-04 19:25:39 +00:00
parent e0d15b7396
commit 2ddcdf9419
4 changed files with 40 additions and 30 deletions

View File

@@ -50,7 +50,6 @@ class CustomersController extends AppController {
('link' =>
array(// Models
'PrimaryContact',
'CurrentLease' => array('fields' => array()),
),
);
}
@@ -64,7 +63,6 @@ class CustomersController extends AppController {
function gridDataFields(&$params, &$model) {
$fields = parent::gridDataFields($params, $model);
$fields[] = ('COUNT(DISTINCT CurrentLease.id) AS lease_count');
return array_merge($fields,
$this->Customer->StatementEntry->chargePaymentFields(true));
}
@@ -73,10 +71,11 @@ class CustomersController extends AppController {
$conditions = parent::gridDataConditions($params, $model);
if ($params['action'] === 'current') {
$conditions[] = 'CurrentLease.id IS NOT NULL';
$conditions[] = array('Customer.current_lease_count >' => 0);
}
elseif ($params['action'] === 'past') {
$conditions[] = 'CurrentLease.id IS NULL';
$conditions[] = array('Customer.current_lease_count' => 0);
$conditions[] = array('Customer.past_lease_count >' => 0);
}
return $conditions;
@@ -99,30 +98,6 @@ class CustomersController extends AppController {
return $order;
}
function gridDataCount(&$params, &$model) {
if ($params['action'] != 'current')
return parent::gridDataCount($params, $model);
// OK, for current customers, we have an issue.
// We don't have a good way to use the query to obtain
// our count. The problem is that we're relying on the
// group by for the query, but that simply won't work
// for the count. However, it's not difficult to simply
// derive it since 'current' customers are mutually
// exclusive to 'past' customers.
$tmp_params = $params;
$tmp_params['action'] = 'all';
$all_count = parent::gridDataCount($tmp_params, $model);
$tmp_params['action'] = 'past';
$past_count = parent::gridDataCount($tmp_params, $model);
// The current customer count is simply calculated
// as all customers that are not past customers.
return $all_count - $past_count;
}
function gridDataPostProcessLinks(&$params, &$model, &$records, $links) {
$links['Customer'] = array('name');
return parent::gridDataPostProcessLinks($params, $model, $records, $links);

View File

@@ -263,6 +263,31 @@ class Customer extends AppModel {
}
/**************************************************************************
**************************************************************************
**************************************************************************
* function: updateLeaseCount
* - Updates the internal lease count
*/
function updateLeaseCount($id) {
$this->id = $id;
$lease_count =
$this->find('count',
array('link' => array('Lease' => array('type' => 'INNER')),
'conditions' => array('Customer.id' => $id)));
$current_count =
$this->find('count',
array('link' => array('CurrentLease' => array('type' => 'INNER')),
'conditions' => array('Customer.id' => $id)));
$this->saveField('lease_count', $lease_count);
$this->saveField('current_lease_count', $current_count);
$this->saveField('past_lease_count', $lease_count - $current_count);
}
/**************************************************************************
**************************************************************************
**************************************************************************

View File

@@ -321,7 +321,10 @@ class Lease extends AppModel {
// Set the lease number to be the same as the lease ID
$this->id;
$this->saveField('number', $this->id);
// Update the current lease count for the customer
$this->Customer->updateLeaseCount($customer_id);
// Update the unit status
$this->Unit->updateStatus($unit_id, 'OCCUPIED');
@@ -369,6 +372,9 @@ class Lease extends AppModel {
if ($close)
$this->close($id, $stamp);
// Update the current lease count for the customer
$this->Customer->updateLeaseCount($this->field('customer_id'));
// Finally, update the unit status
$this->recursive = -1;
$this->read();
@@ -402,6 +408,10 @@ class Lease extends AppModel {
// Save it!
$this->save($this->data, false);
// Update the current lease count for the customer
$this->Customer->updateLeaseCount($this->field('customer_id'));
return $this->prReturn(true);
}

View File

@@ -7,7 +7,7 @@ $cols['Relationship'] = array('index' => 'ContactsCustomer.type', 'formatt
$cols['Name'] = array('index' => 'Customer.name', 'formatter' => 'longname');
$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', 'formatter' => 'number');
$cols['Leases'] = array('index' => 'current_lease_count', 'formatter' => 'number');
$cols['Balance'] = array('index' => 'balance', 'formatter' => 'currency');
$cols['Comment'] = array('index' => 'Customer.comment', 'formatter' => 'comment');