1 package FS::cust_main::Location;
4 use vars qw( $DEBUG $me @location_fields );
5 use FS::Record qw(qsearch qsearchs);
12 $me = '[FS::cust_main::Location]';
16 # set up accessors for location fields
20 qw( address1 address2 city county state zip country district
21 latitude longitude coord_auto censustract censusyear geocode
24 foreach my $f (@location_fields) {
25 *{"FS::cust_main::Location::$f"} = sub {
26 carp "WARNING: tried to set cust_main.$f with accessor" if (@_ > 1);
27 my $l = shift->bill_location;
30 *{"FS::cust_main::Location::ship_$f"} = sub {
31 carp "WARNING: tried to set cust_main.ship_$f with accessor" if (@_ > 1);
32 my $l = shift->ship_location;
40 #debugging shim--probably a performance hit, so remove this at some point
44 if ( $DEBUG and grep (/^(ship_)?($field)$/, @location_fields) ) {
45 carp "WARNING: tried to get() location field $field";
48 $self->FS::Record::get($field);
53 FS::cust_main::Location - Location-related methods for cust_main
57 These methods are available on FS::cust_main objects;
65 Returns an L<FS::cust_location> object for the customer's billing address.
71 $self->hashref->{bill_location}
72 ||= FS::cust_location->by_key($self->bill_locationnum);
77 Returns an L<FS::cust_location> object for the customer's service address.
83 $self->hashref->{ship_location}
84 ||= FS::cust_location->by_key($self->ship_locationnum);
89 An alternative way of saying "bill_location or ship_location, depending on
90 if TYPE is 'bill' or 'ship'".
96 return $self->bill_location if $_[0] eq 'bill';
97 return $self->ship_location if $_[0] eq 'ship';
98 die "bad location type '$_[0]'";
107 =item location_fields
109 Returns a list of fields found in the location objects. All of these fields
110 can be read (but not written) by calling them as methods on the
111 L<FS::cust_main> object (prefixed with 'ship_' for the service address
116 sub location_fields { @location_fields }
120 eval "use FS::contact;
121 use FS::contact_class;
122 use FS::contact_phone;
125 local $FS::cust_location::import = 1;
129 my $tax_prefix = 'bill_';
130 if ( FS::Conf->new->exists('tax-ship_address') ) {
131 $tax_prefix = 'ship_';
134 # Step 0: set up contact classes and phone types
135 my $service_contact_class =
136 qsearchs('contact_class', { classname => 'Service'})
137 || new FS::contact_class { classname => 'Service'};
139 if ( !$service_contact_class->classnum ) {
140 $error = $service_contact_class->insert;
141 die "error creating contact class for Service: $error" if $error;
143 my %phone_type = ( # fudge slightly
150 foreach (keys %phone_type) {
151 $phone_type{$_} = qsearchs('phone_type', { typename => $phone_type{$_}})
152 || new FS::phone_type { typename => $phone_type{$_},
154 # just in case someone still doesn't have these
155 if ( !$phone_type{$_}->phonetypenum ) {
156 $error = $phone_type{$_}->insert;
157 die "error creating phone type '$_': $error" if $error;
161 foreach my $cust_main (qsearch('cust_main', { bill_locationnum => '' })) {
162 # Step 1: extract billing and service addresses into cust_location
163 my $custnum = $cust_main->custnum;
164 my $bill_location = FS::cust_location->new(
167 map { $_ => $cust_main->get($_) } location_fields(),
170 $bill_location->set('censustract', '');
171 $bill_location->set('censusyear', '');
172 # properly goes with ship_location; if they're the same, will be set
173 # on ship_location before inserting either one
174 my $ship_location = $bill_location; # until proven otherwise
176 if ( $cust_main->get('ship_address1') ) {
179 foreach (location_fields()) {
180 if ( length($cust_main->get("ship_$_")) and
181 $cust_main->get($_) ne $cust_main->get("ship_$_") ) {
187 $ship_location = FS::cust_location->new(
190 map { $_ => $cust_main->get("ship_$_") } location_fields()
193 } # else it stays equal to $bill_location
195 # Step 2: Extract shipping address contact fields into contact
196 my %unlike = map { $_ => 1 }
197 grep { $cust_main->get($_) ne $cust_main->get("ship_$_") }
198 qw( last first company daytime night fax mobile );
201 # then there IS a service contact
202 my $contact = FS::contact->new({
203 'custnum' => $custnum,
204 'classnum' => $service_contact_class->classnum,
205 'locationnum' => $ship_location->locationnum,
206 'last' => $cust_main->get('ship_last'),
207 'first' => $cust_main->get('ship_first'),
209 if ( !$cust_main->get('ship_last') or !$cust_main->get('ship_first') )
211 warn "customer $custnum has no service contact name; substituting ".
213 $contact->set('last' => $cust_main->get('last'));
214 $contact->set('first' => $cust_main->get('first'));
217 if ( $unlike{'company'} ) {
218 # there's no contact.company field, but keep a record of it
219 $contact->set(comment => 'Company: '.$cust_main->get('ship_company'));
221 $error = $contact->insert;
222 die "error migrating service contact for customer $custnum: $error"
225 foreach ( grep { $unlike{$_} } qw( daytime night fax mobile ) ) {
226 my $phone = $cust_main->get("ship_$_");
228 my $contact_phone = FS::contact_phone->new({
229 'contactnum' => $contact->contactnum,
230 'phonetypenum' => $phone_type{$_}->phonetypenum,
231 FS::contact::_parse_phonestring( $phone )
233 $error = $contact_phone->insert;
234 # die "whose responsible this"
235 die "error migrating service contact phone for customer $custnum: $error"
237 $cust_main->set("ship_$_" => '');
240 $cust_main->set("ship_$_" => '') foreach qw(last first company);
244 # special case: should go with whichever location is used to calculate
245 # taxes, because that's the one it originally came from
246 if ( my $geocode = $cust_main->get('geocode') ) {
247 $bill_location->set('geocode' => '');
248 $ship_location->set('geocode' => '');
250 if ( $tax_prefix eq 'bill_' ) {
251 $bill_location->set('geocode', $geocode);
252 } elsif ( $tax_prefix eq 'ship_' ) {
253 $ship_location->set('geocode', $geocode);
257 # this always goes with the ship_location (whether it's the same as
258 # bill_location or not)
259 $ship_location->set('censustract', $cust_main->get('censustract'));
260 $ship_location->set('censusyear', $cust_main->get('censusyear'));
262 $error = $bill_location->insert;
263 die "error migrating billing address for customer $custnum: $error"
266 $cust_main->set(bill_locationnum => $bill_location->locationnum);
268 if (!$ship_location->locationnum) {
269 $error = $ship_location->insert;
270 die "error migrating service address for customer $custnum: $error"
274 $cust_main->set(ship_locationnum => $ship_location->locationnum);
276 # Step 3: Wipe the migrated fields and update the cust_main
278 $cust_main->set("ship_$_" => '') foreach location_fields();
279 $cust_main->set($_ => '') foreach location_fields();
281 $error = $cust_main->replace;
282 die "error migrating addresses for customer $custnum: $error"
285 # Step 4: set packages at the "default service location" to ship_location
286 foreach my $cust_pkg (
287 qsearch('cust_pkg', { custnum => $custnum, locationnum => '' })
289 # not a location change
290 $cust_pkg->set('locationnum', $cust_main->ship_locationnum);
291 $error = $cust_pkg->replace;
292 die "error migrating package ".$cust_pkg->pkgnum.": $error"
296 } #foreach $cust_main
298 # repair an error in earlier upgrades
299 if (!FS::upgrade_journal->is_done('cust_location_censustract_repair')
300 and FS::Conf->new->exists('cust_main-require_censustract') ) {
302 foreach my $cust_location (
303 qsearch('cust_location', { 'censustract' => '' })
305 my $custnum = $cust_location->custnum;
306 next if !$custnum; # avoid doing this for prospect locations
307 my $address1 = $cust_location->address1;
308 # find the last history record that had that address
309 my $last_h = qsearchs({
310 table => 'h_cust_main',
311 extra_sql => " WHERE custnum = $custnum AND address1 = ".
312 dbh->quote($address1) .
313 " AND censustract IS NOT NULL",
314 order_by => " ORDER BY history_date DESC LIMIT 1",
317 # this is normal; just means it never had a census tract before
320 $cust_location->set('censustract' => $last_h->get('censustract'));
321 $cust_location->set('censusyear' => $last_h->get('censusyear'));
322 my $error = $cust_location->replace;
323 warn "Error setting census tract for customer #$custnum:\n $error\n"
325 } # foreach $cust_location
326 FS::upgrade_journal->set_done('cust_location_censustract_repair');