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');
42 # Cache the contact_class table
44 map {$_->classnum => $_->classname}
45 qsearch(contact_class => {disabled => ''});
47 # This data structure is used to generate the sql query parameters
49 # These are included regardless of which tables we're viewing
52 contact => [qw/first last title contactnum/],
53 contact_email => [qw/emailaddress/],
58 # These are included if we're viewing customer records
61 cust_main => [qw/first last company/],
63 custnum classnum invoice_dest message_dest selfservice_access comment
67 LEFT JOIN cust_contact
68 ON (cust_main.custnum = cust_contact.custnum)
70 on (cust_contact.contactnum = contact.contactnum)
71 LEFT JOIN contact_email
72 ON (cust_contact.contactnum = contact_email.contactnum)
76 # These are included if we're viewing prospect records
79 prospect_main => [qw/company/],
80 prospect_contact => [qw/prospectnum classnum comment/],
83 LEFT JOIN prospect_contact
84 ON (prospect_main.prospectnum = prospect_contact.prospectnum)
86 on (prospect_contact.contactnum = contact.contactnum)
87 LEFT JOIN contact_email
88 ON (prospect_contact.contactnum = contact_email.contactnum)
97 my $link = $cgi->param('link'); # cust_main or prospect_main
99 push @select,'agentnum';
101 # this shouldn't happen without funny-busines
102 die "Invalid \$link type ($link)"
103 unless $link eq 'cust_main' || $link eq 'prospect_main';
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}};
114 # Filter for Contact Type
115 if (@classnum || $classnum_null) {
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) . ') ';
123 # Filter for destination
124 if (@dest && $link eq 'cust_main') {
126 push @stm, "cust_contact.${_}_dest IS NOT NULL" for @dest;
127 $extra_sql .= "\nAND (".join(' AND ',@stm).') ';
133 print join ",\n",@select;
135 print "from $link \n";
136 print "$addl_from\n";
137 print "WHERE \n $extra_sql\n";
141 # Prepare to display phone numbers
142 # adds 3 additional queries per table record :-(
143 my $get_phone_sub = sub {
147 my @p = qsearch('contact_phone', {
148 contactnum => $rec->contact_contactnum,
149 phonetypenum => $type,
151 @p ? (join ', ',map{$_->phonenum_pretty} @p) : undef;
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), };
160 # Cache contact types
162 map {$_->classnum => $_->classname}
163 qsearch(contact_class => {disabled => ''});
165 # And now for something completly different:
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' },
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',
202 $row->cust_contact_custnum
203 ? 'cust_main.cgi?'.$row->cust_contact_custnum
204 : 'prospect_main.html?'.$row->prospect_contact_prospectnum
209 if ($rec->prospect_contact_prospectnum) {
210 return encode_entities(
211 $rec->contact_company
212 || $rec->contact_last.' '.$rec->contact_first
216 $rec->cust_main_company
217 || $rec->cust_main_last.' '.$rec->cust_main_first
220 { label => 'Self-service',
223 return 'N/A' if $rec->prospect_contact_prospectnum;
224 $rec->cust_contact_selfservice_access ? 'Y' : 'N';
226 { label => 'Comment',
230 $rec->prospect_contact_prospectnum
231 ? $rec->prospect_contact_comment
232 : $rec->cust_contact_comment
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} || "");
244 my $classnum_url_part;
246 $classnum_url_part = join '', map{ "&classnums=$_" } @classnum;
247 $classnum_url_part .= '&classnums=0' if $classnum_null;
252 $dest_url_part = join '', map{ "&dest=$_" } @dest;
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') {
260 "<a href=\"${fsurl}misc/email-customers.html?".
262 '&agentnum='.$cgi->param('agentnum').
264 '&all_pkg_classnums=0'.
271 "\">Email a notice to these customers</a>";