summaryrefslogtreecommitdiff
path: root/httemplate/search/contact.html
blob: 5def389cccb663a26cd7d7d63169806c212b8026 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<& elements/search.html,
  title         => emt('Contacts'),
  name_singular => 'contact',
  query         => {
    select    => join(', ', @select),
    table     => $link,
    addl_from => $addl_from,
    hashref   => {},
    extra_sql => "WHERE $extra_sql",
    order_by  => "ORDER BY contact_last,contact_first,contact_email_emailaddress"
  },
  count_query => "
    SELECT COUNT(*)
    FROM $link
    $addl_from
    WHERE $extra_sql
  ",
  header    => \@header,
  fields    => \@fields,
  links     => \@links,
  html_init => $send_email_link,
  agent_virt => 1,
  agent_pos  => 11,
&>
<%init>

die "access denied"
  unless $FS::CurrentUser::CurrentUser->access_right('List contacts');

my $DEBUG = 0;

# Catch classnum values from multi-select box
# A classnum of 0 indicates to include rows where classnum IS NULL
$CGI::LIST_CONTEXT_WARN = 0;
my @classnum      = grep{ /^\d+$/ && $_ > 0 } $cgi->param('classnum');
my $classnum_null = grep{ $_ eq 0           } $cgi->param('classnum');

# Catch destination values from dest multi-checkbox, default to message
# irrelevant to prospect contacts
my @dest = grep{ /^(message|invoice)$/ } $cgi->param('dest');

# Cache the contact_class table
my %classname =
  map {$_->classnum => $_->classname}
  qsearch(contact_class => {disabled => ''});

# This data structure is used to generate the sql query parameters
my %colmap = (
  # These are included regardless of which tables we're viewing
  common => {
    cols => {
      contact => [qw/first last title contactnum/],
      contact_email => [qw/emailaddress/],
    },
    joinsql => "",
  },

  # These are included if we're viewing customer records
  cust_main => {
    cols => {
      cust_main => [qw/first last company/],
      cust_contact => [qw/
        custnum classnum invoice_dest message_dest selfservice_access comment
      /],
    },
    joinsql => "
      LEFT JOIN cust_contact
        ON (cust_main.custnum = cust_contact.custnum)
      LEFT JOIN contact
        on (cust_contact.contactnum = contact.contactnum)
      LEFT JOIN contact_email
        ON (cust_contact.contactnum = contact_email.contactnum)
    ",
  },

  # These are included if we're viewing prospect records
  prospect_main => {
    cols => {
      prospect_main => [qw/company/],
      prospect_contact => [qw/prospectnum classnum comment/],
    },
    joinsql => "
      LEFT JOIN prospect_contact
        ON (prospect_main.prospectnum = prospect_contact.prospectnum)
      LEFT JOIN contact
        on (prospect_contact.contactnum = contact.contactnum)
      LEFT JOIN contact_email
        ON (prospect_contact.contactnum = contact_email.contactnum)
    ",
  },
);

my @select;
my $addl_from;
my $extra_sql;
my $hashref;
my $link = $cgi->param('link'); # cust_main or prospect_main

push @select,'agentnum';

# this shouldn't happen without funny-busines
die "Invalid \$link type ($link)"
  unless $link eq 'cust_main' || $link eq 'prospect_main';

# Build @select and $addl_from
for my $key ('common', $link) {
  $addl_from .= $colmap{$key}->{joinsql};
  my $cols = $colmap{$key}->{cols};
  for my $tbl (keys %{$cols}) {
    push @select, map{ "$tbl.$_ AS ${tbl}_$_" } @{$cols->{$tbl}};
  }
}

# Filter for Contact Type
if (@classnum || $classnum_null) {
  my @stm;
  my $tbl = $link eq 'cust_main' ? 'cust_contact' : 'prospect_contact';
  push @stm, "${tbl}.classnum IN (".join(',',@classnum).')' if @classnum;
  push @stm, "${tbl}.classnum IS NULL" if $classnum_null;
  $extra_sql .= " (" . join(' OR ',@stm) . ') ';
}

# Filter for destination
if (@dest && $link eq 'cust_main') {
  my @stm;
  push @stm, "cust_contact.${_}_dest IS NOT NULL" for @dest;
  $extra_sql .= "\nAND (".join(' AND ',@stm).') ';
}

if ($DEBUG) {
  print "<pre>\n";
  print "select \n";
  print join ",\n",@select;
  print "\n";
  print "from $link \n";
  print "$addl_from\n";
  print "WHERE \n $extra_sql\n";
  print "</pre>\n";
}

# Prepare to display phone numbers
# adds 3 additional queries per table record :-(
my $get_phone_sub = sub {
  my $type = shift;
  return sub {
    my $rec = shift;
    my @p = qsearch('contact_phone', {
      contactnum => $rec->contact_contactnum,
      phonetypenum => $type,
    });
    @p ? (join ', ',map{$_->phonenum_pretty} @p) : undef;
  };
};

my @phones;
foreach my $phone_type ( FS::phone_type->get_phone_types() ) {
  push @phones, { label => $phone_type->typename.' Phone', field => $get_phone_sub->($phone_type->phonetypenum), };
}

# Cache contact types
my %classname =
  map {$_->classnum => $_->classname}
  qsearch(contact_class => {disabled => ''});

# And now for something completly different:
my @report = (
  { label => 'First',  field => 'contact_first' },
  { label => 'Last',   field => 'contact_last'  },
  { label => 'Title',  field => 'contact_title' },
  { label => 'E-Mail', field => 'contact_email_emailaddress' },
  @phones,
  { label => 'Type',
    field => sub {
      my $rec = shift;
      if ($rec->cust_contact_custnum) {
        return $rec->cust_contact_classnum
               ? $classname{$rec->cust_contact_classnum}
               : undef;
      } else {
        return $rec->prospect_contact_classnum
               ? $classname{$rec->prospect_contact_classnum}
               : undef;
      }
  }},
  { label => 'Send Invoices',
    field => sub {
      my $rec = shift;
      return 'N/A' if $rec->prospect_contact_prospectnum;
      $rec->cust_contact_invoice_dest ? 'Y' : 'N';
    }},
  { label => 'Send Messages',
    field => sub {
      my $rec = shift;
      return 'N/A' if $rec->prospect_contact_prospectnum;
      $rec->cust_contact_message_dest ? 'Y' : 'N';
    }},
  { label => 'Customer',
    link => [
      "${fsurl}view/",
      sub {
        my $row = shift;
        $row->cust_contact_custnum
        ? 'cust_main.cgi?'.$row->cust_contact_custnum
        : 'prospect_main.html?'.$row->prospect_contact_prospectnum
      }
    ],
    field => sub {
      my $rec = shift;
      if ($rec->prospect_contact_prospectnum) {
        return encode_entities(
          $rec->contact_company
          || $rec->contact_last.' '.$rec->contact_first
        );
      }
      encode_entities(
        $rec->cust_main_company
        || $rec->cust_main_last.' '.$rec->cust_main_first
      );
    }},
  { label => 'Self-service',
    field => sub {
      my $rec = shift;
      return 'N/A' if $rec->prospect_contact_prospectnum;
      $rec->cust_contact_selfservice_access ? 'Y' : 'N';
    }},
  { label => 'Comment',
    field => sub {
      my $rec = shift;
      encode_entities(
        $rec->prospect_contact_prospectnum
        ? $rec->prospect_contact_comment
        : $rec->cust_contact_comment
      );
    }},
);

my (@header, @fields, @links);
for my $col (@report) {
  push @header, emt($col->{label});
  push @fields, $col->{field};
  push @links, ($col->{link} || "");
}

my $classnum_url_part;
if (@classnum) {
  $classnum_url_part = join '', map{ "&classnums=$_" } @classnum;
  $classnum_url_part .= '&classnums=0' if $classnum_null;
}

my $dest_url_part;
if (@dest) {
  $dest_url_part = join '', map{ "&dest=$_" } @dest;
}

# E-mail pipeline, from email-customers.html through to email queue job,
# doesn't support cust_prospect table
my $send_email_link = undef;
if ($link eq 'cust_main') {
  $send_email_link =
    "<a href=\"${fsurl}misc/email-customers.html?".
      'table=cust_main'.
      '&agentnum='.$cgi->param('agentnum').
      '&POST=on'.
      '&all_pkg_classnums=0'.
      '&all_tags=0'.
      '&any_pkg_status=0'.
      '&refnum=1'.
      '&with_email=on'.
      $classnum_url_part.
      $dest_url_part.
    "\">Email a notice to these customers</a>";
}

</%init>