1 package FS::cust_main::Location;
4 use vars qw( $DEBUG $me @location_fields );
5 use FS::Record qw(qsearch qsearchs);
13 $me = '[FS::cust_main::Location]';
17 # set up accessors for location fields
21 qw( address1 address2 city county state zip country district
22 latitude longitude coord_auto censustract censusyear geocode
25 foreach my $f (@location_fields) {
26 *{"FS::cust_main::Location::$f"} = sub {
27 carp "WARNING: tried to set cust_main.$f with accessor" if (@_ > 1);
28 my $l = shift->bill_location;
31 *{"FS::cust_main::Location::ship_$f"} = sub {
32 carp "WARNING: tried to set cust_main.ship_$f with accessor" if (@_ > 1);
33 my $l = shift->ship_location;
41 #debugging shim--probably a performance hit, so remove this at some point
45 if ( $DEBUG and grep (/^(ship_)?($field)$/, @location_fields) ) {
46 carp "WARNING: tried to get() location field $field";
49 $self->FS::Record::get($field);
54 FS::cust_main::Location - Location-related methods for cust_main
58 These methods are available on FS::cust_main objects;
66 Returns an L<FS::cust_location> object for the customer's billing address.
72 $self->hashref->{bill_location}
73 ||= FS::cust_location->by_key($self->bill_locationnum)
74 # degraded mode--let the system keep running during upgrades
75 || FS::cust_location->new({
76 map { $_ => $self->get($_) } @location_fields
82 Returns an L<FS::cust_location> object for the customer's service address.
88 $self->hashref->{ship_location}
89 ||= FS::cust_location->by_key($self->ship_locationnum)
90 || FS::cust_location->new({
91 map { $_ => $self->get('ship_'.$_) || $self->get($_) } @location_fields
98 An alternative way of saying "bill_location or ship_location, depending on
99 if TYPE is 'bill' or 'ship'".
105 return $self->bill_location if $_[0] eq 'bill';
106 return $self->ship_location if $_[0] eq 'ship';
107 die "bad location type '$_[0]'";
116 =item location_fields
118 Returns a list of fields found in the location objects. All of these fields
119 can be read (but not written) by calling them as methods on the
120 L<FS::cust_main> object (prefixed with 'ship_' for the service address
125 sub location_fields { @location_fields }
131 eval "use FS::contact;
132 use FS::contact_class;
133 use FS::contact_phone;
136 local $FS::cust_location::import = 1;
140 # Step 0: set up contact classes and phone types
141 my $service_contact_class =
142 qsearchs('contact_class', { classname => 'Service'})
143 || new FS::contact_class { classname => 'Service'};
145 if ( !$service_contact_class->classnum ) {
146 warn "Creating service contact class.\n";
147 $error = $service_contact_class->insert;
148 die "error creating contact class for Service: $error" if $error;
150 my %phone_type = ( # fudge slightly
157 foreach (keys %phone_type) {
158 $phone_type{$_} = qsearchs('phone_type', { typename => $phone_type{$_}})
159 || new FS::phone_type { typename => $phone_type{$_},
161 # just in case someone still doesn't have these
162 if ( !$phone_type{$_}->phonetypenum ) {
163 $error = $phone_type{$_}->insert;
164 die "error creating phone type '$_': $error" if $error;
168 my $num_to_upgrade = FS::cust_main->count('bill_locationnum is null or ship_locationnum is null');
169 my $num_jobs = FS::queue->count('job = \'FS::cust_main::Location::process_upgrade_location\' and status != \'failed\'');
170 if ( $num_to_upgrade > 0 ) {
171 warn "Need to migrate $num_to_upgrade customer locations.\n";
174 if ( $num_jobs > 0 ) {
175 warn "Upgrade already queued.\n";
177 warn "Scheduling upgrade.\n";
178 my $job = FS::queue->new({ job => 'FS::cust_main::Location::process_upgrade_location' });
182 process_upgrade_location();
187 # repair an error in earlier upgrades
188 if (!FS::upgrade_journal->is_done('cust_location_censustract_repair')
189 and FS::Conf->new->exists('cust_main-require_censustract') ) {
191 foreach my $cust_location (
192 qsearch('cust_location', { 'censustract' => '' })
194 my $custnum = $cust_location->custnum;
195 next if !$custnum; # avoid doing this for prospect locations
196 my $address1 = $cust_location->address1;
197 # find the last history record that had that address
198 my $last_h = qsearchs({
199 table => 'h_cust_main',
200 extra_sql => " WHERE custnum = $custnum AND address1 = ".
201 dbh->quote($address1) .
202 " AND censustract IS NOT NULL",
203 order_by => " ORDER BY history_date DESC LIMIT 1",
206 # this is normal; just means it never had a census tract before
209 $cust_location->set('censustract' => $last_h->get('censustract'));
210 $cust_location->set('censusyear' => $last_h->get('censusyear'));
211 my $error = $cust_location->replace;
212 warn "Error setting census tract for customer #$custnum:\n $error\n"
214 } # foreach $cust_location
215 FS::upgrade_journal->set_done('cust_location_censustract_repair');
219 sub process_upgrade_location {
223 local $FS::cust_main::import = 1;
224 local $FS::cust_location::import = 1;
225 local $FS::contact::skip_fuzzyfiles = 1;
226 local $FS::UID::AutoCommit = 0;
228 my $tax_prefix = 'bill_';
229 if ( FS::Conf->new->exists('tax-ship_address') ) {
230 $tax_prefix = 'ship_';
233 # load some records that were created during the initial upgrade
234 my $service_contact_class =
235 qsearchs('contact_class', { classname => 'Service'});
243 foreach (keys %phone_type) {
244 $phone_type{$_} = qsearchs('phone_type', { typename => $phone_type{$_}});
248 tax_prefix => $tax_prefix,
249 service_contact_class => $service_contact_class,
250 phone_type => \%phone_type,
253 my $search = FS::Cursor->new('cust_main',
254 { bill_locationnum => '',
255 address1 => { op=>'!=', value=>'' }
257 while (my $cust_main = $search->fetch) {
258 my $error = $cust_main->upgrade_location(%opt);
260 warn "cust#".$cust_main->custnum.": $error\n";
269 sub upgrade_location { # instance method
270 my $cust_main = shift;
274 # Step 1: extract billing and service addresses into cust_location
275 my $custnum = $cust_main->custnum;
276 my $bill_location = FS::cust_location->new(
279 map { $_ => $cust_main->get($_) } location_fields(),
282 $bill_location->set('censustract', '');
283 $bill_location->set('censusyear', '');
284 # properly goes with ship_location; if they're the same, will be set
285 # on ship_location before inserting either one
286 my $ship_location = $bill_location; # until proven otherwise
288 if ( $cust_main->get('ship_address1') ) {
291 foreach (location_fields()) {
292 if ( length($cust_main->get("ship_$_")) and
293 $cust_main->get($_) ne $cust_main->get("ship_$_") ) {
299 $ship_location = FS::cust_location->new(
302 map { $_ => $cust_main->get("ship_$_") } location_fields()
305 } # else it stays equal to $bill_location
307 # Step 2: Extract shipping address contact fields into contact
308 my %unlike = map { $_ => 1 }
309 grep { $cust_main->get($_) ne $cust_main->get("ship_$_") }
310 qw( last first company daytime night fax mobile );
313 # then there IS a service contact
314 my $contact = FS::contact->new({
315 'custnum' => $custnum,
316 'classnum' => $opt{service_contact_class}->classnum,
317 'locationnum' => $ship_location->locationnum,
318 'last' => $cust_main->get('ship_last'),
319 'first' => $cust_main->get('ship_first'),
321 if ( !$cust_main->get('ship_last') or !$cust_main->get('ship_first') )
323 warn "customer $custnum has no service contact name; substituting ".
325 $contact->set('last' => $cust_main->get('last'));
326 $contact->set('first' => $cust_main->get('first'));
329 if ( $unlike{'company'} ) {
330 # there's no contact.company field, but keep a record of it
331 $contact->set(comment => 'Company: '.$cust_main->get('ship_company'));
333 $error = $contact->insert;
334 return "error migrating service contact for customer $custnum: $error"
337 foreach ( grep { $unlike{$_} } qw( daytime night fax mobile ) ) {
338 my $phone = $cust_main->get("ship_$_");
340 my $contact_phone = FS::contact_phone->new({
341 'contactnum' => $contact->contactnum,
342 'phonetypenum' => $opt{phone_type}->{$_}->phonetypenum,
343 FS::contact::_parse_phonestring( $phone )
345 $error = $contact_phone->insert;
346 return "error migrating service contact phone for customer $custnum: $error"
348 $cust_main->set("ship_$_" => '');
351 $cust_main->set("ship_$_" => '') foreach qw(last first company);
355 # special case: should go with whichever location is used to calculate
356 # taxes, because that's the one it originally came from
357 if ( my $geocode = $cust_main->get('geocode') ) {
358 $bill_location->set('geocode' => '');
359 $ship_location->set('geocode' => '');
361 if ( $opt{tax_prefix} eq 'bill_' ) {
362 $bill_location->set('geocode', $geocode);
363 } elsif ( $opt{tax_prefix} eq 'ship_' ) {
364 $ship_location->set('geocode', $geocode);
368 # this always goes with the ship_location (whether it's the same as
369 # bill_location or not)
370 $ship_location->set('censustract', $cust_main->get('censustract'));
371 $ship_location->set('censusyear', $cust_main->get('censusyear'));
373 $error = $bill_location->insert;
374 return "error migrating billing address for customer $custnum: $error"
377 $cust_main->set(bill_locationnum => $bill_location->locationnum);
379 if (!$ship_location->locationnum) {
380 $error = $ship_location->insert;
381 return "error migrating service address for customer $custnum: $error"
385 $cust_main->set(ship_locationnum => $ship_location->locationnum);
387 # Step 3: Wipe the migrated fields and update the cust_main
389 $cust_main->set("ship_$_" => '') foreach location_fields();
390 $cust_main->set($_ => '') foreach location_fields();
392 $error = $cust_main->replace;
393 return "error migrating addresses for customer $custnum: $error"
396 # Step 4: set packages at the "default service location" to ship_location
398 FS::Cursor->new('cust_pkg', { custnum => $custnum, locationnum => '' });
399 while (my $cust_pkg = $pkg_search->fetch) {
400 # not a location change
401 $cust_pkg->set('locationnum', $cust_main->ship_locationnum);
402 $error = $cust_pkg->replace;
403 return "error migrating package ".$cust_pkg->pkgnum.": $error"