0e52d5fb613ab9e7df9f6b089eadaba86fa50915
[freeside.git] / httemplate / search / svc_broadband.cgi
1 <& elements/svc_Common.html,
2               'title'       => 'Broadband Search Results',
3               'name'        => 'broadband services',
4               'html_init'   => $html_init,
5               'query'       => $sql_query,
6               'count_query' => $sql_query->{'count_query'},
7               'redirect'    => [ popurl(2). "view/svc_broadband.cgi?", 'svcnum' ],
8               'header'      => [ '#',
9                                  'Service',
10                                  'Router',
11                                  @tower_header,
12                                  'IP Address',
13                                  @header_pkg,
14                                  emt('Pkg. Status'),
15                                  FS::UI::Web::cust_header($cgi->param('cust_fields')),
16                                ],
17               'fields'      => [ 'svcnum',
18                                  'svc',
19                                  sub {
20                                    my $router = shift->router; 
21                                    $router ? $router->routername : '';
22                                  },
23                                  @tower_fields,
24                                  'ip_addr',
25                                  @fields_pkg,
26                                  sub {
27                                    $cust_pkg_cache{$_[0]->svcnum} ||= $_[0]->cust_svc->cust_pkg;
28                                    return '' unless $cust_pkg_cache{$_[0]->svcnum};
29                                    $cust_pkg_cache{$_[0]->svcnum}->ucfirst_status
30                                  },
31                                  \&FS::UI::Web::cust_fields,
32                                ],
33               'links'       => [ $link,
34                                  $link,
35                                  '', #$link_router,
36                                  (map '', @tower_fields),
37                                  $link, # ip_addr
38                                  @blank_pkg,
39                                  '', # pkg status
40                                  ( map { $_ ne 'Cust. Status' ? $link_cust : '' }
41                                        FS::UI::Web::cust_header($cgi->param('cust_fields'))
42                                  ),
43                                ],
44               'align'       => 'rll'.('r' x @tower_fields).
45                                 'r'. # ip_addr
46                                 $align_pkg.
47                                 'r'. # pkg status
48                                 FS::UI::Web::cust_aligns(),
49               'color'       => [ 
50                                  '',
51                                  '',
52                                  '',
53                                  (map '', @tower_fields),
54                                  '', # ip_addr
55                                  @blank_pkg,
56                                  sub {
57                                    $cust_pkg_cache{$_[0]->svcnum} ||= $_[0]->cust_svc->cust_pkg;
58                                    return '' unless $cust_pkg_cache{$_[0]->svcnum};
59                                    my $c = FS::cust_pkg::statuscolors;
60                                    $c->{$cust_pkg_cache{$_[0]->svcnum}->status };
61                                  }, # pkg status
62                                  FS::UI::Web::cust_colors(),
63                                ],
64               'style'       => [ 
65                                  '',
66                                  '',
67                                  '',
68                                  (map '', @tower_fields),
69                                  '',  # ip_addr
70                                  @blank_pkg,
71                                  'b', # pkg status
72                                  FS::UI::Web::cust_styles(),
73                                ],
74           
75 &>
76 <%init>
77
78 die "access denied" unless
79   $FS::CurrentUser::CurrentUser->access_right('List services');
80
81 my %cust_pkg_cache;
82
83 my $conf = new FS::Conf;
84
85 my %search_hash;
86 if ( $cgi->param('magic') eq 'unlinked' ) {
87   %search_hash = ( 'unlinked' => 1 );
88 } else {
89   foreach (qw( custnum agentnum svcpart cust_fields )) {
90     $search_hash{$_} = $cgi->param($_) if $cgi->param($_);
91   }
92   foreach (qw(pkgpart routernum towernum sectornum)) {
93     $search_hash{$_} = [ $cgi->param($_) ] if $cgi->param($_);
94   }
95   if ( defined($cgi->param('cancelled')) ) {
96     $search_hash{'cancelled'} = $cgi->param('cancelled') ? 1 : 0;
97   }
98 }
99
100 if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
101   $search_hash{'order_by'} = "ORDER BY $1";
102 }
103
104 my $sql_query = FS::svc_broadband->search(\%search_hash);
105
106 my @tower_header;
107 my @tower_fields;
108 if ( FS::tower_sector->count > 0 ) {
109   push @tower_header, 'Tower/Sector';
110   push @tower_fields, sub { $_[0]->tower_sector ? 
111                             $_[0]->tower_sector->description : '' };
112 }
113
114 my %routerbyblock = ();
115 foreach my $router (qsearch('router', {})) {
116   foreach ($router->addr_block) {
117     $routerbyblock{$_->blocknum} = $router;
118   }
119 }
120
121 my $link = [ $p.'view/svc_broadband.cgi?', 'svcnum' ];
122
123 #XXX get the router link working
124 #my $link_router = sub {
125 #  my $routernum = $routerbyblock{shift->blocknum}->routernum;
126 #  [ $p.'view/router.cgi?'.$routernum, 'routernum' ];
127 #};
128
129 my $link_cust = [ $p.'view/cust_main.cgi?', 'custnum' ];
130
131 my $html_init = include('/elements/email-link.html',
132                   'search_hash' => \%search_hash,
133                   'table' => 'svc_broadband' 
134                 );
135 $html_init .= ' | ' .
136   '<a href="' .
137   $fsurl . 'search/svc_broadband-map.html?' . $cgi->query_string .
138   '">' . emt('View a map of these services') . '</a>';
139
140 my (@header_pkg,@fields_pkg,@blank_pkg);
141 my $align_pkg = '';
142 #false laziness with search/svc_acct.cgi
143 $cgi->param('cust_pkg_fields') =~ /^([\w\,]*)$/ or die "bad cust_pkg_fields";
144 my @pkg_fields = split(',', $1);
145 foreach my $pkg_field ( @pkg_fields ) {
146   ( my $header = ucfirst($pkg_field) ) =~ s/_/ /; #:/
147   push @header_pkg, $header;
148
149   #not the most efficient to do it every field, but this is of niche use. so far
150   push @fields_pkg, sub { my $svc_x = shift;
151                           my $cust_pkg = $svc_x->cust_svc->cust_pkg or return '';
152                           my $value = $cust_pkg->get($pkg_field);#closures help alot
153                           $value ? time2str('%b %d %Y', $value ) : '';
154                         };
155
156   push @blank_pkg, '';
157   $align_pkg .= 'c';
158 }
159
160
161 </%init>