label phone_name correctly
[freeside.git] / FS / FS / svc_phone.pm
1 package FS::svc_phone;
2
3 use strict;
4 use vars qw( @ISA @pw_set $conf );
5 use FS::Conf;
6 use FS::Record qw( qsearch qsearchs );
7 use FS::Msgcat qw(gettext);
8 use FS::svc_Common;
9 use FS::part_svc;
10
11 @ISA = qw( FS::svc_Common );
12
13 #avoid l 1 and o O 0
14 @pw_set = ( 'a'..'k', 'm','n', 'p-z', 'A'..'N', 'P'..'Z' , '2'..'9' );
15
16 #ask FS::UID to run this stuff for us later
17 $FS::UID::callback{'FS::svc_acct'} = sub { 
18   $conf = new FS::Conf;
19 };
20
21 =head1 NAME
22
23 FS::svc_phone - Object methods for svc_phone records
24
25 =head1 SYNOPSIS
26
27   use FS::svc_phone;
28
29   $record = new FS::svc_phone \%hash;
30   $record = new FS::svc_phone { '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_phone object represents a phone number.  FS::svc_phone inherits
49 from FS::Record.  The following fields are currently supported:
50
51 =over 4
52
53 =item svcnum
54
55 primary key
56
57 =item countrycode
58
59 =item phonenum
60
61 =item sip_password
62
63 =item pin
64
65 Voicemail PIN
66
67 =item phone_name
68
69 =back
70
71 =head1 METHODS
72
73 =over 4
74
75 =item new HASHREF
76
77 Creates a new phone number.  To add the number to the database, see L<"insert">.
78
79 Note that this stores the hash reference, not a distinct copy of the hash it
80 points to.  You can ask the object for a copy with the I<hash> method.
81
82 =cut
83
84 # the new method can be inherited from FS::Record, if a table method is defined
85 #
86 sub table_info {
87   {
88     'name' => 'Phone number',
89     'sorts' => 'phonenum',
90     'display_weight' => 60,
91     'cancel_weight'  => 80,
92     'fields' => {
93         'countrycode'  => { label => 'Country code',
94                             type  => 'text',
95                             disable_inventory => 1,
96                             disable_select => 1,
97                           },
98         'phonenum'     => 'Phone number',
99         'pin'          => { label => 'Personal Identification Number',
100                             type  => 'text',
101                             disable_inventory => 1,
102                             disable_select => 1,
103                           },
104         'sip_password' => 'SIP password',
105         'phone_name'   => 'Name',
106     },
107   };
108 }
109
110 sub table { 'svc_phone'; }
111
112 sub table_dupcheck_fields { ( 'countrycode', 'phonenum' ); }
113
114 =item search_sql STRING
115
116 Class method which returns an SQL fragment to search for the given string.
117
118 =cut
119
120 sub search_sql {
121   my( $class, $string ) = @_;
122   $class->search_sql_field('phonenum', $string );
123 }
124
125 =item label
126
127 Returns the phone number.
128
129 =cut
130
131 sub label {
132   my $self = shift;
133   my $phonenum = $self->phonenum; #XXX format it better
134   my $label = $phonenum;
135   $label .= ' ('.$self->phone_name.')' if $self->phone_name;
136   $label;
137 }
138
139 =item insert
140
141 Adds this record to the database.  If there is an error, returns the error,
142 otherwise returns false.
143
144 =cut
145
146 # the insert method can be inherited from FS::Record
147
148 =item delete
149
150 Delete this record from the database.
151
152 =cut
153
154 # the delete method can be inherited from FS::Record
155
156 =item replace OLD_RECORD
157
158 Replaces the OLD_RECORD with this one in the database.  If there is an error,
159 returns the error, otherwise returns false.
160
161 =cut
162
163 # the replace method can be inherited from FS::Record
164
165 =item suspend
166
167 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
168
169 =item unsuspend
170
171 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
172
173 =item cancel
174
175 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
176
177 =item check
178
179 Checks all fields to make sure this is a valid phone number.  If there is
180 an error, returns the error, otherwise returns false.  Called by the insert
181 and replace methods.
182
183 =cut
184
185 # the check method should currently be supplied - FS::Record contains some
186 # data checking routines
187
188 sub check {
189   my $self = shift;
190
191   my $conf = new FS::Conf;
192
193   my $phonenum = $self->phonenum;
194   my $phonenum_check_method;
195   if ( $conf->exists('svc_phone-allow_alpha_phonenum') ) {
196     $phonenum =~ s/\W//g;
197     $phonenum_check_method = 'ut_alpha';
198   } else {
199     $phonenum =~ s/\D//g;
200     $phonenum_check_method = 'ut_number';
201   }
202   $self->phonenum($phonenum);
203
204   my $error = 
205     $self->ut_numbern('svcnum')
206     || $self->ut_numbern('countrycode')
207     || $self->$phonenum_check_method('phonenum')
208     || $self->ut_anything('sip_password')
209     || $self->ut_numbern('pin')
210     || $self->ut_textn('phone_name')
211   ;
212   return $error if $error;
213
214   $self->countrycode(1) unless $self->countrycode;
215
216   unless ( length($self->sip_password) ) {
217
218     $self->sip_password(
219       join('', map $pw_set[ int(rand $#pw_set) ], (0..16) )
220     );
221
222   }
223
224   $self->SUPER::check;
225 }
226
227 =item _check duplicate
228
229 Internal method to check for duplicate phone numers.
230
231 =cut
232
233 #false laziness w/svc_acct.pm's _check_duplicate.
234 sub _check_duplicate {
235   my $self = shift;
236
237   my $global_unique = $conf->config('global_unique-phonenum') || 'none';
238   return '' if $global_unique eq 'disabled';
239
240   $self->lock_table;
241
242   my @dup_ccphonenum =
243     grep { !$self->svcnum || $_->svcnum != $self->svcnum }
244     qsearch( 'svc_phone', {
245       'countrycode' => $self->countrycode,
246       'phonenum'    => $self->phonenum,
247     });
248
249   return gettext('phonenum_in_use')
250     if $global_unique eq 'countrycode+phonenum' && @dup_ccphonenum;
251
252   my $part_svc = qsearchs('part_svc', { 'svcpart' => $self->svcpart } );
253   unless ( $part_svc ) {
254     return 'unknown svcpart '. $self->svcpart;
255   }
256
257   if ( @dup_ccphonenum ) {
258
259     my $exports = FS::part_export::export_info('svc_phone');
260     my %conflict_ccphonenum_svcpart = ( $self->svcpart => 'SELF', );
261
262     foreach my $part_export ( $part_svc->part_export ) {
263
264       #this will catch to the same exact export
265       my @svcparts = map { $_->svcpart } $part_export->export_svc;
266
267       $conflict_ccphonenum_svcpart{$_} = $part_export->exportnum
268         foreach @svcparts;
269
270     }
271
272     foreach my $dup_ccphonenum ( @dup_ccphonenum ) {
273       my $dup_svcpart = $dup_ccphonenum->cust_svc->svcpart;
274       if ( exists($conflict_ccphonenum_svcpart{$dup_svcpart}) ) {
275         return "duplicate phone number ".
276                $self->countrycode. ' '. $self->phonenum.
277                ": conflicts with svcnum ". $dup_ccphonenum->svcnum.
278                " via exportnum ". $conflict_ccphonenum_svcpart{$dup_svcpart};
279       }
280     }
281
282   }
283
284   return '';
285
286 }
287
288 =item check_pin
289
290 Checks the supplied PIN against the PIN in the database.  Returns true for a
291 sucessful authentication, false if no match.
292
293 =cut
294
295 sub check_pin {
296   my($self, $check_pin) = @_;
297   length($self->pin) && $check_pin eq $self->pin;
298 }
299
300 =item radius_reply
301
302 =cut
303
304 sub radius_reply {
305   my $self = shift;
306   #XXX Session-Timeout!  holy shit, need rlm_perl to ask for this in realtime
307   ();
308 }
309
310 =item radius_check
311
312 =cut
313
314 sub radius_check {
315   my $self = shift;
316   my %check = ();
317
318   my $conf = new FS::Conf;
319
320   $check{'User-Password'} = $conf->config('svc_phone-radius-default_password');
321
322   %check;
323 }
324
325 sub radius_groups {
326   ();
327 }
328
329 =back
330
331 =head1 BUGS
332
333 =head1 SEE ALSO
334
335 L<FS::svc_Common>, L<FS::Record>, L<FS::cust_svc>, L<FS::part_svc>,
336 L<FS::cust_pkg>, schema.html from the base documentation.
337
338 =cut
339
340 1;
341