summaryrefslogtreecommitdiff
path: root/httemplate/search/svc_phone.cgi
blob: 29e745690d4845a3ffa7d0aa2ca6df723afbc2e9 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<& elements/svc_Common.html,
                 'title'             => "Phone number search results",
                 'name'              => 'phone numbers',
                 'query'             => $sql_query,
                 'count_query'       => $count_query,
                 'redirect'          => $redirect,
                 'header'            => [ '#',
                                          'Service',
                                          'Country code',
                                          'Phone number',
                                          @header,
                                          emt('Pkg. Status'),
                                          FS::UI::Web::cust_header($cgi->param('cust_fields')),
                                        ],
                 'fields'            => [ 'svcnum',
                                          'svc',
                                          'countrycode',
                                          'phonenum',
                                          @fields,
                                          sub {
                                            $cust_pkg_cache{$_[0]->svcnum} ||= $_[0]->cust_svc->cust_pkg;
                                            $cust_pkg_cache{$_[0]->svcnum}->ucfirst_status
                                          },
                                          \&FS::UI::Web::cust_fields,
                                        ],
                 'links'             => [ $link,
                                          $link,
                                          $link,
                                          $link,
                                          ( map '', @header ),
                                          '', # pkg status
                                          ( map { $_ ne 'Cust. Status' ? $link_cust : '' }
                                                FS::UI::Web::cust_header($cgi->param('cust_fields'))
                                          ),
                                        ],
                 'align' => 'rlrr'.
                            join('', map 'r', @header).
                            'r'.
                            FS::UI::Web::cust_aligns(),
                 'color' => [ 
                              '',
                              '',
                              '',
                              '',
                              ( map '', @header ),
                              sub {
                                my $c = FS::cust_pkg::statuscolors;
                                $c->{$cust_pkg_cache{$_[0]->svcnum}->status };
                              }, # pkg status
                              FS::UI::Web::cust_colors(),
                            ],
                 'style' => [ 
                              '',
                              '',
                              '',
                              '',
                              ( map '', @header ),
                              'b',
                              FS::UI::Web::cust_styles(),
                            ],
              
&>
<%init>

die "access denied"
  unless $FS::CurrentUser::CurrentUser->access_right('List services');

my %cust_pkg_cache;

my $conf = new FS::Conf;

my @select = ();
my $orderby = 'ORDER BY svcnum';

my @header = ();
my @fields = ();
my $link = [ "${p}view/svc_phone.cgi?", 'svcnum' ];
my $redirect = $link;

my %search_hash = ();
my @extra_sql = ();

if ( $cgi->param('magic') =~ /^(all|unlinked)$/ ) {

  $search_hash{'unlinked'} = 1
    if $cgi->param('magic') eq 'unlinked';

  if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
    my $sortby = $1;
    $orderby = "ORDER BY $sortby";
  }

  if ( $cgi->param('usage_total') ) {

    my($beginning,$ending) = FS::UI::Web::parse_beginning_ending($cgi, 'usage');

    $redirect = '';

    #my $and_date = " AND startdate >= $beginning AND startdate <= $ending ";
    my $and_date = " AND enddate >= $beginning AND enddate <= $ending ";

    my $fromwhere = " FROM cdr WHERE cdr.svcnum = svc_phone.svcnum $and_date";

    #more efficient to join against cdr just once... this will do for now
    push @select, map { " ( SELECT SUM($_) $fromwhere ) AS $_ " }
                      qw( billsec rated_price );

    my $money_char = $conf->config('money_char') || '$';

    push @header, 'Minutes', 'Billed';
    push @fields, 
      sub { sprintf('%.3f', shift->get('billsec') / 60 ); },
      sub { $money_char. sprintf('%.2f', shift->get('rated_price') ); };

    #XXX and termination... (this needs a config to turn on, not by default)
    if ( 1 ) { # $conf->exists('cdr-termination_hack') { #}

      my $f_w =
        " FROM cdr_termination LEFT JOIN cdr USING ( acctid ) ".
        " WHERE cdr.carrierid = CAST(svc_phone.phonenum AS BIGINT) ". # XXX connectone-specific, has to match svc_external.id :/
        $and_date;

      push @select,
        " ( SELECT SUM(billsec) $f_w ) AS term_billsec ",
        " ( SELECT SUM(cdr_termination.rated_price) $f_w ) AS term_rated_price";

      push @header, 'Term Min', 'Term Billed';
      push @fields,
        sub { sprintf('%.3f', shift->get('term_billsec') / 60 ); },
        sub { $money_char. sprintf('%.2f', shift->get('rated_price') ); };

    }
                 

  }

} elsif ( $cgi->param('magic') =~ /^advanced$/ ) {

  for (qw( agentnum custnum cust_status balance balance_days cust_fields )) {
    $search_hash{$_} = $cgi->param($_) if length($cgi->param($_));
  }

  for (qw( payby pkgpart svcpart )) {
    $search_hash{$_} = [ $cgi->param($_) ] if $cgi->param($_);
  }

} elsif ( $cgi->param('svcpart') =~ /^(\d+)$/ ) {
  $search_hash{'svcpart'} = [ $1 ];
  if ( defined($cgi->param('cancelled')) ) {
    $search_hash{'cancelled'} = $cgi->param('cancelled') ? 1 : 0;
  }
} else {
  $cgi->param('phonenum') =~ /^([\d\- ]+)$/; 
  my $phonenum = $1;
  $phonenum =~ s/\D//g;
  push @extra_sql, "phonenum = '$phonenum'";
}

$search_hash{'addl_select'} = \@select;
$search_hash{'order_by'} = $orderby;
$search_hash{'where'} = \@extra_sql;

my $sql_query = FS::svc_phone->search(\%search_hash);
my $count_query = delete($sql_query->{'count_query'});

#smaller false laziness w/svc_*.cgi here
my $link_cust = sub {
  my $svc_x = shift;
  $svc_x->custnum ? [ "${p}view/cust_main.cgi?", 'custnum' ] : '';
};

</%init>