1 package FS::cust_location;
4 use base qw( FS::geocode_Mixin FS::Record );
7 use FS::Record qw( qsearch ); #qsearchs );
11 use FS::cust_main_county;
15 FS::cust_location - Object methods for cust_location records
19 use FS::cust_location;
21 $record = new FS::cust_location \%hash;
22 $record = new FS::cust_location { 'column' => 'value' };
24 $error = $record->insert;
26 $error = $new_record->replace($old_record);
28 $error = $record->delete;
30 $error = $record->check;
34 An FS::cust_location object represents a customer location. FS::cust_location
35 inherits from FS::Record. The following fields are currently supported:
49 Address line one (required)
53 Address line two (optional)
61 County (optional, see L<FS::cust_main_county>)
65 State (see L<FS::cust_main_county>)
73 Country (see L<FS::cust_main_county>)
81 Disabled flag; set to 'Y' to disable the location.
91 Creates a new location. To add the location to the database, see L<"insert">.
93 Note that this stores the hash reference, not a distinct copy of the hash it
94 points to. You can ask the object for a copy with the I<hash> method.
98 sub table { 'cust_location'; }
102 Adds this record to the database. If there is an error, returns the error,
103 otherwise returns false.
107 Delete this record from the database.
109 =item replace OLD_RECORD
111 Replaces the OLD_RECORD with this one in the database. If there is an error,
112 returns the error, otherwise returns false.
116 Checks all fields to make sure this is a valid location. If there is
117 an error, returns the error, otherwise returns false. Called by the insert
122 #some false laziness w/cust_main, but since it should eventually lose these
128 $self->ut_numbern('locationnum')
129 || $self->ut_foreign_keyn('prospectnum', 'prospect_main', 'prospectnum')
130 || $self->ut_foreign_keyn('custnum', 'cust_main', 'custnum')
131 || $self->ut_text('address1')
132 || $self->ut_textn('address2')
133 || $self->ut_text('city')
134 || $self->ut_textn('county')
135 || $self->ut_textn('state')
136 || $self->ut_country('country')
137 || $self->ut_zip('zip', $self->country)
138 || $self->ut_alphan('location_type')
139 || $self->ut_textn('location_number')
140 || $self->ut_enum('location_kind', [ '', 'R', 'B' ] )
141 || $self->ut_alphan('geocode')
143 return $error if $error;
145 return "No prospect or customer!" unless $self->prospectnum || $self->custnum;
146 return "Prospect and customer!" if $self->prospectnum && $self->custnum;
148 my $conf = new FS::Conf;
149 return 'Location kind is required'
150 if $self->prospectnum
151 && $conf->exists('prospect_main-alt_address_format')
152 && ! $self->location_kind;
154 unless ( qsearch('cust_main_county', {
155 'country' => $self->country,
158 return "Unknown state/county/country: ".
159 $self->state. "/". $self->county. "/". $self->country
160 unless qsearch('cust_main_county',{
161 'state' => $self->state,
162 'county' => $self->county,
163 'country' => $self->country,
172 Returns this locations's full country name
178 code2country($self->country);
183 Synonym for location_label
189 $self->location_label;
192 =item has_ship_address
194 Returns false since cust_location objects do not have a separate shipping
199 sub has_ship_address {
205 Returns a list of key/value pairs, with the following keys: address1, address2,
206 city, county, state, zip, country, geocode.
210 =item move_to HASHREF
212 Takes a hashref with one or more cust_location fields. Creates a duplicate
213 of the existing location with all fields set to the values in the hashref.
214 Moves all packages that use the existing location to the new one, then sets
215 the "disabled" flag on the old location. Returns nothing on success, an
216 error message on error.
224 local $SIG{HUP} = 'IGNORE';
225 local $SIG{INT} = 'IGNORE';
226 local $SIG{QUIT} = 'IGNORE';
227 local $SIG{TERM} = 'IGNORE';
228 local $SIG{TSTP} = 'IGNORE';
229 local $SIG{PIPE} = 'IGNORE';
231 my $oldAutoCommit = $FS::UID::AutoCommit;
232 local $FS::UID::AutoCommit = 0;
236 my $new = FS::cust_location->new({
238 'custnum' => $old->custnum,
239 'prospectnum' => $old->prospectnum,
242 $error = $new->insert;
244 $dbh->rollback if $oldAutoCommit;
245 return "Error creating location: $error";
248 my @pkgs = qsearch('cust_pkg', {
249 'locationnum' => $old->locationnum,
252 foreach my $cust_pkg (@pkgs) {
253 $error = $cust_pkg->change(
254 'locationnum' => $new->locationnum,
257 if ( $error and not ref($error) ) {
258 $dbh->rollback if $oldAutoCommit;
259 return "Error moving pkgnum ".$cust_pkg->pkgnum.": $error";
264 $error = $old->replace;
266 $dbh->rollback if $oldAutoCommit;
267 return "Error disabling old location: $error";
270 $dbh->commit if $oldAutoCommit;
278 Not yet used for cust_main billing and shipping addresses.
282 L<FS::cust_main_county>, L<FS::cust_pkg>, L<FS::Record>,
283 schema.html from the base documentation.