RT# 77532 - Updated customer contact reports to display contact phone numers
[freeside.git] / httemplate / search / contact.html
1 <& elements/search.html,
2   title         => 'Contacts',
3   name_singular => 'contact',
4   query         => { select    => join(', ', @select),
5                      table     => 'contact',
6                      addl_from => $addl_from,
7                      hashref   => \%hash,
8                      extra_sql => $extra_sql,
9                    },
10   count_query   => "SELECT COUNT(*) FROM contact $addl_from $extra_sql", #XXX
11   header        => \@header,
12   fields        => \@fields,
13   links         => \@links,
14 &>
15 <%init>
16
17 die "access denied"
18   unless $FS::CurrentUser::CurrentUser->access_right('List contacts');
19
20 my @select = 'contact.contactnum AS contact_contactnum'; #if we select it as bare contactnum, the multi-customer listings go away
21 push @select, map "contact.$_", qw( first last title );
22 my %hash = ();
23 my $addl_from = '';
24
25 my $email_sub = sub {
26   my $contact = shift;
27   #can't because contactnum is in the wrong field #my @contact_email = $contact->contact_email;
28   my @contact_email = qsearch('contact_email', { 'contactnum' => $contact->contact_contactnum } );
29   join(', ', map $_->emailaddress, @contact_email);
30 };
31
32 my $work_phone_sub = sub {
33   my $contact = shift;
34   #can't because contactnum is in the wrong field
35   my @contact_workphone = qsearch('contact_phone', { 'contactnum' => $contact->contact_contactnum, 'phonetypenum' => '1' } );
36   join(', ', map $_->phonenum, @contact_workphone);
37 };
38
39 my $mobile_phone_sub = sub {
40   my $contact = shift;
41   #can't because contactnum is in the wrong field
42   my @contact_mobilephone = qsearch('contact_phone', { 'contactnum' => $contact->contact_contactnum, 'phonetypenum' => '3' } );
43   join(', ', map $_->phonenum, @contact_mobilephone);
44 };
45
46 my $home_phone_sub = sub {
47   my $contact = shift;
48   #can't because contactnum is in the wrong field
49   my @contact_homephone = qsearch('contact_phone', { 'contactnum' => $contact->contact_contactnum, 'phonetypenum' => '2' } );
50   join(', ', map $_->phonenum, @contact_homephone);
51 };
52
53 my $link; #for closure in this sub, we'll define it later
54 my $contact_classname_sub = sub {
55   my $contact = shift;
56   my %hash = ( 'contactnum' => $contact->contact_contactnum );
57   my $X_contact;
58   if ( $link eq 'cust_main' ) {
59     $X_contact = qsearchs('cust_contact', { %hash, 'custnum' => $contact->custnum } );
60   } elsif ( $link eq 'prospect_main' ) {
61     $X_contact = qsearchs('prospect_contact', { %hash, 'prospectnum' => $contact->prospectnum } );
62   } else {
63     die 'guru meditation #5555';
64   }
65   $X_contact->contact_classname;
66 };
67
68 my @header = ( 'First', 'Last', 'Title', 'Email', 'Work Phone', 'Mobile Phone', 'Home Phone', 'Type' );
69 my @fields = ( 'first', 'last', 'title', $email_sub, $work_phone_sub, $mobile_phone_sub, $home_phone_sub, $contact_classname_sub );
70 my @links = ( '', '', '', '', '', '', '', '', );
71
72 my $company_link = '';
73
74 if ( $cgi->param('selfservice_access') eq 'Y' ) {
75   $hash{'selfservice_access'} = 'Y';
76 }
77
78 my $extra_sql = '';
79 $link = $cgi->param('link');
80 if ( $link ) {
81
82   my $as       = ') AS prospect_or_customer';
83
84   if ( $link eq 'cust_main' ) {
85     push @header, 'Customer';
86     push @select,
87        "COALESCE( cust_main.company, cust_main.first||' '||cust_main.last $as",
88        map "cust_contact.$_", qw( custnum classnum comment selfservice_access );
89     $addl_from =
90       ' LEFT JOIN cust_contact USING ( contactnum ) '.
91       ' LEFT JOIN cust_main ON ( cust_contact.custnum = cust_main.custnum )';
92     $extra_sql = ' cust_contact.custnum IS NOT NULL ';
93     $company_link  = [ $p.'view/cust_main.cgi?', 'custnum' ];
94   } elsif ( $link eq 'prospect_main' ) {
95     push @header, 'Prospect';
96     push @select,
97       "COALESCE( prospect_main.company, contact.first||'  '||contact.last $as",
98       map "prospect_contact.$_", qw( prospectnum classnum comment );
99     $addl_from =
100       ' LEFT JOIN prospect_contact USING ( contactnum ) '.
101       ' LEFT JOIN prospect_main ON ( prospect_contact.prospectnum = prospect_main.prospectnum )';
102     $extra_sql = ' prospect_contact.prospectnum IS NOT NULL ';
103     $company_link  = [ $p.'view/prospect_main.html?', 'prospectnum' ];
104   } else {
105     die "don't know how to report on contacts linked to specified table";
106   }
107
108   #because right now its harder to show it for both kinds of contacts
109   push @fields, 'prospect_or_customer';
110   push @links, $company_link; 
111
112 }
113
114 push @header, 'Self-service';
115 push @fields, 'selfservice_access';
116
117 push @header, 'Comment';
118 push @fields, 'comment';
119
120 $extra_sql = (keys(%hash) ? ' AND ' : ' WHERE '). $extra_sql
121  if $extra_sql;
122
123 </%init>