diff --git a/controllers/customers_controller.php b/controllers/customers_controller.php index 7258294..5244feb 100644 --- a/controllers/customers_controller.php +++ b/controllers/customers_controller.php @@ -326,7 +326,37 @@ class CustomersController extends AppController { } if ($id) { - $this->data = $this->Customer->details($id); + // REVISIT : 20090816 + // This should never need to be done by a controller. + // However, until things stabilize, this gives the + // user a way to update any cached items on the + // customer, by just clicking Edit then Cancel. + $this->Customer->update($id); + + // Get details on this customer, its contacts and leases + $customer = $this->Customer->find + ('first', array + ('contain' => array + (// Models + 'Contact' => + array('order' => array('Contact.display_name'), + // Models + 'ContactPhone', + 'ContactEmail', + 'ContactAddress', + ), + 'Lease' => + array('Unit' => + array('order' => array('sort_order'), + 'fields' => array('id', 'name'), + ), + ), + ), + + 'conditions' => array('Customer.id' => $id), + )); + + $this->data = $customer; $title = 'Customer: ' . $this->data['Customer']['name'] . " : Edit"; } else { diff --git a/models/customer.php b/models/customer.php index b3c6fa1..dd9bd91 100644 --- a/models/customer.php +++ b/models/customer.php @@ -208,6 +208,7 @@ class Customer extends AppModel { * - Update any cached or calculated fields */ function update($id) { + $this->prEnter(compact('id')); // REVISIT : 20090812 // updateLeaseCount is handled directly when needed. @@ -216,11 +217,21 @@ class Customer extends AppModel { $current_leases = $this->find('all', - array('link' => array('CurrentLease' => array('type' => 'INNER')), + // REVISIT : 20090816 + // Do we need to update leases other than the current ones? + // It may be necessary. For example, a non-current lease + // can still be hit with an NSF item. In that case, it + // could have stale data if we look only to current leases. + //array('link' => array('CurrentLease' => array('type' => 'INNER')), + array('link' => array('Lease' => array('type' => 'INNER')), 'conditions' => array('Customer.id' => $id))); - foreach ($current_leases AS $lease) - $this->Lease->update($lease['CurrentLease']['id']); + foreach ($current_leases AS $lease) { + if (!empty($lease['CurrentLease']['id'])) + $this->Lease->update($lease['CurrentLease']['id']); + if (!empty($lease['Lease']['id'])) + $this->Lease->update($lease['Lease']['id']); + } } diff --git a/models/lease.php b/models/lease.php index 525d3a0..437b607 100644 --- a/models/lease.php +++ b/models/lease.php @@ -792,6 +792,8 @@ class Lease extends AppModel { * - Update any cached or calculated fields */ function update($id) { + $this->prEnter(compact('id')); + $this->id = $id; $this->saveField('charge_through_date', $this->rentChargeThrough($id)); $this->saveField('paid_through_date', $this->rentPaidThrough($id));