Remove remaining calls to idiot/eidiot. Should fix 1479
[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         return format_time($seconds) unless $timepermonth;
95         #my $recur = $part_pkg->calc_recur($cust_pkg);
96         my $recur = $part_pkg->base_recur($cust_pkg);
97         my $balance = $cust_pkg->cust_main->balance;
98         my $months_unpaid = $balance / $recur;
99         my $time_unpaid = $months_unpaid * $timepermonth;
100         format_time($seconds-$time_unpaid);
101       };
102       push @links, '';
103       $align .= 'r';
104       push @color, '';
105       push @style, '';
106     }
107
108   }
109
110 } elsif ( $cgi->param('popnum') =~ /^(\d+)$/ ) {
111   push @extra_sql, "popnum = $1";
112   $orderby = "ORDER BY LOWER(username)";
113 } elsif ( $cgi->param('svcpart') =~ /^(\d+)$/ ) {
114   push @extra_sql, "svcpart = $1";
115   $orderby = "ORDER BY uid";
116   #$orderby = "ORDER BY svcnum";
117 } else {
118   $orderby = "ORDER BY uid";
119
120   my @username_sql;
121
122   my %username_type;
123   foreach ( $cgi->param('username_type') ) {
124     $username_type{$_}++;
125   }
126
127   $cgi->param('username') =~ /^([\w\-\.\&]+)$/; #untaint username_text
128   my $username = $1;
129
130   push @username_sql, "username ILIKE '$username'"
131     if $username_type{'Exact'}
132     || $username_type{'Fuzzy'};
133
134   push @username_sql, "username ILIKE '\%$username\%'"
135     if $username_type{'Substring'}
136     || $username_type{'All'};
137
138   if ( $username_type{'Fuzzy'} || $username_type{'All'} ) {
139     &FS::svc_acct::check_and_rebuild_fuzzyfiles;
140     my $all_username = &FS::svc_acct::all_username;
141
142     my %username;
143     if ( $username_type{'Fuzzy'} || $username_type{'All'} ) { 
144       foreach ( amatch($username, [ qw(i) ], @$all_username) ) {
145         $username{$_}++; 
146       }
147     }
148
149     #if ($username_type{'Sound-alike'}) {
150     #}
151
152     push @username_sql, "username = '$_'"
153       foreach (keys %username);
154
155   }
156
157   push @extra_sql, '( '. join( ' OR ', @username_sql). ' )';
158
159 }
160
161 push @header, FS::UI::Web::cust_header();
162 push @fields, \&FS::UI::Web::cust_fields,
163 push @links, map { $_ ne 'Cust. Status' ? $link_cust : '' }
164                  FS::UI::Web::cust_header();
165 $align .= FS::UI::Web::cust_aligns();
166 push @color, FS::UI::Web::cust_colors();
167 push @style, FS::UI::Web::cust_styles();
168
169 my $addl_from = ' LEFT JOIN cust_svc  USING ( svcnum  ) '.
170                 ' LEFT JOIN part_svc  USING ( svcpart ) '.
171                 ' LEFT JOIN cust_pkg  USING ( pkgnum  ) '.
172                 ' LEFT JOIN cust_main USING ( custnum ) ';
173
174 #here is the agent virtualization
175 push @extra_sql, $FS::CurrentUser::CurrentUser->agentnums_sql( 
176                    'null_right' => 'View/link unlinked services'
177                  );
178
179 my $extra_sql = 
180   scalar(@extra_sql)
181     ? ' WHERE '. join(' AND ', @extra_sql )
182     : '';
183
184 my $count_query = "SELECT COUNT(*) FROM svc_acct $addl_from $extra_sql";
185 #if ( keys %svc_acct ) {
186 #  $count_query .= ' WHERE '.
187 #                    join(' AND ', map "$_ = ". dbh->quote($svc_acct{$_}),
188 #                                      keys %svc_acct
189 #                        );
190 #}
191
192 my $sql_query = {
193   'table' => 'svc_acct',
194   'hashref'   => {}, # \%svc_acct,
195   'select'    => join(', ',
196                     'svc_acct.*',
197                     'part_svc.svc',
198                     'cust_main.custnum',
199                     FS::UI::Web::cust_sql_fields(),
200                   ),
201   'extra_sql' => "$extra_sql $orderby",
202   'addl_from' => $addl_from,
203 };
204
205 </%init>