diff --git a/controllers/customers_controller.php b/controllers/customers_controller.php index d4e710e..b2dbada 100644 --- a/controllers/customers_controller.php +++ b/controllers/customers_controller.php @@ -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); diff --git a/models/customer.php b/models/customer.php index 6c5b62e..762c86e 100644 --- a/models/customer.php +++ b/models/customer.php @@ -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); + } + + /************************************************************************** ************************************************************************** ************************************************************************** diff --git a/models/lease.php b/models/lease.php index bd1d880..4ab7624 100644 --- a/models/lease.php +++ b/models/lease.php @@ -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); } diff --git a/views/elements/customers.ctp b/views/elements/customers.ctp index fd966ae..0e25850 100644 --- a/views/elements/customers.ctp +++ b/views/elements/customers.ctp @@ -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');