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