6 use FS::Record qw(qsearchs qsearch dbh);
13 getopts('r:g:'); # ratename, granularity
15 $FS::UID::AutoCommit = 0;
18 my $file = shift or usage();
19 open my $in, '<', $file or die "$file: $!\n";
20 my $csv = Text::CSV->new({ binary => 1, auto_diag => 2 });
22 $csv->column_names($csv->getline($in));
24 # Destination (regionname)
25 # BilledPrefix (prefix)
26 # RetailRate (per minute)
30 my $granularity = $opt{g} || 1;
32 usage() unless $opt{r};
33 my $rate = qsearchs('rate', { 'ratename' => $opt{r} });
35 $rate = FS::rate->new({ 'ratename' => $opt{r} });
36 $error = $rate->insert;
40 while (my $row = $csv->getline_hr($in)) {
43 my $region = qsearchs('rate_region', {
44 'regionname' => $row->{'Destination'}
47 $region = FS::rate_region->new({
48 'regionname' => $row->{'Destination'}
50 $error = $region->insert;
54 my $prefix = $row->{'BilledPrefix'};
61 my $rate_prefix = qsearchs('rate_prefix', \%prefix);
63 $rate_prefix = FS::rate_prefix->new(\%prefix);
64 $rate_prefix->set('regionnum', $region->regionnum);
65 $error = $rate_prefix->insert;
67 $rate_prefix->set('regionnum', $region->regionnum);
68 $error = $rate_prefix->replace;
72 # enough to identify the detail
74 'ratenum' => $rate->ratenum,
75 'dest_regionnum' => $region->regionnum,
79 my $dest_detail = qsearchs('rate_detail', \%detail);
80 # ProfileRate is 5 decimal places, same as rate_detail.min_charge
82 $dest_detail = FS::rate_detail->new({
85 'min_charge' => $row->{RetailRate},
86 'sec_granularity' => $granularity,
88 $error = $dest_detail->insert;
90 local $FS::Record::nowarn_identical = 1;
91 $dest_detail->set('min_charge' => $row->{RetailRate});
92 $error = $dest_detail->replace;
101 die "Usage: rate-intl.import -r ratename
103 <user> <file>.csv\n\n";