RT 77532 - added contact phone numbers to advanced customer report
[freeside.git] / httemplate / search / contact.html
index 35a74a5..0f2b283 100644 (file)
@@ -38,7 +38,6 @@ my $classnum_null = grep{ $_ eq 0           } $cgi->param('classnum');
 # Catch destination values from dest multi-checkbox, default to message
 # irrelevant to prospect contacts
 my @dest = grep{ /^(message|invoice)$/ } $cgi->param('dest');
-@dest = ('message') unless @dest;
 
 # Cache the contact_class table
 my %classname =
@@ -125,7 +124,7 @@ if (@classnum || $classnum_null) {
 if (@dest && $link eq 'cust_main') {
   my @stm;
   push @stm, "cust_contact.${_}_dest IS NOT NULL" for @dest;
-  $extra_sql .= "\nAND (".join(' OR ',@stm).') ';
+  $extra_sql .= "\nAND (".join(' AND ',@stm).') ';
 }
 
 if ($DEBUG) {
@@ -141,20 +140,23 @@ if ($DEBUG) {
 
 # Prepare to display phone numbers
 # adds 3 additional queries per table record :-(
-my %phonetype = (qw/1 Work 2 Home 3 Mobile 4 Fax/);
-my %phoneid   = (qw/Work 1 Home 2 Mobile 3 Fax 4/);
 my $get_phone_sub = sub {
   my $type = shift;
   return sub {
     my $rec = shift;
     my @p = qsearch('contact_phone', {
       contactnum => $rec->contact_contactnum,
-      phonetypenum => $phoneid{$type}
+      phonetypenum => $type,
     });
-    @p ? (join ', ',map{$_->phonenum} @p) : undef;
+    @p ? (join ', ',map{$_->phonenum_pretty} @p) : undef;
   };
 };
 
+my @phones;
+foreach my $phone_type ( qsearch({table=>'phone_type', order_by=>'weight'}) ) {
+  push @phones, { label => $phone_type->typename.' Phone', field => $get_phone_sub->($phone_type->phonetypenum), };
+}
+
 # Cache contact types
 my %classname =
   map {$_->classnum => $_->classname}
@@ -162,13 +164,11 @@ my %classname =
 
 # And now for something completly different:
 my @report = (
-  { label => 'First',  field => sub { encode_entities shift->contact_first }},
-  { label => 'Last',   field => sub { encode_entities shift->contact_last }},
-  { label => 'Title',  field => sub { encode_entities shift->contact_title }},
-  { label => 'E-Mail', field => sub { encode_entities shift->contact_email_emailaddress }},
-  { label => 'Work Phone',   field => $get_phone_sub->('Work') },
-  { label => 'Mobile Phone', field => $get_phone_sub->('Mobile') },
-  { label => 'Home Phone',   field => $get_phone_sub->('Home') },
+  { label => 'First',  field => 'contact_first' },
+  { label => 'Last',   field => 'contact_last'  },
+  { label => 'Title',  field => 'contact_title' },
+  { label => 'E-Mail', field => 'contact_email_emailaddress' },
+  @phones,
   { label => 'Type',
     field => sub {
       my $rec = shift;
@@ -195,12 +195,15 @@ my @report = (
       $rec->cust_contact_message_dest ? 'Y' : 'N';
     }},
   { label => 'Customer',
-    link  => sub {
-      my $rec = shift;
-      $rec->cust_main_custnum
-      ? ["${p}view/cust_main.cgi?", 'cust_main_custnum' ]
-      : ["${p}view/prospect_main.html?", 'prospect_main_prospectnum' ];
-    },
+    link => [
+      "${fsurl}view/",
+      sub {
+        my $row = shift;
+        $row->cust_contact_custnum
+        ? 'cust_main.cgi?'.$row->cust_contact_custnum
+        : 'prospect_main.html?'.$row->prospect_contact_prospectnum
+      }
+    ],
     field => sub {
       my $rec = shift;
       if ($rec->prospect_contact_prospectnum) {
@@ -240,10 +243,15 @@ for my $col (@report) {
 
 my $classnum_url_part;
 if (@classnum) {
-  $classnum_url_part = join '', map{ "&classnums=$_" } @classnum, @dest;
+  $classnum_url_part = join '', map{ "&classnums=$_" } @classnum;
   $classnum_url_part .= '&classnums=0' if $classnum_null;
 }
 
+my $dest_url_part;
+if (@dest) {
+  $dest_url_part = join '', map{ "&dest=$_" } @dest;
+}
+
 # E-mail pipeline, from email-customers.html through to email queue job,
 # doesn't support cust_prospect table
 my $send_email_link = undef;
@@ -259,6 +267,7 @@ if ($link eq 'cust_main') {
       '&refnum=1'.
       '&with_email=on'.
       $classnum_url_part.
+      $dest_url_part.
     "\">Email a notice to these customers</a>";
 }