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