5 use Date::Parse 'str2time';
6 use FS::UID qw(adminsuidsetup);
7 use FS::Record qw(qsearch dbh);
10 use FS::h_cust_location;
15 my $user = shift or die &usage;
16 adminsuidsetup($user);
17 $FS::UID::AutoCommit = 0;
20 my $conf = FS::Conf->new;
21 my $current_year = $conf->config('census_legacy') || '2020';
22 my $date = str2time($opt{d}) if $opt{d};
24 # This now operates on cust_location, not cust_main.
25 # Find all locations that, as of $date, did not have
26 # censusyear = the current year. This includes those
27 # that have no censusyear.
28 my %h_cust_location = map { $_->locationnum => $_ }
31 { censusyear => { op => '!=', value => $current_year } },
32 FS::h_cust_location->sql_h_search($date),
35 # Find all locations that don't have censusyear = the current
37 my @cust_location = qsearch({
38 'table' => 'cust_location',
39 'hashref' => { 'country' => 'US', },
40 'extra_sql' => " AND ( censusyear != '$current_year'
41 OR censustract IS NULL
46 warn scalar(@cust_location)." records found.\n";
47 my $queued = 0; my $updated = 0;
48 foreach my $cust_location (@cust_location) {
50 my $h = $h_cust_location{$cust_location->locationnum};
51 if ( defined($h) and $h->censustract eq $cust_location->censustract ) {
52 # Then the location's censustract hasn't been changed since $date
53 # (or it didn't exist on $date, or $date is now). Queue a censustract
55 my $job = FS::queue->new({
56 job => 'FS::cust_location::process_censustract_update'
58 $error = $job->insert($cust_location->locationnum);
61 elsif ($cust_location->censusyear eq '') {
62 # Then it's been updated since $date, but somehow has a null censusyear.
63 # (Is this still relevant?)
64 $cust_location->set('censusyear', $current_year);
65 $error = $cust_location->replace;
67 } # Else it's been updated since $date, so leave it alone.
70 die "error updating ".$cust_location->locationnum.": $error\n";
73 warn "Queued $queued census code lookups, updated year in $updated records.\n";
77 "Usage:\n\n freeside-censustract-update [ -d date ] user\n\n"
82 freeside-censustract-update - Update census tract codes to the current year.
86 freeside-censustract-update [ -d date ] user
90 Finds all customers whose census tract codes don't appear to be current
91 and updates them to the current year. The "current year" is 2020, unless the
92 I<census_legacy> configuration variable is set.
94 The -d option tells the script to assume that tract codes last modified
95 after some date are already current. Those customers will just have
96 their 'censusyear' field set to the current year. For all other
97 customers with non-current censusyear values, the current tract code
98 will be looked up externally and stored in the censustract field.
100 The actual tract code lookup runs from the job queue, because it's slow.
101 A separate job will be created for each customer.