diff --git a/controllers/customers_controller.php b/controllers/customers_controller.php index 4a0bf71..2512555 100644 --- a/controllers/customers_controller.php +++ b/controllers/customers_controller.php @@ -272,7 +272,7 @@ class CustomersController extends AppController { function edit($id = null) { if (isset($this->data)) { - + // Check to see if the operation was cancelled. if (isset($this->params['form']['cancel'])) { if (isset($this->data['Customer']['id'])) $this->redirect(array('action'=>'view', $this->data['Customer']['id'])); @@ -281,56 +281,23 @@ class CustomersController extends AppController { return; } - $this->data['Customer']['primary_contact_id'] - = $this->data['Contact'][$this->data['Customer']['primary_contact_entry']]['id']; - - $this->Customer->create(); - if (!$this->Customer->save($this->data, false)) { - pr("CUSTOMER SAVE FAILED"); - $this->Session->setFlash("CUSTOMER SAVE FAILED", true); - return; - } - $this->data['Customer']['id'] = $this->Customer->id; - - // Remove all associated Customer Contacts, as it ensures - // any entries deleted by the user actually get deleted - // in the system. We'll recreate the needed ones anyway. - $this->Customer->ContactsCustomer->deleteAll - (array('customer_id' => $this->data['Customer']['id']), false); - - if (!isset($this->data['Contact'])) - $this->data['Contact'] = array(); - - // Go through each entry of this customer method + // Go through each customer and strip the bogus ID if new foreach ($this->data['Contact'] AS &$contact) { - - // Check if the user has entered a brand new contact - if (isset($contact['source']) && $contact['source'] === 'new') { + if (isset($contact['source']) && $contact['source'] === 'new') unset($contact['id']); - $I = new Contact(); - $I->create(); - if (!$I->save($contact, false)) { - pr("CONTACT SAVE FAILED"); - $this->Session->setFlash("CONTACT SAVE FAILED", true); - continue; - } - $contact['id'] = $I->id; - } + } - // Update the ContactsCustomer to reflect the appropriate IDs - $contact['ContactsCustomer']['customer_id'] = $this->data['Customer']['id']; - $contact['ContactsCustomer']['contact_id'] = $contact['id']; - - // Save the relationship between customer and contact - $CM = new ContactsCustomer(); - if (!$CM->save($contact['ContactsCustomer'], false)) { - die("ContactsCustomer Save FAILED!"); - } + // Save the customer and all associated data + if (!$this->Customer->saveCustomer($this->data['Customer']['id'], + $this->data, + $this->data['Customer']['primary_contact_entry'])) { + $this->Session->setFlash("CUSTOMER SAVE FAILED", true); + pr("CUSTOMER SAVE FAILED"); } // Now that the work is done, let the user view the updated customer - $this->redirect(array('action'=>'view', $this->data['Customer']['id'])); - //$this->autoRender = false; + $this->redirect(array('action'=>'view', $this->Customer->id)); + //$this->render('/empty'); return; } diff --git a/models/customer.php b/models/customer.php index de625c3..2e23a59 100644 --- a/models/customer.php +++ b/models/customer.php @@ -186,6 +186,75 @@ class Customer extends AppModel { } + /************************************************************************** + ************************************************************************** + ************************************************************************** + * function: stats + * - Returns summary data from the requested customer. + */ + + function saveCustomer($id, $data, $primary_contact_entry) { + $this->create(); + if ($id) + $this->id = $id; + if (!$this->save($data, false)) { + return false; + } + $id = $this->id; + + // Remove all associated Customer Contacts, as it ensures + // any entries deleted by the user actually get deleted + // in the system. We'll recreate the needed ones anyway. + // REVISIT : 20090706 + // Appears that $this->save() is already doing the + // delete. I would have thought this would only happen + // on a saveAll?? +/* $this->ContactsCustomer->deleteAll */ +/* (array('customer_id' => $id), false); */ + + // Avoid PHP notifications by making sure we have an array + if (!isset($data['Contact'])) + $data['Contact'] = array(); + + // Assume this operation will succeed + $ret = true; + + // Go through each entry of this customer method + foreach ($data['Contact'] AS &$contact) { + + // Check if the user has entered a brand new contact + if (!isset($contact['id'])) { + $I = new Contact(); + $I->create(); + if (!$I->save($contact, false)) { + $ret = false; + continue; + } + $contact['id'] = $I->id; + } + + // Update the ContactsCustomer to reflect the appropriate IDs + $contact['ContactsCustomer']['customer_id'] = $id; + $contact['ContactsCustomer']['contact_id'] = $contact['id']; + + // Save the relationship between customer and contact + $CM = new ContactsCustomer(); + if (!$CM->save($contact['ContactsCustomer'], false)) { + $ret = false; + continue; + } + } + + // Set the primary contact ID based on caller selection + if (!$this->saveField('primary_contact_id', + $data['Contact'][$primary_contact_entry]['id'])) + $ret = false; + + // Return the result + return $ret; + } + + /************************************************************************** ************************************************************************** **************************************************************************