Merge branch 'master' of git.freeside.biz:/home/git/freeside
[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                                      emt('Pkg. Status'),
16                                      FS::UI::Web::cust_header(),
17                                    ],
18             'fields'            => [ 'svcnum',
19                                      'svc',
20                                      'model',
21                                      'revision',
22                                      'serial',
23                                      'display_hw_addr',
24                                      'ip_addr',
25                                      'smartcard',
26                                      sub {
27                                        $cust_pkg_cache{$_[0]->svcnum} ||= $_[0]->cust_svc->cust_pkg;
28                                        $cust_pkg_cache{$_[0]->svcnum}->ucfirst_status
29                                      },
30                                      \&FS::UI::Web::cust_fields,
31                                    ],
32             'links'             => [ ($link_svc) x 8,
33                                      '', # pkg status
34                                      ( map { $_ ne 'Cust. Status' ? 
35                                                 $link_cust : '' }
36                                        FS::UI::Web::cust_header() )
37                                    ],
38             'align'             => 'rlllllllr' . FS::UI::Web::cust_aligns(),
39             'color'             => [ ('') x 8,
40                                      sub {
41                                        my $c = FS::cust_pkg::statuscolors;
42                                        $c->{$cust_pkg_cache{$_[0]->svcnum}->status };
43                                      }, # pkg status
44                                      FS::UI::Web::cust_colors() ],
45             'style'             => [ $svc_cancel_style, ('') x 7,
46                                      'b',
47                                      FS::UI::Web::cust_styles() ],
48 &>
49 <%init>
50
51 die "access denied"
52   unless $FS::CurrentUser::CurrentUser->access_right('List services');
53
54 my %cust_pkg_cache;
55
56 my $addl_from = '
57  LEFT JOIN cust_svc  USING ( svcnum  )
58  LEFT JOIN part_svc  USING ( svcpart )
59  LEFT JOIN cust_pkg  USING ( pkgnum  )'.
60  FS::UI::Web::join_cust_main('cust_pkg', 'cust_pkg').'
61  LEFT JOIN hardware_type USING ( typenum )';
62
63 my @extra_sql;
64 push @extra_sql, $FS::CurrentUser::CurrentUser->agentnums_sql(
65                     'null_right' => 'View/link unlinked services'
66                     );
67
68 if ( $cgi->param('magic') =~ /^(unlinked)$/ ) {
69   push @extra_sql, 'pkgnum IS NULL';
70 }
71
72 if ( lc($cgi->param('serial')) =~ /^(\w+)$/ ) {
73   push @extra_sql, "LOWER(serial) LIKE '%$1%'";
74 }
75
76 if ( $cgi->param('hw_addr') =~ /^(\S+)$/ ) {
77   my $hw_addr = uc($1);
78   $hw_addr =~ s/\W//g;
79   push @extra_sql, "hw_addr LIKE '%$hw_addr%'";
80 }
81
82 if ( $cgi->param('ip_addr') ) {
83   my $ip = NetAddr::IP->new($cgi->param('ip_addr'));
84   push @extra_sql, "ip_addr = '".lc($ip->addr)."'" if $ip;
85 }
86
87 if ( lc($cgi->param('smartcard')) =~ /^(\w+)$/ ) {
88   push @extra_sql, "LOWER(smartcard) LIKE '%$1%'";
89 }
90
91 if ( $cgi->param('statusnum') =~ /^(\d+)$/ ) {
92   push @extra_sql, "statusnum = $1";
93 }
94
95 if ( $cgi->param('classnum') =~ /^(\d+)$/ ) {
96   push @extra_sql, "hardware_type.classnum = $1";
97 }
98
99 if ( $cgi->param('model') =~ /^([\w\s]+)$/ ) {
100   push @extra_sql, "hardware_type.model = '$1'";
101 }
102
103 if ( $cgi->param('typenum') =~ /^(\d+)$/ ) {
104   push @extra_sql, "svc_hardware.typenum = $1";
105 }
106
107 if ( $cgi->param('svcpart') =~ /^(\d+)$/ ) {
108   push @extra_sql, "cust_svc.svcpart = $1";
109   if (defined($cgi->param('cancelled'))) {
110     if ($cgi->param('cancelled')) {
111       push @extra_sql, "cust_pkg.cancel IS NOT NULL";
112     } else {
113       push @extra_sql, "cust_pkg.cancel IS NULL";
114     }
115   }
116 }
117
118 my ($orderby) = $cgi->param('orderby') =~ /^(\w+( ASC| DESC)?)$/i;
119 $orderby ||= 'svcnum';
120
121 my $extra_sql = '';
122 $extra_sql = ' WHERE '.join(' AND ', @extra_sql) if @extra_sql;
123
124 my $sql_query = {
125   'table'     => 'svc_hardware',
126   'select'    => join(', ', 
127                     'svc_hardware.*',
128                     'part_svc.svc',
129                     'cust_main.custnum',
130                     'hardware_type.model',
131                     'hardware_type.revision',
132                     'cust_pkg.cancel',
133                     FS::UI::Web::cust_sql_fields(),
134                  ),
135   'hashref'   => {},
136   'extra_sql' => $extra_sql,
137   'order_by'  => "ORDER BY $orderby",
138   'addl_from' => $addl_from,
139 };
140 my $count_query = "SELECT COUNT(*) FROM svc_hardware $addl_from $extra_sql";
141 my $link_svc = [ $p.'view/svc_hardware.cgi?', 'svcnum' ];
142 my $link_cust = [ $p.'view/cust_main.cgi?', 'custnum' ];
143
144 my $svc_cancel_style = sub {
145   my $svc = shift;
146   ( $svc->getfield('cancel') == 0 ) ? '' : 's';
147 };
148
149 </%init>