Merge branch 'master' of git.freeside.biz:/home/git/freeside
[freeside.git] / FS / FS / svc_fiber.pm
1 package FS::svc_fiber;
2
3 use strict;
4 use base qw( FS::svc_Common );
5 use FS::cust_svc;
6 use FS::hardware_type;
7 use FS::fiber_olt;
8 use FS::Record 'dbh';
9
10 =head1 NAME
11
12 FS::svc_fiber - Object methods for svc_fiber records
13
14 =head1 SYNOPSIS
15
16   use FS::table_name;
17
18   $record = new FS::table_name \%hash;
19   $record = new FS::table_name { 'column' => 'value' };
20
21   $error = $record->insert;
22
23   $error = $new_record->replace($old_record);
24
25   $error = $record->delete;
26
27   $error = $record->check;
28
29   $error = $record->suspend;
30
31   $error = $record->unsuspend;
32
33   $error = $record->cancel;
34
35 =head1 DESCRIPTION
36
37 An FS::svc_fiber object represents a fiber-to-the-premises service.  
38 FS::svc_fiber inherits from FS::svc_Common.  The following fields are
39 currently supported:
40
41 =over 4
42
43 =item svcnum - Primary key
44
45 =item oltnum - The Optical Line Terminal this service connects to (see
46 L<FS::fiber_olt>).
47
48 =item shelf - The shelf number on the OLT.
49
50 =item card - The card number on the OLT shelf.
51
52 =item olt_port - The port number on that card.
53
54 =item vlan - The VLAN number.
55
56 =item signal - Measured signal strength in dB.
57
58 =item speed_up - Measured uplink speed in Mbps.
59
60 =item speed_down - Measured downlink speed in Mbps.
61
62 =back
63
64 =head1 METHODS
65
66 =over 4
67
68 =item new HASHREF
69
70 Creates a new fiber service record.  To add it to the database, see L<"insert">.
71
72 =cut
73
74 sub table { 'svc_fiber'; }
75
76 sub table_info {
77   {
78     'name' => 'Fiber',
79     'name_plural' => 'Fiber', # really the name of the ACL
80     'longname_plural' => 'Fiber services',
81     'sorts' => [ 'oltnum', ],
82     'display_weight' => 74,
83     'cancel_weight'  => 74,
84     'fields' => {
85       'oltnum'        => {
86                           'label'        => 'OLT',
87                           'type'         => 'select',
88                           'select_table' => 'fiber_olt',
89                           'select_key'   => 'oltnum',
90                           'select_label' => 'oltname',
91                           'disable_inventory' => 1,
92                          },
93       'shelf'         => {
94                           'label' => 'Shelf',
95                           'type'  => 'text',
96                           'disable_inventory' => 1,
97                           'disable_select'    => 1,
98                          },
99       'card'          => {
100                           'label' => 'Card',
101                           'type'  => 'text',
102                           'disable_inventory' => 1,
103                           'disable_select'    => 1,
104                          },
105       'olt_port'      => {
106                           'label' => 'GPON port',
107                           'type'  => 'text',
108                           'disable_inventory' => 1,
109                           'disable_select'    => 1,
110                          },
111       # ONT stuff
112       'ont_id'        => {
113                           'label' => 'ONT #',
114                           'disable_select'    => 1,
115                          },
116       'ont_typenum'   => {
117                           'label' => 'Device type',
118                           'type'  => 'select-hardware',
119                           'disable_select'    => 1,
120                           'disable_default'   => 1,
121                           'disable_inventory' => 1,
122                          },
123       'ont_serial'    => {
124                           'label' => 'Serial number',
125                           'disable_select'    => 1,
126                          },
127       'ont_port'      => {
128                           'label' => 'GE port',
129                           'type'  => 'text',
130                           'disable_inventory' => 1,
131                           'disable_select'    => 1,
132                          },
133       'vlan'          => {
134                           'label' => 'VLAN #',
135                           'type'  => 'text',
136                           'disable_inventory' => 1,
137                           'disable_select'    => 1,
138                          },
139       'signal'        => {
140                           'label' => 'Signal strength (dB)',
141                           'type'  => 'text',
142                           'disable_inventory' => 1,
143                           'disable_select'    => 1,
144                          },
145       'speed_down'    => {
146                           'label' => 'Download speed (Mbps)',
147                           'type'  => 'text',
148                           'disable_inventory' => 1,
149                           'disable_select'    => 1,
150                          },
151       'speed_up'      => {
152                           'label' => 'Upload speed (Mbps)',
153                           'type'  => 'text',
154                           'disable_inventory' => 1,
155                           'disable_select'    => 1,
156                          },
157       'ont_install'   => {
158                           'label' => 'ONT install location',
159                           'type'  => 'text',
160                           'disable_inventory' => 1,
161                           'disable_select'    => 1,
162                          },
163     },
164   };
165 }
166
167 =item search_sql STRING
168
169 Class method which returns an SQL fragment to search for the given string.
170 For svc_fiber, STRING can be a full or partial ONT serial number.
171
172 =cut
173
174 #or something more complicated if necessary
175 sub search_sql {
176   my($class, $string) = @_;
177   $string = dbh->quote('%' . $string . '%');
178   "LOWER(svc_fiber.ont_serial) LIKE LOWER($string)";
179 }
180
181 =item label
182
183 Returns a description of this fiber service containing the OLT name and
184 port location, and the ONT serial number.
185
186 =cut
187
188 sub label {
189   my $self = shift;
190   $self->ont_serial . ' @ ' . $self->fiber_olt->oltname . ' ' .
191   join('-', $self->shelf, $self->card, $self->olt_port);
192 }
193
194 # nothing special for insert, delete, or replace
195
196 =item insert
197
198 Adds this record to the database.  If there is an error, returns the error,
199 otherwise returns false.
200
201 The additional fields pkgnum and svcpart (see L<FS::cust_svc>) should be 
202 defined.  An FS::cust_svc record will be created and inserted.
203
204 =item delete
205
206 Delete this record from the database.
207
208 =item replace OLD_RECORD
209
210 Replaces the OLD_RECORD with this one in the database.  If there is an error,
211 returns the error, otherwise returns false.
212
213 =item suspend
214
215 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
216
217 =item unsuspend
218
219 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
220
221 =item cancel
222
223 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
224
225 =item check
226
227 Checks all fields to make sure this is a valid example.  If there is
228 an error, returns the error, otherwise returns false.  Called by the insert
229 and repalce methods.
230
231 =cut
232
233 sub check {
234   my $self = shift;
235
236   my $x = $self->setfixed;
237   return $x unless ref($x);
238   my $part_svc = $x;
239
240   my $error =
241        $self->ut_number('oltnum')
242     || $self->ut_numbern('shelf')
243     || $self->ut_numbern('card')
244     || $self->ut_numbern('olt_port')
245     || $self->ut_number('ont_id')
246     || $self->ut_number('ont_typenum')
247     || $self->ut_alphan('ont_serial')
248     || $self->ut_alphan('ont_port')
249     || $self->ut_numbern('vlan')
250     || $self->ut_snumbern('signal')
251     || $self->ut_numbern('speed_up')
252     || $self->ut_numbern('speed_down')
253     || $self->ut_textn('ont_install')
254   ;
255   return $error if $error;
256
257   $self->SUPER::check;
258 }
259
260 =item ont_description
261
262 Returns the description of the ONT hardware, if there is one.
263
264 =cut
265
266 sub ont_description {
267   my $self = shift;
268   $self->ont_typenum ? $self->hardware_type->description : '';
269 }
270
271 =item search HASHREF
272
273 Returns a qsearch hash expression to search for parameters specified in
274 HASHREF.
275
276 Parameters are those in L<FS::svc_Common/search>, plus:
277
278 ont_typenum - the ONT L<FS::hardware_type> key
279
280 oltnum - the OLT L<FS::fiber_olt> key
281
282 shelf, card, olt_port - the OLT port location fields
283
284 vlan - the VLAN number
285
286 ont_serial - the ONT serial number
287
288 =cut
289
290 sub _search_svc {
291   my ($class, $params, $from, $where) = @_;
292
293   # make this simple: all of these are numeric fields, except that 0 means null
294   foreach my $field (qw(ont_typenum oltnum shelf olt_port card vlan)) {
295     if ( $params->{$field} =~ /^(\d+)$/ ) {
296       push @$where, "COALESCE($field,0) = $1";
297     }
298   }
299   if ( length($params->{ont_serial}) ) {
300     my $string = dbh->quote('%'.$params->{ont_serial}.'%');
301     push @$where, "LOWER(ont_serial) LIKE LOWER($string)";
302   }
303
304 }
305
306 #stub still needed under 4.x+
307
308 sub hardware_type {
309   my $self = shift;
310   $self->ont_typenum ? FS::hardware_type->by_key($self->ont_typenum) : '';
311 }
312
313 =back
314
315 =head1 SEE ALSO
316
317 L<FS::svc_Common>, L<FS::Record>, L<FS::cust_svc>, L<FS::part_svc>,
318 L<FS::cust_pkg>, schema.html from the base documentation.
319
320 =cut
321
322 1;
323