Moved the customer save logic into the model
git-svn-id: file:///svn-source/pmgr/branches/invoice_receipt_20090629@227 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
@@ -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