[freeside-commits] branch FREESIDE_3_BRANCH updated. b75b481f255cbe08d8e6e6768924a68af4e15f82

Christopher Burger burgerc at freeside.biz
Tue Nov 20 16:07:24 PST 2018


The branch, FREESIDE_3_BRANCH has been updated
       via  b75b481f255cbe08d8e6e6768924a68af4e15f82 (commit)
       via  0ae51bc70e1b166f09063d153ebab99b623b5617 (commit)
       via  9e867f9412a7d52e7d95e265f742a28facfd3e8d (commit)
       via  482bcebb8d76081175201e444232b1408ea88e77 (commit)
       via  f4fc05af8e1ba11e79c70dfa074b143185c4cb02 (commit)
       via  203aa3dbe95397f85b02e5c626cae1419ca6c9c9 (commit)
       via  747f42e8f7279caa9c50472694dcfa6067e148b7 (commit)
      from  2b0e2b4daf4e4239297d6cda1679d2f09525ed6c (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit b75b481f255cbe08d8e6e6768924a68af4e15f82
Author: Christopher Burger <burgerc at freeside.biz>
Date:   Tue Nov 20 19:02:02 2018 -0500

    RT# 77532 - V3 fix changing cust_contact to contact

diff --git a/FS/FS/cust_main/Search.pm b/FS/FS/cust_main/Search.pm
index 1c7a64402..b1af41c2a 100644
--- a/FS/FS/cust_main/Search.pm
+++ b/FS/FS/cust_main/Search.pm
@@ -97,7 +97,7 @@ sub smart_search {
     push @cust_main, qsearch( {
       'select'    => 'cust_main.*',
       'table'     => 'cust_main',
-      'addl_from' => ' left join cust_contact  using (custnum) '.
+      'addl_from' => ' left join contact  using (custnum) '.
                      ' left join contact_phone using (contactnum) ',
       'hashref'   => { %options },
       'extra_sql' => ( scalar(keys %options) ? ' AND ' : ' WHERE ' ).
@@ -136,7 +136,7 @@ sub smart_search {
       'select'    => 'cust_main.*',
       'table'     => 'cust_main',
       'addl_from' => ' left join cust_main_invoice using (custnum) '.
-                     ' left join cust_contact      using (custnum) '.
+                     ' left join contact      using (custnum) '.
                      ' left join contact_email     using (contactnum) ',
       'hashref'   => { %options },
       'extra_sql' => ( scalar(keys %options) ? ' AND ' : ' WHERE ' ).
@@ -289,8 +289,7 @@ sub smart_search {
       push @cust_main, qsearch( {
         'select'    => 'cust_main.*',
         'table'     => 'cust_main',
-        'addl_from' => ' left join cust_contact using (custnum) '.
-                       ' left join contact using (contactnum) ',
+        'addl_from' => ' left join contact using (custnum) ',
         'hashref'   => { %options },
         'extra_sql' => "$sql AND $agentnums_sql", #agent virtualization
       } );
@@ -322,8 +321,7 @@ sub smart_search {
     push @cust_main, qsearch( {
       'select'    => 'cust_main.*',
       'table'     => 'cust_main',
-      'addl_from' => ' left join cust_contact using (custnum) '.
-                     ' left join contact using (contactnum) ',
+      'addl_from' => ' left join contact using (custnum) ',
       'hashref'   => { %options },
       'extra_sql' => "$sql AND $agentnums_sql", #agent virtualization
     } );
@@ -1094,8 +1092,6 @@ sub search {
   #   (maybe we should be using FS::UI::Web::join_cust_main instead?)
   $addl_from .= ' LEFT JOIN (select refnum, referral from part_referral) AS part_referral_x ON (cust_main.refnum = part_referral_x.refnum) ';
 
-  my $count_query = "SELECT COUNT(*) FROM cust_main $addl_from $extra_sql";
-
   my @select = (
                  'cust_main.custnum',
                  'cust_main.salesnum',
@@ -1114,10 +1110,9 @@ sub search {
     my $contact_params = $params->{'contacts'};
 
     $addl_from .=
-      ' LEFT JOIN cust_contact ON ( cust_main.custnum = cust_contact.custnum ) ';
+      ' LEFT JOIN contact ON ( cust_main.custnum = contact.custnum ) ';
 
     if ($contact_params->{'contacts_firstname'} || $contact_params->{'contacts_lastname'}) {
-      $addl_from .= ' LEFT JOIN contact ON ( cust_contact.contactnum = contact.contactnum ) ';
       my $first_query = " AND contact.first = '" . $contact_params->{'contacts_firstname'} . "'"
         unless !$contact_params->{'contacts_firstname'};
       my $last_query = " AND contact.last = '" . $contact_params->{'contacts_lastname'} . "'"
@@ -1126,12 +1121,12 @@ sub search {
     }
 
     if ($contact_params->{'contacts_email'}) {
-      $addl_from .= ' LEFT JOIN contact_email ON ( cust_contact.contactnum = contact_email.contactnum ) ';
+      $addl_from .= ' LEFT JOIN contact_email ON ( contact.contactnum = contact_email.contactnum ) ';
       $extra_sql .= " AND ( contact_email.emailaddress = '" . $contact_params->{'contacts_email'} . "' )";
     }
 
     if ($contact_params->{'contacts_homephone'} || $contact_params->{'contacts_workphone'} || $contact_params->{'contacts_mobilephone'}) {
-      $addl_from .= ' LEFT JOIN contact_phone ON ( cust_contact.contactnum = contact_phone.contactnum ) ';
+      $addl_from .= ' LEFT JOIN contact_phone ON ( contact.contactnum = contact_phone.contactnum ) ';
       my $contacts_mobilephone;
       foreach my $phone (qw( contacts_homephone contacts_workphone contacts_mobilephone )) {
         (my $num = $contact_params->{$phone}) =~ s/\W//g;
@@ -1148,6 +1143,8 @@ sub search {
 
   }
 
+  my $count_query = "SELECT COUNT(*) FROM cust_main $addl_from $extra_sql";
+
   if ($params->{'flattened_pkgs'}) {
 
     #my $pkg_join = '';
diff --git a/httemplate/search/contact.html b/httemplate/search/contact.html
index 2121527a5..1ddfabb8b 100644
--- a/httemplate/search/contact.html
+++ b/httemplate/search/contact.html
@@ -44,48 +44,30 @@ my $dbh = dbh;
 
 my $DEBUG = 0;
 
-my $work_phone_sub = sub {
-  my $contact = shift;
-  my $phone_type = qsearchs('phone_type', { 'typename' => 'Work' });
-  #can't because contactnum is in the wrong field
-  my @contact_workphone = qsearch('contact_phone', { 'contactnum' => $contact->contact_contactnum, 'phonetypenum' => $phone_type->phonetypenum } );
-  join(', ', map $_->phonenum, @contact_workphone);
+my $format_phone_sub = sub {
+
+  my $pn = $_[0] || return '';
+  $pn =~ s/\D//g;
+  my @pn = split //, $pn;
+
+  return sprintf(
+    '(%s) %s-%s',
+    join( '', @pn[0..2] ),
+    join( '', @pn[3..5] ),
+    join( '', @pn[6..9] )
+  ) if @pn == 10;
+
+  return sprintf(
+    '+%s (%s) %s-%s',
+    $pn[0],
+    join( '', @pn[1..3] ),
+    join( '', @pn[4..6] ),
+    join( '', @pn[7..10] )
+  ) if @pn == 11 && $pn[0] == 1;
+
+  encode_entities $_[0];
 };
 
-my $mobile_phone_sub = sub {
-  my $contact = shift;
-  my $phone_type = qsearchs('phone_type', { 'typename' => 'Mobile' });
-  #can't because contactnum is in the wrong field
-  my @contact_mobilephone = qsearch('contact_phone', { 'contactnum' => $contact->contact_contactnum, 'phonetypenum' => $phone_type->phonetypenum } );
-  join(', ', map $_->phonenum, @contact_mobilephone);
-};
-
-my $home_phone_sub = sub {
-  my $contact = shift;
-  my $phone_type = qsearchs('phone_type', { 'typename' => 'Home' });
-  #can't because contactnum is in the wrong field
-  my @contact_homephone = qsearch('contact_phone', { 'contactnum' => $contact->contact_contactnum, 'phonetypenum' => $phone_type->phonetypenum } );
-  join(', ', map $_->phonenum, @contact_homephone);
-};
-
-my $link; #for closure in this sub, we'll define it later
-my $contact_classname_sub = sub {
-  my $contact = shift;
-  my %hash = ( 'contactnum' => $contact->contact_contactnum );
-  my $X_contact;
-  if ( $link eq 'cust_main' ) {
-    $X_contact = qsearchs('cust_contact', { %hash, 'custnum' => $contact->custnum } );
-  } elsif ( $link eq 'prospect_main' ) {
-    $X_contact = qsearchs('prospect_contact', { %hash, 'prospectnum' => $contact->prospectnum } );
-  } else {
-    die 'guru meditation #5555';
-  }
-  $X_contact->contact_classname;
-};
-
-my @header = ( 'First', 'Last', 'Title', 'Email', 'Work Phone', 'Mobile Phone', 'Home Phone', 'Type' );
-my @fields = ( 'first', 'last', 'title', $email_sub, $work_phone_sub, $mobile_phone_sub, $home_phone_sub, $contact_classname_sub );
-my @links = ( '', '', '', '', '', '', '', '', );
 
 my @report = (
 

commit 0ae51bc70e1b166f09063d153ebab99b623b5617
Author: Ivan Kohler <ivan at freeside.biz>
Date:   Wed Aug 1 14:05:02 2018 -0700

    spacing, and order like sql, RT#77532

diff --git a/FS/FS/cust_main/Search.pm b/FS/FS/cust_main/Search.pm
index f552a2726..1c7a64402 100644
--- a/FS/FS/cust_main/Search.pm
+++ b/FS/FS/cust_main/Search.pm
@@ -95,8 +95,11 @@ sub smart_search {
 
     #cust_main phone numbers and contact phone number
     push @cust_main, qsearch( {
-      'table'   => 'cust_main',
-      'hashref' => { %options },
+      'select'    => 'cust_main.*',
+      'table'     => 'cust_main',
+      'addl_from' => ' left join cust_contact  using (custnum) '.
+                     ' left join contact_phone using (contactnum) ',
+      'hashref'   => { %options },
       'extra_sql' => ( scalar(keys %options) ? ' AND ' : ' WHERE ' ).
                      ' ( '.
                          join(' OR ', map "$_ = '$phonen'",
@@ -105,15 +108,14 @@ sub smart_search {
                           " OR phonenum = '$phonenum' ".
                      ' ) '.
                      " AND $agentnums_sql", #agent virtualization
-      'addl_from' => ' left join cust_contact using (custnum) left join contact_phone using (contactnum) ',
     } );
 
     unless ( @cust_main || $phonen =~ /x\d+$/ ) { #no exact match
       #try looking for matches with extensions unless one was specified
 
       push @cust_main, qsearch( {
-        'table'   => 'cust_main',
-        'hashref' => { %options },
+        'table'     => 'cust_main',
+        'hashref'   => { %options },
         'extra_sql' => ( scalar(keys %options) ? ' AND ' : ' WHERE ' ).
                        ' ( '.
                            join(' OR ', map "$_ LIKE '$phonen\%'",
@@ -131,8 +133,12 @@ sub smart_search {
   if ( $search =~ /@/ ) { #email address from cust_main_invoice and contact_email
 
     push @cust_main, qsearch( {
-      'table'   => 'cust_main',
-      'hashref' => { %options },
+      'select'    => 'cust_main.*',
+      'table'     => 'cust_main',
+      'addl_from' => ' left join cust_main_invoice using (custnum) '.
+                     ' left join cust_contact      using (custnum) '.
+                     ' left join contact_email     using (contactnum) ',
+      'hashref'   => { %options },
       'extra_sql' => ( scalar(keys %options) ? ' AND ' : ' WHERE ' ).
                      ' ( '.
                          join(' OR ', map "$_ = '$search'",
@@ -140,7 +146,6 @@ sub smart_search {
                              ).
                      ' ) '.
                      " AND $agentnums_sql", #agent virtualization
-      'addl_from' => ' left join cust_main_invoice using (custnum) left join cust_contact using (custnum) left join contact_email using (contactnum) ',
     } );
 
   # custnum search (also try agent_custid), with some tweaking options if your
@@ -282,11 +287,12 @@ sub smart_search {
 
       #cust_main and contacts
       push @cust_main, qsearch( {
+        'select'    => 'cust_main.*',
         'table'     => 'cust_main',
-        'select'    => 'cust_main.*, cust_contact.*, contact.contactnum, contact.last as contact_last, contact.first as contact_first, contact.title',
+        'addl_from' => ' left join cust_contact using (custnum) '.
+                       ' left join contact using (contactnum) ',
         'hashref'   => { %options },
         'extra_sql' => "$sql AND $agentnums_sql", #agent virtualization
-        'addl_from' => ' left join cust_contact on cust_main.custnum = cust_contact.custnum left join contact using (contactnum) ',
       } );
 
       # or it just be something that was typed in... (try that in a sec)
@@ -314,11 +320,12 @@ sub smart_search {
       if $conf->exists('address1-search');
 
     push @cust_main, qsearch( {
+      'select'    => 'cust_main.*',
       'table'     => 'cust_main',
-      'select'    => 'cust_main.*, cust_contact.*, contact.contactnum, contact.last as contact_last, contact.first as contact_first, contact.title',
+      'addl_from' => ' left join cust_contact using (custnum) '.
+                     ' left join contact using (contactnum) ',
       'hashref'   => { %options },
       'extra_sql' => "$sql AND $agentnums_sql", #agent virtualization
-      'addl_from' => 'left join cust_contact on cust_main.custnum = cust_contact.custnum left join contact using (contactnum) ',
     } );
 
     #no exact match, trying substring/fuzzy

commit 9e867f9412a7d52e7d95e265f742a28facfd3e8d
Author: Ivan Kohler <ivan at freeside.biz>
Date:   Sun Oct 1 14:39:47 2017 -0700

    fix error w/current perl - can't call keys on a scalar hashref anymore, RT#77532
    
    Conflicts:
            httemplate/search/cust_main.html

diff --git a/httemplate/search/cust_main.html b/httemplate/search/cust_main.html
index 0d80b6dfe..d4b4fafc1 100755
--- a/httemplate/search/cust_main.html
+++ b/httemplate/search/cust_main.html
@@ -72,9 +72,13 @@ for my $param (qw( classnum refnum payby tagnum pkg_classnum )) {
   $search_hash{$param} = [ $cgi->param($param) ];
 }
 
+my $params = $cgi->Vars;
+
 #contacts
 $search_hash{'contacts'} = {
-  map { $_ => $cgi->param($_), } grep { /^(contacts_*)/ && $cgi->param($_) } keys $cgi->Vars
+  map { $_ => $cgi->param($_), }
+    grep { /^(contacts_*)/ && $cgi->param($_) }
+      keys %$params
 };
 
 ###

commit 482bcebb8d76081175201e444232b1408ea88e77
Author: Christopher Burger <burgerc at freeside.biz>
Date:   Tue Sep 26 13:43:31 2017 -0400

    RT# 77532 - fixed search to use phone type from database

diff --git a/httemplate/search/contact.html b/httemplate/search/contact.html
index 1f7385866..2121527a5 100644
--- a/httemplate/search/contact.html
+++ b/httemplate/search/contact.html
@@ -46,22 +46,25 @@ my $DEBUG = 0;
 
 my $work_phone_sub = sub {
   my $contact = shift;
+  my $phone_type = qsearchs('phone_type', { 'typename' => 'Work' });
   #can't because contactnum is in the wrong field
-  my @contact_workphone = qsearch('contact_phone', { 'contactnum' => $contact->contact_contactnum, 'phonetypenum' => '1' } );
+  my @contact_workphone = qsearch('contact_phone', { 'contactnum' => $contact->contact_contactnum, 'phonetypenum' => $phone_type->phonetypenum } );
   join(', ', map $_->phonenum, @contact_workphone);
 };
 
 my $mobile_phone_sub = sub {
   my $contact = shift;
+  my $phone_type = qsearchs('phone_type', { 'typename' => 'Mobile' });
   #can't because contactnum is in the wrong field
-  my @contact_mobilephone = qsearch('contact_phone', { 'contactnum' => $contact->contact_contactnum, 'phonetypenum' => '3' } );
+  my @contact_mobilephone = qsearch('contact_phone', { 'contactnum' => $contact->contact_contactnum, 'phonetypenum' => $phone_type->phonetypenum } );
   join(', ', map $_->phonenum, @contact_mobilephone);
 };
 
 my $home_phone_sub = sub {
   my $contact = shift;
+  my $phone_type = qsearchs('phone_type', { 'typename' => 'Home' });
   #can't because contactnum is in the wrong field
-  my @contact_homephone = qsearch('contact_phone', { 'contactnum' => $contact->contact_contactnum, 'phonetypenum' => '2' } );
+  my @contact_homephone = qsearch('contact_phone', { 'contactnum' => $contact->contact_contactnum, 'phonetypenum' => $phone_type->phonetypenum } );
   join(', ', map $_->phonenum, @contact_homephone);
 };
 

commit f4fc05af8e1ba11e79c70dfa074b143185c4cb02
Author: Christopher Burger <burgerc at freeside.biz>
Date:   Tue Sep 19 12:12:16 2017 -0400

    RT# 77532 - Updated advanced customer report to search contacts
    
    Conflicts:
            httemplate/search/cust_main.html
            httemplate/search/report_cust_main.html

diff --git a/FS/FS/cust_main/Search.pm b/FS/FS/cust_main/Search.pm
index 78c7320e5..f552a2726 100644
--- a/FS/FS/cust_main/Search.pm
+++ b/FS/FS/cust_main/Search.pm
@@ -1102,6 +1102,45 @@ sub search {
   my @extra_fields      = ();
   my @extra_sort_fields = ();
 
+  ## search contacts
+  if ($params->{'contacts'}) {
+    my $contact_params = $params->{'contacts'};
+
+    $addl_from .=
+      ' LEFT JOIN cust_contact ON ( cust_main.custnum = cust_contact.custnum ) ';
+
+    if ($contact_params->{'contacts_firstname'} || $contact_params->{'contacts_lastname'}) {
+      $addl_from .= ' LEFT JOIN contact ON ( cust_contact.contactnum = contact.contactnum ) ';
+      my $first_query = " AND contact.first = '" . $contact_params->{'contacts_firstname'} . "'"
+        unless !$contact_params->{'contacts_firstname'};
+      my $last_query = " AND contact.last = '" . $contact_params->{'contacts_lastname'} . "'"
+        unless !$contact_params->{'contacts_lastname'};
+      $extra_sql .= " AND ( '1' $first_query $last_query )";
+    }
+
+    if ($contact_params->{'contacts_email'}) {
+      $addl_from .= ' LEFT JOIN contact_email ON ( cust_contact.contactnum = contact_email.contactnum ) ';
+      $extra_sql .= " AND ( contact_email.emailaddress = '" . $contact_params->{'contacts_email'} . "' )";
+    }
+
+    if ($contact_params->{'contacts_homephone'} || $contact_params->{'contacts_workphone'} || $contact_params->{'contacts_mobilephone'}) {
+      $addl_from .= ' LEFT JOIN contact_phone ON ( cust_contact.contactnum = contact_phone.contactnum ) ';
+      my $contacts_mobilephone;
+      foreach my $phone (qw( contacts_homephone contacts_workphone contacts_mobilephone )) {
+        (my $num = $contact_params->{$phone}) =~ s/\W//g;
+        if ( $num =~ /^1?(\d{3})(\d{3})(\d{4})(\d*)$/ ) { $contact_params->{$phone} = "$1$2$3"; }
+      }
+      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'};
+      $extra_sql .= " AND ( '1' $home_query $work_query $mobile_query )";
+    }
+
+  }
+
   if ($params->{'flattened_pkgs'}) {
 
     #my $pkg_join = '';
diff --git a/httemplate/search/cust_main.html b/httemplate/search/cust_main.html
index 18c9e153d..0d80b6dfe 100755
--- a/httemplate/search/cust_main.html
+++ b/httemplate/search/cust_main.html
@@ -72,6 +72,11 @@ for my $param (qw( classnum refnum payby tagnum pkg_classnum )) {
   $search_hash{$param} = [ $cgi->param($param) ];
 }
 
+#contacts
+$search_hash{'contacts'} = {
+  map { $_ => $cgi->param($_), } grep { /^(contacts_*)/ && $cgi->param($_) } keys $cgi->Vars
+};
+
 ###
 # parse dates
 ###
diff --git a/httemplate/search/elements/options_cust_contacts.html b/httemplate/search/elements/options_cust_contacts.html
new file mode 100644
index 000000000..cfbf834b0
--- /dev/null
+++ b/httemplate/search/elements/options_cust_contacts.html
@@ -0,0 +1,36 @@
+    <TR>
+      <TH ALIGN="right" VALIGN="center"><% mt('First name') |h %></TH>
+      <TD><INPUT TYPE="text" NAME="<%$field_prefix%>firstname" SIZE=54></TD>
+    </TR>
+
+    <TR>
+      <TH ALIGN="right" VALIGN="center"><% mt('Last name') |h %></TH>
+      <TD><INPUT TYPE="text" NAME="<%$field_prefix%>lastname" SIZE=54></TD>
+    </TR>
+
+    <TR>
+      <TH ALIGN="right" VALIGN="center"><% mt('Email') |h %></TH>
+      <TD><INPUT TYPE="text" NAME="<%$field_prefix%>email" SIZE=54></TD>
+    </TR>
+
+    <TR>
+      <TH ALIGN="right" VALIGN="center"><% mt('Home Phone') |h %></TH>
+      <TD><INPUT TYPE="text" NAME="<%$field_prefix%>homephone" SIZE=54></TD>
+    </TR>
+
+    <TR>
+      <TH ALIGN="right" VALIGN="center"><% mt('Work Phone') |h %></TH>
+      <TD><INPUT TYPE="text" NAME="<%$field_prefix%>workphone" SIZE=54></TD>
+    </TR>
+
+    <TR>
+      <TH ALIGN="right" VALIGN="center"><% mt('Mobile Phone') |h %></TH>
+      <TD><INPUT TYPE="text" NAME="<%$field_prefix%>mobilephone" SIZE=54></TD>
+    </TR>
+
+<%init>
+
+my %opt = @_;
+my $field_prefix = $opt{'pre_fix'} ? $opt{'pre_fix'} : '';
+
+</%init>
\ No newline at end of file
diff --git a/httemplate/search/report_cust_main.html b/httemplate/search/report_cust_main.html
index 9edd3ee5c..47d46e1cd 100755
--- a/httemplate/search/report_cust_main.html
+++ b/httemplate/search/report_cust_main.html
@@ -228,6 +228,18 @@
     </TR>
 
     <TR>
+      <TH CLASS="background" COLSPAN=2 ALIGN="left"><FONT SIZE="+1"><% emt('Contacts search options') %></FONT></TH>
+    </TR>
+
+    <& elements/options_cust_contacts.html,
+      'pre_fix'   => 'contacts_',
+    &>
+
+    <TR>
+      <TH CLASS="background" COLSPAN=2> </TH>
+    </TR>
+
+    <TR>
       <TH CLASS="background" COLSPAN=2 ALIGN="left"><FONT SIZE="+1"><% mt('Billing search options') |h %></FONT></TH>
     </TR>
 

commit 203aa3dbe95397f85b02e5c626cae1419ca6c9c9
Author: Christopher Burger <burgerc at freeside.biz>
Date:   Mon Sep 18 13:25:39 2017 -0400

    RT# 77532 - Updated customer contact reports to display contact phone numers
    
    Conflicts:
            httemplate/search/contact.html

diff --git a/httemplate/search/contact.html b/httemplate/search/contact.html
index 1ddfabb8b..1f7385866 100644
--- a/httemplate/search/contact.html
+++ b/httemplate/search/contact.html
@@ -44,30 +44,45 @@ my $dbh = dbh;
 
 my $DEBUG = 0;
 
-my $format_phone_sub = sub {
-
-  my $pn = $_[0] || return '';
-  $pn =~ s/\D//g;
-  my @pn = split //, $pn;
-
-  return sprintf(
-    '(%s) %s-%s',
-    join( '', @pn[0..2] ),
-    join( '', @pn[3..5] ),
-    join( '', @pn[6..9] )
-  ) if @pn == 10;
-
-  return sprintf(
-    '+%s (%s) %s-%s',
-    $pn[0],
-    join( '', @pn[1..3] ),
-    join( '', @pn[4..6] ),
-    join( '', @pn[7..10] )
-  ) if @pn == 11 && $pn[0] == 1;
-
-  encode_entities $_[0];
+my $work_phone_sub = sub {
+  my $contact = shift;
+  #can't because contactnum is in the wrong field
+  my @contact_workphone = qsearch('contact_phone', { 'contactnum' => $contact->contact_contactnum, 'phonetypenum' => '1' } );
+  join(', ', map $_->phonenum, @contact_workphone);
 };
 
+my $mobile_phone_sub = sub {
+  my $contact = shift;
+  #can't because contactnum is in the wrong field
+  my @contact_mobilephone = qsearch('contact_phone', { 'contactnum' => $contact->contact_contactnum, 'phonetypenum' => '3' } );
+  join(', ', map $_->phonenum, @contact_mobilephone);
+};
+
+my $home_phone_sub = sub {
+  my $contact = shift;
+  #can't because contactnum is in the wrong field
+  my @contact_homephone = qsearch('contact_phone', { 'contactnum' => $contact->contact_contactnum, 'phonetypenum' => '2' } );
+  join(', ', map $_->phonenum, @contact_homephone);
+};
+
+my $link; #for closure in this sub, we'll define it later
+my $contact_classname_sub = sub {
+  my $contact = shift;
+  my %hash = ( 'contactnum' => $contact->contact_contactnum );
+  my $X_contact;
+  if ( $link eq 'cust_main' ) {
+    $X_contact = qsearchs('cust_contact', { %hash, 'custnum' => $contact->custnum } );
+  } elsif ( $link eq 'prospect_main' ) {
+    $X_contact = qsearchs('prospect_contact', { %hash, 'prospectnum' => $contact->prospectnum } );
+  } else {
+    die 'guru meditation #5555';
+  }
+  $X_contact->contact_classname;
+};
+
+my @header = ( 'First', 'Last', 'Title', 'Email', 'Work Phone', 'Mobile Phone', 'Home Phone', 'Type' );
+my @fields = ( 'first', 'last', 'title', $email_sub, $work_phone_sub, $mobile_phone_sub, $home_phone_sub, $contact_classname_sub );
+my @links = ( '', '', '', '', '', '', '', '', );
 
 my @report = (
 

commit 747f42e8f7279caa9c50472694dcfa6067e148b7
Author: Christopher Burger <burgerc at freeside.biz>
Date:   Mon Sep 18 10:26:23 2017 -0400

    RT# 77532 - updated customer search bar to search contacts for info.

diff --git a/FS/FS/cust_main/Search.pm b/FS/FS/cust_main/Search.pm
index 58bdd8835..78c7320e5 100644
--- a/FS/FS/cust_main/Search.pm
+++ b/FS/FS/cust_main/Search.pm
@@ -93,7 +93,7 @@ sub smart_search {
     my $phonenum = "$1$2$3";
     #my $extension = $4;
 
-    #cust_main phone numbers
+    #cust_main phone numbers and contact phone number
     push @cust_main, qsearch( {
       'table'   => 'cust_main',
       'hashref' => { %options },
@@ -102,20 +102,12 @@ sub smart_search {
                          join(' OR ', map "$_ = '$phonen'",
                                           qw( daytime night mobile fax )
                              ).
+                          " OR phonenum = '$phonenum' ".
                      ' ) '.
                      " AND $agentnums_sql", #agent virtualization
+      'addl_from' => ' left join cust_contact using (custnum) left join contact_phone using (contactnum) ',
     } );
 
-    #contact phone numbers
-    push @cust_main,
-      grep $agentnums_href->{$_->agentnum}, #agent virt
-        grep $_, #skip contacts that don't have cust_main records
-          map $_->contact->cust_main,
-            qsearch({
-                      'table'   => 'contact_phone',
-                      'hashref' => { 'phonenum' => $phonenum },
-                   });
-
     unless ( @cust_main || $phonen =~ /x\d+$/ ) { #no exact match
       #try looking for matches with extensions unless one was specified
 
@@ -136,28 +128,20 @@ sub smart_search {
   } 
   
   
-  if ( $search =~ /@/ ) { #email address
-
-      # invoicing email address
-      push @cust_main,
-        grep $agentnums_href->{$_->agentnum}, #agent virt
-	  map $_->cust_main,
-	      qsearch( {
-			 'table'     => 'cust_main_invoice',
-			 'hashref'   => { 'dest' => $search },
-		       }
-		     );
-
-      # contact email address
-      push @cust_main,
-        grep $agentnums_href->{$_->agentnum}, #agent virt
-          grep $_, #skip contacts that don't have cust_main records
-	    map $_->contact->cust_main,
-	      qsearch( {
-			 'table'     => 'contact_email',
-			 'hashref'   => { 'emailaddress' => $search },
-		       }
-		     );
+  if ( $search =~ /@/ ) { #email address from cust_main_invoice and contact_email
+
+    push @cust_main, qsearch( {
+      'table'   => 'cust_main',
+      'hashref' => { %options },
+      'extra_sql' => ( scalar(keys %options) ? ' AND ' : ' WHERE ' ).
+                     ' ( '.
+                         join(' OR ', map "$_ = '$search'",
+                                          qw( dest emailaddress )
+                             ).
+                     ' ) '.
+                     " AND $agentnums_sql", #agent virtualization
+      'addl_from' => ' left join cust_main_invoice using (custnum) left join cust_contact using (custnum) left join contact_email using (contactnum) ',
+    } );
 
   # custnum search (also try agent_custid), with some tweaking options if your
   # legacy cust "numbers" have letters
@@ -282,8 +266,8 @@ sub smart_search {
     } elsif ( ! $NameParse->parse($value) ) {
 
       my %name = $NameParse->components;
-      $first = $name{'given_name_1'} || $name{'initials_1'}; #wtf NameParse, Ed?
-      $last  = $name{'surname_1'};
+      $first = lc($name{'given_name_1'}) || $name{'initials_1'}; #wtf NameParse, Ed?
+      $last  = lc($name{'surname_1'});
 
     }
 
@@ -293,28 +277,18 @@ sub smart_search {
 
       #exact
       my $sql = scalar(keys %options) ? ' AND ' : ' WHERE ';
-      $sql .= "( LOWER(cust_main.last) = $q_last AND LOWER(cust_main.first) = $q_first )";
+      $sql .= "( (LOWER(cust_main.last) = $q_last AND LOWER(cust_main.first) = $q_first)
+                 OR (LOWER(contact.last) = $q_last AND LOWER(contact.first) = $q_first) )";
 
-      #cust_main
+      #cust_main and contacts
       push @cust_main, qsearch( {
         'table'     => 'cust_main',
-        'hashref'   => \%options,
+        'select'    => 'cust_main.*, cust_contact.*, contact.contactnum, contact.last as contact_last, contact.first as contact_first, contact.title',
+        'hashref'   => { %options },
         'extra_sql' => "$sql AND $agentnums_sql", #agent virtualization
+        'addl_from' => ' left join cust_contact on cust_main.custnum = cust_contact.custnum left join contact using (contactnum) ',
       } );
 
-      #contacts
-      push @cust_main,
-        grep $agentnums_href->{$_->agentnum}, #agent virt
-          grep $_, #skip contacts that don't have cust_main records
-	    map $_->cust_main,
-	      qsearch( {
-			 'table'     => 'contact',
-			 'hashref'   => { 'first' => $first,
-                                          'last'  => $last,
-                                        }, 
-		       }
-		     );
-
       # or it just be something that was typed in... (try that in a sec)
 
     }
@@ -327,7 +301,9 @@ sub smart_search {
                 OR LOWER(cust_main.last)          = $q_value
                 OR LOWER(cust_main.company)       = $q_value
                 OR LOWER(cust_main.ship_company)  = $q_value
-            ";
+                OR LOWER(contact.first)           = $q_value
+                OR LOWER(contact.last)            = $q_value
+            )";
 
     #address1 (yes, it's a kludge)
     $sql .= "   OR EXISTS ( 
@@ -337,20 +313,12 @@ sub smart_search {
                           )"
       if $conf->exists('address1-search');
 
-    #contacts (look, another kludge)
-    $sql .= "   OR EXISTS ( SELECT 1 FROM contact
-                              WHERE (    LOWER(contact.first) = $q_value
-                                      OR LOWER(contact.last)  = $q_value
-                                    )
-                                AND contact.custnum IS NOT NULL
-                                AND contact.custnum = cust_main.custnum
-                          )
-              ) ";
-
     push @cust_main, qsearch( {
       'table'     => 'cust_main',
-      'hashref'   => \%options,
+      'select'    => 'cust_main.*, cust_contact.*, contact.contactnum, contact.last as contact_last, contact.first as contact_first, contact.title',
+      'hashref'   => { %options },
       'extra_sql' => "$sql AND $agentnums_sql", #agent virtualization
+      'addl_from' => 'left join cust_contact on cust_main.custnum = cust_contact.custnum left join contact using (contactnum) ',
     } );
 
     #no exact match, trying substring/fuzzy
diff --git a/httemplate/elements/searchbar-cust_main.html b/httemplate/elements/searchbar-cust_main.html
index 5bfef484a..7f8f9d850 100644
--- a/httemplate/elements/searchbar-cust_main.html
+++ b/httemplate/elements/searchbar-cust_main.html
@@ -26,7 +26,7 @@ my $curuser = $FS::CurrentUser::CurrentUser;
 my $menu_position = $curuser->option('menu_position') || 'top';
 
 my $cust_width = 246;
-my $cust_label = '(cust #, name, company';
+my $cust_label = '(cust #, name, company, email';
 if ( $conf->exists('address1-search') ) {
   $cust_label .= ', address';
   $cust_width += 56;

-----------------------------------------------------------------------

Summary of changes:
 FS/FS/cust_main/Search.pm                          | 149 +++++++++++----------
 httemplate/elements/searchbar-cust_main.html       |   2 +-
 httemplate/search/cust_main.html                   |   9 ++
 .../search/elements/options_cust_contacts.html     |  36 +++++
 httemplate/search/report_cust_main.html            |  12 ++
 5 files changed, 138 insertions(+), 70 deletions(-)
 create mode 100644 httemplate/search/elements/options_cust_contacts.html




More information about the freeside-commits mailing list