summaryrefslogtreecommitdiff
path: root/FS/bin/freeside-censustract-update
blob: f9b6d119705322fb70a75c9b464ed1ea2ccbbc2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/perl

use strict;
use Getopt::Std;
use Date::Parse 'str2time';
use FS::UID qw(adminsuidsetup);
use FS::Record qw(qsearch dbh);
use FS::Conf;
use FS::cust_location;
use FS::h_cust_location;

my %opt;
getopts('d:', \%opt);

my $user = shift or die &usage;
adminsuidsetup($user);
$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 $date = str2time($opt{d}) if $opt{d};
$date ||= time;
# 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_location',
    { censusyear => { op => '!=', value => $current_year } },
    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( 'cust_location',
  { censusyear => { op => '!=', value => $current_year } },
);

warn scalar(@cust_location)." records found.\n";
my $queued = 0; my $updated = 0;
foreach my $cust_location (@cust_location) {
  my $error;
  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_location::process_censustract_update'
    });
    $error = $job->insert($cust_location->locationnum);
    $queued++;
  }
  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_location->locationnum.": $error\n";
  }
}
warn "Queued $queued census code lookups, updated year in $updated records.\n";
$dbh->commit;

sub usage {
    "Usage:\n\n  freeside-censustract-update [ -d date ] user\n\n"
  }

=head1 NAME

freeside-censustract-update - Update census tract codes to the current year.

=head1 SYNOPSIS

  freeside-censustract-update [ -d date ] user

=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<census_tract> configuration variable, not the calendar year.

The -d option tells the script to assume that tract codes last modified
after some date are already current.  Those customers will just have 
their 'censusyear' field set to the current year.  For all other 
customers with non-current censusyear values, the current tract code 
will be looked up externally and stored in the censustract field.

The actual tract code lookup runs from the job queue, because it's slow.
A separate job will be created for each customer.

=cut