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