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