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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
package FS::state;
use strict;
use base qw( FS::Record );
use FS::Record qw( qsearch qsearchs );
use Locale::SubCountry;
=head1 NAME
FS::state - Object methods for state/province records
=head1 SYNOPSIS
use FS::state;
$record = new FS::state \%hash;
$record = new FS::state { 'column' => 'value' };
$error = $record->insert;
$error = $new_record->replace($old_record);
$error = $record->delete;
$error = $record->check;
=head1 DESCRIPTION
An FS::state object represents a state, province, or other top-level
subdivision of a sovereign nation. FS::state inherits from FS::Record.
The following fields are currently supported:
=over 4
=item statenum
primary key
=item country
two-letter country code
=item state
state code/abbreviation/name (as used in cust_location.state)
=item fips
FIPS 10-4 code (not including country code)
=back
=head1 METHODS
=cut
sub table { 'state'; }
# no external API; this table maintains itself
sub check {
my $self = shift;
my $error =
$self->ut_numbern('statenum')
|| $self->ut_alpha('country')
|| $self->ut_alpha('state')
|| $self->ut_alpha('fips')
;
return $error if $error;
$self->SUPER::check;
}
=back
=cut
our %state2fips = (
'AL' => '01',
'AK' => '02',
'AZ' => '04',
'AR' => '05',
'CA' => '06',
'CO' => '08',
'CT' => '09',
'DE' => '10',
'DC' => '11',
'FL' => '12',
'GA' => '13',
'HI' => '15',
'ID' => '16',
'IL' => '17',
'IN' => '18',
'IA' => '19',
'KS' => '20',
'KY' => '21',
'LA' => '22',
'ME' => '23',
'MD' => '24',
'MA' => '25',
'MI' => '26',
'MN' => '27',
'MS' => '28',
'MO' => '29',
'MT' => '30',
'NE' => '31',
'NV' => '32',
'NH' => '33',
'NJ' => '34',
'NM' => '35',
'NY' => '36',
'NC' => '37',
'ND' => '38',
'OH' => '39',
'OK' => '40',
'OR' => '41',
'PA' => '42',
'RI' => '44',
'SC' => '45',
'SD' => '46',
'TN' => '47',
'TX' => '48',
'UT' => '49',
'VT' => '50',
'VA' => '51',
'WA' => '53',
'WV' => '54',
'WI' => '55',
'WY' => '56',
'AS' => '60', #American Samoa
'GU' => '66', #Guam
'MP' => '69', #Northern Mariana Islands
'PR' => '72', #Puerto Rico
'VI' => '78', #Virgin Islands
);
sub _upgrade_data {
# we only need U.S. state codes at this point (for FCC 477 reporting)
warn "Updating state FIPS codes...\n";
my %existing;
foreach my $state ( qsearch('state', {'country'=>'US'}) ) {
$existing{$state->country} ||= {};
$existing{$state->country}{$state->state} = $state;
}
foreach my $country_code ('US') {
my $country = Locale::SubCountry->new($country_code);
next unless $country->has_sub_countries;
$existing{$country} ||= {};
foreach my $state_code ($country->all_codes) {
my $fips = $state2fips{$state_code} || next;
my $this_state = $existing{$country_code}{$state_code};
if ($this_state) {
if ($this_state->fips ne $fips) { # this should never happen...
die "guru meditation #414: State FIPS codes shouldn't change";
#$this_state->set(fips => $fips);
#my $error = $this_state->replace;
#die "error updating $country_code/$state_code:\n$error\n" if $error;
}
delete $existing{$country_code}{$state_code};
} else {
$this_state = FS::state->new({
country => $country_code,
state => $state_code,
fips => $fips,
});
my $error = $this_state->insert;
die "error inserting $country_code/$state_code:\n$error\n" if $error;
}
}
# clean up states that no longer exist (does this ever happen?)
foreach my $state (values %{ $existing{$country_code} }) {
die "guru meditation #415: State that no longer exists?";
#my $error = $state->delete;
#die "error removing expired state ".$state->country.'/'.$state->state.
# "\n$error\n" if $error;
}
} # foreach $country_code
'';
}
=head1 BUGS
=head1 SEE ALSO
L<FS::Record>
=cut
1;
|