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