summaryrefslogtreecommitdiff
path: root/bin/v-rate-reimport
blob: 8b5305895980eecb4ad7b85149f4205c696766b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/perl

use strict;
use DBI;
use FS::UID qw(adminsuidsetup);
use FS::rate_prefix;
use FS::rate_region;
use FS::rate_detail;
use FS::Record qw(qsearch qsearchs dbh);

# #delete from rate;
# Create interstate and intrastate rate plans
#
# #delete from rate_detail;
# #delete from rate_region;
# #delete from rate_prefix;

# Assumption: 1-to-1 relationship between rate_region and rate_prefix, with
# two rate_detail per rate_region: one for interstate; one for intrastate
#
# run the script, setting the appropriate values below.

####### SET THESE! ####################

my $DRY_RUN = 0;

my $intra_ratenum = 5;
my $inter_ratenum = 6;
my $intra_class = 1;
my $inter_class = 2;
#my $file = "/home/levinse/domestic_interstate.xls";
#my $file = "/home/ivan/vnes/New VNES Rate Table.xlsx";
my $file = "/home/ivan/New VNES Rate Table.csv";
#my $sheet_name = 'Sheet1';
#######################################

my $user = shift or die "no user specified";
adminsuidsetup $user;

local $SIG{HUP} = 'IGNORE';
local $SIG{INT} = 'IGNORE';
local $SIG{QUIT} = 'IGNORE';
local $SIG{TERM} = 'IGNORE';
local $SIG{TSTP} = 'IGNORE';
local $SIG{PIPE} = 'IGNORE';

my $oldAutoCommit = $FS::UID::AutoCommit;
local $FS::UID::AutoCommit = 0;
my $dbhfs = dbh;

#my $dbh = DBI->connect("DBI:Excel:file=$file")
#  or die "can't connect: $DBI::errstr";

#my $sth = $dbh->prepare("select * from $sheet_name")
#  or die "can't prepare: ". $dbh->errstr;
#$sth->execute
#  or die "can't execute: ". $sth->errstr;

use Text::CSV_XS;
my $csv = Text::CSV_XS->new or die Text::CSV->error_diag;

open(my $fh, "<$file") or die $!;
my $header = scalar(<$fh>); #NPA, NXX, LATA, State, Intrastate, Interstate

my @rp_cache = qsearch('rate_prefix', {} );# or die "can't cache rate_prefix";
my %rp_cache = map { $_->npa => $_ } @rp_cache;

sub fatal {
    my $msg = shift;
    $dbhfs->rollback; # if $oldAutoCommit;
    die $msg;
}

while ( my $row = $csv->getline($fh) ) {

  #my $lata = $row->{'lata'};
  #my $ocn = $row->{'ocn'};
  #my $state = $row->{'state'};
  #my $rate = $row->{'rate'};
  #my $npanxx = $row->{'lrn'};

  #NPA, NXX, LATA, State, Intrastate, Interstate
  my $npa        = $row->[0];
  my $nxx        = $row->[1];
  my $lata       = $row->[2];
  my $state      = $row->[3];
  ( my $intra_rate = $row->[4] ) =~ s/^\s*\$//;
  ( my $inter_rate = $row->[5] ) =~ s/^\s*\$//;

  #in the new data, instead of being "$-", these are all identical to the
  #rate from the immediatelly preceeding cell/NPANXX... probably an artifact
  #rather than real rates then?  so also skipping this import
  #import
  next if $lata == '99999';

  my $error = '';

  my $rp;
  if ( $rp_cache{$npa.$nxx} ) {
      $rp = $rp_cache{$npa.$nxx};
  } 
  else {

     #warn "inserting new rate_region / rate_prefix for $npa-$nxx\n";
     die "new rate_region / rate_prefix $npa-$nxx\n";

     my $rr = new FS::rate_region { 'regionname' => $state };
     $error = $rr->insert;
     fatal("can't insert rr") if $error;

     $rp = new FS::rate_prefix {   'countrycode'   => '1',
                                   'npa'           => $npa.$nxx, #$npanxx
                                   #'ocn'           => $ocn,
                                   'state'         => $state,
                                   'latanum'       => $lata,
                                   'regionnum'     => $rr->regionnum,
                               }; 
     $error = $rp->insert;
     fatal("can't insert rp") if $error;
     $rp_cache{$npa.$nxx} = $rp;
  }

  #use Data::Dumper;
  #warn Dumper($rp);

  my %hash = ( 'dest_regionnum'  => $rp->regionnum, );

  my %intra_hash = ( 'ratenum'     => $intra_ratenum,
                     'intra_class' => $intra_class,
                     %hash,
                   );

  my $intra_rd = qsearchs( 'rate_detail', \%intra_hash )
                 || die; #new FS::rate_detail   \%intra_hash;
 
  $intra_rd->min_included( 0 );
  $intra_rd->sec_granularity( 6 ); #60
  die if $intra_rd->min_charge > 0;
  $intra_rd->min_charge( $intra_rate );

  #$error = $intra_rd->ratedetailnum ? $intra_rd->replace : $intra_rd->insert;
  $error = $intra_rd->replace;
  fatal("can't insert/replace (intra) rd: $error") if $error;

  my %inter_hash = ( 'ratenum'     => $inter_ratenum,
                     'inter_class' => $inter_class,
                     %hash,
                   );

  my $inter_rd = qsearchs( 'rate_detail', \%inter_hash )
                 || die; #new FS::rate_detail \%inter_hash;

  $inter_rd->min_included( 0 );
  $inter_rd->sec_granularity( 6 ); #60
  die if $inter_rd->min_charge > 0;
  $inter_rd->min_charge( $inter_rate );

  #$error = $inter_rd->ratedetailnum ? $inter_rd->replace : $inter_rd->insert;
  $error = $inter_rd->replace;
  fatal("can't insert/replace (inter) rd: $error") if $error;
}
$csv->eof or $csv->error_diag ();
close $fh;

if ( $DRY_RUN ) {
  $dbhfs->rollback or die $dbhfs->errstr; # if $oldAutoCommit;
} else {
  $dbhfs->commit or die $dbhfs->errstr; # if $oldAutoCommit;
}

1;