- print "</TR>";
-
- my(%saw,$svc_acct);
- my $p = popurl(2);
- foreach $svc_acct (
- sort $sortby grep(!$saw{$_->svcnum}++, @svc_acct)
- ) {
- my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $svc_acct->svcnum })
- or die "No cust_svc record for svcnum ". $svc_acct->svcnum;
- my $part_svc = qsearchs('part_svc', { 'svcpart' => $cust_svc->svcpart })
- or die "No part_svc record for svcpart ". $cust_svc->svcpart;
-
- my $domain;
- my $svc_domain = qsearchs('svc_domain', { 'svcnum' => $svc_acct->domsvc });
- if ( $svc_domain ) {
- $domain = "<A HREF=\"${p}view/svc_domain.cgi?". $svc_domain->svcnum.
- "\">". $svc_domain->domain. "</A>";
- } else {
- unless ( $mydomain ) {
- my $conf = new FS::Conf;
- unless ( $mydomain = $conf->config('domain') ) {
- die "No legacy domain config file and no svc_domain.svcnum record ".
- "for svc_acct.domsvc: ". $svc_acct->domsvc;
- }
- }
- $domain = "<i>$mydomain</i><FONT COLOR=\"#FF0000\">*</FONT>";
- }
- my($cust_pkg,$cust_main);
- if ( $cust_svc->pkgnum ) {
- $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $cust_svc->pkgnum })
- or die "No cust_pkg record for pkgnum ". $cust_svc->pkgnum;
- $cust_main = qsearchs('cust_main', { 'custnum' => $cust_pkg->custnum })
- or die "No cust_main record for custnum ". $cust_pkg->custnum;
- }
- my($svcnum, $username, $uid, $svc, $custnum, $last, $first, $company) = (
- $svc_acct->svcnum,
- $svc_acct->getfield('username'),
- $svc_acct->getfield('uid'),
- $part_svc->svc,
- $cust_svc->pkgnum ? $cust_main->custnum : '',
- $cust_svc->pkgnum ? $cust_main->getfield('last') : '',
- $cust_svc->pkgnum ? $cust_main->getfield('first') : '',
- $cust_svc->pkgnum ? $cust_main->company : '',
- );
- my($pcustnum) = $custnum
- ? "<A HREF=\"${p}view/cust_main.cgi?$custnum\"><FONT SIZE=-1>$custnum</FONT></A>"
- : "<I>(unlinked)</I>"
- ;
- my $pname = $custnum ? "<A HREF=\"${p}view/cust_main.cgi?$custnum\">$last, $first</A>" : '';
- my $pcompany = $custnum ? "<A HREF=\"${p}view/cust_main.cgi?$custnum\">$company</A>" : '';
- my($pship_name, $pship_company);
- if ( defined dbdef->table('cust_main')->column('ship_last') ) {
- my($ship_last, $ship_first, $ship_company) = (
- $cust_svc->pkgnum ? ( $cust_main->ship_last || $last ) : '',
- $cust_svc->pkgnum ? ( $cust_main->ship_last
- ? $cust_main->ship_first
- : $first
- ) : '',
- $cust_svc->pkgnum ? ( $cust_main->ship_last
- ? $cust_main->ship_company
- : $company
- ) : '',
- );
- $pship_name = $custnum ? "<A HREF=\"${p}view/cust_main.cgi?$custnum\">$ship_last, $ship_first</A>" : '';
- $pship_company = $custnum ? "<A HREF=\"${p}view/cust_main.cgi?$custnum\">$ship_company</A>" : '';
- }
- print <<END;
- <TR>
- <TD><A HREF="${p}view/svc_acct.cgi?$svcnum"><FONT SIZE=-1>$svcnum</FONT></A></TD>
- <TD><A HREF="${p}view/svc_acct.cgi?$svcnum"><FONT SIZE=-1>$username</FONT></A></TD>
- <TD><FONT SIZE=-1>$domain</FONT></TD>
- <TD><A HREF="${p}view/svc_acct.cgi?$svcnum"><FONT SIZE=-1>$uid</FONT></A></TD>
- <TD><FONT SIZE=-1>$svc</FONT></TH>
- <TD><FONT SIZE=-1>$pcustnum</FONT></TH>
- <TD><FONT SIZE=-1>$pname<FONT></TH>
- <TD><FONT SIZE=-1>$pcompany</FONT></TH>
-END
- if ( defined dbdef->table('cust_main')->column('ship_last') ) {
- print <<END;
- <TD><FONT SIZE=-1>$pship_name<FONT></TH>
- <TD><FONT SIZE=-1>$pship_company</FONT></TH>
-END
+
+ if ( $sortby eq 'seconds' ) {
+ my $tot_time = 0;
+ push @header, emt('Time');
+ 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
+ ;