1ddfabb8bf1b96c21f470f89f49e7a2f133c6ea3
[freeside.git] / httemplate / search / contact.html
1 <& elements/search.html,
2   title            => 'Contacts',
3   name_singular    => 'contact',
4
5   query => {
6     select    => $select,
7     table     => 'contact',
8     addl_from => $addl_from,
9     hashref   => {},
10     extra_sql => $extra_sql,
11   },
12   count_query => "SELECT COUNT(*) FROM contact $addl_from $extra_sql",
13
14   header           => \@header,
15   fields           => \@fields,
16   links            => \@links,
17
18   agent_virt       => 1,
19   agent_column     => $agentnum_coalesce,
20   agent_pos        => 10,
21   agent_null_right => 'View customers of all agents',
22   agent_null_right_link => 'View customer',
23
24 &>
25
26 % if ( $DEBUG ) {
27   <pre>
28   SELECT <% $select %>
29   FROM contact
30   <% $addl_from %>
31   <% $extra_sql %>
32   ---
33   SELECT COUNT(*) FROM contact <% $addl_from %> <% $extra_sql %>
34   </pre>
35 % }
36
37 <%init>
38
39 die "access denied"
40   unless $FS::CurrentUser::CurrentUser->access_right('List contacts');
41
42 use FS::UID 'dbh';
43 my $dbh = dbh;
44
45 my $DEBUG = 0;
46
47 my $format_phone_sub = sub {
48
49   my $pn = $_[0] || return '';
50   $pn =~ s/\D//g;
51   my @pn = split //, $pn;
52
53   return sprintf(
54     '(%s) %s-%s',
55     join( '', @pn[0..2] ),
56     join( '', @pn[3..5] ),
57     join( '', @pn[6..9] )
58   ) if @pn == 10;
59
60   return sprintf(
61     '+%s (%s) %s-%s',
62     $pn[0],
63     join( '', @pn[1..3] ),
64     join( '', @pn[4..6] ),
65     join( '', @pn[7..10] )
66   ) if @pn == 11 && $pn[0] == 1;
67
68   encode_entities $_[0];
69 };
70
71
72 my @report = (
73
74   { # Column: First
75     select => 'contact.first',
76     fields => 'first',
77     header => 'First',
78     links  => undef,
79   },
80
81   { # Column: Last
82     select => 'contact.last',
83     fields => 'last',
84     header => 'Last',
85     links  => undef,
86   },
87
88   { # Column: Title
89     select => 'contact.title',
90     fields => 'title',
91     header => 'Title',
92     links  => undef,
93   },
94
95   { # Column: E-Mail
96     select => 'contact_email.emailaddress',
97     fields => 'emailaddress',
98     header => 'E-Mail',
99     links  => undef,
100   },
101
102   { # Column: Work Phone
103     select => '
104       ( SELECT contact_phone.phonenum
105         FROM contact_phone
106         WHERE contact.contactnum = contact_phone.contactnum
107           AND phonetypenum = 1
108       ) AS work_phone
109     ',
110     fields => sub { $format_phone_sub->( shift->work_phone ) },
111     header => 'Work Phone',
112     links  => undef,
113   },
114
115   { # Column: Mobile Phone
116     select => '
117       ( SELECT contact_phone.phonenum
118         FROM contact_phone
119         WHERE contact.contactnum = contact_phone.contactnum
120           AND phonetypenum = 3
121       ) AS mobile_phone
122     ',
123     fields => sub { $format_phone_sub->( shift->mobile_phone ) },
124     header => 'Mobile Phone',
125     links  => undef,
126   },
127
128   # Column: Home Phone
129   #  ( skipped, contact edit screen does not include this )
130
131   { # Column: Contact Type (contact_class)
132     select => 'contact_class.classname',
133     fields => 'classname',
134     header => 'Type',
135     links  => undef,
136   },
137
138   { # Column: Send invoices
139     select => 'cust_main.invoice_noemail',
140     fields => sub {
141       # Prospects cannot opt out (not implemented)
142       # Contacts cannot opt out, but the attached cust_main records can.
143       # Therefore, always YES unless cust_main record is opt-out
144       my $row = shift;
145       return 'No' if $row->invoice_noemail && $row->invoice_noemail eq 'Y';
146       'Yes';
147     },
148     header => 'Receive Invoices',
149     links  => undef,
150   },
151
152   { # Column: Send messages
153     select => 'cust_main.message_noemail',
154     fields => sub {
155       # Same as invoice_noemail, see above
156       my $row = shift;
157       return 'No' if $row->message_noemail && $row->message_noemail eq 'Y';
158       'Yes';
159     },
160     header => 'Receive Messages',
161     links  => undef,
162   },
163
164   { # Column: Customer
165     # The first of these with a value will be displayed:
166     #   1) cust_main.company
167     #   2) cust_main.first + cust_main.last
168     #   3) prospect_main.company
169     #   4) contact.first + contact.last
170     select => q{
171       cust_main.custnum,
172       prospect_main.prospectnum,
173       COALESCE (
174         cust_main.company,
175         prospect_main.company,
176         cust_main.first||' '||cust_main.last,
177         contact.first||' '||contact.last
178       ) as customer_name
179     },
180     fields => 'customer_name',
181     header => 'Customer',
182     links  => [
183       "${fsurl}view/",
184       sub {
185         my $row = shift;
186         $row->custnum
187         ? 'cust_main.cgi?'.$row->custnum
188         : 'prospect_main.html?'.$row->prospectnum
189       }
190     ],
191   },
192
193   # Column: Agent
194   # Inserted by search.html (hopefully)
195
196   { # Column: Self-service
197     select => 'contact.selfservice_access',
198     fields => sub { shift->selfservice_access eq 'Y' ? 'Yes' : 'No' },
199     header => 'Self-Service',
200     links  => undef,
201   },
202
203   { # Column: Comments
204     select => 'contact.comment',
205     fields => 'comment',
206     header => 'Comment',
207     links  => undef,
208   },
209 );
210
211 my $agentnum_coalesce = 'COALESCE( cust_main.agentnum, prospect_main.agentnum )';
212 my @where;
213
214 if ( scalar $cgi->param('agentnum') =~ /^(\d+)$/ ) {
215   push @where, "$agentnum_coalesce = $1";
216 }
217
218 if ( my $contact_source = scalar $cgi->param('contact_source') ) {
219   my $col = $contact_source eq 'prospect_main'
220           ? 'prospect_main.prospectnum'
221           : 'cust_main.custnum';
222   push @where, "$col IS NOT NULL"
223 }
224
225 # SQL to filter classnums is only invoked if at least one classnum
226 # checkbox is selected
227 if (
228   my @classnums = 
229     map{ /^contact_classnum_(null|\d+)$/ ? $1 : () }
230     $cgi->param
231 ) {
232   my @where_classnum;
233   for my $classnum ( @classnums ) {
234     push @where_classnum,
235       $classnum eq 'null'
236       ? ' contact.classnum IS NULL '
237       : sprintf( ' contact.classnum = %s ', $dbh->quote( $classnum ));
238   }
239   push( @where,
240     sprintf(
241       ' ( %s ) ',
242       join( ' OR ', @where_classnum )
243     )
244   );
245 }
246
247 my $select = join ', ', ( map{ $_->{select} } @report );
248 my $addl_from = '
249   LEFT JOIN contact_email USING (contactnum)
250   LEFT JOIN cust_main USING (custnum)
251   LEFT JOIN prospect_main USING (prospectnum)
252   LEFT JOIN contact_class ON contact.classnum = contact_class.classnum
253 ';
254 my $extra_sql = '';
255 my @header = map{ $_->{header} } @report;
256 my @fields = map{ $_->{fields} } @report;
257 my @links  = map{ $_->{links}  } @report;
258 my $extra_sql = 'WHERE ( ' . join( ' AND ', @where ) . ' ) '
259   if @where;
260
261 </%init>