From 913b38d717fc7f2fae32722e9f30462fc9344fb3 Mon Sep 17 00:00:00 2001 From: Mark Wells Date: Sat, 5 Oct 2013 20:03:49 -0700 Subject: [PATCH] restore duplicate customer checking with 3.x locations, #16582 --- FS/FS/Conf.pm | 2 +- FS/FS/cust_main/Search.pm | 26 ++++++++++++++++------- httemplate/edit/cust_main/bottomfixup.js | 21 ++++++++++++------ httemplate/misc/xmlhttp-cust_main-duplicates.html | 4 +++- 4 files changed, 36 insertions(+), 17 deletions(-) diff --git a/FS/FS/Conf.pm b/FS/FS/Conf.pm index baca21dbd..16bbaad60 100644 --- a/FS/FS/Conf.pm +++ b/FS/FS/Conf.pm @@ -4044,7 +4044,7 @@ and customer address. Include units.', 'type' => 'select', 'multiple' => 1, 'select_hash' => [ - #'address1' => 'Billing address', + 'address' => 'Billing or service address', ], }, diff --git a/FS/FS/cust_main/Search.pm b/FS/FS/cust_main/Search.pm index f14f897ea..182527ff9 100644 --- a/FS/FS/cust_main/Search.pm +++ b/FS/FS/cust_main/Search.pm @@ -610,14 +610,24 @@ sub search { ## # address ## - if ( $params->{'address'} =~ /\S/ ) { - my $address = dbh->quote('%'. lc($params->{'address'}). '%'); - push @where, "EXISTS( - SELECT 1 FROM cust_location - WHERE cust_location.custnum = cust_main.custnum - AND (LOWER(cust_location.address1) LIKE $address OR - LOWER(cust_location.address2) LIKE $address) - )"; + if ( $params->{'address'} ) { + # allow this to be an arrayref + my @values = ($params->{'address'}); + @values = @{$values[0]} if ref($values[0]); + my @orwhere; + foreach (grep /\S/, @values) { + my $address = dbh->quote('%'. lc($_). '%'); + push @orwhere, + "LOWER(cust_location.address1) LIKE $address", + "LOWER(cust_location.address2) LIKE $address"; + } + if (@orwhere) { + push @where, "EXISTS( + SELECT 1 FROM cust_location + WHERE cust_location.custnum = cust_main.custnum + AND (".join(' OR ',@orwhere).") + )"; + } } ## diff --git a/httemplate/edit/cust_main/bottomfixup.js b/httemplate/edit/cust_main/bottomfixup.js index 9e18fa0df..ecfcb3cbc 100644 --- a/httemplate/edit/cust_main/bottomfixup.js +++ b/httemplate/edit/cust_main/bottomfixup.js @@ -10,17 +10,20 @@ my @fixups = ('copy_payby_fields', 'standardize_locations'); push @fixups, 'confirm_censustract' if $conf->exists('cust_main-require_censustract'); -# currently doesn't work; disable to avoid problems -#push @fixups, 'check_unique' -# if $conf->exists('cust_main-check_unique') and !$opt{'custnum'}; +my $uniqueness = $conf->config('cust_main-check_unique'); +push @fixups, 'check_unique' + if $uniqueness and !$opt{'custnum'}; push @fixups, 'do_submit'; # always last - var fixups = <% encode_json(\@fixups) %>; var fixup_position; var running = false; +<&| /elements/onload.js &> +submit_abort(); + + %# state machine to deal with all the asynchronous stuff we're doing %# call this after each fixup on success: function submit_continue() { @@ -132,10 +135,14 @@ function set_censustract(tract, year) { } function check_unique() { - var search_hash = new Object; -% foreach ($conf->config('cust_main-check_unique')) { - search_hash['<% $_ %>'] = document.CustomerForm.elements['<% $_ %>'].value; + var search_hash = {}; +% if ($uniqueness eq 'address') { + search_hash['address'] = [ + document.CustomerForm.elements['bill_address1'].value, + document.CustomerForm.elements['ship_address1'].value + ]; % } +%# no other options yet %# supported in IE8+, Firefox 3.5+, WebKit, Opera 10.5+ duplicates_form(JSON.stringify(search_hash), confirm_unique); diff --git a/httemplate/misc/xmlhttp-cust_main-duplicates.html b/httemplate/misc/xmlhttp-cust_main-duplicates.html index 7ee00af66..7cd463371 100644 --- a/httemplate/misc/xmlhttp-cust_main-duplicates.html +++ b/httemplate/misc/xmlhttp-cust_main-duplicates.html @@ -50,7 +50,9 @@ my $conf = new FS::Conf; my $sub = $cgi->param('sub'); my $hashref = decode_json($cgi->param('arg')); -my @cust_main = qsearch('cust_main', $hashref); +my $search = FS::cust_main->search($hashref); +#warn Dumper($search); +my @cust_main = qsearch( $search ); my $set_to_customer = <