view svc_pbx usage in selfservice, #34836
authorMark Wells <mark@freeside.biz>
Fri, 15 May 2015 17:22:50 +0000 (10:22 -0700)
committerMark Wells <mark@freeside.biz>
Fri, 15 May 2015 17:22:50 +0000 (10:22 -0700)
FS/FS/ClientAPI.pm
FS/FS/ClientAPI/MyAccount.pm
FS/FS/svc_pbx.pm
fs_selfservice/FS-SelfService/cgi/selfservice.cgi
fs_selfservice/FS-SelfService/cgi/view_usage.html

index 1fea28c..e4031b2 100644 (file)
@@ -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 = ();
index e2f8595..c89b7bb 100644 (file)
@@ -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') 
index e19dc88..b28f057 100644 (file)
@@ -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
index 2337fb5..8fec011 100755 (executable)
@@ -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}) {
index 2aa7c1e..4099a57 100644 (file)
@@ -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, $_;
       }
     }
     '';
 
 <%= scalar(@svc_acct) ? '</TABLE><BR><BR>' : '' %>
 
-<%= 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.= '<FONT SIZE="4">Call usage</FONT><BR><BR>
               <TABLE BGCOLOR="#cccccc" STYLE="display:inline-block">
     }
 %>
 
-<%= foreach my $svc_phone ( @svc_phone ) {
-      my $link = "${url}view_cdr_details;".
-        "svcnum=$svc_phone->{'svcnum'};beginning=0;ending=0";
-  $OUT .= '<TR><TD>'. $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 .= '<TR><TD>'. $svc_x->{'label'}. ': '. $svc_x->{'value'};
   $OUT .= '</TD>';
   # 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 .= '<TD ALIGN="right">'.qq!<A HREF="$link">! .
         sprintf('%d calls (%.0f minutes)',
-          $svc_phone->{$dir}->{'count'},
-          $svc_phone->{$dir}->{'duration'} / 60
+          $svc_x->{$dir}->{'count'},
+          $svc_x->{$dir}->{'duration'} / 60
         ) .
         '</A></TD>';
     } elsif ( $any{$dir} )  {
             '<TD COLSPAN=4>* shared among all your phone plans</TD></TR>';
   }
 }
-if ( scalar(@svc_phone) or scalar(@usage_pools) ) {
+if ( @svc_phone or @svc_pbx or @usage_pools ) {
   $OUT .= '</TABLE><BR><BR>';
 }
 '';