restore duplicate customer checking with 3.x locations, #16582
authorMark Wells <mark@freeside.biz>
Sun, 6 Oct 2013 03:03:49 +0000 (20:03 -0700)
committerMark Wells <mark@freeside.biz>
Sun, 6 Oct 2013 03:03:49 +0000 (20:03 -0700)
FS/FS/Conf.pm
FS/FS/cust_main/Search.pm
httemplate/edit/cust_main/bottomfixup.js
httemplate/misc/xmlhttp-cust_main-duplicates.html

index baca21d..16bbaad 100644 (file)
@@ -4044,7 +4044,7 @@ and customer address. Include units.',
     'type'        => 'select',
     'multiple'    => 1,
     'select_hash' => [ 
-      #'address1' => 'Billing address',
+      'address' => 'Billing or service address',
     ],
   },
 
index f14f897..182527f 100644 (file)
@@ -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).")
+        )";
+    }
   }
 
   ##
index 9e18fa0..ecfcb3c 100644 (file)
@@ -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
 </%init>
-
 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);
index 7ee00af..7cd4633 100644 (file)
@@ -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 = <<EOF;
   var custnum_array = document.getElementsByName('dup_custnum');