quiet warnings about CGI::param in list context
[freeside.git] / httemplate / search / contact.html
1 <& elements/search.html,
2   title         => emt('Contacts'),
3   name_singular => 'contact',
4   query         => {
5     select    => join(', ', @select),
6     table     => $link,
7     addl_from => $addl_from,
8     hashref   => {},
9     extra_sql => "WHERE $extra_sql",
10     order_by  => "ORDER BY contact_last,contact_first,contact_email_emailaddress"
11   },
12   count_query => "
13     SELECT COUNT(*)
14     FROM $link
15     $addl_from
16     WHERE $extra_sql
17   ",
18   header    => \@header,
19   fields    => \@fields,
20   links     => \@links,
21   html_init => $send_email_link,
22   agent_virt => 1,
23   agent_pos  => 11,
24 &>
25 <%init>
26
27 die "access denied"
28   unless $FS::CurrentUser::CurrentUser->access_right('List contacts');
29
30 my $DEBUG = 0;
31
32 # Catch classnum values from multi-select box
33 # A classnum of 0 indicates to include rows where classnum IS NULL
34 $CGI::LIST_CONTEXT_WARN = 0;
35 my @classnum      = grep{ /^\d+$/ && $_ > 0 } $cgi->param('classnum');
36 my $classnum_null = grep{ $_ eq 0           } $cgi->param('classnum');
37
38 # Catch destination values from dest multi-checkbox, default to message
39 # irrelevant to prospect contacts
40 my @dest = grep{ /^(message|invoice)$/ } $cgi->param('dest');
41
42 # Cache the contact_class table
43 my %classname =
44   map {$_->classnum => $_->classname}
45   qsearch(contact_class => {disabled => ''});
46
47 # This data structure is used to generate the sql query parameters
48 my %colmap = (
49   # These are included regardless of which tables we're viewing
50   common => {
51     cols => {
52       contact => [qw/first last title contactnum/],
53       contact_email => [qw/emailaddress/],
54     },
55     joinsql => "",
56   },
57
58   # These are included if we're viewing customer records
59   cust_main => {
60     cols => {
61       cust_main => [qw/first last company/],
62       cust_contact => [qw/
63         custnum classnum invoice_dest message_dest selfservice_access comment
64       /],
65     },
66     joinsql => "
67       LEFT JOIN cust_contact
68         ON (cust_main.custnum = cust_contact.custnum)
69       LEFT JOIN contact
70         on (cust_contact.contactnum = contact.contactnum)
71       LEFT JOIN contact_email
72         ON (cust_contact.contactnum = contact_email.contactnum)
73     ",
74   },
75
76   # These are included if we're viewing prospect records
77   prospect_main => {
78     cols => {
79       prospect_main => [qw/company/],
80       prospect_contact => [qw/prospectnum classnum comment/],
81     },
82     joinsql => "
83       LEFT JOIN prospect_contact
84         ON (prospect_main.prospectnum = prospect_contact.prospectnum)
85       LEFT JOIN contact
86         on (prospect_contact.contactnum = contact.contactnum)
87       LEFT JOIN contact_email
88         ON (prospect_contact.contactnum = contact_email.contactnum)
89     ",
90   },
91 );
92
93 my @select;
94 my $addl_from;
95 my $extra_sql;
96 my $hashref;
97 my $link = $cgi->param('link'); # cust_main or prospect_main
98
99 push @select,'agentnum';
100
101 # this shouldn't happen without funny-busines
102 die "Invalid \$link type ($link)"
103   unless $link eq 'cust_main' || $link eq 'prospect_main';
104
105 # Build @select and $addl_from
106 for my $key ('common', $link) {
107   $addl_from .= $colmap{$key}->{joinsql};
108   my $cols = $colmap{$key}->{cols};
109   for my $tbl (keys %{$cols}) {
110     push @select, map{ "$tbl.$_ AS ${tbl}_$_" } @{$cols->{$tbl}};
111   }
112 }
113
114 # Filter for Contact Type
115 if (@classnum || $classnum_null) {
116   my @stm;
117   my $tbl = $link eq 'cust_main' ? 'cust_contact' : 'prospect_contact';
118   push @stm, "${tbl}.classnum IN (".join(',',@classnum).')' if @classnum;
119   push @stm, "${tbl}.classnum IS NULL" if $classnum_null;
120   $extra_sql .= " (" . join(' OR ',@stm) . ') ';
121 }
122
123 # Filter for destination
124 if (@dest && $link eq 'cust_main') {
125   my @stm;
126   push @stm, "cust_contact.${_}_dest IS NOT NULL" for @dest;
127   $extra_sql .= "\nAND (".join(' AND ',@stm).') ';
128 }
129
130 if ($DEBUG) {
131   print "<pre>\n";
132   print "select \n";
133   print join ",\n",@select;
134   print "\n";
135   print "from $link \n";
136   print "$addl_from\n";
137   print "WHERE \n $extra_sql\n";
138   print "</pre>\n";
139 }
140
141 # Prepare to display phone numbers
142 # adds 3 additional queries per table record :-(
143 my $get_phone_sub = sub {
144   my $type = shift;
145   return sub {
146     my $rec = shift;
147     my @p = qsearch('contact_phone', {
148       contactnum => $rec->contact_contactnum,
149       phonetypenum => $type,
150     });
151     @p ? (join ', ',map{$_->phonenum_pretty} @p) : undef;
152   };
153 };
154
155 my @phones;
156 foreach my $phone_type ( FS::phone_type->get_phone_types() ) {
157   push @phones, { label => $phone_type->typename.' Phone', field => $get_phone_sub->($phone_type->phonetypenum), };
158 }
159
160 # Cache contact types
161 my %classname =
162   map {$_->classnum => $_->classname}
163   qsearch(contact_class => {disabled => ''});
164
165 # And now for something completly different:
166 my @report = (
167   { label => 'First',  field => 'contact_first' },
168   { label => 'Last',   field => 'contact_last'  },
169   { label => 'Title',  field => 'contact_title' },
170   { label => 'E-Mail', field => 'contact_email_emailaddress' },
171   @phones,
172   { label => 'Type',
173     field => sub {
174       my $rec = shift;
175       if ($rec->cust_contact_custnum) {
176         return $rec->cust_contact_classnum
177                ? $classname{$rec->cust_contact_classnum}
178                : undef;
179       } else {
180         return $rec->prospect_contact_classnum
181                ? $classname{$rec->prospect_contact_classnum}
182                : undef;
183       }
184   }},
185   { label => 'Send Invoices',
186     field => sub {
187       my $rec = shift;
188       return 'N/A' if $rec->prospect_contact_prospectnum;
189       $rec->cust_contact_invoice_dest ? 'Y' : 'N';
190     }},
191   { label => 'Send Messages',
192     field => sub {
193       my $rec = shift;
194       return 'N/A' if $rec->prospect_contact_prospectnum;
195       $rec->cust_contact_message_dest ? 'Y' : 'N';
196     }},
197   { label => 'Customer',
198     link => [
199       "${fsurl}view/",
200       sub {
201         my $row = shift;
202         $row->cust_contact_custnum
203         ? 'cust_main.cgi?'.$row->cust_contact_custnum
204         : 'prospect_main.html?'.$row->prospect_contact_prospectnum
205       }
206     ],
207     field => sub {
208       my $rec = shift;
209       if ($rec->prospect_contact_prospectnum) {
210         return encode_entities(
211           $rec->contact_company
212           || $rec->contact_last.' '.$rec->contact_first
213         );
214       }
215       encode_entities(
216         $rec->cust_main_company
217         || $rec->cust_main_last.' '.$rec->cust_main_first
218       );
219     }},
220   { label => 'Self-service',
221     field => sub {
222       my $rec = shift;
223       return 'N/A' if $rec->prospect_contact_prospectnum;
224       $rec->cust_contact_selfservice_access ? 'Y' : 'N';
225     }},
226   { label => 'Comment',
227     field => sub {
228       my $rec = shift;
229       encode_entities(
230         $rec->prospect_contact_prospectnum
231         ? $rec->prospect_contact_comment
232         : $rec->cust_contact_comment
233       );
234     }},
235 );
236
237 my (@header, @fields, @links);
238 for my $col (@report) {
239   push @header, emt($col->{label});
240   push @fields, $col->{field};
241   push @links, ($col->{link} || "");
242 }
243
244 my $classnum_url_part;
245 if (@classnum) {
246   $classnum_url_part = join '', map{ "&classnums=$_" } @classnum;
247   $classnum_url_part .= '&classnums=0' if $classnum_null;
248 }
249
250 my $dest_url_part;
251 if (@dest) {
252   $dest_url_part = join '', map{ "&dest=$_" } @dest;
253 }
254
255 # E-mail pipeline, from email-customers.html through to email queue job,
256 # doesn't support cust_prospect table
257 my $send_email_link = undef;
258 if ($link eq 'cust_main') {
259   $send_email_link =
260     "<a href=\"${fsurl}misc/email-customers.html?".
261       'table=cust_main'.
262       '&agentnum='.$cgi->param('agentnum').
263       '&POST=on'.
264       '&all_pkg_classnums=0'.
265       '&all_tags=0'.
266       '&any_pkg_status=0'.
267       '&refnum=1'.
268       '&with_email=on'.
269       $classnum_url_part.
270       $dest_url_part.
271     "\">Email a notice to these customers</a>";
272 }
273
274 </%init>