domain names in netsapiens export (domain name association w/svc_phone), RT#5864
[freeside.git] / FS / FS / svc_phone.pm
1 package FS::svc_phone;
2
3 use strict;
4 use base qw( FS::svc_Domain_Mixin FS::svc_Common );
5 use vars qw( @pw_set $conf );
6 use FS::Conf;
7 use FS::Record qw( qsearch qsearchs dbh );
8 use FS::Msgcat qw(gettext);
9 use FS::part_svc;
10 use FS::phone_device;
11 use FS::svc_pbx;
12 use FS::svc_domain;
13
14 #avoid l 1 and o O 0
15 @pw_set = ( 'a'..'k', 'm','n', 'p-z', 'A'..'N', 'P'..'Z' , '2'..'9' );
16
17 #ask FS::UID to run this stuff for us later
18 $FS::UID::callback{'FS::svc_acct'} = sub { 
19   $conf = new FS::Conf;
20 };
21
22 =head1 NAME
23
24 FS::svc_phone - Object methods for svc_phone records
25
26 =head1 SYNOPSIS
27
28   use FS::svc_phone;
29
30   $record = new FS::svc_phone \%hash;
31   $record = new FS::svc_phone { 'column' => 'value' };
32
33   $error = $record->insert;
34
35   $error = $new_record->replace($old_record);
36
37   $error = $record->delete;
38
39   $error = $record->check;
40
41   $error = $record->suspend;
42
43   $error = $record->unsuspend;
44
45   $error = $record->cancel;
46
47 =head1 DESCRIPTION
48
49 An FS::svc_phone object represents a phone number.  FS::svc_phone inherits
50 from FS::Record.  The following fields are currently supported:
51
52 =over 4
53
54 =item svcnum
55
56 primary key
57
58 =item countrycode
59
60 =item phonenum
61
62 =item sip_password
63
64 =item pin
65
66 Voicemail PIN
67
68 =item phone_name
69
70 =item pbxsvc
71
72 Optional svcnum from svc_pbx
73
74 =back
75
76 =head1 METHODS
77
78 =over 4
79
80 =item new HASHREF
81
82 Creates a new phone number.  To add the number to the database, see L<"insert">.
83
84 Note that this stores the hash reference, not a distinct copy of the hash it
85 points to.  You can ask the object for a copy with the I<hash> method.
86
87 =cut
88
89 # the new method can be inherited from FS::Record, if a table method is defined
90 #
91 sub table_info {
92   {
93     'name' => 'Phone number',
94     'sorts' => 'phonenum',
95     'display_weight' => 60,
96     'cancel_weight'  => 80,
97     'fields' => {
98         'countrycode'  => { label => 'Country code',
99                             type  => 'text',
100                             disable_inventory => 1,
101                             disable_select => 1,
102                           },
103         'phonenum'     => 'Phone number',
104         'pin'          => { label => 'Personal Identification Number',
105                             type  => 'text',
106                             disable_inventory => 1,
107                             disable_select => 1,
108                           },
109         'sip_password' => 'SIP password',
110         'phone_name'   => 'Name',
111         'pbxsvc'       => { label => 'PBX',
112                             type  => 'select-svc_pbx.html',
113                             disable_inventory => 1,
114                             disable_select => 1, #UI wonky, pry works otherwise
115                           },
116         'domsvc'    => {
117                          label     => 'Domain',
118                          type      => 'select',
119                          select_table => 'svc_domain',
120                          select_key   => 'svcnum',
121                          select_label => 'domain',
122                          disable_inventory => 1,
123                        },
124     },
125   };
126 }
127
128 sub table { 'svc_phone'; }
129
130 sub table_dupcheck_fields { ( 'countrycode', 'phonenum' ); }
131
132 =item search_sql STRING
133
134 Class method which returns an SQL fragment to search for the given string.
135
136 =cut
137
138 sub search_sql {
139   my( $class, $string ) = @_;
140
141   if ( $conf->exists('svc_phone-allow_alpha_phonenum') ) {
142     $string =~ s/\W//g;
143   } else {
144     $string =~ s/\D//g;
145   }
146
147   my $conf = new FS::Conf;
148   my $ccode = (    $conf->exists('default_phone_countrycode')
149                 && $conf->config('default_phone_countrycode')
150               )
151                 ? $conf->config('default_phone_countrycode') 
152                 : '1';
153
154   $string =~ s/^$ccode//;
155
156   $class->search_sql_field('phonenum', $string );
157 }
158
159 =item label
160
161 Returns the phone number.
162
163 =cut
164
165 sub label {
166   my $self = shift;
167   my $phonenum = $self->phonenum; #XXX format it better
168   my $label = $phonenum;
169   $label .= '@'.$self->domain if $self->domsvc;
170   $label .= ' ('.$self->phone_name.')' if $self->phone_name;
171   $label;
172 }
173
174 =item insert
175
176 Adds this record to the database.  If there is an error, returns the error,
177 otherwise returns false.
178
179 =cut
180
181 # the insert method can be inherited from FS::Record
182
183 =item delete
184
185 Delete this record from the database.
186
187 =cut
188
189 sub delete {
190   my $self = shift;
191
192   local $SIG{HUP} = 'IGNORE';
193   local $SIG{INT} = 'IGNORE';
194   local $SIG{QUIT} = 'IGNORE';
195   local $SIG{TERM} = 'IGNORE';
196   local $SIG{TSTP} = 'IGNORE';
197   local $SIG{PIPE} = 'IGNORE';
198
199   my $oldAutoCommit = $FS::UID::AutoCommit;
200   local $FS::UID::AutoCommit = 0;
201   my $dbh = dbh;
202
203   foreach my $phone_device ( $self->phone_device ) {
204     my $error = $phone_device->delete;
205     if ( $error ) {
206       $dbh->rollback if $oldAutoCommit;
207       return $error;
208     }
209   }
210
211   my $error = $self->SUPER::delete;
212   if ( $error ) {
213     $dbh->rollback if $oldAutoCommit;
214     return $error;
215   }
216
217   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
218   '';
219
220 }
221
222 # the delete method can be inherited from FS::Record
223
224 =item replace OLD_RECORD
225
226 Replaces the OLD_RECORD with this one in the database.  If there is an error,
227 returns the error, otherwise returns false.
228
229 =cut
230
231 # the replace method can be inherited from FS::Record
232
233 =item suspend
234
235 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
236
237 =item unsuspend
238
239 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
240
241 =item cancel
242
243 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
244
245 =item check
246
247 Checks all fields to make sure this is a valid phone number.  If there is
248 an error, returns the error, otherwise returns false.  Called by the insert
249 and replace methods.
250
251 =cut
252
253 # the check method should currently be supplied - FS::Record contains some
254 # data checking routines
255
256 sub check {
257   my $self = shift;
258
259   my $conf = new FS::Conf;
260
261   my $phonenum = $self->phonenum;
262   my $phonenum_check_method;
263   if ( $conf->exists('svc_phone-allow_alpha_phonenum') ) {
264     $phonenum =~ s/\W//g;
265     $phonenum_check_method = 'ut_alpha';
266   } else {
267     $phonenum =~ s/\D//g;
268     $phonenum_check_method = 'ut_number';
269   }
270   $self->phonenum($phonenum);
271
272   my $error = 
273     $self->ut_numbern('svcnum')
274     || $self->ut_numbern('countrycode')
275     || $self->$phonenum_check_method('phonenum')
276     || $self->ut_anything('sip_password')
277     || $self->ut_numbern('pin')
278     || $self->ut_textn('phone_name')
279     || $self->ut_foreign_keyn('pbxsvc', 'svc_pbx',    'svcnum' )
280     || $self->ut_foreign_keyn('domsvc', 'svc_domain', 'svcnum' )
281   ;
282   return $error if $error;
283
284   $self->countrycode(1) unless $self->countrycode;
285
286   unless ( length($self->sip_password) ) {
287
288     $self->sip_password(
289       join('', map $pw_set[ int(rand $#pw_set) ], (0..16) )
290     );
291
292   }
293
294   $self->SUPER::check;
295 }
296
297 =item _check duplicate
298
299 Internal method to check for duplicate phone numers.
300
301 =cut
302
303 #false laziness w/svc_acct.pm's _check_duplicate.
304 sub _check_duplicate {
305   my $self = shift;
306
307   my $global_unique = $conf->config('global_unique-phonenum') || 'none';
308   return '' if $global_unique eq 'disabled';
309
310   $self->lock_table;
311
312   my @dup_ccphonenum =
313     grep { !$self->svcnum || $_->svcnum != $self->svcnum }
314     qsearch( 'svc_phone', {
315       'countrycode' => $self->countrycode,
316       'phonenum'    => $self->phonenum,
317     });
318
319   return gettext('phonenum_in_use')
320     if $global_unique eq 'countrycode+phonenum' && @dup_ccphonenum;
321
322   my $part_svc = qsearchs('part_svc', { 'svcpart' => $self->svcpart } );
323   unless ( $part_svc ) {
324     return 'unknown svcpart '. $self->svcpart;
325   }
326
327   if ( @dup_ccphonenum ) {
328
329     my $exports = FS::part_export::export_info('svc_phone');
330     my %conflict_ccphonenum_svcpart = ( $self->svcpart => 'SELF', );
331
332     foreach my $part_export ( $part_svc->part_export ) {
333
334       #this will catch to the same exact export
335       my @svcparts = map { $_->svcpart } $part_export->export_svc;
336
337       $conflict_ccphonenum_svcpart{$_} = $part_export->exportnum
338         foreach @svcparts;
339
340     }
341
342     foreach my $dup_ccphonenum ( @dup_ccphonenum ) {
343       my $dup_svcpart = $dup_ccphonenum->cust_svc->svcpart;
344       if ( exists($conflict_ccphonenum_svcpart{$dup_svcpart}) ) {
345         return "duplicate phone number ".
346                $self->countrycode. ' '. $self->phonenum.
347                ": conflicts with svcnum ". $dup_ccphonenum->svcnum.
348                " via exportnum ". $conflict_ccphonenum_svcpart{$dup_svcpart};
349       }
350     }
351
352   }
353
354   return '';
355
356 }
357
358 =item check_pin
359
360 Checks the supplied PIN against the PIN in the database.  Returns true for a
361 sucessful authentication, false if no match.
362
363 =cut
364
365 sub check_pin {
366   my($self, $check_pin) = @_;
367   length($self->pin) && $check_pin eq $self->pin;
368 }
369
370 =item radius_reply
371
372 =cut
373
374 sub radius_reply {
375   my $self = shift;
376   #XXX Session-Timeout!  holy shit, need rlm_perl to ask for this in realtime
377   ();
378 }
379
380 =item radius_check
381
382 =cut
383
384 sub radius_check {
385   my $self = shift;
386   my %check = ();
387
388   my $conf = new FS::Conf;
389
390   $check{'User-Password'} = $conf->config('svc_phone-radius-default_password');
391
392   %check;
393 }
394
395 sub radius_groups {
396   ();
397 }
398
399 =item phone_device
400
401 Returns any FS::phone_device records associated with this service.
402
403 =cut
404
405 sub phone_device {
406   my $self = shift;
407   qsearch('phone_device', { 'svcnum' => $self->svcnum } );
408 }
409
410 =back
411
412 =head1 BUGS
413
414 =head1 SEE ALSO
415
416 L<FS::svc_Common>, L<FS::Record>, L<FS::cust_svc>, L<FS::part_svc>,
417 L<FS::cust_pkg>, schema.html from the base documentation.
418
419 =cut
420
421 1;
422