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