summaryrefslogtreecommitdiff
path: root/httemplate/search/svc_acct.cgi
blob: 7f423d521e6a7ed0581e2979df6d48dd036807a4 (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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
<& elements/svc_Common.html,
                 'title'       => emt('Account Search Results'),
                 'name'        => emt('accounts'),
                 'query'       => $sql_query,
                 'count_query' => $count_query,
                 'redirect'    => $link,
                 'header'      => \@header,
                 'fields'      => \@fields,
                 'links'       => \@links,
                 'align'       => $align,
                 'color'       => \@color,
                 'style'       => \@style,
                 'footer'      => \@footer,
                 'html_init'   => include('/elements/email-link.html',
                                    'search_hash' => \%search_hash,
                                    'table'       => 'svc_acct'
                                  ),
&>
<%once>

#false laziness w/ClientAPI/MyAccount.pm
sub format_time { 
  my $support = shift;
  (($support < 0) ? '-' : '' ). int(abs($support)/3600)."h".sprintf("%02d",(abs($support)%3600)/60)."m";
}

sub timelast {
  my( $svc_acct, $last, $permonth ) = @_;

  my $sql = "
    SELECT SUM(support) FROM acct_rt_transaction
      LEFT JOIN Transactions
        ON Transactions.Id = acct_rt_transaction.transaction_id
    WHERE svcnum = ? 
      AND Transactions.Created >= ?
  ";

  my $sth = dbh->prepare($sql) or die dbh->errstr;
  $sth->execute( $svc_acct->svcnum,
                 time2str('%Y-%m-%d %X', time - $last*86400 ) 
               )
    or die $sth->errstr;

  my $seconds = $sth->fetchrow_arrayref->[0];

  #my $return = (($seconds < 0) ? '-' : '') . concise(duration($seconds));
  my $return = (($seconds < 0) ? '-' : '') . format_time($seconds);

  $return .= sprintf(' (%.2fx)', $seconds / $permonth ) if $permonth;

  $return;

}

</%once>
<%init>

my $curuser =  $FS::CurrentUser::CurrentUser;

die "access denied" unless $curuser->access_right('List services');

my %cust_pkg_cache;

my $link      = [ "${p}view/svc_acct.cgi?",   'svcnum'  ];
my $link_cust = sub {
  my $svc_acct = shift;
  if ( $svc_acct->custnum ) {
    [ "${p}view/cust_main.cgi?", 'custnum' ];
  } else {
    '';
  }
};

my %search_hash = ();
my @extra_sql = ();

my @header = ( 'Service', 'Account' );
my @fields = ( 'svc', 'email' );
my @links = ( $link, $link );
my $align = 'll';
my @color = ( '', '' );
my @style = ( '', '' );
my @footer = ();

my $conf = new FS::Conf;

if ( $conf->exists('report-showpasswords') #its a terrible idea
     && $curuser->access_right('List service passwords') #but if you insist...
   )
{
  push @header, emt('Password');
  push @fields, 'get_cleartext_password';
  push @links, $link;
  $align .= 'l';
  push @color, '';
  push @style, '';
}

push @header, emt('Real Name');
push @fields, 'finger';
push @links, $link;
$align .= 'l';
push @color, '';
push @style, '';

#hide the UID, its much less useful these days
if ( $cgi->param('show_uid') ) { #XXX add a checkbox
  push @header, emt('UID');
  push @fields, 'uid';
  push @links, $link;
  $align .= 'l';
  push @color, '';
  push @style, '';
}

push @header, emt('Last Login');
push @fields, 'last_login_text';
push @links, $link;
$align .= 'r';
push @color, '';
push @style, '';


for (qw( domain domsvc agentnum custnum popnum svcpart cust_fields )) {
  $search_hash{$_} = $cgi->param($_) if length($cgi->param($_));
}

for (qw( towernum sectornum )) {
  $search_hash{$_} = [ $cgi->param($_) ] if $cgi->param($_);
}

my $timepermonth = '';

my $orderby = 'ORDER BY svcnum';
my $addl_from = '';
if ( $cgi->param('magic') =~ /^(all|unlinked)$/ ) {

  $search_hash{'unlinked'} = 1
    if $cgi->param('magic') eq 'unlinked';

  my $sortby = '';
  if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
    $sortby = $1;
    $sortby = "LOWER($sortby)"
      if $sortby eq 'username';
    push @extra_sql, "$sortby IS NOT NULL" #XXX search_hash
      if $sortby eq 'uid' || $sortby eq 'seconds' || $sortby eq 'last_login';
    $orderby = "ORDER BY $sortby";
  }

  if ( $sortby eq 'seconds' ) {
    my $tot_time = 0;
    push @header, emt('Time');

    if ( $conf->exists('svc_acct-display_paid_time_remaining') ) {
      push @fields, sub { my $svc_acct = shift;
                          my $seconds = $svc_acct->seconds;
                          my $cust_pkg = $svc_acct->cust_svc->cust_pkg;
                          my $part_pkg = $cust_pkg->part_pkg;

                          $tot_time += $svc_acct->seconds;

                          $timepermonth = $part_pkg->option('seconds');
                          $timepermonth = $timepermonth / $part_pkg->freq
                            if $part_pkg->freq =~ /^\d+$/ && $part_pkg->freq != 0;
                          my $recur = $part_pkg->base_recur($cust_pkg);

                          return format_time($seconds)
                            unless $timepermonth && $recur;

                          format_time($seconds).
                            sprintf(' (%.2fx monthly)', $seconds / $timepermonth );

                        };
    } else {
      push @fields, sub { my $svc_acct = shift;
                          $tot_time += $svc_acct->seconds;
                          format_time($svc_acct->seconds);
                        };
    }

    push @links, '';
    $align .= 'r';
    push @color, '';
    push @style, '';

    @footer = ( 'Total', '', '', '',
                sub { format_time($tot_time) }, #time
              );

    if ( $conf->exists('svc_acct-display_paid_time_remaining') ) {
      my $tot_paid_time = 0;
      my %tot = ( '30'=>0, '60'=>0, '90'=>0 );
      push @header, emt('Paid time'), emt('Last 30'), emt('Last 60'), emt('Last 90');
      push @fields,
        sub {
          my $svc_acct = shift;
          my $seconds = $svc_acct->seconds;
          my $cust_pkg = $svc_acct->cust_svc->cust_pkg;
          my $part_pkg = $cust_pkg->part_pkg;

          #my $timepermonth = $part_pkg->option('seconds');
          $timepermonth = $part_pkg->option('seconds');
          $timepermonth = $timepermonth / $part_pkg->freq
            if $part_pkg->freq =~ /^\d+$/ && $part_pkg->freq != 0;

          #my $recur = $part_pkg->calc_recur($cust_pkg);
          my $recur = $part_pkg->base_recur($cust_pkg);

          return format_time($seconds) unless $timepermonth && $recur;

          my $balance = $cust_pkg->cust_main->balance;
          my $periods_unpaid = $balance / $recur;
          my $time_unpaid = $periods_unpaid * $timepermonth;
          $time_unpaid *= $part_pkg->freq
            if $part_pkg->freq =~ /^\d+$/ && $part_pkg->freq != 0;
          $tot_paid_time += $seconds-$time_unpaid;
          format_time($seconds-$time_unpaid).
            sprintf(' (%.2fx monthly)', ( $seconds-$time_unpaid ) / $timepermonth );
        },
        sub { timelast( shift, 30, $timepermonth ); },
        sub { timelast( shift, 60, $timepermonth ); },
        sub { timelast( shift, 90, $timepermonth ); },
      ;
      push @links, '', '', '', '';
      $align .= 'rrrr';
      push @color, '', '', '', '';
      push @style, '', '', '', '';
      push @footer, 
        sub { format_time($tot_paid_time) }, #paid time
        '', #XXX sub { $tot{'30'} }, #30
        '', #XXX sub { $tot{'60'} }, #60
        '', #XXX sub { $tot{'90'} }, #90
      ;
    }

    push @footer, '', '';

  }

} elsif ( $cgi->param('magic') =~ /^nologin$/ ) {

  if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
    my $sortby = $1;
    $sortby = "LOWER($sortby)"
      if $sortby eq 'username';
    push @extra_sql, "last_login IS NULL";
    $orderby = "ORDER BY $sortby";
  }

} elsif ( $cgi->param('magic') =~ /^advanced$/ ) {

  $orderby = "";

  $search_hash{'pkgpart'} = [ $cgi->param('pkgpart') ];

  $search_hash{'svcpart'} = [ $cgi->param('svcpart') ];

  foreach my $field (qw( last_login last_logout )) {

    my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, $field);

    next if $beginning == 0 && $ending == 4294967295;

    if ($cgi->param($field."_invert")) {
      push @extra_sql,
        "(svc_acct.$field IS NULL OR ".
        "svc_acct.$field < $beginning AND ".
        "svc_acct.$field > $ending)";
    } else {
      push @extra_sql,
        "svc_acct.$field IS NOT NULL",
        "svc_acct.$field >= $beginning",
        "svc_acct.$field <= $ending";
    }
  
    $orderby ||= "ORDER BY svc_acct.$field" .
      ($cgi->param($field."_invert") ? ' DESC' : '');

  }

  $orderby ||= "ORDER BY svcnum";

} elsif ( $cgi->param('popnum') ) {
  $orderby = "ORDER BY LOWER(username)";
} elsif ( $cgi->param('svcpart') ) {
  $orderby = "ORDER BY uid";
  #$orderby = "ORDER BY svcnum";
  if ( defined($cgi->param('cancelled')) ) {
    $search_hash{'cancelled'} = $cgi->param('cancelled') ? 1 : 0;
  }
} else {
  $orderby = "ORDER BY uid";

  my @username_sql;

  my %username_type;
  foreach ( $cgi->param('username_type') ) {
    $username_type{$_}++;
  }

  $cgi->param('username') =~ /^([\w\-\.\&]+)$/; #untaint username_text
  my $username = lc($1);

  push @username_sql, "LOWER(username) LIKE '$username'"
    if $username_type{'Exact'}
    || $username_type{'Fuzzy'};

  push @username_sql, "LOWER(username) LIKE '\%$username\%'"
    if $username_type{'Substring'}
    || $username_type{'All'};

  if ( $username_type{'Fuzzy'} || $username_type{'All'} ) {
    &FS::svc_acct::check_and_rebuild_fuzzyfiles;
    my $all_username = &FS::svc_acct::all_username;

    my %username;
    if ( $username_type{'Fuzzy'} || $username_type{'All'} ) { 
      foreach ( amatch($username, [ qw(i) ], @$all_username) ) {
        $username{$_}++; 
      }
    }

    #if ($username_type{'Sound-alike'}) {
    #}

    push @username_sql, "username = '$_'"
      foreach (keys %username);

  }

  push @extra_sql, '( '. join( ' OR ', @username_sql). ' )';

}

my $date_format = $conf->config('date_format') || '%m/%d/%Y';

$cgi->param('cust_pkg_fields') =~ /^([\w\,]*)$/ or die "bad cust_pkg_fields";
my @pkg_fields = split(',', $1);
foreach my $pkg_field ( @pkg_fields ) {
  ( my $header = ucfirst($pkg_field) ) =~ s/_/ /; #:/
  push @header, $header;

  #not the most efficient to do it every field, but this is of niche use. so far
  push @fields, sub { my $svc_acct = shift;
                      my $cust_pkg = $svc_acct->cust_svc->cust_pkg or return '';
                      my $value = $cust_pkg->get($pkg_field);#closures help alot
                      $value ? time2str('%b %d %Y', $value ) : '';
                    };

  push @links, '';
  $align .= 'c';
  push @color, '';
  push @style, '';
  
}

push @header, emt('Pkg. Status');
push @fields, sub {
  $cust_pkg_cache{$_[0]->svcnum} ||= $_[0]->cust_svc->cust_pkg;
  return '' unless $cust_pkg_cache{$_[0]->svcnum};
  $cust_pkg_cache{$_[0]->svcnum}->ucfirst_status;
};
push @links, '';
$align .= 'r';
push @color, sub {
  $cust_pkg_cache{$_[0]->svcnum} ||= $_[0]->cust_svc->cust_pkg;
  return '' unless $cust_pkg_cache{$_[0]->svcnum};
  my $c = FS::cust_pkg::statuscolors;
  $c->{$cust_pkg_cache{$_[0]->svcnum}->status };
};
push @style, 'b';

push @header, FS::UI::Web::cust_header($cgi->param('cust_fields'));
push @fields, \&FS::UI::Web::cust_fields,
push @links, map { $_ ne 'Cust. Status' ? $link_cust : '' }
                 FS::UI::Web::cust_header($cgi->param('cust_fields'));
$align .= FS::UI::Web::cust_aligns();
push @color, FS::UI::Web::cust_colors();
push @style, FS::UI::Web::cust_styles();

$search_hash{'order_by'} = $orderby;
$search_hash{'where'} = \@extra_sql;
$search_hash{'addl_from'} = $addl_from;

my $sql_query = FS::svc_acct->search(\%search_hash);
my $count_query = delete($sql_query->{'count_query'});

</%init>