Merge branch 'master' of git.freeside.biz:/home/git/freeside
[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   my $ip = NetAddr::IP->new($string);
109   if ( $ip ) {
110     push @where, $class->search_sql_field('ip_addr', $ip->addr);
111   }
112   
113   if ( $string =~ /^(\w+)$/ ) {
114     push @where, 'LOWER(svc_hardware.serial) LIKE \'%'.lc($string).'%\'';
115   }
116
117   if ( $string =~ /^([0-9A-Fa-f]|\W)+$/ ) {
118     my $hex = uc($string);
119     $hex =~ s/\W//g;
120     push @where, 'svc_hardware.hw_addr LIKE \'%'.$hex.'%\'';
121   }
122
123   if ( @where ) {
124     '(' . join(' OR ', @where) . ')';
125   } else {
126     '1 = 0'; #false
127   }
128 }
129
130 sub label {
131   my $self = shift;
132   $self->serial || $self->display_hw_addr;
133 }
134
135 =item insert
136
137 Adds this record to the database.  If there is an error, returns the error,
138 otherwise returns false.
139
140 =item delete
141
142 Delete this record from the database.
143
144 =item replace OLD_RECORD
145
146 Replaces the OLD_RECORD with this one in the database.  If there is an error,
147 returns the error, otherwise returns false.
148
149 # the replace method can be inherited from FS::Record
150
151 =item check
152
153 Checks all fields to make sure this is a valid service.  If there is
154 an error, returns the error, otherwise returns false.  Called by the insert
155 and replace methods.
156
157 =cut
158
159 sub check {
160   my $self = shift;
161   my $conf = FS::Conf->new;
162
163   my $x = $self->setfixed;
164   return $x unless ref $x;
165
166   my $hw_addr = $self->getfield('hw_addr');
167   $hw_addr = join('', split(/[_\W]/, $hw_addr));
168   if ( $conf->exists('svc_hardware-check_mac_addr') ) {
169     $hw_addr = uc($hw_addr);
170     $hw_addr =~ /^[0-9A-F]{12}$/ 
171       or return "Illegal (MAC address) '".$self->getfield('hw_addr')."'";
172   }
173   $self->setfield('hw_addr', $hw_addr);
174
175   my $error = 
176     $self->ut_numbern('svcnum')
177     || $self->ut_foreign_key('typenum', 'hardware_type', 'typenum')
178     || $self->ut_ip46n('ip_addr')
179     || $self->ut_alphan('hw_addr')
180     || $self->ut_alphan('serial')
181     || $self->ut_alphan('smartcard')
182     || $self->ut_foreign_keyn('statusnum', 'hardware_status', 'statusnum')
183     || $self->ut_anything('note')
184   ;
185   return $error if $error;
186
187   if ( !length($self->getfield('hw_addr')) 
188         and !length($self->getfield('serial')) ) {
189     return 'Serial number or hardware address required';
190   }
191  
192   $self->SUPER::check;
193 }
194
195 =item hardware_type
196
197 Returns the L<FS::hardware_type> object associated with this installation.
198
199 =cut
200
201 sub hardware_type {
202   my $self = shift;
203   return qsearchs('hardware_type', { 'typenum' => $self->typenum });
204 }
205
206 =item status_label
207
208 Returns the 'label' field of the L<FS::hardware_status> object associated 
209 with this installation.
210
211 =cut
212
213 sub status_label {
214   my $self = shift;
215   my $status = qsearchs('hardware_status', { 'statusnum' => $self->statusnum })
216     or return '';
217   $status->label;
218 }
219
220 =item display_hw_addr
221
222 Returns the 'hw_addr' field, formatted as a MAC address if the
223 'svc_hardware-check_mac_addr' option is enabled.
224
225 =cut
226
227 sub display_hw_addr {
228   my $self = shift;
229   ($conf->exists('svc_hardware-check_mac_addr') ? 
230     join(':', $self->hw_addr =~ /../g) : $self->hw_addr)
231 }
232
233 =back
234
235 =head1 SEE ALSO
236
237 L<FS::Record>, L<FS::svc_Common>, schema.html from the base documentation.
238
239 =cut
240
241 1;
242