Added the front end code to perform a customer merge. Seems to work, although I don't fully trust it yet. Now that implementation is done, I realize I don't care for the interface. Really, the destination customer should keep its primary contact and all other contacts. The user should only be given the choice of adding zero to all contacts from the source customer. A change in primary contact could always be handled after the merge.
git-svn-id: file:///svn-source/pmgr/branches/v0.3_work@994 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
|
||||
class CustomersController extends AppController {
|
||||
|
||||
var $components = array('RequestHandler');
|
||||
|
||||
// DEBUG FUNCTION ONLY!
|
||||
// Call without id to update ALL customers
|
||||
function force_update($id = null) {
|
||||
@@ -311,6 +313,11 @@ class CustomersController extends AppController {
|
||||
array('action' => 'edit', $id), null,
|
||||
'ACTION');
|
||||
|
||||
if ($this->admin())
|
||||
$this->addSideMenuLink('Merge',
|
||||
array('action' => 'merge', $id), null,
|
||||
'ACTION');
|
||||
|
||||
// Prepare to render.
|
||||
$title = 'Customer: ' . $customer['Customer']['name'];
|
||||
$this->set(compact('customer', 'title',
|
||||
@@ -445,6 +452,42 @@ class CustomersController extends AppController {
|
||||
$this->edit();
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
**************************************************************************
|
||||
**************************************************************************
|
||||
* action: merge
|
||||
* - Merges two customers
|
||||
*/
|
||||
|
||||
function merge($id = null) {
|
||||
if ($id) {
|
||||
$this->Customer->recursive = -1;
|
||||
$customer = $this->Customer->read(null, $id);
|
||||
$customer = $customer['Customer'];
|
||||
if (empty($customer))
|
||||
$this->INTERNAL_ERROR("Customer $id does not exist");
|
||||
$this->set('src_customer', $customer);
|
||||
$this->set('src_name', $customer['name']);
|
||||
$this->set('src_id', $id);
|
||||
}
|
||||
else {
|
||||
$this->INTERNAL_ERROR("Merge called with invalid customer");
|
||||
}
|
||||
}
|
||||
|
||||
function mergeFinal() {
|
||||
if (!$this->RequestHandler->isPost()) {
|
||||
echo('<H2>THIS IS NOT A POST FOR SOME REASON</H2>');
|
||||
return;
|
||||
}
|
||||
|
||||
$post = $this->params['form'];
|
||||
$this->Customer->merge($post['dst-id'], $post['src-id'],
|
||||
unserialize($post['contact-ids']),
|
||||
$post['primary-contact-id']);
|
||||
$this->redirect(array('action'=>'view', $post['dst-id']));
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
**************************************************************************
|
||||
**************************************************************************
|
||||
|
||||
@@ -340,7 +340,9 @@ class Customer extends AppModel {
|
||||
|
||||
$this->pr(17, $this->data);
|
||||
|
||||
$this->data['Customer']['primary_contact_id'] = $primary_contact_id;
|
||||
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,
|
||||
|
||||
258
site/views/customers/merge.ctp
Normal file
258
site/views/customers/merge.ctp
Normal file
@@ -0,0 +1,258 @@
|
||||
<?php /* -*- mode:PHP -*- */ ?>
|
||||
|
||||
<div class="customer merge">
|
||||
<?php
|
||||
; // Editor alignment
|
||||
|
||||
/**********************************************************************
|
||||
**********************************************************************
|
||||
**********************************************************************
|
||||
**********************************************************************
|
||||
* Javascript
|
||||
*/
|
||||
|
||||
// Warnings _really_ screw up javascript
|
||||
$saved_debug_state = Configure::read('debug');
|
||||
Configure::write('debug', '0');
|
||||
?>
|
||||
|
||||
<script type="text/javascript"><!--
|
||||
|
||||
// pre-submit callback
|
||||
function verifyRequest() {
|
||||
var pid;
|
||||
var rows;
|
||||
|
||||
if (!$("#dst-customer-id").val()) {
|
||||
alert("Must select destination customer");
|
||||
return false;
|
||||
}
|
||||
|
||||
pid = $('#<?php echo "primary-contacts-jqGrid"; ?>').getGridParam('selrow');
|
||||
if (!pid) {
|
||||
alert("Must select a primary contact");
|
||||
return false;
|
||||
}
|
||||
|
||||
rows = $('#<?php echo "contacts-jqGrid"; ?>').getGridParam('selarrrow');
|
||||
if (!rows.length) {
|
||||
alert("Must select at least one contact");
|
||||
return false;
|
||||
}
|
||||
|
||||
var found = false;
|
||||
for (var i=0; i < rows.length; ++i) {
|
||||
if (pid == rows[i]) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
alert("Primary contact is not a member of the selected contacts");
|
||||
return false;
|
||||
}
|
||||
|
||||
$('#<?php echo "primary-contact-id"; ?>').val(pid);
|
||||
$('#<?php echo "contact-ids"; ?>').val(serialize(rows));
|
||||
|
||||
// return false to prevent the form from being submitted;
|
||||
// anything other than false will allow submission.
|
||||
return true;
|
||||
}
|
||||
|
||||
// Reset the form
|
||||
function resetForm() {
|
||||
$('#payment-entry-id').val(1);
|
||||
$('#payments').html('');
|
||||
|
||||
//updateContacts();
|
||||
}
|
||||
|
||||
function updateContacts() {
|
||||
$('#contacts-jqGrid').clearGridData();
|
||||
|
||||
var filter = new Array();
|
||||
//filter['ContactsCustomer.customer_id'] = <?php echo $src_id ?>;
|
||||
//filter['ContactsCustomer.customer_id'] = $("#dst-customer-id").val();
|
||||
filter['ContactsCustomer.customer_id'] = new Array(<?php echo $src_id ?>, $("#dst-customer-id").val());
|
||||
|
||||
var dynamic_post = new Array();
|
||||
dynamic_post['filter'] = filter;
|
||||
|
||||
$('#contacts-jqGrid').setPostDataItem('dynamic_post_replace', serialize(dynamic_post));
|
||||
$('#contacts-jqGrid')
|
||||
.setGridParam({ page: 1 })
|
||||
.trigger("reloadGrid");
|
||||
|
||||
$('#primary-contacts-jqGrid').setPostDataItem('dynamic_post_replace', serialize(dynamic_post));
|
||||
$('#primary-contacts-jqGrid')
|
||||
.setGridParam({ page: 1 })
|
||||
.trigger("reloadGrid");
|
||||
|
||||
/* var gridstate = $('#contacts-jqGrid').getGridParam('gridstate'); */
|
||||
/* var gridstate = $('#primary-contacts-jqGrid').getGridParam('gridstate'); */
|
||||
/* if (gridstate == 'hidden') */
|
||||
/* $('#contacts .HeaderButton').click(); */
|
||||
}
|
||||
|
||||
function onRowSelect(grid_id, customer_id) {
|
||||
//$('#output-debug').append("select: "+grid_id+"; "+customer_id+"<BR>\n");
|
||||
|
||||
// Set the item id that will be returned with the form
|
||||
$("#dst-customer-id").val(customer_id);
|
||||
|
||||
// Get the item name from the grid
|
||||
$("#dst-customer-name").html($(grid_id).getCell(customer_id, "Customer-name"));
|
||||
|
||||
updateContacts();
|
||||
|
||||
$("#customers-list .HeaderButton").click();
|
||||
}
|
||||
|
||||
function onGridState(grid_id, state) {
|
||||
//$('#output-debug').append("state: "+grid_id+"; "+state+"<BR>\n");
|
||||
|
||||
if (state == 'visible') {
|
||||
$(".customer-selection-invalid").hide();
|
||||
$(".customer-selection-valid").hide();
|
||||
}
|
||||
else {
|
||||
if ($("#dst-customer-id").val() > 0) {
|
||||
$(".customer-selection-invalid").hide();
|
||||
$(".customer-selection-valid").show();
|
||||
} else {
|
||||
$(".customer-selection-invalid").show();
|
||||
$(".customer-selection-valid").hide();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* function onContactsGridLoadComplete() { */
|
||||
/* var userdata = $('#contacts-jqGrid').getGridParam('userData'); */
|
||||
/* } */
|
||||
|
||||
--></script>
|
||||
|
||||
<?php
|
||||
; // align
|
||||
// Re-Enable warnings
|
||||
Configure::write('debug', $saved_debug_state);
|
||||
|
||||
echo $form->create(null, array('id' => 'customer-merge-form',
|
||||
'onsubmit' => 'return verifyRequest();',
|
||||
'url' => array('controller' => 'customers',
|
||||
'action' => 'mergeFinal')))."\n";
|
||||
|
||||
|
||||
echo '<input type="hidden" id="src-customer-id" name="src-id" value="'.$src_id.'" />'."\n";
|
||||
echo '<input type="hidden" id="dst-customer-id" name="dst-id" value="0" />'."\n";
|
||||
|
||||
echo $this->element('customers', array
|
||||
('config' => array
|
||||
('grid_div_id' => 'customers-list',
|
||||
'grid_div_class' => 'text-below',
|
||||
'caption' => ('<A HREF="#" ONCLICK="$(\'#customers-list .HeaderButton\').click();'.
|
||||
' return false;">Select Customer</A>'),
|
||||
//'grid_setup' => array('hiddengrid' => isset($customer['id'])),
|
||||
'grid_events' => array('onSelectRow' =>
|
||||
array('ids' =>
|
||||
'if (ids != null){onRowSelect("#"+$(this).attr("id"), ids);}'),
|
||||
'onHeaderClick' =>
|
||||
array('gridstate' =>
|
||||
'onGridState("#"+$(this).attr("id"), gridstate)'),
|
||||
),
|
||||
//'action' => 'current',
|
||||
//'nolinks' => true,
|
||||
'limit' => 10,
|
||||
)));
|
||||
|
||||
echo ('<DIV CLASS="customer-merge grid-selection-text">' .
|
||||
|
||||
'<DIV CLASS="customer-selection-valid" style="display:none">' .
|
||||
'Destination Customer: <SPAN id="dst-customer-name"></SPAN>' .
|
||||
'</DIV>' .
|
||||
|
||||
'<DIV CLASS="customer-selection-invalid" style="display:none">' .
|
||||
'Please select customer to merge into' .
|
||||
'</DIV>' .
|
||||
|
||||
'</DIV>' . "\n");
|
||||
|
||||
echo $this->element('contacts', array
|
||||
(// Grid configuration
|
||||
'config' => array
|
||||
(
|
||||
'grid_div_id' => 'primary-contacts',
|
||||
/* 'grid_events' => array('onSelectRow' => */
|
||||
/* array('ids' => */
|
||||
/* 'if (ids != null){$("#primary-contacts-list .HeaderButton").click();}')), */
|
||||
'grid_setup' => array('select' => true),
|
||||
'caption' => 'Primary Contact',
|
||||
'filter' => array('ContactsCustomer.customer_id' => $src_id),
|
||||
//'exclude' => array('Customer', 'Type', 'Debit', 'Credit'),
|
||||
//'include' => array('Applied', 'Balance'),
|
||||
//'remap' => array('Received' => 'Paid'),
|
||||
//'limit' => 20,
|
||||
),
|
||||
));
|
||||
|
||||
// Add a hidden item to hold the jqGrid selection,
|
||||
// which we'll populate prior to form submission.
|
||||
echo "\n";
|
||||
echo '<input type="hidden" id="primary-contact-id" name="primary-contact-id" value="0" />'."\n";
|
||||
|
||||
echo $this->element('contacts', array
|
||||
(// Grid configuration
|
||||
'config' => array
|
||||
(
|
||||
'grid_div_id' => 'contacts',
|
||||
//'grid_events' => array('loadComplete' => 'onContactsGridLoadComplete()'),
|
||||
'grid_setup' => array('multiselect' => true),
|
||||
'caption' => 'Combined Contacts',
|
||||
'filter' => array('ContactsCustomer.customer_id' => $src_id),
|
||||
//'exclude' => array('Customer', 'Type', 'Debit', 'Credit'),
|
||||
//'include' => array('Applied', 'Balance'),
|
||||
//'remap' => array('Received' => 'Paid'),
|
||||
//'limit' => 20,
|
||||
),
|
||||
));
|
||||
|
||||
// Add a hidden item to hold the jqGrid selection,
|
||||
// which we'll populate prior to form submission.
|
||||
echo "\n";
|
||||
echo '<input type="hidden" id="contact-ids" name="contact-ids" value="" />'."\n";
|
||||
|
||||
?>
|
||||
|
||||
<H3>WARNING!</H3>
|
||||
<?php echo $src_name ?> is about to be deleted, and all data (leases, transactions, etc) from that customer will be merged into the destination customer selected above. This process is NOT reversible, so please ensure the selections are correct before proceeding.<BR>
|
||||
|
||||
<?php
|
||||
echo $form->end('Merge Customers') . "\n";
|
||||
?>
|
||||
|
||||
<div id="output-debug" style="display:none"></div>
|
||||
|
||||
<?php
|
||||
// Warnings _really_ screw up javascript
|
||||
Configure::write('debug', '0');
|
||||
?>
|
||||
|
||||
<script type="text/javascript"><!--
|
||||
$(document).ready(function(){
|
||||
|
||||
$("#dst-customer-id").val(0);
|
||||
$("#dst-customer-name").html("INTERNAL ERROR");
|
||||
//onGridState(null, 'visible');
|
||||
|
||||
resetForm();
|
||||
|
||||
<?php if ($this->params['dev']): ?>
|
||||
$('#output-debug').show();
|
||||
<?php endif; ?>
|
||||
|
||||
});
|
||||
--></script>
|
||||
|
||||
</div>
|
||||
Reference in New Issue
Block a user