+ my $tax_prefix = 'bill_';
+ if ( FS::Conf->new->exists('tax-ship_address') ) {
+ $tax_prefix = 'ship_';
+ }
+
+ # load some records that were created during the initial upgrade
+ my $service_contact_class =
+ qsearchs('contact_class', { classname => 'Service'});
+
+ my %phone_type = (
+ daytime => 'Work',
+ night => 'Home',
+ mobile => 'Mobile',
+ fax => 'Fax'
+ );
+ foreach (keys %phone_type) {
+ $phone_type{$_} = qsearchs('phone_type', { typename => $phone_type{$_}});
+ }
+
+ my %opt = (
+ tax_prefix => $tax_prefix,
+ service_contact_class => $service_contact_class,
+ phone_type => \%phone_type,
+ );
+
+ my $search = FS::Cursor->new('cust_main', { bill_locationnum => '' });
+ while (my $cust_main = $search->fetch) {
+ my $error = $cust_main->upgrade_location(%opt);
+ if ( $error ) {
+ warn "cust#".$cust_main->custnum.": $error\n";
+ $dbh->rollback;
+ } else {
+ # commit as we go
+ $dbh->commit;
+ }
+ }
+}
+
+sub upgrade_location { # instance method
+ my $cust_main = shift;
+ my %opt = @_;
+ my $error;
+
+ # Step 1: extract billing and service addresses into cust_location
+ my $custnum = $cust_main->custnum;
+ my $bill_location = FS::cust_location->new(
+ {
+ custnum => $custnum,
+ map { $_ => $cust_main->get($_) } location_fields(),
+ }
+ );
+ $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') ) {
+ # detect duplicates
+ my $same = 1;
+ foreach (location_fields()) {
+ if ( length($cust_main->get("ship_$_")) and
+ $cust_main->get($_) ne $cust_main->get("ship_$_") ) {
+ $same = 0;
+ }
+ }
+
+ if ( !$same ) {
+ $ship_location = FS::cust_location->new(