delete fees, RT#81713
[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     'manual_require' => 1,
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                        required => 1,
88                      },
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', 
94                        type  => 'select',
95                        select_table => 'hardware_status',
96                        select_key   => 'statusnum',
97                        select_label => 'label',
98                        disable_inventory => 1,
99                      },
100       'note'      => { label => 'Installation notes', %opts },
101     }
102   }
103 }
104
105 sub search_sql {
106   my ($class, $string) = @_;
107   my @where = ();
108
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);
113     if ( $ip ) {
114       push @where, $class->search_sql_field('ip_addr', $ip->addr);
115     }
116   }
117   
118   if ( $string =~ /^(\w+)$/ ) {
119     push @where, 'LOWER(svc_hardware.serial) LIKE \'%'.lc($string).'%\'';
120   }
121
122   if ( $string =~ /^([0-9A-Fa-f]|\W)+$/ ) {
123     my $hex = uc($string);
124     $hex =~ s/\W//g;
125     push @where, 'svc_hardware.hw_addr LIKE \'%'.$hex.'%\'';
126   }
127
128   if ( @where ) {
129     '(' . join(' OR ', @where) . ')';
130   } else {
131     '1 = 0'; #false
132   }
133 }
134
135 sub label {
136   my $self = shift;
137   my $part_svc = $self->cust_svc->part_svc;
138   my @label = ();
139   if (my $type = $self->hardware_type) {
140     my $typenum_label = $part_svc->part_svc_column('typenum');
141     push @label, ( $typenum_label && $typenum_label->columnlabel || 'Type:' ).
142                  $type->description;
143   }
144   if (my $ser = $self->serial) {
145     my $serial_label = $part_svc->part_svc_column('serial');
146     push @label, ( $serial_label && $serial_label->columnlabel || 'Serial#' ).
147                  $ser;
148   }
149   if (my $mac = $self->display_hw_addr) {
150     my $hw_addr_label = $part_svc->part_svc_column('hw_addr');
151     push @label, ( $hw_addr_label && $hw_addr_label->columnlabel || 'MAC:').
152     $mac;
153   }
154   return join(', ', @label);
155 }
156
157 =item insert
158
159 Adds this record to the database.  If there is an error, returns the error,
160 otherwise returns false.
161
162 =item delete
163
164 Delete this record from the database.
165
166 =item replace OLD_RECORD
167
168 Replaces the OLD_RECORD with this one in the database.  If there is an error,
169 returns the error, otherwise returns false.
170
171 # the replace method can be inherited from FS::Record
172
173 =item check
174
175 Checks all fields to make sure this is a valid service.  If there is
176 an error, returns the error, otherwise returns false.  Called by the insert
177 and replace methods.
178
179 =cut
180
181 sub check {
182   my $self = shift;
183   my $conf = FS::Conf->new;
184
185   my $x = $self->setfixed;
186   return $x unless ref $x;
187
188   my $hw_addr = $self->getfield('hw_addr');
189   $hw_addr = join('', split(/[_\W]/, $hw_addr));
190   if ( $conf->exists('svc_hardware-check_mac_addr') ) {
191     $hw_addr = uc($hw_addr);
192     $hw_addr =~ /^[0-9A-F]{12}$/ 
193       or return "Illegal (MAC address) '".$self->getfield('hw_addr')."'";
194   }
195   $self->setfield('hw_addr', $hw_addr);
196
197   my $error = 
198     $self->ut_numbern('svcnum')
199     || $self->ut_foreign_key('typenum', 'hardware_type', 'typenum')
200     || $self->ut_ip46n('ip_addr')
201     || $self->ut_alphan('hw_addr')
202     || $self->ut_alphan('serial')
203     || $self->ut_alphan('smartcard')
204     || $self->ut_foreign_keyn('statusnum', 'hardware_status', 'statusnum')
205     || $self->ut_anything('note')
206   ;
207   return $error if $error;
208
209   if ( !length($self->getfield('hw_addr')) 
210         and !length($self->getfield('serial')) ) {
211     return 'Serial number or hardware address required';
212   }
213  
214   $self->SUPER::check;
215 }
216
217 #false laziness w/svc_cable
218 sub _check_duplicate {
219   my $self = shift;
220
221   # Not reliable checks because the table isn't locked, but that's why we have
222   # unique indices.  These are just to give friendlier error messages.
223
224   if ( $self->hw_addr ) {
225     my @dup_mac;
226     @dup_mac = $self->find_duplicates('global', 'hw_addr');
227     if ( @dup_mac ) {
228       return "Hardware address in use (svcnum ".$dup_mac[0]->svcnum.")";
229     }
230   }
231
232   if ( $self->ip_addr ) {
233     my @dup_ip;
234     @dup_ip = $self->find_duplicates('global', 'ip_addr');
235     if ( @dup_ip ) {
236       return "IP address in use (svcnum ".$dup_ip[0]->svcnum.")";
237     }
238   }
239
240   if ( $self->serial ) {
241     my @dup_serial;
242     @dup_serial = $self->find_duplicates('global', 'typenum', 'serial');
243     if ( @dup_serial ) {
244       return "Serial number in use (svcnum ".$dup_serial[0]->svcnum.")";
245     }
246   }
247
248   '';
249 }
250
251 =item hardware_type
252
253 Returns the L<FS::hardware_type> object associated with this installation.
254
255 =item status_label
256
257 Returns the 'label' field of the L<FS::hardware_status> object associated 
258 with this installation.
259
260 =cut
261
262 sub status_label {
263   my $self = shift;
264   my $status = qsearchs('hardware_status', { 'statusnum' => $self->statusnum })
265     or return '';
266   $status->label;
267 }
268
269 =item display_hw_addr
270
271 Returns the 'hw_addr' field, formatted as a MAC address if the
272 'svc_hardware-check_mac_addr' option is enabled.
273
274 =cut
275
276 sub display_hw_addr {
277   my $self = shift;
278   ($conf->exists('svc_hardware-check_mac_addr') ? 
279     join(':', $self->hw_addr =~ /../g) : $self->hw_addr)
280 }
281
282 sub _upgrade_data {
283
284   require FS::Misc::FixIPFormat;
285   FS::Misc::FixIPFormat::fix_bad_addresses_in_table(
286       'svc_hardware', 'svcnum', 'ip_addr',
287   );
288
289   '';
290
291 }
292
293 =back
294
295 =head1 SEE ALSO
296
297 L<FS::Record>, L<FS::svc_Common>, schema.html from the base documentation.
298
299 =cut
300
301 1;