X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2Fbin%2Ffreeside-censustract-update;h=49505ee818aec07bf3f89bf534e9ffbd97e81e74;hb=HEAD;hp=8c6721b3ed4ffa3628ced2f50d4fc9aa7c6bcd78;hpb=fb4ab1073f0d15d660c6cdc4e07afebf68ef3924;p=freeside.git diff --git a/FS/bin/freeside-censustract-update b/FS/bin/freeside-censustract-update old mode 100644 new mode 100755 index 8c6721b3e..49505ee81 --- a/FS/bin/freeside-censustract-update +++ b/FS/bin/freeside-censustract-update @@ -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); @@ -18,44 +18,57 @@ $FS::UID::AutoCommit = 0; my $dbh = dbh; my $conf = FS::Conf->new; -my $current_year = $conf->config('census_year') - or die "No current census year configured.\n"; +my $current_year = $conf->config('census_legacy') || '2020'; 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. +local($FS::Record::qsearch_qualify_columns) = 0; +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 - -my @cust_main = qsearch( 'cust_main', - { censusyear => { op => '!=', value => $current_year } }, -); # all possibly interesting customers - -warn scalar(@cust_main)." records found.\n"; + FS::h_cust_location->sql_h_search($date), + ) ; + +# Find all locations that don't have censusyear = the current +# year as of now. +my @cust_location = qsearch({ + 'table' => 'cust_location', + 'hashref' => { 'country' => 'US', }, + 'extra_sql' => " AND ( censusyear != '$current_year' + OR censustract IS NULL + ) + ", +}); + +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"; @@ -76,8 +89,8 @@ freeside-censustract-update - Update census tract codes to the current year. =head1 DESCRIPTION Finds all customers whose census tract codes don't appear to be current -and updates them to the current year. The "current year" is defined by -the I configuration variable, not the calendar year. +and updates them to the current year. The "current year" is 2020, unless the +I configuration variable is set. The -d option tells the script to assume that tract codes last modified after some date are already current. Those customers will just have