Moved the customer save logic into the model

git-svn-id: file:///svn-source/pmgr/branches/invoice_receipt_20090629/site@227 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
abijah
2009-07-06 15:45:49 +00:00
parent 4dac7a7f0c
commit 47104919f2
2 changed files with 81 additions and 45 deletions

View File

@@ -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;
}