6 use FS::Record qw( qsearch qsearchs dbh );
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' => 'Toll-Free base rates',
27 my $r_error = $rate->insert;
28 die $r_error if $r_error;
29 my $ratenum = $rate->ratenum;
33 my ($rd, $rp, $nr) = (0, 0, 0);
35 while (my $row = $csv->getline_hr($in)) {
39 #no, this creates duplicate regions/prefixes
40 #my $rate_region = new FS::rate_region {
41 # 'regionname' => $row->{'Originating Location'},
43 #my $rr_error = $rate_region->insert;
44 #die $rr_error if $rr_error;
46 my $cc = $row->{'Country Code'};
48 if ( $row->{'World Zone'} eq '1' ) {
55 push @rate_prefix, qsearchs('rate_prefix', { countrycode=>$cc, npa=>$npa } )
57 my $regionname = $row->{'Originating Location'};
59 warn "WARNING: previously unknown countrycode/npa $cc/$npa; ".
60 "make sure to update previous rates for new '$regionname' ".
63 my $rate_region = new FS::rate_region {
64 'regionname' => $row->{'Originating Location'},
66 my $rr_error = $rate_region->insert;
67 die $rr_error if $rr_error;
71 my $rate_prefix = new FS::rate_prefix {
72 'regionnum' => $rate_region->regionnum,
76 my $rp_error = $rate_prefix->insert;
77 die $rp_error if $rp_error;
79 push @rate_prefix, $rate_prefix;
83 push @rate_prefix, qsearch('rate_prefix', { countrycode=>$cc } )
84 or die "unknown countrycode/npa $cc/$npa\n";
88 my @regionnum = grep !$saw{$_}++, map $_->regionnum, @rate_prefix;
90 foreach my $regionnum (@regionnum) {
92 my $rate_detail = new FS::rate_detail {
93 'ratenum' => $ratenum,
94 'dest_regionnum' => $regionnum,
95 'conn_charge' => ( ( $row->{'minimum seconds'} / 60 ) * $row->{'Dedicated Carrier (in US)'} ),
96 'conn_sec' => $row->{'minimum seconds'},
97 'min_charge' => $row->{'Dedicated Carrier (in US)'},
98 'sec_granularity' => $row->{"add'l sec increment"},
101 my $rd_error = $rate_detail->insert;
102 die $rd_error if $rd_error;
109 #no, this creates duplicate regions/prefixes
110 #my $rate_prefix = new FS::rate_prefix {
111 # 'regionnum' => $rate_region->regionnum,
112 # 'countrycode' => $cc,
115 #my $rp_error = $rate_prefix->insert;
116 #die $rp_error if $rp_error;
122 print "Inserted $rd rates for $rp regions\n";
123 print "(Inserted $nr new regions)\n";
128 die "Usage: rate-threshold_tollfree.import <user> <file>.csv\n\n";