1 package FS::svc_hardware;
2 use base qw( FS::svc_Common );
6 use FS::Record qw( qsearchs ); #qsearch qsearchs );
7 use FS::hardware_status;
10 FS::UID->install_callback(sub { $conf = FS::Conf->new; });
14 FS::svc_hardware - Object methods for svc_hardware records
20 $record = new FS::svc_hardware \%hash;
21 $record = new FS::svc_hardware { 'column' => 'value' };
23 $error = $record->insert;
25 $error = $new_record->replace($old_record);
27 $error = $record->delete;
29 $error = $record->check;
33 An FS::svc_hardware object represents an equipment installation, such as
34 a wireless broadband receiver, satellite antenna, or DVR. FS::svc_hardware
35 inherits from FS::svc_Common.
37 The following fields are currently supported:
41 =item svcnum - Primary key
43 =item typenum - Device type number (see L<FS::hardware_type>)
45 =item ip_addr - IP address
47 =item hw_addr - Hardware address
49 =item serial - Serial number
51 =item smartcard - Smartcard number, for devices that use a smartcard
53 =item statusnum - Service status (see L<FS::hardware_status>)
55 =item note - Installation notes: location on property, physical access, etc.
65 Creates a new svc_hardware object.
69 sub table { 'svc_hardware'; }
72 my %opts = ( 'type' => 'text', 'disable_select' => 1 );
74 'name' => 'Hardware', #?
75 'name_plural' => 'Hardware',
76 'display_weight' => 59,
77 'cancel_weight' => 86,
78 'manual_require' => 1,
80 'svcnum' => { label => 'Service' },
81 'typenum' => { label => 'Device type',
82 type => 'select-hardware',
86 disable_inventory => 1,
89 'serial' => { label => 'Serial number', %opts },
90 'hw_addr' => { label => 'Hardware address', %opts },
91 'ip_addr' => { label => 'IP address', %opts },
92 'smartcard' => { label => 'Smartcard #', %opts },
93 'statusnum' => { label => 'Service status',
95 select_table => 'hardware_status',
96 select_key => 'statusnum',
97 select_label => 'label',
98 disable_inventory => 1,
100 'note' => { label => 'Installation notes', %opts },
106 my ($class, $string) = @_;
109 if ( $string =~ /^[\d\.:]+$/ ) {
110 # if the string isn't an IP address, this will waste several seconds
111 # attempting a DNS lookup. so try to filter those out.
112 my $ip = NetAddr::IP->new($string);
114 push @where, $class->search_sql_field('ip_addr', $ip->addr);
118 if ( $string =~ /^(\w+)$/ ) {
119 push @where, 'LOWER(svc_hardware.serial) LIKE \'%'.lc($string).'%\'';
122 if ( $string =~ /^([0-9A-Fa-f]|\W)+$/ ) {
123 my $hex = uc($string);
125 push @where, 'svc_hardware.hw_addr LIKE \'%'.$hex.'%\'';
129 '(' . join(' OR ', @where) . ')';
138 if (my $type = $self->hardware_type) {
139 push @label, 'Type:' . $type->description;
141 if (my $ser = $self->serial) {
142 push @label, 'Serial#' . $ser;
144 if (my $mac = $self->display_hw_addr) {
145 push @label, 'MAC:'. $mac;
147 return join(', ', @label);
152 Adds this record to the database. If there is an error, returns the error,
153 otherwise returns false.
157 Delete this record from the database.
159 =item replace OLD_RECORD
161 Replaces the OLD_RECORD with this one in the database. If there is an error,
162 returns the error, otherwise returns false.
164 # the replace method can be inherited from FS::Record
168 Checks all fields to make sure this is a valid service. If there is
169 an error, returns the error, otherwise returns false. Called by the insert
176 my $conf = FS::Conf->new;
178 my $x = $self->setfixed;
179 return $x unless ref $x;
181 my $hw_addr = $self->getfield('hw_addr');
182 $hw_addr = join('', split(/[_\W]/, $hw_addr));
183 if ( $conf->exists('svc_hardware-check_mac_addr') ) {
184 $hw_addr = uc($hw_addr);
185 $hw_addr =~ /^[0-9A-F]{12}$/
186 or return "Illegal (MAC address) '".$self->getfield('hw_addr')."'";
188 $self->setfield('hw_addr', $hw_addr);
191 $self->ut_numbern('svcnum')
192 || $self->ut_foreign_key('typenum', 'hardware_type', 'typenum')
193 || $self->ut_ip46n('ip_addr')
194 || $self->ut_alphan('hw_addr')
195 || $self->ut_alphan('serial')
196 || $self->ut_alphan('smartcard')
197 || $self->ut_foreign_keyn('statusnum', 'hardware_status', 'statusnum')
198 || $self->ut_anything('note')
200 return $error if $error;
202 if ( !length($self->getfield('hw_addr'))
203 and !length($self->getfield('serial')) ) {
204 return 'Serial number or hardware address required';
212 Returns the L<FS::hardware_type> object associated with this installation.
216 Returns the 'label' field of the L<FS::hardware_status> object associated
217 with this installation.
223 my $status = qsearchs('hardware_status', { 'statusnum' => $self->statusnum })
228 =item display_hw_addr
230 Returns the 'hw_addr' field, formatted as a MAC address if the
231 'svc_hardware-check_mac_addr' option is enabled.
235 sub display_hw_addr {
237 ($conf->exists('svc_hardware-check_mac_addr') ?
238 join(':', $self->hw_addr =~ /../g) : $self->hw_addr)
245 L<FS::Record>, L<FS::svc_Common>, schema.html from the base documentation.