enable CardFortress in test database, #71513
[freeside.git] / bin / v-rate-import
1 #!/usr/bin/perl
2
3 use strict;
4 use DBI;
5 use FS::UID qw(adminsuidsetup);
6 use FS::rate_prefix;
7 use FS::rate_region;
8 use FS::rate_detail;
9 use FS::Record qw(qsearch qsearchs dbh);
10
11 # delete from rate;
12 # Create interstate and intrastate rate plans
13 #
14 # delete from rate_detail;
15 # delete from rate_region;
16 # delete from rate_prefix;
17
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
20 #
21 # run the script, setting the appropriate values below.
22
23 ####### SET THESE! ####################
24 my $intra_ratenum = 3;
25 my $inter_ratenum = 2;
26 my $intra_class = 1;
27 my $inter_class = 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 #######################################
32
33 my $user = shift or die "no user specified";
34 adminsuidsetup $user;
35
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';
42
43 my $oldAutoCommit = $FS::UID::AutoCommit;
44 local $FS::UID::AutoCommit = 0;
45 my $dbhfs = dbh;
46
47 #my $dbh = DBI->connect("DBI:Excel:file=$file")
48 #  or die "can't connect: $DBI::errstr";
49
50 #my $sth = $dbh->prepare("select * from $sheet_name")
51 #  or die "can't prepare: ". $dbh->errstr;
52 #$sth->execute
53 #  or die "can't execute: ". $sth->errstr;
54
55 use Text::CSV_XS;
56 my $csv = Text::CSV_XS->new or die Text::CSV->error_diag;
57
58 open(my $fh, "<$file") or die $!;
59 my $header = scalar(<$fh>); #NPA, NXX, LATA, State, Intrastate, Interstate
60
61 my @rp_cache = qsearch('rate_prefix', {} );# or die "can't cache rate_prefix";
62 my %rp_cache = map { $_->npa => $_ } @rp_cache;
63
64 sub fatal {
65     my $msg = shift;
66     $dbhfs->rollback if $oldAutoCommit;
67     die $msg;
68 }
69
70 while ( my $row = $csv->getline($fh) ) {
71
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'};
77
78   #NPA, NXX, LATA, State, Intrastate, Interstate
79   my $npa        = $row->[0];
80   my $nxx        = $row->[1];
81   my $lata       = $row->[2];
82   my $state      = $row->[3];
83   ( my $intra_rate = $row->[4] ) =~ s/^\s*\$//;
84   ( my $inter_rate = $row->[5] ) =~ s/^\s*\$//;
85
86   next if $lata == '99999';
87
88   my $error = '';
89
90   my $rp;
91   if ( $rp_cache{$npa.$nxx} ) {
92       $rp = $rp_cache{$npa.$nxx};
93   } 
94   else {
95      my $rr = new FS::rate_region { 'regionname' => $state };
96      $error = $rr->insert;
97      fatal("can't insert rr") if $error;
98
99      $rp = new FS::rate_prefix {   'countrycode'   => '1',
100                                    'npa'           => $npa.$nxx, #$npanxx
101                                    #'ocn'           => $ocn,
102                                    'state'         => $state,
103                                    'latanum'       => $lata,
104                                    'regionnum'     => $rr->regionnum,
105                                }; 
106      $error = $rp->insert;
107      fatal("can't insert rp") if $error;
108      $rp_cache{$npa.$nxx} = $rp;
109   }
110
111   #use Data::Dumper;
112   #warn Dumper($rp);
113
114   my %hash = ( 'min_included'    => 0,
115                'sec_granularity' => 6, #60,
116                'dest_regionnum'  => $rp->regionnum,
117              );
118
119   my $intra_rd = new FS::rate_detail { 'ratenum'         => $intra_ratenum,
120                                        'min_charge'      => $intra_rate,
121                                        'intra_class'     => $intra_class,
122                                        %hash,
123                                      };
124   $error = $intra_rd->insert;
125   fatal("can't insert (intra) rd: $error") if $error;
126
127   my $inter_rd = new FS::rate_detail { 'ratenum'         => $inter_ratenum,
128                                        'min_charge'      => $inter_rate,
129                                        'inter_class'     => $inter_class,
130                                        %hash,
131                                      };
132   $error = $inter_rd->insert;
133   fatal("can't insert (inter) rd: $error") if $error;
134 }
135 $csv->eof or $csv->error_diag ();
136 close $fh;
137
138 $dbhfs->commit or die $dbhfs->errstr if $oldAutoCommit;