1 package FS::svc_hardware;
4 use base qw( FS::svc_Common );
6 use FS::Record qw( qsearch qsearchs );
8 use FS::hardware_status;
11 FS::UID->install_callback(sub { $conf = FS::Conf->new; });
15 FS::svc_hardware - Object methods for svc_hardware records
21 $record = new FS::svc_hardware \%hash;
22 $record = new FS::svc_hardware { 'column' => 'value' };
24 $error = $record->insert;
26 $error = $new_record->replace($old_record);
28 $error = $record->delete;
30 $error = $record->check;
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.
38 The following fields are currently supported:
42 =item svcnum - Primary key
44 =item typenum - Device type number (see L<FS::hardware_type>)
46 =item ip_addr - IP address
48 =item hw_addr - Hardware address
50 =item serial - Serial number
52 =item smartcard - Smartcard number, for devices that use a smartcard
54 =item statusnum - Service status (see L<FS::hardware_status>)
56 =item note - Installation notes: location on property, physical access, etc.
66 Creates a new svc_hardware object.
70 sub table { 'svc_hardware'; }
73 my %opts = ( 'type' => 'text', 'disable_select' => 1 );
75 'name' => 'Hardware', #?
76 'name_plural' => 'Hardware',
77 'display_weight' => 59,
78 'cancel_weight' => 86,
80 'svcnum' => { label => 'Service' },
81 'typenum' => { label => 'Device type',
82 type => 'select-hardware',
86 disable_inventory => 1,
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',
94 select_table => 'hardware_status',
95 select_key => 'statusnum',
96 select_label => 'label',
97 disable_inventory => 1,
99 'note' => { label => 'Installation notes', %opts },
105 my ($class, $string) = @_;
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);
113 push @where, $class->search_sql_field('ip_addr', $ip->addr);
117 if ( $string =~ /^(\w+)$/ ) {
118 push @where, 'LOWER(svc_hardware.serial) LIKE \'%'.lc($string).'%\'';
121 if ( $string =~ /^([0-9A-Fa-f]|\W)+$/ ) {
122 my $hex = uc($string);
124 push @where, 'svc_hardware.hw_addr LIKE \'%'.$hex.'%\'';
128 '(' . join(' OR ', @where) . ')';
137 if (my $type = $self->hardware_type) {
138 push @label, 'Type:' . $type->description;
140 if (my $ser = $self->serial) {
141 push @label, 'Serial#' . $ser;
143 if (my $mac = $self->display_hw_addr) {
144 push @label, 'MAC:'. $mac;
146 return join(', ', @label);
151 Adds this record to the database. If there is an error, returns the error,
152 otherwise returns false.
156 Delete this record from the database.
158 =item replace OLD_RECORD
160 Replaces the OLD_RECORD with this one in the database. If there is an error,
161 returns the error, otherwise returns false.
163 # the replace method can be inherited from FS::Record
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
175 my $conf = FS::Conf->new;
177 my $x = $self->setfixed;
178 return $x unless ref $x;
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')."'";
187 $self->setfield('hw_addr', $hw_addr);
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')
199 return $error if $error;
201 if ( !length($self->getfield('hw_addr'))
202 and !length($self->getfield('serial')) ) {
203 return 'Serial number or hardware address required';
211 Returns the L<FS::hardware_type> object associated with this installation.
217 return qsearchs('hardware_type', { 'typenum' => $self->typenum });
222 Returns the 'label' field of the L<FS::hardware_status> object associated
223 with this installation.
229 my $status = qsearchs('hardware_status', { 'statusnum' => $self->statusnum })
234 =item display_hw_addr
236 Returns the 'hw_addr' field, formatted as a MAC address if the
237 'svc_hardware-check_mac_addr' option is enabled.
241 sub display_hw_addr {
243 ($conf->exists('svc_hardware-check_mac_addr') ?
244 join(':', $self->hw_addr =~ /../g) : $self->hw_addr)
251 L<FS::Record>, L<FS::svc_Common>, schema.html from the base documentation.