3a9cc7f972c94230bd5de371784fb9a37d494684
[freeside.git] / FS / FS / cust_location.pm
1 package FS::cust_location;
2
3 use strict;
4 use base qw( FS::Record );
5 use Locale::Country;
6 use FS::Record qw( qsearch ); #qsearchs );
7 use FS::cust_main;
8 use FS::cust_main_county;
9
10 =head1 NAME
11
12 FS::cust_location - Object methods for cust_location records
13
14 =head1 SYNOPSIS
15
16   use FS::cust_location;
17
18   $record = new FS::cust_location \%hash;
19   $record = new FS::cust_location { 'column' => 'value' };
20
21   $error = $record->insert;
22
23   $error = $new_record->replace($old_record);
24
25   $error = $record->delete;
26
27   $error = $record->check;
28
29 =head1 DESCRIPTION
30
31 An FS::cust_location object represents a customer location.  FS::cust_location
32 inherits from FS::Record.  The following fields are currently supported:
33
34 =over 4
35
36 =item locationnum
37
38 primary key
39
40 =item custnum
41
42 custnum
43
44 =item address1
45
46 Address line one (required)
47
48 =item address2
49
50 Address line two (optional)
51
52 =item city
53
54 City
55
56 =item county
57
58 County (optional, see L<FS::cust_main_county>)
59
60 =item state
61
62 State (see L<FS::cust_main_county>)
63
64 =item zip
65
66 Zip
67
68 =item country
69
70 Country (see L<FS::cust_main_county>)
71
72 =item geocode
73
74 Geocode
75
76 =back
77
78 =head1 METHODS
79
80 =over 4
81
82 =item new HASHREF
83
84 Creates a new location.  To add the location to the database, see L<"insert">.
85
86 Note that this stores the hash reference, not a distinct copy of the hash it
87 points to.  You can ask the object for a copy with the I<hash> method.
88
89 =cut
90
91 sub table { 'cust_location'; }
92
93 =item insert
94
95 Adds this record to the database.  If there is an error, returns the error,
96 otherwise returns false.
97
98 =item delete
99
100 Delete this record from the database.
101
102 =item replace OLD_RECORD
103
104 Replaces the OLD_RECORD with this one in the database.  If there is an error,
105 returns the error, otherwise returns false.
106
107 =item check
108
109 Checks all fields to make sure this is a valid location.  If there is
110 an error, returns the error, otherwise returns false.  Called by the insert
111 and replace methods.
112
113 =cut
114
115 #some false laziness w/cust_main, but since it should eventually lose these
116 #fields anyway...
117 sub check {
118   my $self = shift;
119
120   my $error = 
121     $self->ut_numbern('locationnum')
122     || $self->ut_foreign_key('custnum', 'cust_main', 'custnum')
123     || $self->ut_text('address1')
124     || $self->ut_textn('address2')
125     || $self->ut_text('city')
126     || $self->ut_textn('county')
127     || $self->ut_textn('state')
128     || $self->ut_country('country')
129     || $self->ut_zip('zip', $self->country)
130     || $self->ut_alphan('geocode')
131   ;
132   return $error if $error;
133
134   unless ( qsearch('cust_main_county', {
135     'country' => $self->country,
136     'state'   => '',
137    } ) ) {
138     return "Unknown state/county/country: ".
139       $self->state. "/". $self->county. "/". $self->country
140       unless qsearch('cust_main_county',{
141         'state'   => $self->state,
142         'county'  => $self->county,
143         'country' => $self->country,
144       } );
145   }
146
147   $self->SUPER::check;
148 }
149
150 =item country_full
151
152 Returns this locations's full country name
153
154 =cut
155
156 sub country_full {
157   my $self = shift;
158   code2country($self->country);
159 }
160
161 =item line
162
163 Returns this location on one line
164
165 =cut
166
167 sub line {
168   my $self = shift;
169   my $cydefault = FS::conf->new->config('countrydefault') || 'US';
170
171   my $line =       $self->address1;
172   $line   .= ', '. $self->address2              if $self->address2;
173   $line   .= ', '. $self->city;
174   $line   .= ' ('. $self->county. ' county)'    if $self->county;
175   $line   .= ', '. $self->state                 if $self->state;
176   $line   .= '  '. $self->zip                   if $self->zip;
177   $line   .= '  '. code2country($self->country) if $self->country ne $cydefault;
178
179   $line;
180 }
181
182 =item line_short
183
184 Returns this location on one line in a shortened form
185
186 =cut
187
188 # configurable?
189
190 sub line_short {
191   my $self = shift;
192   my $cydefault = FS::conf->new->config('countrydefault') || 'US';
193
194   my $line =       $self->address1;
195   #$line   .= ', '. $self->address2              if $self->address2;
196   $line   .= ', '. $self->city;
197   $line   .= ', '. $self->state                 if $self->state;
198   $line   .= '  '. $self->zip                   if $self->zip;
199   $line   .= '  '. code2country($self->country) if $self->country ne $cydefault;
200
201   $line;
202 }
203
204 =item location_label_short
205
206 Synonym for line_short
207
208 =cut
209
210 sub location_label_short {
211   my $self = shift;
212   $self->line_short;
213 }
214
215 =back
216
217 =head1 BUGS
218
219 Not yet used for cust_main billing and shipping addresses.
220
221 =head1 SEE ALSO
222
223 L<FS::cust_main_county>, L<FS::cust_pkg>, L<FS::Record>,
224 schema.html from the base documentation.
225
226 =cut
227
228 1;
229