6 use FS::Record qw( dbh );
8 # default is to charge per second; edit this if needed
13 $FS::UID::AutoCommit = 0;
16 my $file = shift or usage();
17 open my $in, '<', $file or die "$file: $!\n";
18 my $csv = Text::CSV->new({ binary => 1, auto_diag => 2 });
20 $csv->column_names($csv->getline($in));
24 my $rate = new FS::rate {
25 'ratename' => 'INTERNATIONAL',
27 my $r_error = $rate->insert;
28 die $r_error if $r_error;
29 my $ratenum = $rate->ratenum;
33 my( $rr, $rp ) = (0,0);
35 while (my $row = $csv->getline_hr($in)) {
38 my $key = $row->{country}.'|'.$row->{rate};
40 unless ( $rate_region{$key} ) {
42 my $rate_region = new FS::rate_region {
43 'regionname' => $row->{country},
45 my $rr_error = $rate_region->insert;
46 die $rr_error if $rr_error;
47 $rate_region{$key} = $rate_region;
49 my $rate_detail = new FS::rate_detail {
50 'ratenum' => $ratenum,
51 'dest_regionnum' => $rate_region->regionnum,
52 'min_charge' => $row->{rate},
53 'sec_granularity' => $granularity,
56 my $rd_error = $rate_detail->insert;
57 die $rd_error if $rd_error;
63 my($countrycode, $npa);
64 if ( $row->{prefix} =~ /^(2[078]|3[0123469]|4[01356789]|5[12345678]|6[0123456]|7[67]|7|8[123469]|9[0123458])(\d*)$/ ) { #https://en.wikipedia.org/wiki/List_of_country_calling_codes
65 ( $countrycode, $npa ) = ( $1, $2 );
66 } elsif ( length($row->{prefix}) <= 3 ) {
67 $countrycode = $row->{prefix};
70 $countrycode = substr($row->{prefix}, 0, 3);
71 $npa = substr($row->{prefix}, 3);
74 my $rate_prefix = new FS::rate_prefix {
75 'regionnum' => $rate_region{$key}->regionnum,
76 'countrycode' => $countrycode,
79 my $rp_error = $rate_prefix->insert;
80 die $rp_error if $rp_error;
86 print "Inserted $rp prefixes in $rr regions\n";
91 die "Usage: rate-login.import <user> <file>.csv\n\n";