1 package FS::cust_location;
4 use base qw( FS::geocode_Mixin FS::Record );
7 use FS::Record qw( qsearch ); #qsearchs );
10 use FS::cust_main_county;
14 FS::cust_location - Object methods for cust_location records
18 use FS::cust_location;
20 $record = new FS::cust_location \%hash;
21 $record = new FS::cust_location { 'column' => 'value' };
23 $error = $record->insert;
25 $error = $new_record->replace($old_record);
27 $error = $record->delete;
29 $error = $record->check;
33 An FS::cust_location object represents a customer location. FS::cust_location
34 inherits from FS::Record. The following fields are currently supported:
48 Address line one (required)
52 Address line two (optional)
60 County (optional, see L<FS::cust_main_county>)
64 State (see L<FS::cust_main_county>)
72 Country (see L<FS::cust_main_county>)
80 Disabled flag; set to 'Y' to disable the location.
90 Creates a new location. To add the location to the database, see L<"insert">.
92 Note that this stores the hash reference, not a distinct copy of the hash it
93 points to. You can ask the object for a copy with the I<hash> method.
97 sub table { 'cust_location'; }
101 Adds this record to the database. If there is an error, returns the error,
102 otherwise returns false.
106 Delete this record from the database.
108 =item replace OLD_RECORD
110 Replaces the OLD_RECORD with this one in the database. If there is an error,
111 returns the error, otherwise returns false.
115 Checks all fields to make sure this is a valid location. If there is
116 an error, returns the error, otherwise returns false. Called by the insert
121 #some false laziness w/cust_main, but since it should eventually lose these
127 $self->ut_numbern('locationnum')
128 || $self->ut_foreign_keyn('prospectnum', 'prospect_main', 'prospectnum')
129 || $self->ut_foreign_keyn('custnum', 'cust_main', 'custnum')
130 || $self->ut_text('address1')
131 || $self->ut_textn('address2')
132 || $self->ut_text('city')
133 || $self->ut_textn('county')
134 || $self->ut_textn('state')
135 || $self->ut_country('country')
136 || $self->ut_zip('zip', $self->country)
137 || $self->ut_alphan('geocode')
139 return $error if $error;
141 return "No prospect or customer!" unless $self->prospectnum || $self->custnum;
142 return "Prospect and customer!" if $self->prospectnum && $self->custnum;
144 unless ( qsearch('cust_main_county', {
145 'country' => $self->country,
148 return "Unknown state/county/country: ".
149 $self->state. "/". $self->county. "/". $self->country
150 unless qsearch('cust_main_county',{
151 'state' => $self->state,
152 'county' => $self->county,
153 'country' => $self->country,
162 Returns this locations's full country name
168 code2country($self->country);
173 Synonym for location_label
179 $self->location_label;
182 =item has_ship_address
184 Returns false since cust_location objects do not have a separate shipping
189 sub has_ship_address {
195 Returns a list of key/value pairs, with the following keys: address1, address2,
196 city, county, state, zip, country, geocode.
200 =item move_to HASHREF
202 Takes a hashref with one or more cust_location fields. Creates a duplicate
203 of the existing location with all fields set to the values in the hashref.
204 Moves all packages that use the existing location to the new one, then sets
205 the "disabled" flag on the old location. Returns nothing on success, an
206 error message on error.
214 local $SIG{HUP} = 'IGNORE';
215 local $SIG{INT} = 'IGNORE';
216 local $SIG{QUIT} = 'IGNORE';
217 local $SIG{TERM} = 'IGNORE';
218 local $SIG{TSTP} = 'IGNORE';
219 local $SIG{PIPE} = 'IGNORE';
221 my $oldAutoCommit = $FS::UID::AutoCommit;
222 local $FS::UID::AutoCommit = 0;
226 my $new = FS::cust_location->new({
228 'custnum' => $old->custnum,
229 'prospectnum' => $old->prospectnum,
232 $error = $new->insert;
234 $dbh->rollback if $oldAutoCommit;
235 return "Error creating location: $error";
238 my @pkgs = qsearch('cust_pkg', {
239 'locationnum' => $old->locationnum,
242 foreach my $cust_pkg (@pkgs) {
243 $error = $cust_pkg->change(
244 'locationnum' => $new->locationnum,
247 if ( $error and not ref($error) ) {
248 $dbh->rollback if $oldAutoCommit;
249 return "Error moving pkgnum ".$cust_pkg->pkgnum.": $error";
254 $error = $old->replace;
256 $dbh->rollback if $oldAutoCommit;
257 return "Error disabling old location: $error";
260 $dbh->commit if $oldAutoCommit;
268 Not yet used for cust_main billing and shipping addresses.
272 L<FS::cust_main_county>, L<FS::cust_pkg>, L<FS::Record>,
273 schema.html from the base documentation.