svc_pbx and whole-customer links to CDRs, RT#10315
authorivan <ivan>
Mon, 25 Oct 2010 00:28:41 +0000 (00:28 +0000)
committerivan <ivan>
Mon, 25 Oct 2010 00:28:41 +0000 (00:28 +0000)
httemplate/search/cdr.html
httemplate/search/report_cdr.html
httemplate/view/cust_main/packages.html
httemplate/view/svc_pbx.cgi [new file with mode: 0644]

index 702dc1b..5544ff5 100644 (file)
@@ -147,7 +147,7 @@ foreach my $param ( grep /^termpart\d+status$/, $cgi->param ) {
 }
 
 ###
-# src/dest/charged_party
+# src/dest/charged_party/svcnum
 ###
 
 my $phonenum = qr/^\s*([\d\-\+\ ]+)\s*$/;
@@ -175,27 +175,32 @@ if ( $cgi->param('dcontext') =~ /^\s*(.+)\s*$/ ) {
   push @search, "dcontext = '$dcontext'";
 }
 
-if ( $cgi->param('charged_party') =~ $phonenum ) {
-  ( my $charged_party = $1 ) =~ s/$x//g;
-  #$hashref->{'charged_party'} = $charged_party;
-  #push @search, "charged_party = '$charged_party'";
-  #XXX countrycode
+if ( $cgi->param('charged_party') ) {
 
-  my $search = " (    charged_party IN ('$charged_party', '1$charged_party') )";
+  my @cp = map { $_, "1$_" }
+             split(/\s*,\s*/, $cgi->param('charged_party') );
+  
+  my $search = 'charged_party IN ('. join(',', map dbh->quote($_), @cp). ')';
 
   push @search,  $search;
   push @qsearch, $search;
 }
 
-if ( $cgi->param('charged_party_or_src') =~ $phonenum ) {
-  ( my $charged_party = $1 ) =~ s/$x//g;
-  #$hashref->{'charged_party'} = $charged_party;
-  #push @search, "charged_party = '$charged_party'";
-  #XXX countrycode
+if ( $cgi->param('charged_party_or_src') ) {
 
-  my $search = " (    charged_party IN ('$charged_party', '1$charged_party')
-                   OR src           IN ('$charged_party', '1$charged_party') )";
+  my @cp = map { $_, "1$_" }
+             split(/\s*,\s*/, $cgi->param('charged_party_or_src') );
+  my $in = join(',', map dbh->quote($_), @cp);
 
+  my $search = "( charged_party IN ($in) OR src IN ($in) )";
+
+  push @search,  $search;
+  push @qsearch, $search;
+}
+
+if ( $cgi->param('svcnum') =~ /^([\d, ]+)$/ ) {
+  my $svcnum = $1;
+  my $search = "svcnum IN ($svcnum)";
   push @search,  $search;
   push @qsearch, $search;
 }
index a50e4db..866606c 100644 (file)
   <TR>
     <TD ALIGN="right">Charged Party #: </TD>
     <TD>
-      <INPUT TYPE="text" NAME="charged_party">
+      <INPUT TYPE="text" NAME="charged_party" VALUE="<% join(',', @charged_party) |h %>">
+    </TD>
+  </TR>
+
+  <TR>
+    <TD ALIGN="right">Charged Party or Source #: </TD>
+    <TD>
+      <INPUT TYPE="text" NAME="charged_party_or_src" VALUE="<% join(',', @charged_party_or_src ) |h %>" >
+    </TD>
+  </TR>
+
+  <TR>
+    <TD ALIGN="right">Freeside service #: </TD>
+    <TD>
+      <INPUT TYPE="text" NAME="svcnum" VALUE="<% join(',', @svcnum ) %>" >
     </TD>
   </TR>
 
@@ -145,4 +159,72 @@ my $names_list = [ map {
                    @fields
                  ];
 
+my @charged_party = ();
+my @charged_party_or_src = ();
+my @svcnum = ();
+if ( $cgi->param('custnum') =~ /^(\d+)$/ ) {
+  my $custnum = $1;
+
+  my $cust_main = qsearchs( {
+    'table'     => 'cust_main',
+    'hashref'   => { 'custnum' => $custnum },
+    'extra_sql' => ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql,
+  });
+  die "Customer not found!" unless $cust_main;
+
+  #historical?
+  foreach my $cust_pkg ( $cust_main->ncancelled_pkgs ) {
+
+    my @voip_pkgs =
+      grep { $_->plan eq 'voip_cdr' } $cust_pkg->part_pkg->self_and_bill_linked;
+    if ( scalar(@voip_pkgs) > 1 ) { 
+      die "multiple voip_cdr packages bundled\n";
+    } elsif ( !@voip_pkgs ) {
+      next;
+    }
+    my $voip_pkg = @voip_pkgs[0];
+
+    my $cdr_svc_method = $voip_pkg->option('cdr_svc_method')
+                         || 'svc_phone.phonenum';
+
+    my @cust_svc = $cust_pkg->cust_svc; #historical?
+
+    if ( $cdr_svc_method eq 'svc_phone.phonenum' ) {
+
+      my @svc_phone = map $_->svc_x,
+                        grep { $_->part_svc->svcdb eq 'svc_phone' } @cust_svc;
+
+      my @numbers = map {
+                          my $number = $_->phonenum;
+                          $number = $_->countrycode. $number
+                            unless $_->countrycode eq '1';
+                          $number;
+                        }
+                      @svc_phone;
+
+      if ( $voip_pkg->option('disable_src') ) {
+        push @charged_party, @numbers;
+      } else {
+        push @charged_party_or_src, @numbers;
+      }
+
+    } elsif ( $cdr_svc_method eq 'svc_pbx.title' ) {
+      my @svc_pbx = map $_->svc_x,
+                      grep { $_->part_svc->svcdb eq 'svc_pbx' } @cust_svc;
+      push @charged_party, map $_->title, @svc_pbx;
+    } elsif ( $cdr_svc_method eq 'svc_pbx.svcnum' ) {
+      my @cust_svc_pbx = grep { $_->part_svc->svcdb eq 'svc_pbx' } @cust_svc;
+      push @svcnum, map $_->svcnum, @cust_svc_pbx;
+    }
+
+  }
+
+  die "No CDR packages for customer $custnum\n"
+    unless @charged_party || @charged_party_or_src || @svcnum;
+
+  #die "Multiple matching metods for customer $custnum\n"
+  #  if #there's more than one
+
+}
+
 </%init>
index 811ac3c..f0f156b 100755 (executable)
@@ -57,7 +57,9 @@ Current packages
     <TD ALIGN="right">
       <A HREF="<%$p%>search/report_cust_pkg.html?custnum=<% $cust_main->custnum %>">Package reports</A><BR>
       Service reports:
-        <A HREF="<%$p%>search/report_svc_acct.html?custnum=<% $cust_main->custnum %>">accounts</A>
+        <A HREF="<%$p%>search/report_svc_acct.html?custnum=<% $cust_main->custnum %>">accounts</A><BR>
+      Usage reports:
+        <A HREF="<%$p%>search/report_cdr.html?custnum=<% $cust_main->custnum %>">CDRs</A>
     </TD>
   </TR>
 
diff --git a/httemplate/view/svc_pbx.cgi b/httemplate/view/svc_pbx.cgi
new file mode 100644 (file)
index 0000000..79cafed
--- /dev/null
@@ -0,0 +1,72 @@
+<% include('elements/svc_Common.html',
+             'table'     => 'svc_pbx',
+            'edit_url'  => $p."edit/svc_Common.html?svcdb=svc_pbx;svcnum=",
+             'labels'    => \%labels,
+             'html_foot' => $html_foot,
+          )
+%>
+<%init>
+
+my $fields = FS::svc_pbx->table_info->{'fields'};
+my %labels = map { $_ =>  ( ref($fields->{$_})
+                             ? $fields->{$_}{'label'}
+                             : $fields->{$_}
+                         );
+                 }
+             keys %$fields;
+
+my $html_foot = sub {
+  my $svc_pbx = shift;
+
+  ##
+  # CDR links
+  ##
+
+  tie my %what, 'Tie::IxHash',
+    'pending' => 'NULL',
+    'billed'  => 'done',
+  ;
+
+  #matching as per package def cdr_svc_method
+  my $cust_pkg = $svc_pbx->cust_svc->cust_pkg;
+  return '' unless $cust_pkg;
+
+  my @voip_pkgs =
+    grep { $_->plan eq 'voip_cdr' } $cust_pkg->part_pkg->self_and_bill_linked;
+  if ( scalar(@voip_pkgs) > 1 ) { 
+    warn "multiple voip_cdr packages bundled\n";
+    return '';
+  } elsif ( !@voip_pkgs ) {
+    warn "no voip_cdr packages\n";
+  }
+  my $voip_pkg = @voip_pkgs[0];
+
+  my $cdr_svc_method = $voip_pkg->option('cdr_svc_method')
+                       || 'svc_phone.phonenum';
+  return '' unless $cdr_svc_method =~ /^svc_pbx\.(\w+)$/;
+  my $field = $1;
+
+  my $search;
+  if ( $field eq 'title' ) {
+    $search = 'charged_party='. uri_escape($svc_pbx->title);
+  } elsif ( $field eq 'svcnum' ) {
+    $search = 'svcnum='. $svc_pbx->svcnum;
+  } else {
+    warn "unknown cdr_svc_method svc_pbx.$field";
+    return '';
+  }
+
+  my @links = map {
+    qq(<A HREF="${p}search/cdr.html?cdrbatchnum=__ALL__;$search;freesidestatus=$what{$_}">).
+    "View $_ CDRs</A>";
+  } keys(%what);
+
+  ###
+  # concatenate & return
+  ###
+
+  join(' | ', @links ). '<BR>';
+
+};
+
+</%init>