5b81a9c2b69bdfea4652c523971c71c7862eca7d
[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   '(' . join(' OR ', @where) . ')';
116 }
117
118 sub label {
119   my $self = shift;
120   $self->serial || $self->hw_addr;
121 }
122
123 =item insert
124
125 Adds this record to the database.  If there is an error, returns the error,
126 otherwise returns false.
127
128 =item delete
129
130 Delete this record from the database.
131
132 =item replace OLD_RECORD
133
134 Replaces the OLD_RECORD with this one in the database.  If there is an error,
135 returns the error, otherwise returns false.
136
137 # the replace method can be inherited from FS::Record
138
139 =item check
140
141 Checks all fields to make sure this is a valid service.  If there is
142 an error, returns the error, otherwise returns false.  Called by the insert
143 and replace methods.
144
145 =cut
146
147 sub check {
148   my $self = shift;
149
150   my $x = $self->setfixed;
151   return $x unless ref $x;
152
153   my $hw_addr = $self->getfield('hw_addr');
154   $hw_addr = join('', split(/\W/, $hw_addr));
155   $self->setfield('hw_addr', $hw_addr);
156
157   my $error = 
158     $self->ut_numbern('svcnum')
159     || $self->ut_foreign_key('typenum', 'hardware_type', 'typenum')
160     || $self->ut_ip46n('ip_addr')
161     || $self->ut_hexn('hw_addr')
162     || $self->ut_alphan('serial')
163     || $self->ut_foreign_keyn('statusnum', 'hardware_status', 'statusnum')
164     || $self->ut_textn('note')
165   ;
166   return $error if $error;
167
168   if ( !length($self->getfield('hw_addr')) 
169         and !length($self->getfield('serial')) ) {
170     return 'Serial number or hardware address required';
171   }
172  
173   $self->SUPER::check;
174 }
175
176 =item hardware_type
177
178 Returns the L<FS::hardware_type> object associated with this installation.
179
180 =cut
181
182 sub hardware_type {
183   my $self = shift;
184   return qsearchs('hardware_type', { 'typenum' => $self->typenum });
185 }
186
187 =item status_label
188
189 Returns the 'label' field of the L<FS::hardware_status> object associated 
190 with this installation.
191
192 =cut
193
194 sub status_label {
195   my $self = shift;
196   my $status = qsearchs('hardware_status', { 'statusnum' => $self->statusnum })
197     or return '';
198   $status->label;
199 }
200
201
202 =back
203
204 =head1 SEE ALSO
205
206 L<FS::Record>, L<FS::svc_Common>, schema.html from the base documentation.
207
208 =cut
209
210 1;
211