summaryrefslogtreecommitdiff
path: root/FS/FS/svc_hardware.pm
blob: 7159f6dda8b6545d371e6ca8b85a28a1f06ece2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package FS::svc_hardware;
use base qw( FS::svc_Common );

use strict;
use vars qw( $conf );
use FS::Record qw( qsearchs ); #qsearch qsearchs );
use FS::hardware_status;
use FS::Conf;

FS::UID->install_callback(sub { $conf = FS::Conf->new; });

=head1 NAME

FS::svc_hardware - Object methods for svc_hardware records

=head1 SYNOPSIS

  use FS::svc_hardware;

  $record = new FS::svc_hardware \%hash;
  $record = new FS::svc_hardware { 'column' => 'value' };

  $error = $record->insert;

  $error = $new_record->replace($old_record);

  $error = $record->delete;

  $error = $record->check;

=head1 DESCRIPTION

An FS::svc_hardware object represents an equipment installation, such as 
a wireless broadband receiver, satellite antenna, or DVR.  FS::svc_hardware 
inherits from FS::svc_Common.

The following fields are currently supported:

=over 4

=item svcnum - Primary key

=item typenum - Device type number (see L<FS::hardware_type>)

=item ip_addr - IP address

=item hw_addr - Hardware address

=item serial - Serial number

=item smartcard - Smartcard number, for devices that use a smartcard

=item statusnum - Service status (see L<FS::hardware_status>)

=item note - Installation notes: location on property, physical access, etc.

=back

=head1 METHODS

=over 4

=item new HASHREF

Creates a new svc_hardware object.

=cut

sub table { 'svc_hardware'; }

sub table_info {
  my %opts = ( 'type' => 'text', 'disable_select' => 1 );
  {
    'name'           => 'Hardware', #?
    'name_plural'    => 'Hardware',
    'display_weight' => 59,
    'cancel_weight'  => 86,
    'manual_require' => 1,
    'fields' => {
      'svcnum'    => { label => 'Service' },
      'typenum'   => { label => 'Device type',
                       type  => 'select-hardware',
                       disable_select    => 1,
                       disable_fixed     => 1,
                       disable_default   => 1,
                       disable_inventory => 1,
                       required => 1,
                     },
      'serial'    => { label => 'Serial number', %opts },
      'hw_addr'   => { label => 'Hardware address', %opts },
      'ip_addr'   => { label => 'IP address', %opts },
      'smartcard' => { label => 'Smartcard #', %opts },
      'statusnum' => { label => 'Service status', 
                       type  => 'select',
                       select_table => 'hardware_status',
                       select_key   => 'statusnum',
                       select_label => 'label',
                       disable_inventory => 1,
                     },
      'note'      => { label => 'Installation notes', %opts },
    }
  }
}

sub search_sql {
  my ($class, $string) = @_;
  my @where = ();

  if ( $string =~ /^[\d\.:]+$/ ) {
    # if the string isn't an IP address, this will waste several seconds
    # attempting a DNS lookup.  so try to filter those out.
    my $ip = NetAddr::IP->new($string);
    if ( $ip ) {
      push @where, $class->search_sql_field('ip_addr', $ip->addr);
    }
  }
  
  if ( $string =~ /^(\w+)$/ ) {
    push @where, 'LOWER(svc_hardware.serial) LIKE \'%'.lc($string).'%\'';
  }

  if ( $string =~ /^([0-9A-Fa-f]|\W)+$/ ) {
    my $hex = uc($string);
    $hex =~ s/\W//g;
    push @where, 'svc_hardware.hw_addr LIKE \'%'.$hex.'%\'';
  }

  if ( @where ) {
    '(' . join(' OR ', @where) . ')';
  } else {
    '1 = 0'; #false
  }
}

sub label {
  my $self = shift;
  my $part_svc = $self->cust_svc->part_svc;
  my @label = ();
  if (my $type = $self->hardware_type) {
    my $typenum_label = $part_svc->part_svc_column('typenum');
    push @label, ( $typenum_label && $typenum_label->columnlabel || 'Type' ).
                 ':'. $type->description;
  }
  if (my $ser = $self->serial) {
    my $serial_label = $part_svc->part_svc_column('serial');
    push @label, ( $serial_label && $serial_label->columnlabel || 'Serial' ).
                 '#'. $ser;
  }
  if (my $mac = $self->display_hw_addr) {
    my $hw_addr_label = $part_svc->part_svc_column('hw_addr');
    push @label, ( $hw_addr_label && $hw_addr_label->columnlabel || 'MAC').
                 ':'. $mac;
  }
  return join(', ', @label);
}

=item insert

Adds this record to the database.  If there is an error, returns the error,
otherwise returns false.

=item delete

Delete this record from the database.

=item replace OLD_RECORD

Replaces the OLD_RECORD with this one in the database.  If there is an error,
returns the error, otherwise returns false.

# the replace method can be inherited from FS::Record

=item check

Checks all fields to make sure this is a valid service.  If there is
an error, returns the error, otherwise returns false.  Called by the insert
and replace methods.

=cut

sub check {
  my $self = shift;
  my $conf = FS::Conf->new;

  my $x = $self->setfixed;
  return $x unless ref $x;

  my $hw_addr = $self->getfield('hw_addr');
  $hw_addr = join('', split(/[_\W]/, $hw_addr));
  if ( $conf->exists('svc_hardware-check_mac_addr') ) {
    $hw_addr = uc($hw_addr);
    $hw_addr =~ /^[0-9A-F]{12}$/ 
      or return "Illegal (MAC address) '".$self->getfield('hw_addr')."'";
  } else {
    return "Illegal (hardware address) '".$self->getfield('hw_addr')."': ".
           "12 alphanumeric characters maximum"
      if length($hw_addr) > 12;
  }
  $self->setfield('hw_addr', $hw_addr);

  my $error = 
    $self->ut_numbern('svcnum')
    || $self->ut_foreign_key('typenum', 'hardware_type', 'typenum')
    || $self->ut_ip46n('ip_addr')
    || $self->ut_alphan('hw_addr')
    || $self->ut_alphan('serial')
    || $self->ut_alphan('smartcard')
    || $self->ut_foreign_keyn('statusnum', 'hardware_status', 'statusnum')
    || $self->ut_anything('note')
  ;
  return $error if $error;

  if ( !length($self->getfield('hw_addr')) 
        and !length($self->getfield('serial')) ) {
    return 'Serial number or hardware address required';
  }
 
  $self->SUPER::check;
}

#false laziness w/svc_cable
sub _check_duplicate {
  my $self = shift;

  # Not reliable checks because the table isn't locked, but that's why we have
  # unique indices.  These are just to give friendlier error messages.

  if ( $self->hw_addr ) {
    my @dup_mac;
    @dup_mac = $self->find_duplicates('global', 'hw_addr');
    if ( @dup_mac ) {
      return "Hardware address in use (svcnum ".$dup_mac[0]->svcnum.")";
    }
  }

  if ( $self->ip_addr ) {
    my @dup_ip;
    @dup_ip = $self->find_duplicates('global', 'ip_addr');
    if ( @dup_ip ) {
      return "IP address in use (svcnum ".$dup_ip[0]->svcnum.")";
    }
  }

  if ( $self->serial ) {
    my @dup_serial;
    @dup_serial = $self->find_duplicates('global', 'typenum', 'serial');
    if ( @dup_serial ) {
      return "Serial number in use (svcnum ".$dup_serial[0]->svcnum.")";
    }
  }

  '';
}

=item hardware_type

Returns the L<FS::hardware_type> object associated with this installation.

=item status_label

Returns the 'label' field of the L<FS::hardware_status> object associated 
with this installation.

=cut

sub status_label {
  my $self = shift;
  my $status = qsearchs('hardware_status', { 'statusnum' => $self->statusnum })
    or return '';
  $status->label;
}

=item display_hw_addr

Returns the 'hw_addr' field, formatted as a MAC address if the
'svc_hardware-check_mac_addr' option is enabled.

=cut

sub display_hw_addr {
  my $self = shift;
  ($conf->exists('svc_hardware-check_mac_addr') ? 
    join(':', $self->hw_addr =~ /../g) : $self->hw_addr)
}

sub _upgrade_data {

  require FS::Misc::FixIPFormat;
  FS::Misc::FixIPFormat::fix_bad_addresses_in_table(
      'svc_hardware', 'svcnum', 'ip_addr',
  );

  '';

}

=back

=head1 SEE ALSO

L<FS::Record>, L<FS::svc_Common>, schema.html from the base documentation.

=cut

1;