Modified customer merge functionality to leave everything from the destination customer in-tact, and simply add on contacts from the source customer, if requested. This simplifies things significantly, and also the paradigm shift of starting the operation from the destination customer allows us to merge multiple customers into one with ease (no need to dig around for each source customer before clicking merge)
git-svn-id: file:///svn-source/pmgr/branches/v0.3_work@995 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
@@ -20,7 +20,13 @@ class Customer extends AppModel {
|
||||
),
|
||||
'Lease',
|
||||
'StatementEntry',
|
||||
'ContactsCustomer',
|
||||
'ContactsCustomer' => array(
|
||||
// It would be nice to claim a dependency here, which would
|
||||
// simplify deletion of a customer. However, for this to work
|
||||
// Cake must have a primaryKey as a single field. This table
|
||||
// makes use of a complex key, so we're out of luck.
|
||||
/* 'dependent' => true, */
|
||||
),
|
||||
|
||||
'Transaction',
|
||||
'Tender',
|
||||
@@ -290,91 +296,54 @@ class Customer extends AppModel {
|
||||
* - Merges two customers into one
|
||||
*/
|
||||
|
||||
function merge($dst_id, $src_id, $contacts, $primary_contact_id) {
|
||||
$this->prEnter(compact('dst_id', 'src_id', 'contacts', 'primary_contact_id'));
|
||||
|
||||
$contacts = array_unique($contacts, SORT_NUMERIC);
|
||||
if (!in_array($primary_contact_id, $contacts))
|
||||
return $this->prReturn(false);
|
||||
|
||||
$all_contacts = array();
|
||||
|
||||
$result = $this->find('all',
|
||||
array('link' => array('ContactsCustomer'),
|
||||
'fields' => array('ContactsCustomer.contact_id', 'ContactsCustomer.type'),
|
||||
'conditions' => array(array('id' => $src_id,
|
||||
'ContactsCustomer.active' => true))));
|
||||
$this->pr(17, compact('result'));
|
||||
|
||||
foreach ($result AS $contact) {
|
||||
$this->pr(17, compact('contact'), "Add contact");
|
||||
$all_contacts[$contact['ContactsCustomer']['contact_id']] = $contact['ContactsCustomer'];
|
||||
}
|
||||
function merge($dst_id, $src_id, $contacts) {
|
||||
$this->prEnter(compact('dst_id', 'src_id', 'contacts'));
|
||||
|
||||
// Get the entire list of destination customer contacts
|
||||
$dst_contacts = array();
|
||||
$result = $this->find('all',
|
||||
array('link' => array('ContactsCustomer'),
|
||||
'fields' => array('ContactsCustomer.contact_id', 'ContactsCustomer.type'),
|
||||
'conditions' => array(array('id' => $dst_id,
|
||||
'ContactsCustomer.active' => true))));
|
||||
/* array('link' => array('Contact'), */
|
||||
/* 'fields' => array('Contact.id'), */
|
||||
/* 'conditions' => array(array('Customer.id' => $src_id)))); */
|
||||
$this->pr(17, compact('result'));
|
||||
|
||||
foreach ($result AS $contact) {
|
||||
$this->pr(17, compact('contact'), "Add contact");
|
||||
$all_contacts[$contact['ContactsCustomer']['contact_id']] = $contact['ContactsCustomer'];
|
||||
$dst_contacts[$contact['ContactsCustomer']['contact_id']] = $contact['ContactsCustomer'];
|
||||
}
|
||||
$this->pr(17, compact('dst_contacts'));
|
||||
|
||||
$this->pr(17, compact('all_contacts'));
|
||||
// Get the entire list of source customer contacts
|
||||
$src_contacts = array();
|
||||
$result = $this->find('all',
|
||||
array('link' => array('ContactsCustomer'),
|
||||
'fields' => array('ContactsCustomer.contact_id', 'ContactsCustomer.type'),
|
||||
'conditions' => array(array('id' => $src_id,
|
||||
'ContactsCustomer.active' => true))));
|
||||
foreach ($result AS $contact) {
|
||||
$src_contacts[$contact['ContactsCustomer']['contact_id']] = $contact['ContactsCustomer'];
|
||||
}
|
||||
$this->pr(17, compact('src_contacts'));
|
||||
|
||||
foreach ($contacts AS $cid) {
|
||||
if (!array_key_exists($cid, $all_contacts))
|
||||
// Verify the contacts list are all valid source customer contacts
|
||||
foreach ($contacts AS $contact_id) {
|
||||
if (!array_key_exists($contact_id, $src_contacts))
|
||||
return $this->prReturn(false);
|
||||
}
|
||||
|
||||
$this->id = $dst_id;
|
||||
$this->recursive = -1;
|
||||
//$this->recursive = 1;
|
||||
$this->read();
|
||||
// Remove any contacts which are already destination customer contacts
|
||||
$contacts = array_diff($contacts, array_keys($dst_contacts));
|
||||
|
||||
$this->pr(17, $this->data);
|
||||
|
||||
if (!empty($primary_contact_id))
|
||||
$this->data['Customer']['primary_contact_id'] = $primary_contact_id;
|
||||
|
||||
$this->data['Contact'] = array();
|
||||
foreach ($contacts AS $contact_id)
|
||||
$this->data['Contact'][] = array('id' => $contact_id,
|
||||
'ContactsCustomer' =>
|
||||
array('customer_id' => $dst_id) +
|
||||
$all_contacts[$contact_id]);
|
||||
|
||||
/* $this->data['ContactsCustomer'] = array(); */
|
||||
/* foreach ($contacts AS $contact_id) { */
|
||||
/* $this->data['ContactsCustomer'][] = */
|
||||
/* //array('ContactsCustomer' => */
|
||||
/* array('customer_id' => $dst_id) + */
|
||||
/* $all_contacts[$contact_id] */
|
||||
/* //); */
|
||||
/* } */
|
||||
|
||||
$this->pr(17, $this->data);
|
||||
|
||||
/* $this->ContactsCustomer->deleteAll */
|
||||
/* (array('customer_id' => $id), false); */
|
||||
|
||||
//$this->recursive = 1;
|
||||
if (!$this->saveAll($this->data, array('validate' => false))) {
|
||||
return $this->prReturn(false);
|
||||
}
|
||||
|
||||
// At this point, since we've saved data to customer,
|
||||
// we'll proceed forward as much as possible, even
|
||||
// if we encounter an error. For now, we'll assume
|
||||
// the operation will succeed.
|
||||
// For now, we'll assume the operation will succeed.
|
||||
$ret = true;
|
||||
|
||||
// Add each desired source customer contact to the destination customer
|
||||
foreach ($contacts AS $contact_id) {
|
||||
$CM = new ContactsCustomer();
|
||||
if (!$CM->save(array('customer_id' => $dst_id)
|
||||
+ $src_contacts[$contact_id], false)) {
|
||||
$ret = false;
|
||||
}
|
||||
}
|
||||
|
||||
$this->Lease->updateAll
|
||||
(array('Lease.customer_id' => $dst_id),
|
||||
array('Lease.customer_id' => $src_id)
|
||||
@@ -398,23 +367,31 @@ class Customer extends AppModel {
|
||||
// Make sure our lease counts, etc are correct
|
||||
$this->update($dst_id);
|
||||
|
||||
// Delete the old customer
|
||||
$this->pr(12, compact('src_id'), "Delete Customer");
|
||||
$this->delete($src_id);
|
||||
|
||||
foreach (array_diff_key($all_contacts, array_flip($contacts)) AS $contact_id) {
|
||||
// Delete all the orphaned customers
|
||||
foreach (array_diff(array_keys($src_contacts), $contacts) AS $contact_id) {
|
||||
// Delete un-used or duplicate contacts
|
||||
// REVISIT <AP> 20100716:
|
||||
// REVISIT <AP> 20100702:
|
||||
// Not sure if we really want to do this.
|
||||
// On the one hand, they're probably really redundant,
|
||||
// and only clutter up the list of all contacts. On the
|
||||
// other hand, it destroys data, not only losing the
|
||||
// history, but making it difficult to recover if the
|
||||
// merge is a mistake.
|
||||
// merge is a mistake. Additionally, we need to do
|
||||
// extra checking to ensure that the contact is not
|
||||
// in use by some other customer.
|
||||
// We need some sort of Contact.deleted field...
|
||||
$this->pr(12, compact('contact_id'), "Delete Contact");
|
||||
$this->Contact->delete($contact_id);
|
||||
}
|
||||
|
||||
// Finally, delete all customer contact relationships
|
||||
$this->ContactsCustomer->deleteAll
|
||||
(array('customer_id' => $src_id), false);
|
||||
|
||||
// Return the result
|
||||
return $this->prReturn($ret);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user