This commit was generated by cvs2svn to compensate for changes in r11022,
[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 FS::Record qw( qsearch qsearchs );
6 use FS::hardware_type;
7 use FS::hardware_status;
8
9 =head1 NAME
10
11 FS::svc_hardware - Object methods for svc_hardware records
12
13 =head1 SYNOPSIS
14
15   use FS::svc_hardware;
16
17   $record = new FS::svc_hardware \%hash;
18   $record = new FS::svc_hardware { 'column' => 'value' };
19
20   $error = $record->insert;
21
22   $error = $new_record->replace($old_record);
23
24   $error = $record->delete;
25
26   $error = $record->check;
27
28 =head1 DESCRIPTION
29
30 An FS::svc_hardware object represents an equipment installation, such as 
31 a wireless broadband receiver, satellite antenna, or DVR.  FS::svc_hardware 
32 inherits from FS::svc_Common.
33
34 The following fields are currently supported:
35
36 =over 4
37
38 =item svcnum - Primary key
39
40 =item typenum - Device type number (see L<FS::hardware_type>)
41
42 =item ip_addr - IP address
43
44 =item hw_addr - Hardware address
45
46 =item serial - Serial number
47
48 =item statusnum - Service status (see L<FS::hardware_status>)
49
50 =item note - Installation notes: location on property, physical access, etc.
51
52 =back
53
54 =head1 METHODS
55
56 =over 4
57
58 =item new HASHREF
59
60 Creates a new svc_hardware object.
61
62 =cut
63
64 sub table { 'svc_hardware'; }
65
66 sub table_info {
67   my %opts = ( 'type' => 'text', 'disable_select' => 1 );
68   {
69     'name'           => 'Hardware', #?
70     'name_plural'    => 'Hardware',
71     'display_weight' => 59,
72     'cancel_weight'  => 86,
73     'fields' => {
74       'svcnum'    => { label => 'Service' },
75       'typenum'   => { label => 'Device type',
76                        type  => 'select-hardware',
77                        disable_select    => 1,
78                        disable_fixed     => 1,
79                        disable_default   => 1,
80                        disable_inventory => 1,
81                      },
82       'serial'    => { label => 'Serial number', %opts },
83       'hw_addr'   => { label => 'Hardware address', %opts },
84       'ip_addr'   => { label => 'IP address', %opts },
85       'statusnum' => { label => 'Service status', 
86                        type  => 'select',
87                        select_table => 'hardware_status',
88                        select_key   => 'statusnum',
89                        select_label => 'label',
90                        disable_inventory => 1,
91                      },
92       'note'      => { label => 'Installation notes', %opts },
93     }
94   }
95 }
96
97 sub search_sql {
98   my ($class, $string) = @_;
99   my @where = ();
100
101   my $ip = NetAddr::IP->new($string);
102   if ( $ip ) {
103     push @where, $class->search_sql_field('ip_addr', $ip->addr);
104   }
105   
106   if ( $string =~ /^(\w+)$/ ) {
107     push @where, 'LOWER(svc_hardware.serial) LIKE \'%'.lc($string).'%\'';
108   }
109
110   if ( $string =~ /^([0-9A-Fa-f]|\W)+$/ ) {
111     my $hex = uc($string);
112     $hex =~ s/\W//g;
113     push @where, 'svc_hardware.hw_addr LIKE \'%'.$hex.'%\'';
114   }
115
116   if ( @where ) {
117     '(' . join(' OR ', @where) . ')';
118   } else {
119     '1 = 0'; #false
120   }
121 }
122
123 sub label {
124   my $self = shift;
125   $self->serial || $self->hw_addr;
126 }
127
128 =item insert
129
130 Adds this record to the database.  If there is an error, returns the error,
131 otherwise returns false.
132
133 =item delete
134
135 Delete this record from the database.
136
137 =item replace OLD_RECORD
138
139 Replaces the OLD_RECORD with this one in the database.  If there is an error,
140 returns the error, otherwise returns false.
141
142 # the replace method can be inherited from FS::Record
143
144 =item check
145
146 Checks all fields to make sure this is a valid service.  If there is
147 an error, returns the error, otherwise returns false.  Called by the insert
148 and replace methods.
149
150 =cut
151
152 sub check {
153   my $self = shift;
154
155   my $x = $self->setfixed;
156   return $x unless ref $x;
157
158   my $hw_addr = $self->getfield('hw_addr');
159   $hw_addr = join('', split(/\W/, $hw_addr));
160   $self->setfield('hw_addr', $hw_addr);
161
162   my $error = 
163     $self->ut_numbern('svcnum')
164     || $self->ut_foreign_key('typenum', 'hardware_type', 'typenum')
165     || $self->ut_ip46n('ip_addr')
166     || $self->ut_hexn('hw_addr')
167     || $self->ut_alphan('serial')
168     || $self->ut_foreign_keyn('statusnum', 'hardware_status', 'statusnum')
169     || $self->ut_textn('note')
170   ;
171   return $error if $error;
172
173   if ( !length($self->getfield('hw_addr')) 
174         and !length($self->getfield('serial')) ) {
175     return 'Serial number or hardware address required';
176   }
177  
178   $self->SUPER::check;
179 }
180
181 =item hardware_type
182
183 Returns the L<FS::hardware_type> object associated with this installation.
184
185 =cut
186
187 sub hardware_type {
188   my $self = shift;
189   return qsearchs('hardware_type', { 'typenum' => $self->typenum });
190 }
191
192 =item status_label
193
194 Returns the 'label' field of the L<FS::hardware_status> object associated 
195 with this installation.
196
197 =cut
198
199 sub status_label {
200   my $self = shift;
201   my $status = qsearchs('hardware_status', { 'statusnum' => $self->statusnum })
202     or return '';
203   $status->label;
204 }
205
206
207 =back
208
209 =head1 SEE ALSO
210
211 L<FS::Record>, L<FS::svc_Common>, schema.html from the base documentation.
212
213 =cut
214
215 1;
216