Merge branch 'xss_fixes' of https://github.com/mcreenan/Freeside
[freeside.git] / FS / FS / svc_hardware.pm
1 package FS::svc_hardware;
2
3 use strict;
4 use base qw( FS::svc_Common );
5 use vars qw( $conf );
6 use FS::Record qw( qsearch qsearchs );
7 use FS::hardware_type;
8 use FS::hardware_status;
9 use FS::Conf;
10
11 FS::UID->install_callback(sub { $conf = FS::Conf->new; });
12
13 =head1 NAME
14
15 FS::svc_hardware - Object methods for svc_hardware records
16
17 =head1 SYNOPSIS
18
19   use FS::svc_hardware;
20
21   $record = new FS::svc_hardware \%hash;
22   $record = new FS::svc_hardware { 'column' => 'value' };
23
24   $error = $record->insert;
25
26   $error = $new_record->replace($old_record);
27
28   $error = $record->delete;
29
30   $error = $record->check;
31
32 =head1 DESCRIPTION
33
34 An FS::svc_hardware object represents an equipment installation, such as 
35 a wireless broadband receiver, satellite antenna, or DVR.  FS::svc_hardware 
36 inherits from FS::svc_Common.
37
38 The following fields are currently supported:
39
40 =over 4
41
42 =item svcnum - Primary key
43
44 =item typenum - Device type number (see L<FS::hardware_type>)
45
46 =item ip_addr - IP address
47
48 =item hw_addr - Hardware address
49
50 =item serial - Serial number
51
52 =item smartcard - Smartcard number, for devices that use a smartcard
53
54 =item statusnum - Service status (see L<FS::hardware_status>)
55
56 =item note - Installation notes: location on property, physical access, etc.
57
58 =back
59
60 =head1 METHODS
61
62 =over 4
63
64 =item new HASHREF
65
66 Creates a new svc_hardware object.
67
68 =cut
69
70 sub table { 'svc_hardware'; }
71
72 sub table_info {
73   my %opts = ( 'type' => 'text', 'disable_select' => 1 );
74   {
75     'name'           => 'Hardware', #?
76     'name_plural'    => 'Hardware',
77     'display_weight' => 59,
78     'cancel_weight'  => 86,
79     'fields' => {
80       'svcnum'    => { label => 'Service' },
81       'typenum'   => { label => 'Device type',
82                        type  => 'select-hardware',
83                        disable_select    => 1,
84                        disable_fixed     => 1,
85                        disable_default   => 1,
86                        disable_inventory => 1,
87                      },
88       'serial'    => { label => 'Serial number', %opts },
89       'hw_addr'   => { label => 'Hardware address', %opts },
90       'ip_addr'   => { label => 'IP address', %opts },
91       'smartcard' => { label => 'Smartcard #', %opts },
92       'statusnum' => { label => 'Service status', 
93                        type  => 'select',
94                        select_table => 'hardware_status',
95                        select_key   => 'statusnum',
96                        select_label => 'label',
97                        disable_inventory => 1,
98                      },
99       'note'      => { label => 'Installation notes', %opts },
100     }
101   }
102 }
103
104 sub search_sql {
105   my ($class, $string) = @_;
106   my @where = ();
107
108   if ( $string =~ /^[\d\.:]+$/ ) {
109     # if the string isn't an IP address, this will waste several seconds
110     # attempting a DNS lookup.  so try to filter those out.
111     my $ip = NetAddr::IP->new($string);
112     if ( $ip ) {
113       push @where, $class->search_sql_field('ip_addr', $ip->addr);
114     }
115   }
116   
117   if ( $string =~ /^(\w+)$/ ) {
118     push @where, 'LOWER(svc_hardware.serial) LIKE \'%'.lc($string).'%\'';
119   }
120
121   if ( $string =~ /^([0-9A-Fa-f]|\W)+$/ ) {
122     my $hex = uc($string);
123     $hex =~ s/\W//g;
124     push @where, 'svc_hardware.hw_addr LIKE \'%'.$hex.'%\'';
125   }
126
127   if ( @where ) {
128     '(' . join(' OR ', @where) . ')';
129   } else {
130     '1 = 0'; #false
131   }
132 }
133
134 sub label {
135   my $self = shift;
136   $self->serial || $self->display_hw_addr;
137 }
138
139 =item insert
140
141 Adds this record to the database.  If there is an error, returns the error,
142 otherwise returns false.
143
144 =item delete
145
146 Delete this record from the database.
147
148 =item replace OLD_RECORD
149
150 Replaces the OLD_RECORD with this one in the database.  If there is an error,
151 returns the error, otherwise returns false.
152
153 # the replace method can be inherited from FS::Record
154
155 =item check
156
157 Checks all fields to make sure this is a valid service.  If there is
158 an error, returns the error, otherwise returns false.  Called by the insert
159 and replace methods.
160
161 =cut
162
163 sub check {
164   my $self = shift;
165   my $conf = FS::Conf->new;
166
167   my $x = $self->setfixed;
168   return $x unless ref $x;
169
170   my $hw_addr = $self->getfield('hw_addr');
171   $hw_addr = join('', split(/[_\W]/, $hw_addr));
172   if ( $conf->exists('svc_hardware-check_mac_addr') ) {
173     $hw_addr = uc($hw_addr);
174     $hw_addr =~ /^[0-9A-F]{12}$/ 
175       or return "Illegal (MAC address) '".$self->getfield('hw_addr')."'";
176   }
177   $self->setfield('hw_addr', $hw_addr);
178
179   my $error = 
180     $self->ut_numbern('svcnum')
181     || $self->ut_foreign_key('typenum', 'hardware_type', 'typenum')
182     || $self->ut_ip46n('ip_addr')
183     || $self->ut_alphan('hw_addr')
184     || $self->ut_alphan('serial')
185     || $self->ut_alphan('smartcard')
186     || $self->ut_foreign_keyn('statusnum', 'hardware_status', 'statusnum')
187     || $self->ut_anything('note')
188   ;
189   return $error if $error;
190
191   if ( !length($self->getfield('hw_addr')) 
192         and !length($self->getfield('serial')) ) {
193     return 'Serial number or hardware address required';
194   }
195  
196   $self->SUPER::check;
197 }
198
199 =item hardware_type
200
201 Returns the L<FS::hardware_type> object associated with this installation.
202
203 =cut
204
205 sub hardware_type {
206   my $self = shift;
207   return qsearchs('hardware_type', { 'typenum' => $self->typenum });
208 }
209
210 =item status_label
211
212 Returns the 'label' field of the L<FS::hardware_status> object associated 
213 with this installation.
214
215 =cut
216
217 sub status_label {
218   my $self = shift;
219   my $status = qsearchs('hardware_status', { 'statusnum' => $self->statusnum })
220     or return '';
221   $status->label;
222 }
223
224 =item display_hw_addr
225
226 Returns the 'hw_addr' field, formatted as a MAC address if the
227 'svc_hardware-check_mac_addr' option is enabled.
228
229 =cut
230
231 sub display_hw_addr {
232   my $self = shift;
233   ($conf->exists('svc_hardware-check_mac_addr') ? 
234     join(':', $self->hw_addr =~ /../g) : $self->hw_addr)
235 }
236
237 =back
238
239 =head1 SEE ALSO
240
241 L<FS::Record>, L<FS::svc_Common>, schema.html from the base documentation.
242
243 =cut
244
245 1;
246