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