#32873: Hardware Service Display of variable on interface
[freeside.git] / FS / FS / svc_hardware.pm
1 package FS::svc_hardware;
2 use base qw( FS::svc_Common );
3
4 use strict;
5 use vars qw( $conf );
6 use FS::Record qw( qsearchs ); #qsearch qsearchs );
7 use FS::hardware_status;
8 use FS::Conf;
9
10 FS::UID->install_callback(sub { $conf = FS::Conf->new; });
11
12 =head1 NAME
13
14 FS::svc_hardware - Object methods for svc_hardware records
15
16 =head1 SYNOPSIS
17
18   use FS::svc_hardware;
19
20   $record = new FS::svc_hardware \%hash;
21   $record = new FS::svc_hardware { 'column' => 'value' };
22
23   $error = $record->insert;
24
25   $error = $new_record->replace($old_record);
26
27   $error = $record->delete;
28
29   $error = $record->check;
30
31 =head1 DESCRIPTION
32
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.
36
37 The following fields are currently supported:
38
39 =over 4
40
41 =item svcnum - Primary key
42
43 =item typenum - Device type number (see L<FS::hardware_type>)
44
45 =item ip_addr - IP address
46
47 =item hw_addr - Hardware address
48
49 =item serial - Serial number
50
51 =item smartcard - Smartcard number, for devices that use a smartcard
52
53 =item statusnum - Service status (see L<FS::hardware_status>)
54
55 =item note - Installation notes: location on property, physical access, etc.
56
57 =back
58
59 =head1 METHODS
60
61 =over 4
62
63 =item new HASHREF
64
65 Creates a new svc_hardware object.
66
67 =cut
68
69 sub table { 'svc_hardware'; }
70
71 sub table_info {
72   my %opts = ( 'type' => 'text', 'disable_select' => 1 );
73   {
74     'name'           => 'Hardware', #?
75     'name_plural'    => 'Hardware',
76     'display_weight' => 59,
77     'cancel_weight'  => 86,
78     'fields' => {
79       'svcnum'    => { label => 'Service' },
80       'typenum'   => { label => 'Device type',
81                        type  => 'select-hardware',
82                        disable_select    => 1,
83                        disable_fixed     => 1,
84                        disable_default   => 1,
85                        disable_inventory => 1,
86                      },
87       'serial'    => { label => 'Serial number', %opts },
88       'hw_addr'   => { label => 'Hardware address', %opts },
89       'ip_addr'   => { label => 'IP address', %opts },
90       'smartcard' => { label => 'Smartcard #', %opts },
91       'statusnum' => { label => 'Service status', 
92                        type  => 'select',
93                        select_table => 'hardware_status',
94                        select_key   => 'statusnum',
95                        select_label => 'label',
96                        disable_inventory => 1,
97                      },
98       'note'      => { label => 'Installation notes', %opts },
99     }
100   }
101 }
102
103 sub search_sql {
104   my ($class, $string) = @_;
105   my @where = ();
106
107   if ( $string =~ /^[\d\.:]+$/ ) {
108     # if the string isn't an IP address, this will waste several seconds
109     # attempting a DNS lookup.  so try to filter those out.
110     my $ip = NetAddr::IP->new($string);
111     if ( $ip ) {
112       push @where, $class->search_sql_field('ip_addr', $ip->addr);
113     }
114   }
115   
116   if ( $string =~ /^(\w+)$/ ) {
117     push @where, 'LOWER(svc_hardware.serial) LIKE \'%'.lc($string).'%\'';
118   }
119
120   if ( $string =~ /^([0-9A-Fa-f]|\W)+$/ ) {
121     my $hex = uc($string);
122     $hex =~ s/\W//g;
123     push @where, 'svc_hardware.hw_addr LIKE \'%'.$hex.'%\'';
124   }
125
126   if ( @where ) {
127     '(' . join(' OR ', @where) . ')';
128   } else {
129     '1 = 0'; #false
130   }
131 }
132
133 sub label {
134   my $self = shift;
135   my @label = ();
136   if (my $type = $self->hardware_type) {
137     push @label, 'Type:' . $type->description;
138   }
139   if (my $ser = $self->serial) {
140     push @label, 'Serial#' . $ser;
141   }
142   if (my $mac = $self->display_hw_addr) {
143     push @label, 'MAC:'. $mac;
144   }
145   return join(', ', @label);
146 }
147
148 =item insert
149
150 Adds this record to the database.  If there is an error, returns the error,
151 otherwise returns false.
152
153 =item delete
154
155 Delete this record from the database.
156
157 =item replace OLD_RECORD
158
159 Replaces the OLD_RECORD with this one in the database.  If there is an error,
160 returns the error, otherwise returns false.
161
162 # the replace method can be inherited from FS::Record
163
164 =item check
165
166 Checks all fields to make sure this is a valid service.  If there is
167 an error, returns the error, otherwise returns false.  Called by the insert
168 and replace methods.
169
170 =cut
171
172 sub check {
173   my $self = shift;
174   my $conf = FS::Conf->new;
175
176   my $x = $self->setfixed;
177   return $x unless ref $x;
178
179   my $hw_addr = $self->getfield('hw_addr');
180   $hw_addr = join('', split(/[_\W]/, $hw_addr));
181   if ( $conf->exists('svc_hardware-check_mac_addr') ) {
182     $hw_addr = uc($hw_addr);
183     $hw_addr =~ /^[0-9A-F]{12}$/ 
184       or return "Illegal (MAC address) '".$self->getfield('hw_addr')."'";
185   }
186   $self->setfield('hw_addr', $hw_addr);
187
188   my $error = 
189     $self->ut_numbern('svcnum')
190     || $self->ut_foreign_key('typenum', 'hardware_type', 'typenum')
191     || $self->ut_ip46n('ip_addr')
192     || $self->ut_alphan('hw_addr')
193     || $self->ut_alphan('serial')
194     || $self->ut_alphan('smartcard')
195     || $self->ut_foreign_keyn('statusnum', 'hardware_status', 'statusnum')
196     || $self->ut_anything('note')
197   ;
198   return $error if $error;
199
200   if ( !length($self->getfield('hw_addr')) 
201         and !length($self->getfield('serial')) ) {
202     return 'Serial number or hardware address required';
203   }
204  
205   $self->SUPER::check;
206 }
207
208 =item hardware_type
209
210 Returns the L<FS::hardware_type> object associated with this installation.
211
212 =item status_label
213
214 Returns the 'label' field of the L<FS::hardware_status> object associated 
215 with this installation.
216
217 =cut
218
219 sub status_label {
220   my $self = shift;
221   my $status = qsearchs('hardware_status', { 'statusnum' => $self->statusnum })
222     or return '';
223   $status->label;
224 }
225
226 =item display_hw_addr
227
228 Returns the 'hw_addr' field, formatted as a MAC address if the
229 'svc_hardware-check_mac_addr' option is enabled.
230
231 =cut
232
233 sub display_hw_addr {
234   my $self = shift;
235   ($conf->exists('svc_hardware-check_mac_addr') ? 
236     join(':', $self->hw_addr =~ /../g) : $self->hw_addr)
237 }
238
239 =back
240
241 =head1 SEE ALSO
242
243 L<FS::Record>, L<FS::svc_Common>, schema.html from the base documentation.
244
245 =cut
246
247 1;
248