RT# 73422 Agent virt update for report Customer Contacts
[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 => 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') },
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  => sub {
199       my $rec = shift;
200       $rec->cust_main_custnum
201       ? ["${p}view/cust_main.cgi?", 'cust_main_custnum' ]
202       : ["${p}view/prospect_main.html?", 'prospect_main_prospectnum' ];
203     },
204     field => sub {
205       my $rec = shift;
206       if ($rec->prospect_contact_prospectnum) {
207         return $rec->contact_company
208           || $rec->contact_last.' '.$rec->contact_first;
209       }
210       $rec->cust_main_company || $rec->cust_main_last.' '.$rec->cust_main_first;
211     }},
212   { label => 'Self-service',
213     field => sub {
214       my $rec = shift;
215       return 'N/A' if $rec->prospect_contact_prospectnum;
216       $rec->cust_contact_selfservice_access ? 'Y' : 'N';
217     }},
218   { label => 'Comment',
219     field => sub {
220       my $rec = shift;
221       $rec->prospect_contact_prospectnum
222       ? $rec->prospect_contact_comment
223       : $rec->cust_contact_comment;
224     }},
225 );
226
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} || "");
232 }
233
234 my $classnum_url_part;
235 if (@classnum) {
236   $classnum_url_part = join '', map{ "&classnums=$_" } @classnum, @dest;
237   $classnum_url_part .= '&classnums=0' if $classnum_null;
238 }
239
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') {
244   $send_email_link =
245     "<a href=\"${fsurl}misc/email-customers.html?".
246       'table=cust_main'.
247       '&agentnum='.$cgi->param('agentnum').
248       '&POST=on'.
249       '&all_pkg_classnums=0'.
250       '&all_tags=0'.
251       '&any_pkg_status=0'.
252       '&refnum=1'.
253       '&with_email=on'.
254       $classnum_url_part.
255     "\">Email a notice to these customers</a>";
256 }
257
258 </%init>