add customer status to advanced package report, RT#24631
[freeside.git] / httemplate / search / svc_hardware.cgi
1 <& elements/svc_Common.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 <%init>
39
40 die "access denied"
41   unless $FS::CurrentUser::CurrentUser->access_right('List services');
42
43 my $addl_from = '
44  LEFT JOIN cust_svc  USING ( svcnum  )
45  LEFT JOIN part_svc  USING ( svcpart )
46  LEFT JOIN cust_pkg  USING ( pkgnum  )'.
47  FS::UI::Web::join_cust_main('cust_pkg', 'cust_pkg').'
48  LEFT JOIN hardware_type USING ( typenum )';
49
50 my @extra_sql;
51 push @extra_sql, $FS::CurrentUser::CurrentUser->agentnums_sql(
52                     'null_right' => 'View/link unlinked services'
53                     );
54
55 if ( $cgi->param('magic') =~ /^(unlinked)$/ ) {
56   push @extra_sql, 'pkgnum IS NULL';
57 }
58
59 if ( lc($cgi->param('serial')) =~ /^(\w+)$/ ) {
60   push @extra_sql, "LOWER(serial) LIKE '%$1%'";
61 }
62
63 if ( $cgi->param('hw_addr') =~ /^(\S+)$/ ) {
64   my $hw_addr = uc($1);
65   $hw_addr =~ s/\W//g;
66   push @extra_sql, "hw_addr LIKE '%$hw_addr%'";
67 }
68
69 if ( $cgi->param('ip_addr') ) {
70   my $ip = NetAddr::IP->new($cgi->param('ip_addr'));
71   push @extra_sql, "ip_addr = '".lc($ip->addr)."'" if $ip;
72 }
73
74 if ( lc($cgi->param('smartcard')) =~ /^(\w+)$/ ) {
75   push @extra_sql, "LOWER(smartcard) LIKE '%$1%'";
76 }
77
78 if ( $cgi->param('statusnum') =~ /^(\d+)$/ ) {
79   push @extra_sql, "statusnum = $1";
80 }
81
82 if ( $cgi->param('classnum') =~ /^(\d+)$/ ) {
83   push @extra_sql, "hardware_type.classnum = $1";
84 }
85
86 if ( $cgi->param('model') =~ /^([\w\s]+)$/ ) {
87   push @extra_sql, "hardware_type.model = '$1'";
88 }
89
90 if ( $cgi->param('typenum') =~ /^(\d+)$/ ) {
91   push @extra_sql, "svc_hardware.typenum = $1";
92 }
93
94 if ( $cgi->param('svcpart') =~ /^(\d+)$/ ) {
95   push @extra_sql, "cust_svc.svcpart = $1";
96 }
97
98 my ($orderby) = $cgi->param('orderby') =~ /^(\w+( ASC| DESC)?)$/i;
99 $orderby ||= 'svcnum';
100
101 my $extra_sql = '';
102 $extra_sql = ' WHERE '.join(' AND ', @extra_sql) if @extra_sql;
103
104 my $sql_query = {
105   'table'     => 'svc_hardware',
106   'select'    => join(', ', 
107                     'svc_hardware.*',
108                     'part_svc.svc',
109                     'cust_main.custnum',
110                     'hardware_type.model',
111                     'hardware_type.revision',
112                     'cust_pkg.cancel',
113                     FS::UI::Web::cust_sql_fields(),
114                  ),
115   'hashref'   => {},
116   'extra_sql' => $extra_sql,
117   'order_by'  => "ORDER BY $orderby",
118   'addl_from' => $addl_from,
119 };
120 my $count_query = "SELECT COUNT(*) FROM svc_hardware $addl_from $extra_sql";
121 my $link_svc = [ $p.'view/svc_hardware.cgi?', 'svcnum' ];
122 my $link_cust = [ $p.'view/cust_main.cgi?', 'custnum' ];
123
124 my $svc_cancel_style = sub {
125   my $svc = shift;
126   ( $svc->getfield('cancel') == 0 ) ? '' : 's';
127 };
128
129 </%init>