5 use FS::UID qw(adminsuidsetup);
9 use FS::Record qw(qsearch qsearchs dbh);
12 # Create interstate and intrastate rate plans
14 # delete from rate_detail;
15 # delete from rate_region;
16 # delete from rate_prefix;
18 # Assumption: 1-to-1 relationship between rate_region and rate_prefix, with
19 # two rate_detail per rate_region: one for interstate; one for intrastate
21 # run the script, setting the appropriate values below.
23 ####### SET THESE! ####################
24 my $intra_ratenum = 3;
25 my $inter_ratenum = 2;
28 #my $file = "/home/levinse/domestic_interstate.xls";
29 my $file = "/home/ivan/vnes/VNES Domestic VoIP Termination Deck 8-9-11.csv";
30 #my $sheet_name = 'domestic_interstate';
31 #######################################
33 my $user = shift or die "no user specified";
36 local $SIG{HUP} = 'IGNORE';
37 local $SIG{INT} = 'IGNORE';
38 local $SIG{QUIT} = 'IGNORE';
39 local $SIG{TERM} = 'IGNORE';
40 local $SIG{TSTP} = 'IGNORE';
41 local $SIG{PIPE} = 'IGNORE';
43 my $oldAutoCommit = $FS::UID::AutoCommit;
44 local $FS::UID::AutoCommit = 0;
47 #my $dbh = DBI->connect("DBI:Excel:file=$file")
48 # or die "can't connect: $DBI::errstr";
50 #my $sth = $dbh->prepare("select * from $sheet_name")
51 # or die "can't prepare: ". $dbh->errstr;
53 # or die "can't execute: ". $sth->errstr;
56 my $csv = Text::CSV_XS->new or die Text::CSV->error_diag;
58 open(my $fh, "<$file") or die $!;
59 my $header = scalar(<$fh>); #NPA, NXX, LATA, State, Intrastate, Interstate
61 my @rp_cache = qsearch('rate_prefix', {} );# or die "can't cache rate_prefix";
62 my %rp_cache = map { $_->npa => $_ } @rp_cache;
66 $dbhfs->rollback if $oldAutoCommit;
70 while ( my $row = $csv->getline($fh) ) {
72 #my $lata = $row->{'lata'};
73 #my $ocn = $row->{'ocn'};
74 #my $state = $row->{'state'};
75 #my $rate = $row->{'rate'};
76 #my $npanxx = $row->{'lrn'};
78 #NPA, NXX, LATA, State, Intrastate, Interstate
82 my $state = $row->[3];
83 ( my $intra_rate = $row->[4] ) =~ s/^\s*\$//;
84 ( my $inter_rate = $row->[5] ) =~ s/^\s*\$//;
86 next if $lata == '99999';
91 if ( $rp_cache{$npa.$nxx} ) {
92 $rp = $rp_cache{$npa.$nxx};
95 my $rr = new FS::rate_region { 'regionname' => $state };
97 fatal("can't insert rr") if $error;
99 $rp = new FS::rate_prefix { 'countrycode' => '1',
100 'npa' => $npa.$nxx, #$npanxx
104 'regionnum' => $rr->regionnum,
106 $error = $rp->insert;
107 fatal("can't insert rp") if $error;
108 $rp_cache{$npa.$nxx} = $rp;
114 my %hash = ( 'min_included' => 0,
115 'sec_granularity' => 60,
116 'dest_regionnum' => $rp->regionnum,
119 my $intra_rd = new FS::rate_detail { 'ratenum' => $intra_ratenum,
120 'min_charge' => $intra_rate,
121 'intra_class' => $intra_class,
124 $error = $intra_rd->insert;
125 fatal("can't insert (intra) rd: $error") if $error;
127 my $inter_rd = new FS::rate_detail { 'ratenum' => $inter_ratenum,
128 'min_charge' => $inter_rate,
129 'inter_class' => $inter_class,
132 $error = $inter_rd->insert;
133 fatal("can't insert (inter) rd: $error") if $error;
135 $csv->eof or $csv->error_diag ();
138 $dbhfs->commit or die $dbhfs->errstr if $oldAutoCommit;