- my @fcust = ();
- foreach ( keys %match ) {
- if ( $field eq 'address1' ) {
- #because it lives outside the table
- my $addl_from = $fuzopts{addl_from} .
- 'JOIN cust_location USING (custnum)';
- my $extra_sql = $fuzopts{extra_sql} .
- " AND cust_location.address1 = ".dbh->quote($_);
- push @fcust, qsearch({
- %fuzopts,
- 'addl_from' => $addl_from,
- 'extra_sql' => $extra_sql,
- });
- } else {
- my $hash = $fuzopts{hashref};
- $hash->{$field} = $_;
- push @fcust, qsearch({
- %fuzopts,
- 'hashref' => $hash
- });
- }
+ my $in_matches = 'IN (' .
+ join(',', map { dbh->quote($_) } keys %match) .
+ ')';
+
+ my $extra_sql = $fuzopts{extra_sql};
+ if ($extra_sql =~ /^\s*where /i or keys %{ $fuzopts{hashref} }) {
+ $extra_sql .= ' AND ';
+ } else {
+ $extra_sql .= 'WHERE ';