add a switch to allow letters in phone numbers, RT#4195
[freeside.git] / FS / FS / svc_phone.pm
1 package FS::svc_phone;
2
3 use strict;
4 use vars qw( @ISA @pw_set );
5 use FS::Conf;
6 #use FS::Record qw( qsearch qsearchs );
7 use FS::svc_Common;
8
9 @ISA = qw( FS::svc_Common );
10
11 #avoid l 1 and o O 0
12 @pw_set = ( 'a'..'k', 'm','n', 'p-z', 'A'..'N', 'P'..'Z' , '2'..'9' );
13
14 =head1 NAME
15
16 FS::svc_phone - Object methods for svc_phone records
17
18 =head1 SYNOPSIS
19
20   use FS::svc_phone;
21
22   $record = new FS::svc_phone \%hash;
23   $record = new FS::svc_phone { 'column' => 'value' };
24
25   $error = $record->insert;
26
27   $error = $new_record->replace($old_record);
28
29   $error = $record->delete;
30
31   $error = $record->check;
32
33   $error = $record->suspend;
34
35   $error = $record->unsuspend;
36
37   $error = $record->cancel;
38
39 =head1 DESCRIPTION
40
41 An FS::svc_phone object represents a phone number.  FS::svc_phone inherits
42 from FS::Record.  The following fields are currently supported:
43
44 =over 4
45
46 =item svcnum
47
48 primary key
49
50 =item countrycode
51
52 =item phonenum
53
54 =item sip_password
55
56 =item pin
57
58 Voicemail PIN
59
60 =item phone_name
61
62 =back
63
64 =head1 METHODS
65
66 =over 4
67
68 =item new HASHREF
69
70 Creates a new phone number.  To add the number to the database, see L<"insert">.
71
72 Note that this stores the hash reference, not a distinct copy of the hash it
73 points to.  You can ask the object for a copy with the I<hash> method.
74
75 =cut
76
77 # the new method can be inherited from FS::Record, if a table method is defined
78 #
79 sub table_info {
80   {
81     'name' => 'Phone number',
82     'sorts' => 'phonenum',
83     'display_weight' => 60,
84     'cancel_weight'  => 80,
85     'fields' => {
86         'countrycode'  => { label => 'Country code',
87                             type  => 'text',
88                             disable_inventory => 1,
89                             disable_select => 1,
90                           },
91         'phonenum'     => 'Phone number',
92         'pin'          => { label => 'Personal Identification Number',
93                             type  => 'text',
94                             disable_inventory => 1,
95                             disable_select => 1,
96                           },
97         'sip_password' => 'SIP password',
98         'name'         => 'Name',
99     },
100   };
101 }
102
103 sub table { 'svc_phone'; }
104
105 =item search_sql STRING
106
107 Class method which returns an SQL fragment to search for the given string.
108
109 =cut
110
111 sub search_sql {
112   my( $class, $string ) = @_;
113   $class->search_sql_field('phonenum', $string );
114 }
115
116 =item label
117
118 Returns the phone number.
119
120 =cut
121
122 sub label {
123   my $self = shift;
124   my $phonenum = $self->phonenum; #XXX format it better
125   my $label = $phonenum;
126   $label .= ' ('.$self->phone_name.')' if $self->phone_name;
127   $label;
128 }
129
130 =item insert
131
132 Adds this record to the database.  If there is an error, returns the error,
133 otherwise returns false.
134
135 =cut
136
137 # the insert method can be inherited from FS::Record
138
139 =item delete
140
141 Delete this record from the database.
142
143 =cut
144
145 # the delete method can be inherited from FS::Record
146
147 =item replace OLD_RECORD
148
149 Replaces the OLD_RECORD with this one in the database.  If there is an error,
150 returns the error, otherwise returns false.
151
152 =cut
153
154 # the replace method can be inherited from FS::Record
155
156 =item suspend
157
158 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
159
160 =item unsuspend
161
162 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
163
164 =item cancel
165
166 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
167
168 =item check
169
170 Checks all fields to make sure this is a valid phone number.  If there is
171 an error, returns the error, otherwise returns false.  Called by the insert
172 and replace methods.
173
174 =cut
175
176 # the check method should currently be supplied - FS::Record contains some
177 # data checking routines
178
179 sub check {
180   my $self = shift;
181
182   my $conf = new FS::Conf;
183
184   my $phonenum = $self->phonenum;
185   if ( $conf->exists('svc_phone-allow_alpha_phonenum') ) {
186     $phonenum =~ s/\W//g;
187   } else {
188     $phonenum =~ s/\D//g;
189   }
190   $self->phonenum($phonenum);
191
192   my $error = 
193     $self->ut_numbern('svcnum')
194     || $self->ut_numbern('countrycode')
195     || $self->ut_number('phonenum')
196     || $self->ut_anything('sip_password')
197     || $self->ut_numbern('pin')
198     || $self->ut_textn('phone_name')
199   ;
200   return $error if $error;
201
202   $self->countrycode(1) unless $self->countrycode;
203
204   unless ( length($self->sip_password) ) {
205
206     $self->sip_password(
207       join('', map $pw_set[ int(rand $#pw_set) ], (0..16) )
208     );
209
210   }
211
212   $self->SUPER::check;
213 }
214
215 =item check_pin
216
217 Checks the supplied PIN against the PIN in the database.  Returns true for a
218 sucessful authentication, false if no match.
219
220 =cut
221
222 sub check_pin {
223   my($self, $check_pin) = @_;
224   $check_pin eq $self->pin;
225 }
226
227 =item radius_reply
228
229 =cut
230
231 sub radius_reply {
232   my $self = shift;
233   #XXX Session-Timeout!  holy shit, need rlm_perl to ask for this in realtime
234   ();
235 }
236
237 =item radius_check
238
239 =cut
240
241 sub radius_check {
242   my $self = shift;
243   my %check = ();
244
245   my $conf = new FS::Conf;
246
247   $check{'User-Password'} = $conf->config('svc_phone-radius-default_password');
248
249   %check;
250 }
251
252 sub radius_groups {
253   ();
254 }
255
256 =back
257
258 =head1 BUGS
259
260 =head1 SEE ALSO
261
262 L<FS::svc_Common>, L<FS::Record>, L<FS::cust_svc>, L<FS::part_svc>,
263 L<FS::cust_pkg>, schema.html from the base documentation.
264
265 =cut
266
267 1;
268