Merge branch 'github/pr/55_reprise'
[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   my @contact_email = $contact->contact_email;
28   join(', ', map $_->emailaddress, @contact_email);
29 };
30
31 my $link; #for closure in this sub, we'll define it later
32 my $contact_classname_sub = sub {
33   my $contact = shift;
34   my %hash = ( 'contactnum' => $contact->contact_contactnum );
35   my $X_contact;
36   if ( $link eq 'cust_main' ) {
37     $X_contact = qsearchs('cust_contact', { %hash, 'custnum' => $contact->custnum } );
38   } elsif ( $link eq 'prospect_main' ) {
39     $X_contact = qsearchs('prospect_contact', { %hash, 'prospectnum' => $contact->prospectnum } );
40   } else {
41     die 'guru meditation #5555';
42   }
43   $X_contact->contact_classname;
44 };
45
46 my @header = ( 'First', 'Last', 'Title', 'Email', 'Type' );
47 my @fields = ( 'first', 'last', 'title', $email_sub, $contact_classname_sub );
48 my @links = ( '', '', '', '', '', );
49
50 my $company_link = '';
51
52 if ( $cgi->param('selfservice_access') eq 'Y' ) {
53   $hash{'selfservice_access'} = 'Y';
54 }
55
56 my $extra_sql = '';
57 $link = $cgi->param('link');
58 if ( $link ) {
59
60   my $as       = ') AS prospect_or_customer';
61
62   if ( $link eq 'cust_main' ) {
63     push @header, 'Customer';
64     push @select,
65        "COALESCE( cust_main.company, cust_main.first||' '||cust_main.last $as",
66        map "cust_contact.$_", qw( custnum classnum comment selfservice_access );
67     $addl_from =
68       ' LEFT JOIN cust_contact USING ( contactnum ) '.
69       ' LEFT JOIN cust_main ON ( cust_contact.custnum = cust_main.custnum )';
70     $extra_sql = ' cust_contact.custnum IS NOT NULL ';
71     $company_link  = [ $p.'view/cust_main.cgi?', 'custnum' ];
72   } elsif ( $link eq 'prospect_main' ) {
73     push @header, 'Prospect';
74     push @select,
75       "COALESCE( prospect_main.company, contact.first||'  '||contact.last $as",
76       map "prospect_contact.$_", qw( prospectnum classnum comment );
77     $addl_from =
78       ' LEFT JOIN prospect_contact USING ( contactnum ) '.
79       ' LEFT JOIN prospect_main ON ( prospect_contact.prospectnum = prospect_main.prospectnum )';
80     $extra_sql = ' prospect_contact.prospectnum IS NOT NULL ';
81     $company_link  = [ $p.'view/prospect_main.html?', 'prospectnum' ];
82   } else {
83     die "don't know how to report on contacts linked to specified table";
84   }
85
86   #because right now its harder to show it for both kinds of contacts
87   push @fields, 'prospect_or_customer';
88   push @links, $company_link; 
89
90 }
91
92 push @header, 'Self-service';
93 push @fields, 'selfservice_access';
94
95 push @header, 'Comment';
96 push @fields, 'comment';
97
98 $extra_sql = (keys(%hash) ? ' AND ' : ' WHERE '). $extra_sql
99  if $extra_sql;
100
101 </%init>