tiny nits for phone RADIUS export: allow freeside-sqlradius-reset to reset a phone_sq...
[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 $phonenum = $self->phonenum;
183   $phonenum =~ s/\D//g;
184   $self->phonenum($phonenum);
185
186   my $error = 
187     $self->ut_numbern('svcnum')
188     || $self->ut_numbern('countrycode')
189     || $self->ut_number('phonenum')
190     || $self->ut_anything('sip_password')
191     || $self->ut_numbern('pin')
192     || $self->ut_textn('phone_name')
193   ;
194   return $error if $error;
195
196   $self->countrycode(1) unless $self->countrycode;
197
198   unless ( length($self->sip_password) ) {
199
200     $self->sip_password(
201       join('', map $pw_set[ int(rand $#pw_set) ], (0..16) )
202     );
203
204   }
205
206   $self->SUPER::check;
207 }
208
209 =item check_pin
210
211 Checks the supplied PIN against the PIN in the database.  Returns true for a
212 sucessful authentication, false if no match.
213
214 =cut
215
216 sub check_pin {
217   my($self, $check_pin) = @_;
218   $check_pin eq $self->pin;
219 }
220
221 =item radius_reply
222
223 =cut
224
225 sub radius_reply {
226   my $self = shift;
227   #XXX Session-Timeout!  holy shit, need rlm_perl to ask for this in realtime
228   ();
229 }
230
231 =item radius_check
232
233 =cut
234
235 sub radius_check {
236   my $self = shift;
237   my %check = ();
238
239   my $conf = new FS::Conf;
240
241   $check{'User-Password'} = $conf->config('svc_phone-radius-default_password');
242
243   %check;
244 }
245
246 sub radius_groups {
247   ();
248 }
249
250 =back
251
252 =head1 BUGS
253
254 =head1 SEE ALSO
255
256 L<FS::svc_Common>, L<FS::Record>, L<FS::cust_svc>, L<FS::part_svc>,
257 L<FS::cust_pkg>, schema.html from the base documentation.
258
259 =cut
260
261 1;
262