RT# 77532 - fixed search to use phone type from database
[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   my $phone_type = qsearchs('phone_type', { 'typename' => 'Work' });
35   #can't because contactnum is in the wrong field
36   my @contact_workphone = qsearch('contact_phone', { 'contactnum' => $contact->contact_contactnum, 'phonetypenum' => $phone_type->phonetypenum } );
37   join(', ', map $_->phonenum, @contact_workphone);
38 };
39
40 my $mobile_phone_sub = sub {
41   my $contact = shift;
42   my $phone_type = qsearchs('phone_type', { 'typename' => 'Mobile' });
43   #can't because contactnum is in the wrong field
44   my @contact_mobilephone = qsearch('contact_phone', { 'contactnum' => $contact->contact_contactnum, 'phonetypenum' => $phone_type->phonetypenum } );
45   join(', ', map $_->phonenum, @contact_mobilephone);
46 };
47
48 my $home_phone_sub = sub {
49   my $contact = shift;
50   my $phone_type = qsearchs('phone_type', { 'typename' => 'Home' });
51   #can't because contactnum is in the wrong field
52   my @contact_homephone = qsearch('contact_phone', { 'contactnum' => $contact->contact_contactnum, 'phonetypenum' => $phone_type->phonetypenum } );
53   join(', ', map $_->phonenum, @contact_homephone);
54 };
55
56 my $link; #for closure in this sub, we'll define it later
57 my $contact_classname_sub = sub {
58   my $contact = shift;
59   my %hash = ( 'contactnum' => $contact->contact_contactnum );
60   my $X_contact;
61   if ( $link eq 'cust_main' ) {
62     $X_contact = qsearchs('cust_contact', { %hash, 'custnum' => $contact->custnum } );
63   } elsif ( $link eq 'prospect_main' ) {
64     $X_contact = qsearchs('prospect_contact', { %hash, 'prospectnum' => $contact->prospectnum } );
65   } else {
66     die 'guru meditation #5555';
67   }
68   $X_contact->contact_classname;
69 };
70
71 my @header = ( 'First', 'Last', 'Title', 'Email', 'Work Phone', 'Mobile Phone', 'Home Phone', 'Type' );
72 my @fields = ( 'first', 'last', 'title', $email_sub, $work_phone_sub, $mobile_phone_sub, $home_phone_sub, $contact_classname_sub );
73 my @links = ( '', '', '', '', '', '', '', '', );
74
75 my $company_link = '';
76
77 if ( $cgi->param('selfservice_access') eq 'Y' ) {
78   $hash{'selfservice_access'} = 'Y';
79 }
80
81 my $extra_sql = '';
82 $link = $cgi->param('link');
83 if ( $link ) {
84
85   my $as       = ') AS prospect_or_customer';
86
87   if ( $link eq 'cust_main' ) {
88     push @header, 'Customer';
89     push @select,
90        "COALESCE( cust_main.company, cust_main.first||' '||cust_main.last $as",
91        map "cust_contact.$_", qw( custnum classnum comment selfservice_access );
92     $addl_from =
93       ' LEFT JOIN cust_contact USING ( contactnum ) '.
94       ' LEFT JOIN cust_main ON ( cust_contact.custnum = cust_main.custnum )';
95     $extra_sql = ' cust_contact.custnum IS NOT NULL ';
96     $company_link  = [ $p.'view/cust_main.cgi?', 'custnum' ];
97   } elsif ( $link eq 'prospect_main' ) {
98     push @header, 'Prospect';
99     push @select,
100       "COALESCE( prospect_main.company, contact.first||'  '||contact.last $as",
101       map "prospect_contact.$_", qw( prospectnum classnum comment );
102     $addl_from =
103       ' LEFT JOIN prospect_contact USING ( contactnum ) '.
104       ' LEFT JOIN prospect_main ON ( prospect_contact.prospectnum = prospect_main.prospectnum )';
105     $extra_sql = ' prospect_contact.prospectnum IS NOT NULL ';
106     $company_link  = [ $p.'view/prospect_main.html?', 'prospectnum' ];
107   } else {
108     die "don't know how to report on contacts linked to specified table";
109   }
110
111   #because right now its harder to show it for both kinds of contacts
112   push @fields, 'prospect_or_customer';
113   push @links, $company_link; 
114
115 }
116
117 push @header, 'Self-service';
118 push @fields, 'selfservice_access';
119
120 push @header, 'Comment';
121 push @fields, 'comment';
122
123 $extra_sql = (keys(%hash) ? ' AND ' : ' WHERE '). $extra_sql
124  if $extra_sql;
125
126 </%init>