normalize contact phone formatting
[freeside.git] / FS / FS / contact_phone.pm
1 package FS::contact_phone;
2
3 use strict;
4 use base qw( FS::Record );
5 use FS::Record qw( qsearch qsearchs );
6
7 =head1 NAME
8
9 FS::contact_phone - Object methods for contact_phone records
10
11 =head1 SYNOPSIS
12
13   use FS::contact_phone;
14
15   $record = new FS::contact_phone \%hash;
16   $record = new FS::contact_phone { 'column' => 'value' };
17
18   $error = $record->insert;
19
20   $error = $new_record->replace($old_record);
21
22   $error = $record->delete;
23
24   $error = $record->check;
25
26 =head1 DESCRIPTION
27
28 An FS::contact_phone object represents an example.  FS::contact_phone inherits from
29 FS::Record.  The following fields are currently supported:
30
31 =over 4
32
33 =item contactphonenum
34
35 primary key
36
37 =item contactnum
38
39 contactnum
40
41 =item phonetypenum
42
43 phonetypenum
44
45 =item countrycode
46
47 countrycode
48
49 =item phonenum
50
51 phonenum
52
53 =item extension
54
55 extension
56
57
58 =back
59
60 =head1 METHODS
61
62 =over 4
63
64 =item new HASHREF
65
66 Creates a new example.  To add the example to the database, see L<"insert">.
67
68 Note that this stores the hash reference, not a distinct copy of the hash it
69 points to.  You can ask the object for a copy with the I<hash> method.
70
71 =cut
72
73 # the new method can be inherited from FS::Record, if a table method is defined
74
75 sub table { 'contact_phone'; }
76
77 =item insert
78
79 Adds this record to the database.  If there is an error, returns the error,
80 otherwise returns false.
81
82 =cut
83
84 # the insert method can be inherited from FS::Record
85
86 =item delete
87
88 Delete this record from the database.
89
90 =cut
91
92 # the delete method can be inherited from FS::Record
93
94 =item replace OLD_RECORD
95
96 Replaces the OLD_RECORD with this one in the database.  If there is an error,
97 returns the error, otherwise returns false.
98
99 =cut
100
101 # the replace method can be inherited from FS::Record
102
103 =item check
104
105 Checks all fields to make sure this is a valid example.  If there is
106 an error, returns the error, otherwise returns false.  Called by the insert
107 and replace methods.
108
109 =cut
110
111 # the check method should currently be supplied - FS::Record contains some
112 # data checking routines
113
114 sub check {
115   my $self = shift;
116
117   my $error = 
118     $self->ut_numbern('contactphonenum')
119     || $self->ut_number('contactnum')
120     || $self->ut_number('phonetypenum')
121     || $self->ut_text('countrycode')
122     || $self->ut_text('phonenum')
123     || $self->ut_textn('extension')
124   ;
125   return $error if $error;
126
127   #strip non-digits, UI should format numbers per countrycode
128   (my $phonenum = $self->phonenum ) =~ s/\D//g;
129   $self->phonenum($phonenum);
130
131   $self->SUPER::check;
132 }
133
134 sub phonenum_pretty {
135   my $self = shift;
136
137   #until/unless we have the upgrade strip all whitespace
138   (my $phonenum = $self->phonenum ) =~ s/\D//g;
139
140   if ( $self->countrycode == 1 ) {
141
142     $phonenum =~ /^(\d{3})(\d{3})(\d{4})(\d*)$/
143       or return $self->phonenum; #wtf?
144
145     $phonenum = "($1) $2-$3";
146     $phonenum .= " x$4" if $4;
147     return $phonenum;
148
149   } else {
150     warn "don't know how to format phone numbers for country +". $self->countrycode;
151     #also, the UI doesn't have a good way for you to enter them yet or parse a countrycode from the number
152     return $self->phonenum;
153   }
154
155 }
156
157 =back
158
159 =head1 BUGS
160
161 =head1 SEE ALSO
162
163 L<FS::Record>, schema.html from the base documentation.
164
165 =cut
166
167 1;
168