the future is now
[freeside.git] / httemplate / search / svc_hardware.cgi
1 <% include('elements/search.html',
2             'title'             => 'Hardware service search results',
3             'name'              => 'installations',
4             'query'             => $sql_query,
5             'count_query'       => $count_query,
6             'redirect'          => $link_svc,
7             'header'            => [ '#',
8                                      'Service',
9                                      'Device type',
10                                      '', #revision
11                                      'Serial #',
12                                      'Hardware addr.',
13                                      'IP addr.',
14                                      'Smartcard',
15                                      FS::UI::Web::cust_header(),
16                                    ],
17             'fields'            => [ 'svcnum',
18                                      'svc',
19                                      'model',
20                                      'revision',
21                                      'serial',
22                                      'display_hw_addr',
23                                      'ip_addr',
24                                      'smartcard',
25                                      \&FS::UI::Web::cust_fields,
26                                    ],
27             'links'             => [ ($link_svc) x 8,
28                                      ( map { $_ ne 'Cust. Status' ? 
29                                                 $link_cust : '' }
30                                        FS::UI::Web::cust_header() )
31                                    ],
32             'align'             => 'rlllllll' . FS::UI::Web::cust_aligns(),
33             'color'             => [ ('') x 8,
34                                       FS::UI::Web::cust_colors() ],
35             'style'             => [ $svc_cancel_style, ('') x 7,
36                                       FS::UI::Web::cust_styles() ],
37             )
38 %>
39 <%init>
40
41 die "access denied"
42   unless $FS::CurrentUser::CurrentUser->access_right('List services');
43
44 my $addl_from = '
45  LEFT JOIN cust_svc  USING ( svcnum  )
46  LEFT JOIN part_svc  USING ( svcpart )
47  LEFT JOIN cust_pkg  USING ( pkgnum  )
48  LEFT JOIN cust_main USING ( custnum )
49  LEFT JOIN hardware_type USING ( typenum )';
50
51 my @extra_sql;
52 push @extra_sql, $FS::CurrentUser::CurrentUser->agentnums_sql(
53                     'null_right' => 'View/link unlinked services'
54                     );
55
56 if ( $cgi->param('magic') =~ /^(unlinked)$/ ) {
57   push @extra_sql, 'pkgnum IS NULL';
58 }
59
60 if ( lc($cgi->param('serial')) =~ /^(\w+)$/ ) {
61   push @extra_sql, "LOWER(serial) LIKE '%$1%'";
62 }
63
64 if ( $cgi->param('hw_addr') =~ /^(\S+)$/ ) {
65   my $hw_addr = uc($1);
66   $hw_addr =~ s/\W//g;
67   push @extra_sql, "hw_addr LIKE '%$hw_addr%'";
68 }
69
70 if ( $cgi->param('ip_addr') ) {
71   my $ip = NetAddr::IP->new($cgi->param('ip_addr'));
72   push @extra_sql, "ip_addr = '".lc($ip->addr)."'" if $ip;
73 }
74
75 if ( lc($cgi->param('smartcard')) =~ /^(\w+)$/ ) {
76   push @extra_sql, "LOWER(smartcard) LIKE '%$1%'";
77 }
78
79 if ( $cgi->param('statusnum') =~ /^(\d+)$/ ) {
80   push @extra_sql, "statusnum = $1";
81 }
82
83 if ( $cgi->param('classnum') =~ /^(\d+)$/ ) {
84   push @extra_sql, "hardware_type.classnum = $1";
85 }
86
87 if ( $cgi->param('model') =~ /^([\w\s]+)$/ ) {
88   push @extra_sql, "hardware_type.model = '$1'";
89 }
90
91 if ( $cgi->param('typenum') =~ /^(\d+)$/ ) {
92   push @extra_sql, "svc_hardware.typenum = $1";
93 }
94
95 if ( $cgi->param('svcpart') =~ /^(\d+)$/ ) {
96   push @extra_sql, "cust_svc.svcpart = $1";
97 }
98
99 my ($orderby) = $cgi->param('orderby') =~ /^(\w+( ASC| DESC)?)$/i;
100 $orderby ||= 'svcnum';
101
102 my $extra_sql = '';
103 $extra_sql = ' WHERE '.join(' AND ', @extra_sql) if @extra_sql;
104
105 my $sql_query = {
106   'table'     => 'svc_hardware',
107   'select'    => join(', ', 
108                     'svc_hardware.*',
109                     'part_svc.svc',
110                     'cust_main.custnum',
111                     'hardware_type.model',
112                     'hardware_type.revision',
113                     'cust_pkg.cancel',
114                     FS::UI::Web::cust_sql_fields(),
115                  ),
116   'hashref'   => {},
117   'extra_sql' => $extra_sql,
118   'order_by'  => "ORDER BY $orderby",
119   'addl_from' => $addl_from,
120 };
121 my $count_query = "SELECT COUNT(*) FROM svc_hardware $addl_from $extra_sql";
122 my $link_svc = [ $p.'view/svc_hardware.cgi?', 'svcnum' ];
123 my $link_cust = [ $p.'view/cust_main.cgi?', 'custnum' ];
124
125 my $svc_cancel_style = sub {
126   my $svc = shift;
127   ( $svc->getfield('cancel') == 0 ) ? '' : 's';
128 };
129
130 </%init>