From 34844760e84e67ac3b1c0fcdb88b21a1f381bba2 Mon Sep 17 00:00:00 2001 From: abijah Date: Fri, 2 Jul 2010 19:36:39 +0000 Subject: [PATCH] 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 --- site/controllers/customers_controller.php | 43 ++++ site/models/customer.php | 4 +- site/views/customers/merge.ctp | 258 ++++++++++++++++++++++ 3 files changed, 304 insertions(+), 1 deletion(-) create mode 100644 site/views/customers/merge.ctp diff --git a/site/controllers/customers_controller.php b/site/controllers/customers_controller.php index fb60635..bab2c95 100644 --- a/site/controllers/customers_controller.php +++ b/site/controllers/customers_controller.php @@ -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('

THIS IS NOT A POST FOR SOME REASON

'); + 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'])); + } + /************************************************************************** ************************************************************************** ************************************************************************** diff --git a/site/models/customer.php b/site/models/customer.php index 7db71fc..b76facf 100644 --- a/site/models/customer.php +++ b/site/models/customer.php @@ -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, diff --git a/site/views/customers/merge.ctp b/site/views/customers/merge.ctp new file mode 100644 index 0000000..746e689 --- /dev/null +++ b/site/views/customers/merge.ctp @@ -0,0 +1,258 @@ + + +
+ + + + +create(null, array('id' => 'customer-merge-form', + 'onsubmit' => 'return verifyRequest();', + 'url' => array('controller' => 'customers', + 'action' => 'mergeFinal')))."\n"; + + +echo ''."\n"; +echo ''."\n"; + +echo $this->element('customers', array + ('config' => array + ('grid_div_id' => 'customers-list', + 'grid_div_class' => 'text-below', + 'caption' => ('Select Customer'), + //'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 ('
' . + + '' . + + '' . + + '
' . "\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 ''."\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 ''."\n"; + +?> + +

WARNING!

+ 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.
+ +end('Merge Customers') . "\n"; +?> + + + + + + + +