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