delete fees, RT#81713
[freeside.git] / FS / FS / svc_dsl.pm
1 package FS::svc_dsl;
2 use base qw(FS::Password_Mixin
3             FS::svc_Common);
4
5 use strict;
6 use vars qw( $conf $DEBUG $me );
7 use FS::UID;
8 use FS::Record qw( qsearch qsearchs dbh );
9 use FS::svc_Common;
10 use FS::dsl_note;
11 use FS::qual;
12
13 $DEBUG = 0;
14 $me = '[FS::svc_dsl]';
15
16 FS::UID->install_callback( sub { 
17   $conf = new FS::Conf;
18 }
19 );
20
21 =head1 NAME
22
23 FS::svc_dsl - Object methods for svc_dsl records
24
25 =head1 SYNOPSIS
26
27   use FS::svc_dsl;
28
29   $record = new FS::svc_dsl \%hash;
30   $record = new FS::svc_dsl { 'column' => 'value' };
31
32   $error = $record->insert;
33
34   $error = $new_record->replace($old_record);
35
36   $error = $record->delete;
37
38   $error = $record->check;
39   
40   $error = $record->suspend;
41
42   $error = $record->unsuspend;
43
44   $error = $record->cancel;
45
46 =head1 DESCRIPTION
47
48 An FS::svc_dsl object represents a DSL service.  FS::svc_dsl inherits from
49 FS::svc_Common.  The following fields are currently supported:
50
51 =over 4
52
53 =item svcnum
54
55 Primary key (assigned automatcially for new DSL))
56
57 =item pushed
58
59 Time DSL order pushed to vendor/telco, if applicable
60
61 =item desired_due_date
62
63 Desired Due Date
64
65 =item due_date
66
67 Due Date
68
69 =item vendor_order_id
70
71 Vendor/telco DSL order #
72
73 =item vendor_order_type
74
75 Vendor/telco DSL order type (e.g. (M)ove, (A)dd, (C)hange, (D)elete, or similar)
76
77 =item vendor_order_status
78
79 Vendor/telco DSL order status (e.g. (N)ew, (A)ssigned, (R)ejected, (M)revised,
80 (C)ompleted, (X)cancelled, or similar)
81
82 =item first
83
84 End-user first name
85
86 =item last
87
88 End-user last name
89
90 =item company
91
92 End-user company name
93
94 =item phonenum
95
96 DSL Telephone Number
97
98 =item gateway_access_number
99
100 Gateway access number, if different
101
102 =item loop_type
103
104 Loop-type - vendor/telco-specific
105
106 =item local_voice_provider
107
108 Local Voice Provider's name
109
110 =item circuitnum
111
112 Circuit #
113
114 =item vpi
115
116 =item vci
117
118 =item rate_band
119
120 Rate Band
121
122 =item isp_chg
123
124 =item isp_prev
125
126 =item staticips
127
128 =item vendor_qual_id
129
130 Ikano-specific fields, do not use otherwise
131
132 =item username
133
134 if outsourced PPPoE/RADIUS, username
135
136 =item password
137
138 if outsourced PPPoE/RADIUS, password
139
140 =item monitored
141
142 Order is monitored (auto-pull/sync), either Y or blank
143
144 =item last_pull
145
146 time of last data pull from vendor/telco
147
148
149 =back
150
151 =head1 METHODS
152
153 =over 4
154
155 =item new HASHREF
156
157 Creates a new DSL.  To add the DSL to the database, see L<"insert">.
158
159 Note that this stores the hash reference, not a distinct copy of the hash it
160 points to.  You can ask the object for a copy with the I<hash> method.
161
162 =cut
163
164 # the new method can be inherited from FS::Record, if a table method is defined
165
166 sub table_info {
167     my %dis1 = ( disable_default=>1, disable_fixed=>1, disable_inventory=>1, disable_select=>1 );
168     my %dis2 = ( disable_inventory=>1, disable_select=>1 );
169
170     {
171         'name' => 'DSL',
172         'name_plural' => 'DSLs',
173         'lcname_plural' => 'DSLs',
174         'sorts' => [ 'phonenum' ],
175         'display_weight' => 55,
176         'cancel_weight' => 75,
177         'ip_field' => 'staticips',
178         'fields' => {
179             'pushed'                => { label => 'Pushed', 
180                                          type  => 'disabled' },
181             'desired_due_date'      => { label => 'Desired Due Date', %dis2, },
182             'due_date'              => { label => 'Due Date', %dis2, },
183             'vendor_order_id'       => { label => 'Vendor Order ID', %dis2, },
184             'vendor_qual_id'        => { label => 'Vendor Qualification ID', 
185                                          type => 'disabled' },
186             'vendor_order_type'     => { label => 'Vendor Order Type',
187                                          disable_inventory => 1, },
188             'vendor_order_status'   => { label => 'Vendor Order Status',
189                                          disable_inventory => 1, },
190             'first'                 => { label => 'First Name', %dis2, },
191             'last'                  => { label => 'Last Name', %dis2, },
192             'company'               => { label => 'Company Name', %dis2, },
193             'phonenum'              => { label => 'Service Telephone Number', },
194             'gateway_access_number' => { label => 'Gateway Access Number'.
195                                                   ' (if different)', },
196             'loop_type'             => { label => 'Loop Type',
197                                          disable_inventory => 1, },
198             'local_voice_provider'  => { label => 'Local Voice Provider',
199                                          disable_inventory => 1, },
200             'circuitnum'            => { label => 'Circuit #',        },
201             'rate_band'             => { label => 'Rate Band',
202                                          disable_inventory => 1, },
203             'vpi'                   => { label => 'VPI',
204                                          disable_inventory => 1 },
205             'vci'                   => { label => 'VCI',
206                                          disable_inventory => 1 },
207             'isp_chg'               => { label => 'ISP Changing?', 
208                                          type  => 'checkbox',
209                                          %dis2, },
210             'isp_prev'              => { label => 'Current or Previous ISP',
211                                          disable_inventory => 1, },
212             'username'              => { label => 'PPPoE Username',
213                                          type  => 'text', },
214             'password'              => { label => 'PPPoE Password', %dis2 },
215             'staticips'             => { label => 'Static IPs', %dis1 },
216             'monitored'             => { label => 'Monitored', 
217                                          type => 'checkbox', %dis2 },
218             'last_pull'             => { label => 'Last Pull',
219                                          type  => 'disabled' },
220         },
221     };
222 }
223
224 sub table { 'svc_dsl'; }
225
226 sub label {
227    my $self = shift;
228    return $self->phonenum if $self->phonenum;
229    return $self->username if $self->username;
230    return $self->vendor_order_id if $self->vendor_order_id;
231    return $self->svcnum;
232 }
233
234 =item notes
235
236 Returns the set of FS::dsl_notes associated with this service
237
238 =cut 
239 sub notes {
240   my $self = shift;
241   qsearch( 'dsl_note', { 'svcnum' => $self->svcnum } );
242 }
243
244 =item insert
245
246 Adds this record to the database.  If there is an error, returns the error,
247 otherwise returns false.
248
249 =cut
250
251 sub insert {
252   my $self = shift;
253   my $dbh = dbh;
254   my $oldAutoCommit = $FS::UID::AutoCommit;
255   local $FS::UID::AutoCommit = 0;
256
257   my $error = $self->SUPER::insert(@_);
258   if ( length($self->password) ) {
259     $error ||= $self->insert_password_history;
260   }
261
262   if ( $error ) {
263     $dbh->rollback if $oldAutoCommit;
264     return $error;
265   }
266
267   $dbh->commit if $oldAutoCommit;
268   '';
269 }
270
271 =item delete
272
273 Delete this record from the database.
274
275 =cut
276
277 # the delete method can be inherited from FS::Record
278
279 =item replace OLD_RECORD
280
281 Replaces the OLD_RECORD with this one in the database.  If there is an error,
282 returns the error, otherwise returns false.
283
284 =cut
285
286 sub replace {
287   my $new = shift;
288   my $old = shift || $new->replace_old;
289   my $dbh = dbh;
290   my $oldAutoCommit = $FS::UID::AutoCommit;
291   local $FS::UID::AutoCommit = 0;
292
293   my $error = $new->SUPER::replace($old, @_);
294   if ( $old->password ne $new->password ) {
295     $error ||= $new->insert_password_history;
296   }
297
298   if ( $error ) {
299     $dbh->rollback if $oldAutoCommit;
300     return $error;
301   }
302
303   $dbh->commit if $oldAutoCommit;
304   '';
305 }
306
307 # the replace method can be inherited from FS::Record
308
309 =item check
310
311 Checks all fields to make sure this is a valid DSL.  If there is
312 an error, returns the error, otherwise returns false.  Called by the insert
313 and replace methods.
314
315 =cut
316
317 # the check method should currently be supplied - FS::Record contains some
318 # data checking routines
319
320 sub check {
321   my $self = shift;
322
323   my $error = 
324     $self->ut_numbern('svcnum')
325     || $self->ut_numbern('pushed')
326     || $self->ut_numbern('desired_due_date')
327     || $self->ut_numbern('due_date')
328     || $self->ut_textn('vendor_order_id')
329     || $self->ut_textn('vendor_qual_id')
330     || $self->ut_alphan('vendor_order_type')
331     || $self->ut_alphan('vendor_order_status')
332     || $self->ut_textn('first')
333     || $self->ut_textn('last')
334     || $self->ut_textn('company')
335     || $self->ut_numbern('phonenum')
336     || $self->ut_numbern('gateway_access_number')
337     || $self->ut_alphasn('loop_type')
338     || $self->ut_textn('local_voice_provider')
339     || $self->ut_textn('circuitnum')
340     || $self->ut_textn('rate_band')
341     || $self->ut_numbern('vpi')
342     || $self->ut_numbern('vci')
343     || $self->ut_alphan('isp_chg')
344     || $self->ut_textn('isp_prev')
345     || $self->ut_textn('username')
346     || $self->ut_textn('password')
347     || $self->ut_textn('staticips')
348     || $self->ut_enum('monitored',    [ '', 'Y' ])
349     || $self->ut_numbern('last_pull')
350   ;
351   return $error if $error;
352
353   $self->gateway_access_number('')
354     if $self->phonenum && $self->phonenum eq $self->gateway_access_number;
355
356   $self->SUPER::check;
357 }
358
359 =item gateway_access_or_phonenum
360
361 =cut
362
363 sub gateway_access_or_phonenum {
364   my $self = shift;
365   $self->gateway_access_number || $self->phonenum;
366 }
367
368 =item dsl_device
369
370 Returns the MAC addresses associated with this DSL service, as FS::dsl_device
371 objects.
372
373 =cut
374
375 sub predelete_hook_first {
376     my $self = shift;
377     my @exports = $self->part_svc->part_export_dsl_pull;
378     return 'More than one DSL-pulling export attached' if scalar(@exports) > 1;
379     if ( scalar(@exports) == 1 ) {
380         my $export = $exports[0];
381         return $export->dsl_pull($self);
382     }
383     '';
384 }
385
386 sub predelete_hook {
387     my $self = shift;
388     my @notes = $self->notes;
389     foreach my $note ( @notes ) {
390         my $error = $note->delete;
391         return $error if $error;
392     }
393     '';
394 }
395
396 # password_history compatibility
397
398 sub _password {
399   my $self = shift;
400   $self->get('password');
401 }
402
403 sub _password_encoding { 'plain'; }
404
405 =back
406
407 =head1 SEE ALSO
408
409 L<FS::svc_Common>, edit/part_svc.cgi from an installed web interface,
410 export.html from the base documentation, L<FS::Record>, L<FS::Conf>,
411 L<FS::cust_svc>, L<FS::part_svc>, L<FS::cust_pkg>, L<FS::queue>,
412 L<freeside-queued>, schema.html from the base documentation.
413
414 =cut
415
416 1;
417