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@481 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
abijah
2009-08-04 19:25:39 +00:00
parent 4d4e96fe1c
commit 0ff91bf4d8
6 changed files with 66 additions and 30 deletions

View File

@@ -644,6 +644,17 @@ CREATE TABLE `pmgr_customers` (
-- contacts_customers table?
`primary_contact_id` INT(10) UNSIGNED NOT NULL,
-- Number of different leases for this customer.
-- It's not good to have redundant information,
-- but these fields change infrequently, and make
-- certain queries much easier, most notably for
-- the grid query, in which linking customer to
-- lease results in repeated statement entries
-- when a customer has more than one lease.
`lease_count` SMALLINT UNSIGNED NOT NULL DEFAULT 0,
`current_lease_count` SMALLINT UNSIGNED NOT NULL DEFAULT 0,
`past_lease_count` SMALLINT UNSIGNED NOT NULL DEFAULT 0,
`comment` VARCHAR(255) DEFAULT NULL,
PRIMARY KEY (`id`)

View File

@@ -1530,6 +1530,21 @@ $query = "UPDATE pmgr_units U, pmgr_leases L
WHERE L.unit_id = U.id AND L.close_date IS NULL";
query($db_handle, $query);
# All current_lease_counts will be zero at the moment, since
# everything was just created. This will update any customers
# that have ever leased anything.
$query = "UPDATE pmgr_customers C,
(SELECT L.customer_id,
SUM(IF(L.close_date IS NULL, 1, 0)) AS current,
SUM(IF(L.close_date IS NULL, 0, 1)) AS closed
FROM pmgr_leases L
GROUP BY L.customer_id) AS X
SET C.`lease_count` = X.current + X.closed,
C.`current_lease_count` = X.current,
C.`past_lease_count` = X.closed
WHERE X.customer_id = C.id";
query($db_handle, $query);
######################################################################
## Invoice/Receipt totals

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');