+ { # Column: Work Phone
+ select => '
+ ( SELECT contact_phone.phonenum
+ FROM contact_phone
+ WHERE contact.contactnum = contact_phone.contactnum
+ AND phonetypenum = 1
+ ) AS work_phone
+ ',
+ fields => sub { $format_phone_sub->( shift->work_phone ) },
+ header => 'Work Phone',
+ links => undef,
+ },
+
+ { # Column: Mobile Phone
+ select => '
+ ( SELECT contact_phone.phonenum
+ FROM contact_phone
+ WHERE contact.contactnum = contact_phone.contactnum
+ AND phonetypenum = 3
+ ) AS mobile_phone
+ ',
+ fields => sub { $format_phone_sub->( shift->mobile_phone ) },
+ header => 'Mobile Phone',
+ links => undef,
+ },
+
+ # Column: Home Phone
+ # ( skipped, contact edit screen does not include this )
+
+ { # Column: Contact Type (contact_class)
+ select => 'contact_class.classname',
+ fields => 'classname',
+ header => 'Type',
+ links => undef,
+ },
+
+ { # Column: Send invoices
+ select => 'cust_main.invoice_noemail',
+ fields => sub {
+ # Prospects cannot opt out (not implemented)
+ # Contacts cannot opt out, but the attached cust_main records can.
+ # Therefore, always YES unless cust_main record is opt-out
+ my $row = shift;
+ return 'No' if $row->invoice_noemail && $row->invoice_noemail eq 'Y';
+ 'Yes';
+ },
+ header => 'Receive Invoices',
+ links => undef,
+ },
+
+ { # Column: Send messages
+ select => 'cust_main.message_noemail',
+ fields => sub {
+ # Same as invoice_noemail, see above
+ my $row = shift;
+ return 'No' if $row->message_noemail && $row->message_noemail eq 'Y';
+ 'Yes';
+ },
+ header => 'Receive Messages',
+ links => undef,
+ },
+
+ { # Column: Customer
+ # The first of these with a value will be displayed:
+ # 1) cust_main.company
+ # 2) cust_main.first + cust_main.last
+ # 3) prospect_main.company
+ # 4) contact.first + contact.last
+ select => q{
+ cust_main.custnum,
+ prospect_main.prospectnum,
+ COALESCE (
+ cust_main.company,
+ prospect_main.company,
+ cust_main.first||' '||cust_main.last,
+ contact.first||' '||contact.last
+ ) as customer_name
+ },
+ fields => 'customer_name',
+ header => 'Customer',
+ links => [
+ "${fsurl}view/",
+ sub {
+ my $row = shift;
+ $row->custnum
+ ? 'cust_main.cgi?'.$row->custnum
+ : 'prospect_main.html?'.$row->prospectnum
+ }
+ ],
+ },
+
+ # Column: Agent
+ # Inserted by search.html (hopefully)
+
+ { # Column: Self-service
+ select => 'contact.selfservice_access',
+ fields => sub { shift->selfservice_access eq 'Y' ? 'Yes' : 'No' },
+ header => 'Self-Service',
+ links => undef,
+ },
+
+ { # Column: Comments
+ select => 'contact.comment',
+ fields => 'comment',
+ header => 'Comment',
+ links => undef,
+ },
+);
+
+my $agentnum_coalesce = 'COALESCE( cust_main.agentnum, prospect_main.agentnum )';
+my @where;
+
+if ( scalar $cgi->param('agentnum') =~ /^(\d+)$/ ) {
+ push @where, "$agentnum_coalesce = $1";
+}
+
+if ( my $contact_source = scalar $cgi->param('contact_source') ) {
+ my $col = $contact_source eq 'prospect_main'
+ ? 'prospect_main.prospectnum'
+ : 'cust_main.custnum';
+ push @where, "$col IS NOT NULL"