From 230e099f92541bc3bc0e2a08e81932ee17909fa0 Mon Sep 17 00:00:00 2001 From: Mark Wells Date: Fri, 15 May 2015 10:22:50 -0700 Subject: [PATCH] view svc_pbx usage in selfservice, #34836 --- FS/FS/ClientAPI.pm | 2 +- FS/FS/ClientAPI/MyAccount.pm | 42 +++++++++++------------ FS/FS/svc_pbx.pm | 25 ++++++++++++++ fs_selfservice/FS-SelfService/cgi/selfservice.cgi | 2 +- fs_selfservice/FS-SelfService/cgi/view_usage.html | 24 +++++++------ 5 files changed, 62 insertions(+), 33 deletions(-) diff --git a/FS/FS/ClientAPI.pm b/FS/FS/ClientAPI.pm index 1fea28c67..e4031b26e 100644 --- a/FS/FS/ClientAPI.pm +++ b/FS/FS/ClientAPI.pm @@ -6,7 +6,7 @@ use vars qw( @EXPORT_OK %handler $domain $DEBUG $me ); @EXPORT_OK = qw( load_clientapi_modules ); -$DEBUG = 0; +$DEBUG = 1; $me = '[FS::ClientAPI]'; %handler = (); diff --git a/FS/FS/ClientAPI/MyAccount.pm b/FS/FS/ClientAPI/MyAccount.pm index e2f859527..c89b7bb3f 100644 --- a/FS/FS/ClientAPI/MyAccount.pm +++ b/FS/FS/ClientAPI/MyAccount.pm @@ -1857,18 +1857,20 @@ sub list_svcs { } # no usage to hide here - } elsif ( $svcdb eq 'svc_phone' ) { + } elsif ( $svcdb eq 'svc_phone' or $svcdb eq 'svc_pbx' ) { if (!$hide_usage) { # could potentially show lots of things... $hash{'outbound'} = 1; $hash{'inbound'} = 0; - if ( $part_pkg->plan eq 'voip_inbound' ) { - $hash{'outbound'} = 0; - $hash{'inbound'} = 1; - } elsif ( $part_pkg->option('selfservice_inbound_format') - or $conf->config('selfservice-default_inbound_cdr_format') - ) { - $hash{'inbound'} = 1; + if ( $svcdb eq 'svc_phone' ) { + if ( $part_pkg->plan eq 'voip_inbound' ) { + $hash{'outbound'} = 0; + $hash{'inbound'} = 1; + } elsif ( $part_pkg->option('selfservice_inbound_format') + or $conf->config('selfservice-default_inbound_cdr_format') + ) { + $hash{'inbound'} = 1; + } } foreach (qw(inbound outbound)) { # hmm...we can't filter by status here, because there might @@ -2163,11 +2165,11 @@ sub _list_cdr_usage { # XXX CDR type support... # XXX any way to do a paged search on this? # we have to return the results all at once... - my($svc_phone, $begin, $end, %opt) = @_; + my($svc_x, $begin, $end, %opt) = @_; map [ $_->downstream_csv(%opt, 'keeparray' => 1) ], - $svc_phone->get_cdrs( - 'begin'=>$begin, - 'end'=>$end, + $svc_x->get_cdrs( + 'begin' => $begin, + 'end' => $end, 'disable_charged_party' => 1, %opt ); @@ -2175,9 +2177,7 @@ sub _list_cdr_usage { sub list_cdr_usage { my $p = shift; - _usage_details( \&_list_cdr_usage, $p, - 'svcdb' => 'svc_phone', - ); + _usage_details( \&_list_cdr_usage, $p ); } sub _usage_details { @@ -2194,17 +2194,17 @@ sub _usage_details { my $search = { 'svcnum' => $p->{'svcnum'} }; $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent'; - my $svcdb = $opt{'svcdb'} || 'svc_acct'; - - my $svc_x = qsearchs( $svcdb, $search ); + my $cust_svc = qsearchs( 'cust_svc', $search ); return { 'error' => 'No service selected in list_svc_usage' } - unless $svc_x; + unless $cust_svc; - my $cust_pkg = $svc_x->cust_svc->cust_pkg; + my $svc_x = $cust_svc->svc_x; + my $svcdb = $svc_x->table; + my $cust_pkg = $cust_svc->cust_pkg; my $freq = $cust_pkg->part_pkg->freq; my %callback_opt; my $header = []; - if ( $svcdb eq 'svc_phone' ) { + if ( $svcdb eq 'svc_phone' or $svcdb eq 'svc_pbx' ) { my $format = ''; if ( $p->{inbound} ) { $format = $cust_pkg->part_pkg->option('selfservice_inbound_format') diff --git a/FS/FS/svc_pbx.pm b/FS/FS/svc_pbx.pm index e19dc88dd..b28f0573d 100644 --- a/FS/FS/svc_pbx.pm +++ b/FS/FS/svc_pbx.pm @@ -386,6 +386,31 @@ sub get_cdrs { qsearch ( $psearch->{query} ) } +=item sum_cdrs + +Takes the same options as psearch_cdrs, but returns a single row containing +"count" (the number of CDRs) and the sums of the following fields: duration, +billsec, rated_price, rated_seconds, rated_minutes. + +Note that if any calls are not rated, their rated_* fields will be null. +If you want to use those fields, pass the 'status' option to limit to +calls that have been rated. This is intentional; please don't "fix" it. + +=cut + +sub sum_cdrs { + my $self = shift; + my $psearch = $self->psearch_cdrs(@_); + $psearch->{query}->{'select'} = join(',', + 'COUNT(*) AS count', + map { "SUM($_) AS $_" } + qw(duration billsec rated_price rated_seconds rated_minutes) + ); + # hack + $psearch->{query}->{'extra_sql'} =~ s/ ORDER BY.*$//; + qsearchs ( $psearch->{query} ); +} + =back =head1 BUGS diff --git a/fs_selfservice/FS-SelfService/cgi/selfservice.cgi b/fs_selfservice/FS-SelfService/cgi/selfservice.cgi index 2337fb51e..8fec01121 100755 --- a/fs_selfservice/FS-SelfService/cgi/selfservice.cgi +++ b/fs_selfservice/FS-SelfService/cgi/selfservice.cgi @@ -957,7 +957,7 @@ sub delete_svc { sub view_usage { my $res = list_svcs( 'session_id' => $session_id, - 'svcdb' => [ 'svc_acct', 'svc_phone', 'svc_port', ], + 'svcdb' => [ 'svc_acct', 'svc_phone', 'svc_port', 'svc_pbx' ], 'ncancelled' => 1, ); if ($res->{hide_usage}) { diff --git a/fs_selfservice/FS-SelfService/cgi/view_usage.html b/fs_selfservice/FS-SelfService/cgi/view_usage.html index 2aa7c1efb..4099a57f5 100644 --- a/fs_selfservice/FS-SelfService/cgi/view_usage.html +++ b/fs_selfservice/FS-SelfService/cgi/view_usage.html @@ -3,6 +3,7 @@ @svc_acct = (); @svc_phone = (); @svc_port = (); + @svc_pbx = (); foreach (@svcs) { $by_pkg_label{ $_->{pkg_label} } ||= []; @@ -13,6 +14,8 @@ push @svc_phone, $_; } elsif ( $_->{svcdb} eq 'svc_port' ) { push @svc_port, $_; + } elsif ( $_->{svcdb} eq 'svc_pbx' ) { + push @svc_pbx, $_; } } ''; @@ -74,10 +77,10 @@ <%= scalar(@svc_acct) ? '

' : '' %> -<%= if ( @svc_phone ) { +<%= if ( @svc_phone or @svc_pbx ) { %any = (); for my $dir (qw(outbound inbound)) { - $any{$dir} = grep { $_->{$dir} } @svc_phone; + $any{$dir} = grep { $_->{$dir} } (@svc_phone, @svc_pbx); } $OUT.= 'Call usage

@@ -97,21 +100,22 @@ } %> -<%= foreach my $svc_phone ( @svc_phone ) { - my $link = "${url}view_cdr_details;". - "svcnum=$svc_phone->{'svcnum'};beginning=0;ending=0"; - $OUT .= ''; # usage summary w/ links for my $dir (qw(outbound inbound)) { if ( $dir eq 'inbound' ) { $link .= ';inbound=1'; } - if ( $svc_phone->{$dir} ) { + if ( $svc_x->{$dir} ) { $OUT .= ''; } elsif ( $any{$dir} ) { @@ -154,7 +158,7 @@ ''; } } -if ( scalar(@svc_phone) or scalar(@usage_pools) ) { +if ( @svc_phone or @svc_pbx or @usage_pools ) { $OUT .= '
'. $svc_phone->{'label'}. ': '. $svc_phone->{'value'}; +<%= foreach my $svc_x ( @svc_phone, @svc_pbx ) { + my $link = $url . 'view_cdr_details;' . + 'svcnum='.$svc_x->{'svcnum'}. + ';beginning=0;ending=0'; + $OUT .= '
'. $svc_x->{'label'}. ': '. $svc_x->{'value'}; $OUT .= ''.qq!! . sprintf('%d calls (%.0f minutes)', - $svc_phone->{$dir}->{'count'}, - $svc_phone->{$dir}->{'duration'} / 60 + $svc_x->{$dir}->{'count'}, + $svc_x->{$dir}->{'duration'} / 60 ) . '* shared among all your phone plans


'; } ''; -- 2.11.0