summaryrefslogtreecommitdiff
path: root/bin/v-rate-import
blob: c913c33e0c761fe56fcabff0ffaf72c2adc34f1e (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
#!/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 $intra_ratenum = 3;
my $inter_ratenum = 2;
my $intra_class = 1;
my $inter_class = 2;
#my $file = "/home/levinse/domestic_interstate.xls";
my $file = "/home/ivan/vnes/VNES Domestic VoIP Termination Deck 8-9-11.csv";
#my $sheet_name = 'domestic_interstate';
#######################################

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*\$//;

  next if $lata == '99999';

  my $error = '';

  my $rp;
  if ( $rp_cache{$npa.$nxx} ) {
      $rp = $rp_cache{$npa.$nxx};
  } 
  else {
     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 = ( 'min_included'    => 0,
               'sec_granularity' => 6, #60,
               'dest_regionnum'  => $rp->regionnum,
             );

  my $intra_rd = new FS::rate_detail { 'ratenum'         => $intra_ratenum,
                                       'min_charge'      => $intra_rate,
                                       'intra_class'     => $intra_class,
                                       %hash,
                                     };
  $error = $intra_rd->insert;
  fatal("can't insert (intra) rd: $error") if $error;

  my $inter_rd = new FS::rate_detail { 'ratenum'         => $inter_ratenum,
                                       'min_charge'      => $inter_rate,
                                       'inter_class'     => $inter_class,
                                       %hash,
                                     };
  $error = $inter_rd->insert;
  fatal("can't insert (inter) rd: $error") if $error;
}
$csv->eof or $csv->error_diag ();
close $fh;

$dbhfs->commit or die $dbhfs->errstr if $oldAutoCommit;