RT# 83401 Send country field to B::OP on tokenize
[freeside.git] / FS / FS / cust_main / Location.pm
index 560736d..3cb73ff 100644 (file)
@@ -17,10 +17,12 @@ BEGIN {
   # set up accessors for location fields
   if (!$init) {
     no strict 'refs';
-    @location_fields = 
-      qw( address1 address2 city county state zip country district
-        latitude longitude coord_auto censustract censusyear geocode
-        addr_clean );
+    @location_fields = qw(
+      locationname
+      address1 address2 city county state zip country
+      district latitude longitude coord_auto censustract censusyear geocode
+      addr_clean
+    );
 
     foreach my $f (@location_fields) {
       *{"FS::cust_main::Location::$f"} = sub {
@@ -72,9 +74,11 @@ sub bill_location {
   $self->hashref->{bill_location} 
     ||= FS::cust_location->by_key($self->bill_locationnum)
     # degraded mode--let the system keep running during upgrades
-    ||  FS::cust_location->new({
-        map { $_ => $self->get($_) } @location_fields
-      })
+    ||  ( $self->get('address1')
+            && FS::cust_location->new({
+                 map { $_ => $self->get($_) } @location_fields
+               })
+        );
 }
 
 =item ship_location
@@ -87,9 +91,17 @@ sub ship_location {
   my $self = shift;
   $self->hashref->{ship_location}
     ||= FS::cust_location->by_key($self->ship_locationnum)
-    ||  FS::cust_location->new({
-        map { $_ => $self->get('ship_'.$_) || $self->get($_) } @location_fields
-      })
+    # degraded mode--let the system keep running during upgrades
+    ||  ( $self->get('ship_address1')
+            ? FS::cust_location->new({
+                map { $_ => $self->get('ship_'.$_) } @location_fields
+              })
+            : $self->get('address1')
+                ? FS::cust_location->new({
+                    map { $_ => $self->get($_) } @location_fields
+                  })
+                : ''
+        );
 
 }
 
@@ -126,6 +138,8 @@ sub location_fields { @location_fields }
 
 sub _upgrade_data {
   my $class = shift;
+  my %opt = @_;
+
   eval "use FS::contact;
         use FS::contact_class;
         use FS::contact_phone;
@@ -167,12 +181,17 @@ sub _upgrade_data {
   my $num_jobs = FS::queue->count('job = \'FS::cust_main::Location::process_upgrade_location\' and status != \'failed\'');
   if ( $num_to_upgrade > 0 ) {
     warn "Need to migrate $num_to_upgrade customer locations.\n";
-    if ( $num_jobs > 0 ) {
-      warn "Upgrade already queued.\n";
-    } else {
-      warn "Scheduling upgrade.\n";
-      my $job = FS::queue->new({ job => 'FS::cust_main::Location::process_upgrade_location' });
-      $job->insert;
+
+    if ( $opt{queue} ) {
+      if ( $num_jobs > 0 ) {
+        warn "Upgrade already queued.\n";
+      } else {
+        warn "Scheduling upgrade.\n";
+        my $job = FS::queue->new({ job => 'FS::cust_main::Location::process_upgrade_location' });
+        $job->insert;
+      }
+    } else { #do it now
+      process_upgrade_location();
     }
 
   }
@@ -213,7 +232,9 @@ sub process_upgrade_location {
   my $class = shift;
 
   my $dbh = dbh;
+  local $FS::cust_main::import = 1;
   local $FS::cust_location::import = 1;
+  local $FS::contact::skip_fuzzyfiles = 1;
   local $FS::UID::AutoCommit = 0;
 
   my $tax_prefix = 'bill_';