X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;ds=sidebyside;f=FS%2FFS%2Fcust_main%2FSearch.pm;h=25216c6d65a0414f3dc31a79db64e487eb55df15;hb=07ed221540128b8c75f4cb5a2af1e01b25fa8e18;hp=7216fe604227433588b6ef4125b58988d0d5ada4;hpb=b1d26754480ee77604521236d9cf9952df2d3e12;p=freeside.git diff --git a/FS/FS/cust_main/Search.pm b/FS/FS/cust_main/Search.pm index 7216fe604..25216c6d6 100644 --- a/FS/FS/cust_main/Search.pm +++ b/FS/FS/cust_main/Search.pm @@ -618,7 +618,7 @@ sub search { my $dbh = dbh; my @where = (); - my $orderby = "ORDER BY cust_main.custnum"; + my $orderby; # initialize these to prevent warnings $params = { @@ -789,6 +789,21 @@ sub search { )"; } + ## + # phones + ## + + foreach my $phonet (qw(daytime night mobile fax)) { + if ($params->{$phonet}) { + $params->{$phonet} =~ s/\D//g; + $params->{$phonet} =~ /^(\d{3})(\d{3})(\d{4})(\d*)$/ + or next; + my $phonen = "$1-$2-$3"; + if ($4) { push @where, "cust_main.".$phonet." = '".$phonen." x$4'"; } + else { push @where, "cust_main.".$phonet." like '".$phonen."%'"; } + } + } + ### # refnum ### @@ -937,7 +952,7 @@ sub search { } } - $orderby .= ", cust_main.$field"; + $orderby ||= "ORDER BY cust_main.$field"; } @@ -1090,25 +1105,24 @@ sub search { ) "; } - if ($contact_params->{'contacts_homephone'} || $contact_params->{'contacts_workphone'} || $contact_params->{'contacts_mobilephone'}) { - foreach my $phone (qw( contacts_homephone contacts_workphone contacts_mobilephone )) { + if ( grep { /^contacts_phonetypenum(\d+)$/ } keys %{ $contact_params } ) { + my $phone_query; + foreach my $phone ( grep { /^contacts_phonetypenum(\d+)$/ } keys %{ $contact_params } ) { + $phone =~ /^contacts_phonetypenum(\d+)$/ or die "No phone type num $1 from $phone"; + my $phonetypenum = $1; (my $num = $contact_params->{$phone}) =~ s/\W//g; if ( $num =~ /^1?(\d{3})(\d{3})(\d{4})(\d*)$/ ) { $contact_params->{$phone} = "$1$2$3"; } + $phone_query .= " AND ( contact_phone.phonetypenum = '".$phonetypenum."' AND contact_phone.phonenum = '" . $contact_params->{$phone} . "' )" + unless !$contact_params->{$phone}; } - my $home_query = " AND ( contact_phone.phonetypenum = '2' AND contact_phone.phonenum = '" . $contact_params->{'contacts_homephone'} . "' )" - unless !$contact_params->{'contacts_homephone'}; - my $work_query = " AND ( contact_phone.phonetypenum = '1' AND contact_phone.phonenum = '" . $contact_params->{'contacts_workphone'} . "' )" - unless !$contact_params->{'contacts_workphone'}; - my $mobile_query = " AND ( contact_phone.phonetypenum = '3' AND contact_phone.phonenum = '" . $contact_params->{'contacts_mobilephone'} . "' )" - unless !$contact_params->{'contacts_mobilephone'}; push @where, "EXISTS ( SELECT 1 FROM contact_phone JOIN cust_contact USING (contactnum) WHERE cust_contact.custnum = cust_main.custnum - $home_query $work_query $mobile_query + $phone_query ) "; } -} + } ## @@ -1213,8 +1227,7 @@ sub search { } - # set select a customer only once. some joins create multiple customer records. - my $select = 'DISTINCT ON (cust_main.custnum) ' . join(', ', @select); + my $select = join(', ', @select); my $sql_query = { 'table' => 'cust_main',