svc_hardware: better error messages for bad hw_addr when not validating as a MAC...
[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     'manual_require' => 1,
80     'fields' => {
81       'svcnum'    => { label => 'Service' },
82       'typenum'   => { label => 'Device type',
83                        type  => 'select-hardware',
84                        disable_select    => 1,
85                        disable_fixed     => 1,
86                        disable_default   => 1,
87                        disable_inventory => 1,
88                        required => 1,
89                      },
90       'serial'    => { label => 'Serial number', %opts },
91       'hw_addr'   => { label => 'Hardware address', %opts },
92       'ip_addr'   => { label => 'IP address', %opts },
93       'smartcard' => { label => 'Smartcard #', %opts },
94       'statusnum' => { label => 'Service status', 
95                        type  => 'select',
96                        select_table => 'hardware_status',
97                        select_key   => 'statusnum',
98                        select_label => 'label',
99                        disable_inventory => 1,
100                      },
101       'note'      => { label => 'Installation notes', %opts },
102     }
103   }
104 }
105
106 sub search_sql {
107   my ($class, $string) = @_;
108   my @where = ();
109
110   if ( $string =~ /^[\d\.:]+$/ ) {
111     # if the string isn't an IP address, this will waste several seconds
112     # attempting a DNS lookup.  so try to filter those out.
113     my $ip = NetAddr::IP->new($string);
114     if ( $ip ) {
115       push @where, $class->search_sql_field('ip_addr', $ip->addr);
116     }
117   }
118   
119   if ( $string =~ /^(\w+)$/ ) {
120     push @where, 'LOWER(svc_hardware.serial) LIKE \'%'.lc($string).'%\'';
121   }
122
123   if ( $string =~ /^([0-9A-Fa-f]|\W)+$/ ) {
124     my $hex = uc($string);
125     $hex =~ s/\W//g;
126     push @where, 'svc_hardware.hw_addr LIKE \'%'.$hex.'%\'';
127   }
128
129   if ( @where ) {
130     '(' . join(' OR ', @where) . ')';
131   } else {
132     '1 = 0'; #false
133   }
134 }
135
136 sub label {
137   my $self = shift;
138   my $part_svc = $self->cust_svc->part_svc;
139   my @label = ();
140   if (my $type = $self->hardware_type) {
141     my $typenum_label = $part_svc->part_svc_column('typenum');
142     push @label, ( $typenum_label && $typenum_label->columnlabel || 'Type' ).
143                  ':'. $type->description;
144   }
145   if (my $ser = $self->serial) {
146     my $serial_label = $part_svc->part_svc_column('serial');
147     push @label, ( $serial_label && $serial_label->columnlabel || 'Serial' ).
148                  '#'. $ser;
149   }
150   if (my $mac = $self->display_hw_addr) {
151     my $hw_addr_label = $part_svc->part_svc_column('hw_addr');
152     push @label, ( $hw_addr_label && $hw_addr_label->columnlabel || 'MAC').
153                  ':'. $mac;
154   }
155   return join(', ', @label);
156 }
157
158 =item insert
159
160 Adds this record to the database.  If there is an error, returns the error,
161 otherwise returns false.
162
163 =item delete
164
165 Delete this record from the database.
166
167 =item replace OLD_RECORD
168
169 Replaces the OLD_RECORD with this one in the database.  If there is an error,
170 returns the error, otherwise returns false.
171
172 # the replace method can be inherited from FS::Record
173
174 =item check
175
176 Checks all fields to make sure this is a valid service.  If there is
177 an error, returns the error, otherwise returns false.  Called by the insert
178 and replace methods.
179
180 =cut
181
182 sub check {
183   my $self = shift;
184   my $conf = FS::Conf->new;
185
186   my $x = $self->setfixed;
187   return $x unless ref $x;
188
189   my $hw_addr = $self->getfield('hw_addr');
190   $hw_addr = join('', split(/[_\W]/, $hw_addr));
191   if ( $conf->exists('svc_hardware-check_mac_addr') ) {
192     $hw_addr = uc($hw_addr);
193     $hw_addr =~ /^[0-9A-F]{12}$/ 
194       or return "Illegal (MAC address) '".$self->getfield('hw_addr')."'";
195   } else {
196     return "Illegal (hardware address) '".$self->getfield('hw_addr')."': ".
197            "12 alphanumeric characters maximum"
198       if length($hw_addr) > 12;
199   }
200   $self->setfield('hw_addr', $hw_addr);
201
202   my $error = 
203     $self->ut_numbern('svcnum')
204     || $self->ut_foreign_key('typenum', 'hardware_type', 'typenum')
205     || $self->ut_ip46n('ip_addr')
206     || $self->ut_alphan('hw_addr')
207     || $self->ut_alphan('serial')
208     || $self->ut_alphan('smartcard')
209     || $self->ut_foreign_keyn('statusnum', 'hardware_status', 'statusnum')
210     || $self->ut_anything('note')
211   ;
212   return $error if $error;
213
214   if ( !length($self->getfield('hw_addr')) 
215         and !length($self->getfield('serial')) ) {
216     return 'Serial number or hardware address required';
217   }
218  
219   $self->SUPER::check;
220 }
221
222 sub _check_duplicate {
223   my $self = shift;
224
225   return '' unless $conf->exists('global_unique-svc_hardware');
226
227   $self->lock_table;
228
229   if ( $self->hw_addr ) {
230     my @dup_mac;
231     @dup_mac = $self->find_duplicates('global', 'hw_addr');
232     if ( @dup_mac ) {
233       return "Hardware address in use (svcnum ".$dup_mac[0]->svcnum.")";
234     }
235   }
236
237   if ( $self->ip_addr ) {
238     my @dup_ip;
239     @dup_ip = $self->find_duplicates('global', 'ip_addr');
240     if ( @dup_ip ) {
241       return "IP address in use (svcnum ".$dup_ip[0]->svcnum.")";
242     }
243   }
244
245   if ( $self->serial ) {
246     my @dup_serial;
247     @dup_serial = $self->find_duplicates('global', 'typenum', 'serial');
248     if ( @dup_serial ) {
249       return "Serial number in use (svcnum ".$dup_serial[0]->svcnum.")";
250     }
251   }
252
253   '';
254 }
255
256 =item hardware_type
257
258 Returns the L<FS::hardware_type> object associated with this installation.
259
260 =cut
261
262 sub hardware_type {
263   my $self = shift;
264   return qsearchs('hardware_type', { 'typenum' => $self->typenum });
265 }
266
267 =item status_label
268
269 Returns the 'label' field of the L<FS::hardware_status> object associated 
270 with this installation.
271
272 =cut
273
274 sub status_label {
275   my $self = shift;
276   my $status = qsearchs('hardware_status', { 'statusnum' => $self->statusnum })
277     or return '';
278   $status->label;
279 }
280
281 =item display_hw_addr
282
283 Returns the 'hw_addr' field, formatted as a MAC address if the
284 'svc_hardware-check_mac_addr' option is enabled.
285
286 =cut
287
288 sub display_hw_addr {
289   my $self = shift;
290   ($conf->exists('svc_hardware-check_mac_addr') ? 
291     join(':', $self->hw_addr =~ /../g) : $self->hw_addr)
292 }
293
294 =back
295
296 =head1 SEE ALSO
297
298 L<FS::Record>, L<FS::svc_Common>, schema.html from the base documentation.
299
300 =cut
301
302 1;
303