deb 9
[freeside.git] / bin / rate-threshold.import
1 #!/usr/bin/perl
2
3 use strict;
4 use Text::CSV;
5 use FS::Misc::Getopt;
6 use FS::Record qw( dbh );
7
8 getopts('');
9
10 $FS::UID::AutoCommit = 0;
11 my $dbh = dbh;
12
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 });
16 # set header row
17 $csv->column_names($csv->getline($in));
18
19 #my $error;
20
21 my $rate = new FS::rate {
22   'ratename' => 'Long Distance base rates',
23 };
24 my $r_error = $rate->insert;
25 die $r_error if $r_error;
26 my $ratenum = $rate->ratenum;
27
28 my %rate_region = ();
29
30 my( $rr, $rp ) = (0,0);
31
32 while (my $row = $csv->getline_hr($in)) {
33   print $csv->string;
34
35   my $key = join('|', map $row->{$_}, 'Country Code', 'Destination Name', 'Rate Per Minute (USD)' );
36
37   unless ( $rate_region{$key} ) {
38
39     my $rate_region = new FS::rate_region {
40       'regionname' => $row->{'Destination Name'},
41     };
42     my $rr_error = $rate_region->insert;
43     die $rr_error if $rr_error;
44     $rate_region{$key} = $rate_region;
45
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"},
53       'min_included'    => 0,
54     };
55     my $rd_error = $rate_detail->insert;
56     die $rd_error if $rd_error;
57
58     $rr++;
59
60   }
61
62   my $cc = $row->{'Country Code'};
63   my $npa = $row->{'Digits'};
64
65   if ( $cc =~ /^1(\d{3})$/ ) {
66     $cc = '1';
67     $npa = $1.$npa;
68   }
69
70   my $rate_prefix = new FS::rate_prefix {
71     'regionnum'   => $rate_region{$key}->regionnum,
72     'countrycode' => $cc,
73     'npa'         => $npa,
74   };
75   my $rp_error = $rate_prefix->insert;
76   die $rp_error if $rp_error;
77   
78   $rp++;
79 }
80
81 dbh->commit;
82 print "Inserted $rp prefixes in $rr regions\n";
83
84 1;
85
86 sub usage {
87   die "Usage: rate-threshold.import <user> <file>.csv\n\n";
88 }
89