RT 83251 - moved script
[freeside.git] / bin / v-rate-reimport
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
25 my $DRY_RUN = 0;
26
27 my $intra_ratenum = 5;
28 my $inter_ratenum = 6;
29 my $intra_class = 1;
30 my $inter_class = 2;
31 #my $file = "/home/levinse/domestic_interstate.xls";
32 #my $file = "/home/ivan/vnes/New VNES Rate Table.xlsx";
33 my $file = "/home/ivan/New VNES Rate Table.csv";
34 #my $sheet_name = 'Sheet1';
35 #######################################
36
37 my $user = shift or die "no user specified";
38 adminsuidsetup $user;
39
40 local $SIG{HUP} = 'IGNORE';
41 local $SIG{INT} = 'IGNORE';
42 local $SIG{QUIT} = 'IGNORE';
43 local $SIG{TERM} = 'IGNORE';
44 local $SIG{TSTP} = 'IGNORE';
45 local $SIG{PIPE} = 'IGNORE';
46
47 my $oldAutoCommit = $FS::UID::AutoCommit;
48 local $FS::UID::AutoCommit = 0;
49 my $dbhfs = dbh;
50
51 #my $dbh = DBI->connect("DBI:Excel:file=$file")
52 #  or die "can't connect: $DBI::errstr";
53
54 #my $sth = $dbh->prepare("select * from $sheet_name")
55 #  or die "can't prepare: ". $dbh->errstr;
56 #$sth->execute
57 #  or die "can't execute: ". $sth->errstr;
58
59 use Text::CSV_XS;
60 my $csv = Text::CSV_XS->new or die Text::CSV->error_diag;
61
62 open(my $fh, "<$file") or die $!;
63 my $header = scalar(<$fh>); #NPA, NXX, LATA, State, Intrastate, Interstate
64
65 my @rp_cache = qsearch('rate_prefix', {} );# or die "can't cache rate_prefix";
66 my %rp_cache = map { $_->npa => $_ } @rp_cache;
67
68 sub fatal {
69     my $msg = shift;
70     $dbhfs->rollback; # if $oldAutoCommit;
71     die $msg;
72 }
73
74 while ( my $row = $csv->getline($fh) ) {
75
76   #my $lata = $row->{'lata'};
77   #my $ocn = $row->{'ocn'};
78   #my $state = $row->{'state'};
79   #my $rate = $row->{'rate'};
80   #my $npanxx = $row->{'lrn'};
81
82   #NPA, NXX, LATA, State, Intrastate, Interstate
83   my $npa        = $row->[0];
84   my $nxx        = $row->[1];
85   my $lata       = $row->[2];
86   my $state      = $row->[3];
87   ( my $intra_rate = $row->[4] ) =~ s/^\s*\$//;
88   ( my $inter_rate = $row->[5] ) =~ s/^\s*\$//;
89
90   #in the new data, instead of being "$-", these are all identical to the
91   #rate from the immediatelly preceeding cell/NPANXX... probably an artifact
92   #rather than real rates then?  so also skipping this import
93   #import
94   next if $lata == '99999';
95
96   my $error = '';
97
98   my $rp;
99   if ( $rp_cache{$npa.$nxx} ) {
100       $rp = $rp_cache{$npa.$nxx};
101   } 
102   else {
103
104      #warn "inserting new rate_region / rate_prefix for $npa-$nxx\n";
105      die "new rate_region / rate_prefix $npa-$nxx\n";
106
107      my $rr = new FS::rate_region { 'regionname' => $state };
108      $error = $rr->insert;
109      fatal("can't insert rr") if $error;
110
111      $rp = new FS::rate_prefix {   'countrycode'   => '1',
112                                    'npa'           => $npa.$nxx, #$npanxx
113                                    #'ocn'           => $ocn,
114                                    'state'         => $state,
115                                    'latanum'       => $lata,
116                                    'regionnum'     => $rr->regionnum,
117                                }; 
118      $error = $rp->insert;
119      fatal("can't insert rp") if $error;
120      $rp_cache{$npa.$nxx} = $rp;
121   }
122
123   #use Data::Dumper;
124   #warn Dumper($rp);
125
126   my %hash = ( 'dest_regionnum'  => $rp->regionnum, );
127
128   my %intra_hash = ( 'ratenum'     => $intra_ratenum,
129                      'intra_class' => $intra_class,
130                      %hash,
131                    );
132
133   my $intra_rd = qsearchs( 'rate_detail', \%intra_hash )
134                  || die; #new FS::rate_detail   \%intra_hash;
135  
136   $intra_rd->min_included( 0 );
137   $intra_rd->sec_granularity( 6 ); #60
138   die if $intra_rd->min_charge > 0;
139   $intra_rd->min_charge( $intra_rate );
140
141   #$error = $intra_rd->ratedetailnum ? $intra_rd->replace : $intra_rd->insert;
142   $error = $intra_rd->replace;
143   fatal("can't insert/replace (intra) rd: $error") if $error;
144
145   my %inter_hash = ( 'ratenum'     => $inter_ratenum,
146                      'inter_class' => $inter_class,
147                      %hash,
148                    );
149
150   my $inter_rd = qsearchs( 'rate_detail', \%inter_hash )
151                  || die; #new FS::rate_detail \%inter_hash;
152
153   $inter_rd->min_included( 0 );
154   $inter_rd->sec_granularity( 6 ); #60
155   die if $inter_rd->min_charge > 0;
156   $inter_rd->min_charge( $inter_rate );
157
158   #$error = $inter_rd->ratedetailnum ? $inter_rd->replace : $inter_rd->insert;
159   $error = $inter_rd->replace;
160   fatal("can't insert/replace (inter) rd: $error") if $error;
161 }
162 $csv->eof or $csv->error_diag ();
163 close $fh;
164
165 if ( $DRY_RUN ) {
166   $dbhfs->rollback or die $dbhfs->errstr; # if $oldAutoCommit;
167 } else {
168   $dbhfs->commit or die $dbhfs->errstr; # if $oldAutoCommit;
169 }
170
171 1;
172