X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fcust_main%2FLocation.pm;h=52fe313c641897c14c5e8e882c6b423ea584a2d8;hb=a180208786cccb72ab017e39fff0cb128aa6ba01;hp=22feaf9c82ca65ab6d45042cbb7c7fa40ed77e23;hpb=3d0a1bb06b895c5be6e3f0517d355442a6b1e125;p=freeside.git diff --git a/FS/FS/cust_main/Location.pm b/FS/FS/cust_main/Location.pm index 22feaf9c8..52fe313c6 100644 --- a/FS/FS/cust_main/Location.pm +++ b/FS/FS/cust_main/Location.pm @@ -124,6 +124,11 @@ sub _upgrade_data { local $DEBUG = 0; my $error; + my $tax_prefix = 'bill_'; + if ( FS::Conf->new->exists('tax-ship_address') ) { + $tax_prefix = 'ship_'; + } + # Step 0: set up contact classes and phone types my $service_contact_class = qsearchs('contact_class', { classname => 'Service'}) @@ -160,7 +165,10 @@ sub _upgrade_data { map { $_ => $cust_main->get($_) } location_fields(), } ); - $bill_location->set('censustract', ''); # properly goes with ship_location + $bill_location->set('censustract', ''); + $bill_location->set('censusyear', ''); + # properly goes with ship_location; if they're the same, will be set + # on ship_location before inserting either one my $ship_location = $bill_location; # until proven otherwise if ( $cust_main->get('ship_address1') ) { @@ -182,8 +190,6 @@ sub _upgrade_data { ); } # else it stays equal to $bill_location - $ship_location->set('censustract', $cust_main->get('censustract')); - # Step 2: Extract shipping address contact fields into contact my %unlike = map { $_ => 1 } grep { $cust_main->get($_) ne $cust_main->get("ship_$_") } @@ -198,6 +204,14 @@ sub _upgrade_data { 'last' => $cust_main->get('ship_last'), 'first' => $cust_main->get('ship_first'), }); + if ( !$cust_main->get('ship_last') or !$cust_main->get('ship_first') ) + { + warn "customer $custnum has no service contact name; substituting ". + "customer name\n"; + $contact->set('last' => $cust_main->get('last')); + $contact->set('first' => $cust_main->get('first')); + } + if ( $unlike{'company'} ) { # there's no contact.company field, but keep a record of it $contact->set(comment => 'Company: '.$cust_main->get('ship_company')); @@ -224,6 +238,25 @@ sub _upgrade_data { $cust_main->set("ship_$_" => '') foreach qw(last first company); } #if %unlike } #if ship_address1 + + # special case: should go with whichever location is used to calculate + # taxes, because that's the one it originally came from + if ( my $geocode = $cust_main->get('geocode') ) { + $bill_location->set('geocode' => ''); + $ship_location->set('geocode' => ''); + + if ( $tax_prefix eq 'bill_' ) { + $bill_location->set('geocode', $geocode); + } elsif ( $tax_prefix eq 'ship_' ) { + $ship_location->set('geocode', $geocode); + } + } + + # this always goes with the ship_location (whether it's the same as + # bill_location or not) + $ship_location->set('censustract', $cust_main->get('censustract')); + $ship_location->set('censusyear', $cust_main->get('censusyear')); + $error = $bill_location->insert; die "error migrating billing address for customer $custnum: $error" if $error; @@ -259,6 +292,38 @@ sub _upgrade_data { } } #foreach $cust_main + + # repair an error in earlier upgrades + if (!FS::upgrade_journal->is_done('cust_location_censustract_repair') + and FS::Conf->new->exists('cust_main-require_censustract') ) { + + foreach my $cust_location ( + qsearch('cust_location', { 'censustract' => '' }) + ) { + my $custnum = $cust_location->custnum; + next if !$custnum; # avoid doing this for prospect locations + my $address1 = $cust_location->address1; + # find the last history record that had that address + my $last_h = qsearchs({ + table => 'h_cust_main', + extra_sql => " WHERE custnum = $custnum AND address1 = ". + dbh->quote($address1) . + " AND censustract IS NOT NULL", + order_by => " ORDER BY history_date DESC LIMIT 1", + }); + if (!$last_h) { + # this is normal; just means it never had a census tract before + next; + } + $cust_location->set('censustract' => $last_h->get('censustract')); + $cust_location->set('censusyear' => $last_h->get('censusyear')); + my $error = $cust_location->replace; + warn "Error setting census tract for customer #$custnum:\n $error\n" + if $error; + } # foreach $cust_location + FS::upgrade_journal->set_done('cust_location_censustract_repair'); + } + } =back