4 use base qw( FS::Record );
5 use FS::Record qw( qsearch qsearchs );
6 use Locale::SubCountry;
10 FS::state - Object methods for state/province records
16 $record = new FS::state \%hash;
17 $record = new FS::state { 'column' => 'value' };
19 $error = $record->insert;
21 $error = $new_record->replace($old_record);
23 $error = $record->delete;
25 $error = $record->check;
29 An FS::state object represents a state, province, or other top-level
30 subdivision of a sovereign nation. FS::state inherits from FS::Record.
31 The following fields are currently supported:
41 two-letter country code
45 state code/abbreviation/name (as used in cust_location.state)
49 FIPS 10-4 code (not including country code)
57 sub table { 'state'; }
59 # no external API; this table maintains itself
65 $self->ut_numbern('statenum')
66 || $self->ut_alpha('country')
67 || $self->ut_alpha('state')
68 || $self->ut_alpha('fips')
70 return $error if $error;
132 'AS' => '60', #American Samoa
134 'MP' => '69', #Northern Mariana Islands
135 'PR' => '72', #Puerto Rico
136 'VI' => '78', #Virgin Islands
140 # we only need U.S. state codes at this point (for FCC 477 reporting)
141 warn "Updating state FIPS codes...\n";
143 foreach my $state ( qsearch('state', {'country'=>'US'}) ) {
144 $existing{$state->country} ||= {};
145 $existing{$state->country}{$state->state} = $state;
147 foreach my $country_code ('US') {
148 my $country = Locale::SubCountry->new($country_code);
149 next unless $country->has_sub_countries;
150 $existing{$country} ||= {};
151 foreach my $state_code ($country->all_codes) {
152 my $fips = $state2fips{$state_code} || next;
153 my $this_state = $existing{$country_code}{$state_code};
155 if ($this_state->fips ne $fips) { # this should never happen...
156 die "guru meditation #414: State FIPS codes shouldn't change";
157 #$this_state->set(fips => $fips);
158 #my $error = $this_state->replace;
159 #die "error updating $country_code/$state_code:\n$error\n" if $error;
161 delete $existing{$country_code}{$state_code};
163 $this_state = FS::state->new({
164 country => $country_code,
165 state => $state_code,
168 my $error = $this_state->insert;
169 die "error inserting $country_code/$state_code:\n$error\n" if $error;
172 # clean up states that no longer exist (does this ever happen?)
173 foreach my $state (values %{ $existing{$country_code} }) {
174 die "guru meditation #415: State that no longer exists?";
175 #my $error = $state->delete;
176 #die "error removing expired state ".$state->country.'/'.$state->state.
177 # "\n$error\n" if $error;
179 } # foreach $country_code