First pass at a customer merge function. Preliminary tests seem to be successful.
git-svn-id: file:///svn-source/pmgr/branches/v0.3_work/site@993 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
@@ -23,12 +23,16 @@ class Customer extends AppModel {
|
||||
'ContactsCustomer',
|
||||
|
||||
'Transaction',
|
||||
'Tender',
|
||||
);
|
||||
|
||||
var $hasAndBelongsToMany = array(
|
||||
'Contact',
|
||||
'Contact' => array(
|
||||
'unique' => true,
|
||||
),
|
||||
);
|
||||
|
||||
//var $default_log_level = 20;
|
||||
|
||||
/**************************************************************************
|
||||
**************************************************************************
|
||||
@@ -279,6 +283,141 @@ class Customer extends AppModel {
|
||||
}
|
||||
|
||||
|
||||
/**************************************************************************
|
||||
**************************************************************************
|
||||
**************************************************************************
|
||||
* function: merge
|
||||
* - 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'];
|
||||
}
|
||||
|
||||
$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'];
|
||||
}
|
||||
|
||||
$this->pr(17, compact('all_contacts'));
|
||||
|
||||
foreach ($contacts AS $cid) {
|
||||
if (!array_key_exists($cid, $all_contacts))
|
||||
return $this->prReturn(false);
|
||||
}
|
||||
|
||||
$this->id = $dst_id;
|
||||
$this->recursive = -1;
|
||||
//$this->recursive = 1;
|
||||
$this->read();
|
||||
|
||||
$this->pr(17, $this->data);
|
||||
|
||||
$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.
|
||||
$ret = true;
|
||||
|
||||
$this->Lease->updateAll
|
||||
(array('Lease.customer_id' => $dst_id),
|
||||
array('Lease.customer_id' => $src_id)
|
||||
);
|
||||
|
||||
$this->Tender->updateAll
|
||||
(array('Tender.customer_id' => $dst_id),
|
||||
array('Tender.customer_id' => $src_id)
|
||||
);
|
||||
|
||||
$this->StatementEntry->updateAll
|
||||
(array('StatementEntry.customer_id' => $dst_id),
|
||||
array('StatementEntry.customer_id' => $src_id)
|
||||
);
|
||||
|
||||
$this->Transaction->updateAll
|
||||
(array('Transaction.customer_id' => $dst_id),
|
||||
array('Transaction.customer_id' => $src_id)
|
||||
);
|
||||
|
||||
// Make sure our lease counts, etc are correct
|
||||
$this->update($dst_id);
|
||||
|
||||
$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 un-used or duplicate contacts
|
||||
// REVISIT <AP> 20100716:
|
||||
// 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.
|
||||
// We need some sort of Contact.deleted field...
|
||||
$this->pr(12, compact('contact_id'), "Delete Contact");
|
||||
$this->Contact->delete($contact_id);
|
||||
}
|
||||
|
||||
// Return the result
|
||||
return $this->prReturn($ret);
|
||||
}
|
||||
|
||||
|
||||
/**************************************************************************
|
||||
**************************************************************************
|
||||
**************************************************************************
|
||||
|
||||
Reference in New Issue
Block a user