d9c3c464e3c88f66a4fc587c9a9a6f3f5093c28b
[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   my @label = ();
137   if (my $type = $self->hardware_type) {
138     push @label, 'Type:' . $type->description;
139   }
140   if (my $ser = $self->serial) {
141     push @label, 'Serial#' . $ser;
142   }
143   if (my $mac = $self->display_hw_addr) {
144     push @label, 'MAC:'. $mac;
145   }
146   return join(', ', @label);
147 }
148
149 =item insert
150
151 Adds this record to the database.  If there is an error, returns the error,
152 otherwise returns false.
153
154 =item delete
155
156 Delete this record from the database.
157
158 =item replace OLD_RECORD
159
160 Replaces the OLD_RECORD with this one in the database.  If there is an error,
161 returns the error, otherwise returns false.
162
163 # the replace method can be inherited from FS::Record
164
165 =item check
166
167 Checks all fields to make sure this is a valid service.  If there is
168 an error, returns the error, otherwise returns false.  Called by the insert
169 and replace methods.
170
171 =cut
172
173 sub check {
174   my $self = shift;
175   my $conf = FS::Conf->new;
176
177   my $x = $self->setfixed;
178   return $x unless ref $x;
179
180   my $hw_addr = $self->getfield('hw_addr');
181   $hw_addr = join('', split(/[_\W]/, $hw_addr));
182   if ( $conf->exists('svc_hardware-check_mac_addr') ) {
183     $hw_addr = uc($hw_addr);
184     $hw_addr =~ /^[0-9A-F]{12}$/ 
185       or return "Illegal (MAC address) '".$self->getfield('hw_addr')."'";
186   }
187   $self->setfield('hw_addr', $hw_addr);
188
189   my $error = 
190     $self->ut_numbern('svcnum')
191     || $self->ut_foreign_key('typenum', 'hardware_type', 'typenum')
192     || $self->ut_ip46n('ip_addr')
193     || $self->ut_alphan('hw_addr')
194     || $self->ut_alphan('serial')
195     || $self->ut_alphan('smartcard')
196     || $self->ut_foreign_keyn('statusnum', 'hardware_status', 'statusnum')
197     || $self->ut_anything('note')
198   ;
199   return $error if $error;
200
201   if ( !length($self->getfield('hw_addr')) 
202         and !length($self->getfield('serial')) ) {
203     return 'Serial number or hardware address required';
204   }
205  
206   $self->SUPER::check;
207 }
208
209 =item hardware_type
210
211 Returns the L<FS::hardware_type> object associated with this installation.
212
213 =cut
214
215 sub hardware_type {
216   my $self = shift;
217   return qsearchs('hardware_type', { 'typenum' => $self->typenum });
218 }
219
220 =item status_label
221
222 Returns the 'label' field of the L<FS::hardware_status> object associated 
223 with this installation.
224
225 =cut
226
227 sub status_label {
228   my $self = shift;
229   my $status = qsearchs('hardware_status', { 'statusnum' => $self->statusnum })
230     or return '';
231   $status->label;
232 }
233
234 =item display_hw_addr
235
236 Returns the 'hw_addr' field, formatted as a MAC address if the
237 'svc_hardware-check_mac_addr' option is enabled.
238
239 =cut
240
241 sub display_hw_addr {
242   my $self = shift;
243   ($conf->exists('svc_hardware-check_mac_addr') ? 
244     join(':', $self->hw_addr =~ /../g) : $self->hw_addr)
245 }
246
247 =back
248
249 =head1 SEE ALSO
250
251 L<FS::Record>, L<FS::svc_Common>, schema.html from the base documentation.
252
253 =cut
254
255 1;
256