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:
@@ -272,7 +272,7 @@ class CustomersController extends AppController {
|
|||||||
|
|
||||||
function edit($id = null) {
|
function edit($id = null) {
|
||||||
if (isset($this->data)) {
|
if (isset($this->data)) {
|
||||||
|
// Check to see if the operation was cancelled.
|
||||||
if (isset($this->params['form']['cancel'])) {
|
if (isset($this->params['form']['cancel'])) {
|
||||||
if (isset($this->data['Customer']['id']))
|
if (isset($this->data['Customer']['id']))
|
||||||
$this->redirect(array('action'=>'view', $this->data['Customer']['id']));
|
$this->redirect(array('action'=>'view', $this->data['Customer']['id']));
|
||||||
@@ -281,56 +281,23 @@ class CustomersController extends AppController {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->data['Customer']['primary_contact_id']
|
// Go through each customer and strip the bogus ID if new
|
||||||
= $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
|
|
||||||
foreach ($this->data['Contact'] AS &$contact) {
|
foreach ($this->data['Contact'] AS &$contact) {
|
||||||
|
if (isset($contact['source']) && $contact['source'] === 'new')
|
||||||
// Check if the user has entered a brand new contact
|
|
||||||
if (isset($contact['source']) && $contact['source'] === 'new') {
|
|
||||||
unset($contact['id']);
|
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
|
// Save the customer and all associated data
|
||||||
$contact['ContactsCustomer']['customer_id'] = $this->data['Customer']['id'];
|
if (!$this->Customer->saveCustomer($this->data['Customer']['id'],
|
||||||
$contact['ContactsCustomer']['contact_id'] = $contact['id'];
|
$this->data,
|
||||||
|
$this->data['Customer']['primary_contact_entry'])) {
|
||||||
// Save the relationship between customer and contact
|
$this->Session->setFlash("CUSTOMER SAVE FAILED", true);
|
||||||
$CM = new ContactsCustomer();
|
pr("CUSTOMER SAVE FAILED");
|
||||||
if (!$CM->save($contact['ContactsCustomer'], false)) {
|
|
||||||
die("ContactsCustomer Save FAILED!");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now that the work is done, let the user view the updated customer
|
// Now that the work is done, let the user view the updated customer
|
||||||
$this->redirect(array('action'=>'view', $this->data['Customer']['id']));
|
$this->redirect(array('action'=>'view', $this->Customer->id));
|
||||||
//$this->autoRender = false;
|
//$this->render('/empty');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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 <AP>: 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**************************************************************************
|
/**************************************************************************
|
||||||
**************************************************************************
|
**************************************************************************
|
||||||
**************************************************************************
|
**************************************************************************
|
||||||
|
|||||||
Reference in New Issue
Block a user