1 <& elements/search.html,
2 title => emt('Contacts'),
3 name_singular => 'contact',
5 select => join(', ', @select),
7 addl_from => $addl_from,
9 extra_sql => "WHERE $extra_sql",
10 order_by => "ORDER BY contact_last,contact_first,contact_email_emailaddress"
21 html_init => $send_email_link,
28 unless $FS::CurrentUser::CurrentUser->access_right('List contacts');
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');
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 @dest = ('message') unless @dest;
43 # Cache the contact_class table
45 map {$_->classnum => $_->classname}
46 qsearch(contact_class => {disabled => ''});
48 # This data structure is used to generate the sql query parameters
50 # These are included regardless of which tables we're viewing
53 contact => [qw/first last title contactnum/],
54 contact_email => [qw/emailaddress/],
59 # These are included if we're viewing customer records
62 cust_main => [qw/first last company/],
64 custnum classnum invoice_dest message_dest selfservice_access comment
68 LEFT JOIN cust_contact
69 ON (cust_main.custnum = cust_contact.custnum)
71 on (cust_contact.contactnum = contact.contactnum)
72 LEFT JOIN contact_email
73 ON (cust_contact.contactnum = contact_email.contactnum)
77 # These are included if we're viewing prospect records
80 prospect_main => [qw/company/],
81 prospect_contact => [qw/prospectnum classnum comment/],
84 LEFT JOIN prospect_contact
85 ON (prospect_main.prospectnum = prospect_contact.prospectnum)
87 on (prospect_contact.contactnum = contact.contactnum)
88 LEFT JOIN contact_email
89 ON (prospect_contact.contactnum = contact_email.contactnum)
98 my $link = $cgi->param('link'); # cust_main or prospect_main
100 push @select,'agentnum';
102 # this shouldn't happen without funny-busines
103 die "Invalid \$link type ($link)"
104 unless $link eq 'cust_main' || $link eq 'prospect_main';
106 # Build @select and $addl_from
107 for my $key ('common', $link) {
108 $addl_from .= $colmap{$key}->{joinsql};
109 my $cols = $colmap{$key}->{cols};
110 for my $tbl (keys %{$cols}) {
111 push @select, map{ "$tbl.$_ AS ${tbl}_$_" } @{$cols->{$tbl}};
115 # Filter for Contact Type
116 if (@classnum || $classnum_null) {
118 my $tbl = $link eq 'cust_main' ? 'cust_contact' : 'prospect_contact';
119 push @stm, "${tbl}.classnum IN (".join(',',@classnum).')' if @classnum;
120 push @stm, "${tbl}.classnum IS NULL" if $classnum_null;
121 $extra_sql .= " (" . join(' OR ',@stm) . ') ';
124 # Filter for destination
125 if (@dest && $link eq 'cust_main') {
127 push @stm, "cust_contact.${_}_dest IS NOT NULL" for @dest;
128 $extra_sql .= "\nAND (".join(' OR ',@stm).') ';
134 print join ",\n",@select;
136 print "from $link \n";
137 print "$addl_from\n";
138 print "WHERE \n $extra_sql\n";
142 # Prepare to display phone numbers
143 # adds 3 additional queries per table record :-(
144 my %phonetype = (qw/1 Work 2 Home 3 Mobile 4 Fax/);
145 my %phoneid = (qw/Work 1 Home 2 Mobile 3 Fax 4/);
146 my $get_phone_sub = sub {
150 my @p = qsearch('contact_phone', {
151 contactnum => $rec->contact_contactnum,
152 phonetypenum => $phoneid{$type}
154 @p ? (join ', ',map{$_->phonenum} @p) : undef;
158 # Cache contact types
160 map {$_->classnum => $_->classname}
161 qsearch(contact_class => {disabled => ''});
163 # And now for something completly different:
165 { label => 'First', field => sub { shift->contact_first }},
166 { label => 'Last', field => sub { shift->contact_last }},
167 { label => 'Title', field => sub { shift->contact_title }},
168 { label => 'E-Mail', field => sub { shift->contact_email_emailaddress }},
169 { label => 'Work Phone', field => $get_phone_sub->('Work') },
170 { label => 'Mobile Phone', field => $get_phone_sub->('Mobile') },
171 { label => 'Home Phone', field => $get_phone_sub->('Home') },
175 if ($rec->cust_contact_custnum) {
176 return $rec->cust_contact_classnum
177 ? $classname{$rec->cust_contact_classnum}
180 return $rec->prospect_contact_classnum
181 ? $classname{$rec->prospect_contact_classnum}
185 { label => 'Send Invoices',
188 return 'N/A' if $rec->prospect_contact_prospectnum;
189 $rec->cust_contact_invoice_dest ? 'Y' : 'N';
191 { label => 'Send Messages',
194 return 'N/A' if $rec->prospect_contact_prospectnum;
195 $rec->cust_contact_message_dest ? 'Y' : 'N';
197 { label => 'Customer',
200 $rec->cust_main_custnum
201 ? ["${p}view/cust_main.cgi?", 'cust_main_custnum' ]
202 : ["${p}view/prospect_main.html?", 'prospect_main_prospectnum' ];
206 if ($rec->prospect_contact_prospectnum) {
207 return $rec->contact_company
208 || $rec->contact_last.' '.$rec->contact_first;
210 $rec->cust_main_company || $rec->cust_main_last.' '.$rec->cust_main_first;
212 { label => 'Self-service',
215 return 'N/A' if $rec->prospect_contact_prospectnum;
216 $rec->cust_contact_selfservice_access ? 'Y' : 'N';
218 { label => 'Comment',
221 $rec->prospect_contact_prospectnum
222 ? $rec->prospect_contact_comment
223 : $rec->cust_contact_comment;
227 my (@header, @fields, @links);
228 for my $col (@report) {
229 push @header, emt($col->{label});
230 push @fields, $col->{field};
231 push @links, ($col->{link} || "");
234 my $classnum_url_part;
236 $classnum_url_part = join '', map{ "&classnums=$_" } @classnum, @dest;
237 $classnum_url_part .= '&classnums=0' if $classnum_null;
240 # E-mail pipeline, from email-customers.html through to email queue job,
241 # doesn't support cust_prospect table
242 my $send_email_link = undef;
243 if ($link eq 'cust_main') {
245 "<a href=\"${fsurl}misc/email-customers.html?".
247 '&agentnum='.$cgi->param('agentnum').
249 '&all_pkg_classnums=0'.
255 "\">Email a notice to these customers</a>";