summaryrefslogtreecommitdiff
path: root/httemplate/view/svc_pbx.cgi
blob: 9b18dc3bd721d50396500d31a9e7a6247a2263e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<% include('elements/svc_Common.html',
             'table'     => 'svc_pbx',
             'edit_url'  => $p.'edit/svc_pbx.html?',
             '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;

  ##
  # Extensions
  ##

  my @pbx_extension = sort { $a->extension cmp $b->extension }
                        $svc_pbx->pbx_extension;

  my $extensions = '';
  if ( @pbx_extension ) {

    $extensions .= '<FONT CLASS="fsinnerbox-title">Extensions</FONT>'.
                   include('/elements/table-grid.html');
    my $bgcolor1 = '#eeeeee';
    my $bgcolor2 = '#ffffff';
    my $bgcolor = '';

    #$extensions .= '
    #  <TR>
    #    <TH CLASS="grid" BGCOLOR="#cccccc">Ext</TH>
    #    <TH CLASS="grid" BGCOLOR="#cccccc">Name</TH>
    #  </TR>
    #';

    foreach my $pbx_extension ( @pbx_extension ) {
      if ( $bgcolor eq $bgcolor1 ) {
        $bgcolor = $bgcolor2;
      } else {
        $bgcolor = $bgcolor1;
      }

      $extensions .= qq(
        <TR>
          <TD CLASS="grid" BGCOLOR="$bgcolor">). $pbx_extension->extension. qq(
          <TD CLASS="grid" BGCOLOR="$bgcolor">). $pbx_extension->phone_name. qq(
        </TR>
      );
      
    }

    $extensions .= '</TABLE><BR>';
  }

  ##
  # 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 $extensions 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 && $voip_pkg->option('cdr_svc_method') )
                       || 'svc_phone.phonenum';
  return $extensions unless $cdr_svc_method =~ /^svc_pbx\.(.*)$/;
  my $field = $1;

  my $search;
  if ( $field eq 'title' ) {
    $search = 'charged_party='. uri_escape($svc_pbx->title);
  } elsif ( $field =~ /^ip\.(\w+)$/ ) {
    $search = "$1_ip_addr=". uri_escape($svc_pbx->title);
  } elsif ( $field eq 'svcnum' ) {
    $search = 'svcnum='. $svc_pbx->svcnum;
  } else {
    warn "unknown cdr_svc_method svc_pbx.$field";
    return $extensions
  }

  my @links = map {
    qq(<A HREF="${p}search/cdr.html?cdrbatchnum=__ALL__;$search;freesidestatus=$what{$_}">).
    "View $_ CDRs</A>";
  } keys(%what);

  ###
  # concatenate & return
  ###

  $extensions.
  join(' | ', @links ). '<BR>';

};

</%init>