+# Prepare to display phone numbers
+# adds 3 additional queries per table record :-(
+my $get_phone_sub = sub {
+ my $type = shift;
+ return sub {
+ my $rec = shift;
+ my @p = qsearch('contact_phone', {
+ contactnum => $rec->contact_contactnum,
+ phonetypenum => $type,
+ });
+ @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}
+ qsearch(contact_class => {disabled => ''});
+
+# And now for something completly different:
+my @report = (
+ { 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;
+ if ($rec->cust_contact_custnum) {
+ return $rec->cust_contact_classnum
+ ? $classname{$rec->cust_contact_classnum}
+ : undef;
+ } else {
+ return $rec->prospect_contact_classnum
+ ? $classname{$rec->prospect_contact_classnum}
+ : undef;
+ }
+ }},
+ { label => 'Send Invoices',
+ field => sub {
+ my $rec = shift;
+ return 'N/A' if $rec->prospect_contact_prospectnum;
+ $rec->cust_contact_invoice_dest ? 'Y' : 'N';
+ }},
+ { label => 'Send Messages',
+ field => sub {
+ my $rec = shift;
+ return 'N/A' if $rec->prospect_contact_prospectnum;
+ $rec->cust_contact_message_dest ? 'Y' : 'N';
+ }},
+ { label => 'Customer',
+ 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) {
+ return encode_entities(
+ $rec->contact_company
+ || $rec->contact_last.' '.$rec->contact_first
+ );
+ }
+ encode_entities(
+ $rec->cust_main_company
+ || $rec->cust_main_last.' '.$rec->cust_main_first
+ );
+ }},
+ { label => 'Self-service',
+ field => sub {
+ my $rec = shift;
+ return 'N/A' if $rec->prospect_contact_prospectnum;
+ $rec->cust_contact_selfservice_access ? 'Y' : 'N';
+ }},
+ { label => 'Comment',
+ field => sub {
+ my $rec = shift;
+ encode_entities(
+ $rec->prospect_contact_prospectnum
+ ? $rec->prospect_contact_comment
+ : $rec->cust_contact_comment
+ );
+ }},
+);
+
+my (@header, @fields, @links);
+for my $col (@report) {
+ push @header, emt($col->{label});
+ push @fields, $col->{field};
+ push @links, ($col->{link} || "");
+}
+
+my $classnum_url_part;
+if (@classnum) {
+ $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;
+if ($link eq 'cust_main') {
+ $send_email_link =
+ "<a href=\"${fsurl}misc/email-customers.html?".
+ 'table=cust_main'.
+ '&agentnum='.$cgi->param('agentnum').
+ '&POST=on'.
+ '&all_pkg_classnums=0'.
+ '&all_tags=0'.
+ '&any_pkg_status=0'.
+ '&refnum=1'.
+ '&with_email=on'.
+ $classnum_url_part.
+ $dest_url_part.
+ "\">Email a notice to these customers</a>";
+}