6 use FS::Record qw( dbh );
10 $FS::UID::AutoCommit = 0;
13 my $file = shift or usage();
14 open my $in, '<', $file or die "$file: $!\n";
15 my $csv = Text::CSV->new({ binary => 1, auto_diag => 2 });
17 $csv->column_names($csv->getline($in));
21 my $rate = new FS::rate {
22 'ratename' => 'Long Distance base rates',
24 my $r_error = $rate->insert;
25 die $r_error if $r_error;
26 my $ratenum = $rate->ratenum;
30 my( $rr, $rp ) = (0,0);
32 while (my $row = $csv->getline_hr($in)) {
35 my $key = join('|', map $row->{$_}, 'Country Code', 'Destination Name', 'Rate Per Minute (USD)' );
37 unless ( $rate_region{$key} ) {
39 my $rate_region = new FS::rate_region {
40 'regionname' => $row->{'Destination Name'},
42 my $rr_error = $rate_region->insert;
43 die $rr_error if $rr_error;
44 $rate_region{$key} = $rate_region;
46 my $rate_detail = new FS::rate_detail {
47 'ratenum' => $ratenum,
48 'dest_regionnum' => $rate_region->regionnum,
49 'conn_charge' => ( ( $row->{'minimum seconds'} / 60 ) * $row->{'Rate Per Minute (USD)'} ),
50 'conn_sec' => $row->{'minimum seconds'},
51 'min_charge' => $row->{'Rate Per Minute (USD)'},
52 'sec_granularity' => $row->{"add'l sec increment"},
55 my $rd_error = $rate_detail->insert;
56 die $rd_error if $rd_error;
62 my $cc = $row->{'Country Code'};
63 my $npa = $row->{'Digits'};
65 if ( $cc =~ /^1(\d{3})$/ ) {
70 my $rate_prefix = new FS::rate_prefix {
71 'regionnum' => $rate_region{$key}->regionnum,
75 my $rp_error = $rate_prefix->insert;
76 die $rp_error if $rp_error;
82 print "Inserted $rp prefixes in $rr regions\n";
87 die "Usage: rate-threshold.import <user> <file>.csv\n\n";