add customer fields option with agent, display_custnum, status and name, RT#73721
[freeside.git] / FS / bin / freeside-censustract-update
1 #!/usr/bin/perl
2
3 use strict;
4 use Getopt::Std;
5 use Date::Parse 'str2time';
6 use FS::UID qw(adminsuidsetup);
7 use FS::Record qw(qsearch dbh);
8 use FS::Conf;
9 use FS::cust_location;
10 use FS::h_cust_location;
11
12 my %opt;
13 getopts('d:', \%opt);
14
15 my $user = shift or die &usage;
16 adminsuidsetup($user);
17 $FS::UID::AutoCommit = 0;
18 my $dbh = dbh;
19
20 my $conf = FS::Conf->new;
21 my $current_year = $conf->config('census_year') 
22   or die "No current census year configured.\n";
23 my $date = str2time($opt{d}) if $opt{d};
24 $date ||= time;
25 # This now operates on cust_location, not cust_main.
26 # Find all locations that, as of $date, did not have 
27 # censusyear = the current year.  This includes those 
28 # that have no censusyear.
29 local($FS::Record::qsearch_qualify_columns) = 0;
30 my %h_cust_location = map { $_->locationnum => $_ }
31   qsearch(
32     'h_cust_location',
33     { censusyear => { op => '!=', value => $current_year } },
34     FS::h_cust_location->sql_h_search($date),
35   ) ;
36
37 # Find all locations that don't have censusyear = the current
38 # year as of now.
39 my @cust_location = qsearch( 'cust_location',
40   { censusyear => { op => '!=', value => $current_year } },
41 );
42
43 warn scalar(@cust_location)." records found.\n";
44 my $queued = 0; my $updated = 0;
45 foreach my $cust_location (@cust_location) {
46   my $error;
47   my $h = $h_cust_location{$cust_location->locationnum};
48   if ( defined($h) and $h->censustract eq $cust_location->censustract ) {
49     # Then the location's censustract hasn't been changed since $date
50     # (or it didn't exist on $date, or $date is now).  Queue a censustract 
51     # update for it.
52     my $job = FS::queue->new({
53         job => 'FS::cust_location::process_censustract_update'
54     });
55     $error = $job->insert($cust_location->locationnum);
56     $queued++;
57   }
58   elsif ($cust_location->censusyear eq '') {
59     # Then it's been updated since $date, but somehow has a null censusyear.
60     # (Is this still relevant?)
61     $cust_location->set('censusyear', $current_year);
62     $error = $cust_location->replace;
63     $updated++;
64   } # Else it's been updated since $date, so leave it alone.
65   if ( $error ) {
66     $dbh->rollback;
67     die "error updating ".$cust_location->locationnum.": $error\n";
68   }
69 }
70 warn "Queued $queued census code lookups, updated year in $updated records.\n";
71 $dbh->commit;
72
73 sub usage {
74     "Usage:\n\n  freeside-censustract-update [ -d date ] user\n\n"
75   }
76
77 =head1 NAME
78
79 freeside-censustract-update - Update census tract codes to the current year.
80
81 =head1 SYNOPSIS
82
83   freeside-censustract-update [ -d date ] user
84
85 =head1 DESCRIPTION
86
87 Finds all customers whose census tract codes don't appear to be current 
88 and updates them to the current year.  The "current year" is defined by 
89 the I<census_tract> configuration variable, not the calendar year.
90
91 The -d option tells the script to assume that tract codes last modified
92 after some date are already current.  Those customers will just have 
93 their 'censusyear' field set to the current year.  For all other 
94 customers with non-current censusyear values, the current tract code 
95 will be looked up externally and stored in the censustract field.
96
97 The actual tract code lookup runs from the job queue, because it's slow.
98 A separate job will be created for each customer.
99
100 =cut