freeside-censustract-update + cust_location, #23422
authorMark Wells <mark@freeside.biz>
Thu, 6 Jun 2013 06:12:30 +0000 (23:12 -0700)
committerMark Wells <mark@freeside.biz>
Thu, 6 Jun 2013 06:12:30 +0000 (23:12 -0700)
FS/FS/cust_location.pm
FS/FS/cust_main.pm
FS/FS/h_cust_location.pm [new file with mode: 0644]
FS/bin/freeside-censustract-update

index d772dab..a834859 100644 (file)
@@ -643,6 +643,41 @@ sub in_county_sql {
   }
 }
 
+=back
+
+=head2 SUBROUTINES
+
+=over 4
+
+=item process_censustract_update LOCATIONNUM
+
+Queueable function to update the census tract to the current year (as set in 
+the 'census_year' configuration variable) and retrieve the new tract code.
+
+=cut
+
+sub process_censustract_update {
+  eval "use FS::GeocodeCache";
+  die $@ if $@;
+  my $locationnum = shift;
+  my $cust_location = 
+    qsearchs( 'cust_location', { locationnum => $locationnum })
+      or die "locationnum '$locationnum' not found!\n";
+
+  my $conf = FS::Conf->new;
+  my $new_year = $conf->config('census_year') or return;
+  my $loc = FS::GeocodeCache->new( $cust_location->location_hash );
+  $loc->set_censustract;
+  my $error = $loc->get('censustract_error');
+  die $error if $error;
+  $cust_location->set('censustract', $loc->get('censustract'));
+  $cust_location->set('censusyear',  $new_year);
+  $error = $cust_location->replace;
+  die $error if $error;
+  return;
+}
+
+
 sub process_set_coord {
   my $job = shift;
   # avoid starting multiple instances of this job
index 1d6e845..f4c3beb 100644 (file)
@@ -5017,42 +5017,6 @@ sub process_bill_and_collect {
   $cust_main->bill_and_collect( %$param );
 }
 
-=item process_censustract_update CUSTNUM
-
-Queueable function to update the census tract to the current year (as set in 
-the 'census_year' configuration variable) and retrieve the new tract code.
-
-=cut
-
-sub process_censustract_update { 
-  eval "use FS::Misc::Geo qw(get_censustract)";
-  die $@ if $@;
-  my $custnum = shift;
-  my $cust_main = qsearchs( 'cust_main', { custnum => $custnum })
-      or die "custnum '$custnum' not found!\n";
-
-  my $new_year = $conf->config('census_year') or return;
-  my $new_tract = get_censustract({ $cust_main->location_hash }, $new_year);
-  if ( $new_tract =~ /^\d/ ) {
-    # then it's a tract code
-        $cust_main->set('censustract', $new_tract);
-    $cust_main->set('censusyear',  $new_year);
-
-    local($ignore_expired_card) = 1;
-    local($ignore_illegal_zip) = 1;
-    local($ignore_banned_card) = 1;
-    local($skip_fuzzyfiles) = 1;
-    local($import) = 1; #prevent automatic geocoding (need its own variable?)
-    my $error = $cust_main->replace;
-    die $error if $error;
-  }
-  else {
-    # it's an error message
-    die $new_tract;
-  }
-  return;
-}
-
 #starting to take quite a while for big dbs
 #   (JRNL: journaled so it only happens once per database)
 # - seq scan of h_cust_main (yuck), but not going to index paycvv, so
diff --git a/FS/FS/h_cust_location.pm b/FS/FS/h_cust_location.pm
new file mode 100644 (file)
index 0000000..fc3a930
--- /dev/null
@@ -0,0 +1,26 @@
+package FS::h_cust_location;
+
+use strict;
+use base qw( FS::h_Common FS::cust_location );
+
+sub table { 'h_cust_location' };
+
+=head1 NAME
+
+FS::h_cust_location - Historical customer location records.
+
+=head1 DESCRIPTION
+
+An FS::h_cust_location object represents historical changes to a customer
+location record.  These records normally don't change, so this isn't 
+terribly useful.
+
+=head1 SEE ALSO
+
+L<FS::cust_location>,  L<FS::h_Common>, L<FS::Record>, schema.html from the 
+base documentation.
+
+=cut
+
+1;
+
index 8c6721b..af9ad74 100644 (file)
@@ -6,8 +6,8 @@ use Date::Parse 'str2time';
 use FS::UID qw(adminsuidsetup);
 use FS::Record qw(qsearch dbh);
 use FS::Conf;
-use FS::cust_main;
-use FS::h_cust_main;
+use FS::cust_location;
+use FS::h_cust_location;
 
 my %opt;
 getopts('d:', \%opt);
@@ -22,40 +22,48 @@ my $current_year = $conf->config('census_year')
   or die "No current census year configured.\n";
 my $date = str2time($opt{d}) if $opt{d};
 $date ||= time;
-my %h_cust_main = map { $_->custnum => $_ }
+# This now operates on cust_location, not cust_main.
+# Find all locations that, as of $date, did not have 
+# censusyear = the current year.  This includes those 
+# that have no censusyear.
+my %h_cust_location = map { $_->locationnum => $_ }
   qsearch(
-    'h_cust_main',
+    'h_cust_location',
     { censusyear => { op => '!=', value => $current_year } },
-    FS::h_cust_main->sql_h_search($date),
-  ) ; #the state of these customers as of $date
+    FS::h_cust_location->sql_h_search($date),
+  ) ;
 
-my @cust_main = qsearch( 'cust_main',
+# Find all locations that don't have censusyear = the current
+# year as of now.
+my @cust_location = qsearch( 'cust_location',
   { censusyear => { op => '!=', value => $current_year } },
-); # all possibly interesting customers
+);
 
-warn scalar(@cust_main)." records found.\n";
+warn scalar(@cust_location)." records found.\n";
 my $queued = 0; my $updated = 0;
-foreach my $cust_main (@cust_main) {
+foreach my $cust_location (@cust_location) {
   my $error;
-  my $h = $h_cust_main{$cust_main->custnum};
-  if ( defined($h) and $h->censustract eq $cust_main->censustract ) {
-    # the tract code hasn't been changed since $date
-    # so update it now
+  my $h = $h_cust_location{$cust_location->locationnum};
+  if ( defined($h) and $h->censustract eq $cust_location->censustract ) {
+    # Then the location's censustract hasn't been changed since $date
+    # (or it didn't exist on $date, or $date is now).  Queue a censustract 
+    # update for it.
     my $job = FS::queue->new({
-        job => 'FS::cust_main::process_censustract_update'
+        job => 'FS::cust_location::process_censustract_update'
     });
-    $error = $job->insert($cust_main->custnum);
+    $error = $job->insert($cust_location->locationnum);
     $queued++;
   }
-  elsif ($cust_main->censusyear eq '') {
-    # the tract number is assumed current, so just set the year
-    $cust_main->set('censusyear', $current_year);
-    $error = $cust_main->replace;
+  elsif ($cust_location->censusyear eq '') {
+    # Then it's been updated since $date, but somehow has a null censusyear.
+    # (Is this still relevant?)
+    $cust_location->set('censusyear', $current_year);
+    $error = $cust_location->replace;
     $updated++;
-  }
+  } # Else it's been updated since $date, so leave it alone.
   if ( $error ) {
     $dbh->rollback;
-    die "error updating ".$cust_main->custnum.": $error\n";
+    die "error updating ".$cust_location->locationnum.": $error\n";
   }
 }
 warn "Queued $queued census code lookups, updated year in $updated records.\n";