add "multiplier of monthly" info to "paid time" column in svc_acct-display_paid_time_...
[freeside.git] / httemplate / search / svc_acct.cgi
1 <% include( 'elements/search.html',
2                  'title'       => 'Account Search Results',
3                  'name'        => 'accounts',
4                  'query'       => $sql_query,
5                  'count_query' => $count_query,
6                  'redirect'    => $link,
7                  'header'      => \@header,
8                  'fields'      => \@fields,
9                  'links'       => \@links,
10                  'align'       => $align,
11                  'color'       => \@color,
12                  'style'       => \@style,
13              )
14 %>
15 <%once>
16
17 #false laziness w/ClientAPI/MyAccount.pm
18 sub format_time { 
19   my $support = shift;
20   (($support < 0) ? '-' : '' ). int(abs($support)/3600)."h".sprintf("%02d",(abs($support)%3600)/60)."m";
21 }
22
23 </%once>
24 <%init>
25
26 die "access denied"
27   unless $FS::CurrentUser::CurrentUser->access_right('List services');
28
29 my $link      = [ "${p}view/svc_acct.cgi?",   'svcnum'  ];
30 my $link_cust = sub {
31   my $svc_acct = shift;
32   if ( $svc_acct->custnum ) {
33     [ "${p}view/cust_main.cgi?", 'custnum' ];
34   } else {
35     '';
36   }
37 };
38
39 my @extra_sql = ();
40
41 my @header = ( '#', 'Service', 'Account', 'UID' );
42 my @fields = ( 'svcnum', 'svc', 'email', 'uid' );
43 my @links = ( $link, $link, $link, $link );
44 my $align = 'rlll';
45 my @color = ( '', '', '', '' );
46 my @style = ( '', '', '', '' );
47
48 if ( $cgi->param('domain') ) { 
49   my $svc_domain =
50     qsearchs('svc_domain', { 'domain' => $cgi->param('domain') } );
51   unless ( $svc_domain ) {
52     #it would be nice if this looked more like the other "not found"
53     #errors, but this will do for now.
54     errorpage("Domain ". $cgi->param('domain'). " not found at all");
55   } else {
56     push @extra_sql, 'domsvc = '. $svc_domain->svcnum;
57   }
58 }
59
60 my $orderby = 'ORDER BY svcnum';
61 if ( $cgi->param('magic') =~ /^(all|unlinked)$/ ) {
62
63   push @extra_sql, 'pkgnum IS NULL'
64     if $cgi->param('magic') eq 'unlinked';
65
66   my $sortby = '';
67   if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
68     $sortby = $1;
69     $sortby = "LOWER($sortby)"
70       if $sortby eq 'username';
71     push @extra_sql, "$sortby IS NOT NULL"
72       if $sortby eq 'uid' || $sortby eq 'seconds';
73     $orderby = "ORDER BY $sortby";
74   }
75
76   if ( $sortby eq 'seconds' ) {
77     #push @header, 'Time remaining';
78     push @header, 'Time';
79     push @fields, sub { my $svc_acct = shift; format_time($svc_acct->seconds) };
80     push @links, '';
81     $align .= 'r';
82     push @color, '';
83     push @style, '';
84
85     my $conf = new FS::Conf;
86     if ( $conf->exists('svc_acct-display_paid_time_remaining') ) {
87       push @header, 'Paid time';
88       push @fields, sub {
89         my $svc_acct = shift;
90         my $seconds = $svc_acct->seconds;
91         my $cust_pkg = $svc_acct->cust_svc->cust_pkg;
92         my $part_pkg = $cust_pkg->part_pkg;
93         my $timepermonth = $part_pkg->option('seconds');
94         $timepermonth = $timepermonth / $part_pkg->freq
95           if $part_pkg->freq =~ /^\d+$/ && $part_pkg->freq != 0;
96         return format_time($seconds) unless $timepermonth;
97         #my $recur = $part_pkg->calc_recur($cust_pkg);
98         my $recur = $part_pkg->base_recur($cust_pkg);
99         my $balance = $cust_pkg->cust_main->balance;
100         my $months_unpaid = $balance / $recur;
101         my $time_unpaid = $months_unpaid * $timepermonth;
102         format_time($seconds-$time_unpaid).
103           sprintf(' (%.2fx monthly)', ( $seconds-$time_unpaid ) / $timepermonth );
104       };
105       push @links, '';
106       $align .= 'r';
107       push @color, '';
108       push @style, '';
109     }
110
111   }
112
113 } elsif ( $cgi->param('popnum') =~ /^(\d+)$/ ) {
114   push @extra_sql, "popnum = $1";
115   $orderby = "ORDER BY LOWER(username)";
116 } elsif ( $cgi->param('svcpart') =~ /^(\d+)$/ ) {
117   push @extra_sql, "svcpart = $1";
118   $orderby = "ORDER BY uid";
119   #$orderby = "ORDER BY svcnum";
120 } else {
121   $orderby = "ORDER BY uid";
122
123   my @username_sql;
124
125   my %username_type;
126   foreach ( $cgi->param('username_type') ) {
127     $username_type{$_}++;
128   }
129
130   $cgi->param('username') =~ /^([\w\-\.\&]+)$/; #untaint username_text
131   my $username = $1;
132
133   push @username_sql, "username ILIKE '$username'"
134     if $username_type{'Exact'}
135     || $username_type{'Fuzzy'};
136
137   push @username_sql, "username ILIKE '\%$username\%'"
138     if $username_type{'Substring'}
139     || $username_type{'All'};
140
141   if ( $username_type{'Fuzzy'} || $username_type{'All'} ) {
142     &FS::svc_acct::check_and_rebuild_fuzzyfiles;
143     my $all_username = &FS::svc_acct::all_username;
144
145     my %username;
146     if ( $username_type{'Fuzzy'} || $username_type{'All'} ) { 
147       foreach ( amatch($username, [ qw(i) ], @$all_username) ) {
148         $username{$_}++; 
149       }
150     }
151
152     #if ($username_type{'Sound-alike'}) {
153     #}
154
155     push @username_sql, "username = '$_'"
156       foreach (keys %username);
157
158   }
159
160   push @extra_sql, '( '. join( ' OR ', @username_sql). ' )';
161
162 }
163
164 push @header, FS::UI::Web::cust_header();
165 push @fields, \&FS::UI::Web::cust_fields,
166 push @links, map { $_ ne 'Cust. Status' ? $link_cust : '' }
167                  FS::UI::Web::cust_header();
168 $align .= FS::UI::Web::cust_aligns();
169 push @color, FS::UI::Web::cust_colors();
170 push @style, FS::UI::Web::cust_styles();
171
172 my $addl_from = ' LEFT JOIN cust_svc  USING ( svcnum  ) '.
173                 ' LEFT JOIN part_svc  USING ( svcpart ) '.
174                 ' LEFT JOIN cust_pkg  USING ( pkgnum  ) '.
175                 ' LEFT JOIN cust_main USING ( custnum ) ';
176
177 #here is the agent virtualization
178 push @extra_sql, $FS::CurrentUser::CurrentUser->agentnums_sql( 
179                    'null_right' => 'View/link unlinked services'
180                  );
181
182 my $extra_sql = 
183   scalar(@extra_sql)
184     ? ' WHERE '. join(' AND ', @extra_sql )
185     : '';
186
187 my $count_query = "SELECT COUNT(*) FROM svc_acct $addl_from $extra_sql";
188 #if ( keys %svc_acct ) {
189 #  $count_query .= ' WHERE '.
190 #                    join(' AND ', map "$_ = ". dbh->quote($svc_acct{$_}),
191 #                                      keys %svc_acct
192 #                        );
193 #}
194
195 my $sql_query = {
196   'table' => 'svc_acct',
197   'hashref'   => {}, # \%svc_acct,
198   'select'    => join(', ',
199                     'svc_acct.*',
200                     'part_svc.svc',
201                     'cust_main.custnum',
202                     FS::UI::Web::cust_sql_fields(),
203                   ),
204   'extra_sql' => "$extra_sql $orderby",
205   'addl_from' => $addl_from,
206 };
207
208 </%init>