spacing, RT#83503
[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 @dest = ('message') unless @dest;
42
43 # Cache the contact_class table
44 my %classname =
45   map {$_->classnum => $_->classname}
46   qsearch(contact_class => {disabled => ''});
47
48 # This data structure is used to generate the sql query parameters
49 my %colmap = (
50   # These are included regardless of which tables we're viewing
51   common => {
52     cols => {
53       contact => [qw/first last title contactnum/],
54       contact_email => [qw/emailaddress/],
55     },
56     joinsql => "",
57   },
58
59   # These are included if we're viewing customer records
60   cust_main => {
61     cols => {
62       cust_main => [qw/first last company/],
63       cust_contact => [qw/
64         custnum classnum invoice_dest message_dest selfservice_access comment
65       /],
66     },
67     joinsql => "
68       LEFT JOIN cust_contact
69         ON (cust_main.custnum = cust_contact.custnum)
70       LEFT JOIN contact
71         on (cust_contact.contactnum = contact.contactnum)
72       LEFT JOIN contact_email
73         ON (cust_contact.contactnum = contact_email.contactnum)
74     ",
75   },
76
77   # These are included if we're viewing prospect records
78   prospect_main => {
79     cols => {
80       prospect_main => [qw/company/],
81       prospect_contact => [qw/prospectnum classnum comment/],
82     },
83     joinsql => "
84       LEFT JOIN prospect_contact
85         ON (prospect_main.prospectnum = prospect_contact.prospectnum)
86       LEFT JOIN contact
87         on (prospect_contact.contactnum = contact.contactnum)
88       LEFT JOIN contact_email
89         ON (prospect_contact.contactnum = contact_email.contactnum)
90     ",
91   },
92 );
93
94 my @select;
95 my $addl_from;
96 my $extra_sql;
97 my $hashref;
98 my $link = $cgi->param('link'); # cust_main or prospect_main
99
100 push @select,'agentnum';
101
102 # this shouldn't happen without funny-busines
103 die "Invalid \$link type ($link)"
104   unless $link eq 'cust_main' || $link eq 'prospect_main';
105
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}};
112   }
113 }
114
115 # Filter for Contact Type
116 if (@classnum || $classnum_null) {
117   my @stm;
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) . ') ';
122 }
123
124 # Filter for destination
125 if (@dest && $link eq 'cust_main') {
126   my @stm;
127   push @stm, "cust_contact.${_}_dest IS NOT NULL" for @dest;
128   $extra_sql .= "\nAND (".join(' OR ',@stm).') ';
129 }
130
131 if ($DEBUG) {
132   print "<pre>\n";
133   print "select \n";
134   print join ",\n",@select;
135   print "\n";
136   print "from $link \n";
137   print "$addl_from\n";
138   print "WHERE \n $extra_sql\n";
139   print "</pre>\n";
140 }
141
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 {
147   my $type = shift;
148   return sub {
149     my $rec = shift;
150     my @p = qsearch('contact_phone', {
151       contactnum => $rec->contact_contactnum,
152       phonetypenum => $phoneid{$type}
153     });
154     @p ? (join ', ',map{$_->phonenum} @p) : undef;
155   };
156 };
157
158 # Cache contact types
159 my %classname =
160   map {$_->classnum => $_->classname}
161   qsearch(contact_class => {disabled => ''});
162
163 # And now for something completly different:
164 my @report = (
165   { label => 'First',  field => 'contact_first' },
166   { label => 'Last',   field => 'contact_last'  },
167   { label => 'Title',  field => 'contact_title' },
168   { label => 'E-Mail', field => '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') },
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>