RT# 82137 - default payment amount now has processing fee in total if processing...
[freeside.git] / bin / populate-areacodes
1 #!/usr/bin/perl
2
3 use FS::UID qw(adminsuidsetup dbh);
4 use FS::Record;
5 use FS::areacode;
6 use Locale::SubCountry;
7
8 my $fsuser = shift @ARGV or die $usage;
9 my $path = shift @ARGV or die $usage;
10
11 adminsuidsetup($fsuser);
12 local $FS::UID::AutoCommit = 0;
13 my $dbh = dbh;
14
15 #horribly inefficient but you only have to do it once
16 my %state_to_country;
17 my $world = Locale::SubCountry::World->new;
18 foreach my $countrycode (qw(US CA MX)) {
19   my $c = Locale::SubCountry->new($countrycode);
20   next if !$c->has_sub_countries;
21   $state_to_country{uc $_} = $countrycode foreach $c->all_full_names;
22 }
23 my %name_to_country = $world->full_name_code_hash;
24
25 my $fh;
26 open $fh, '<', $path
27   or die "couldn't open $path\n";
28 while(<$fh>) {
29   my ($npa, $statecode, $statename, $desc) = 
30     /^(\d{3}) ([A-Z]{2}) ([\w\s]*\w) \(([^)]*)\)/;
31   if (!$npa) {
32     warn "couldn't read $_";
33     next;
34   }
35   my $countrycode = $state_to_country{uc $statename} || 
36                     $name_to_country{uc $statename};
37   if (!$countrycode) {
38     warn "couldn't find country for $statename\n";
39     next;
40   }
41
42   my $areacode = FS::areacode->new({ 
43      'code'     => $npa,
44      'state'    => $statecode,
45      'country'  => $countrycode,
46      'description' => $desc,
47   });
48   my $error = $areacode->insert;
49   if ($error) {
50     $dbh->rollback;
51     die $error;
52   }
53   print "$npa => $statecode, $countrycode\n";
54 }
55 $dbh->commit;
56