summaryrefslogtreecommitdiff
path: root/bin/rate-level3-us.import
blob: 804cb5f451d6760b756ea82b405ac1a5905cca2f (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/perl -w

use strict;

use FS::UID qw(adminsuidsetup);
use Spreadsheet::ParseExcel;
use FS::Record qw(qsearchs);
use FS::rate;
use FS::rate_region;
use FS::rate_prefix;
use FS::rate_detail;
use FS::usage_class;

use Text::CSV_XS;

my $user = shift or usage();
my $file = shift or usage();
adminsuidsetup $user;

sub usage {
  die "Usage:\n\n  rate-level3-us.import user rates.xls [ multiplier ]\n";
}

my %lata_ocn;

my $csvfile = 'npa-nxx-companytype-ocn.csv'; # not distributed here
#NPA,NXX,COMPANY TYPE,OCN,COMPANY NAME,LATA,RATECENTER,STATE
open my $fh, '<', $csvfile
  or die $!;

my $csv = Text::CSV_XS->new;
while (!$csv->eof) {
  my $row = $csv->getline($fh);
  my $lata = $row->[5] or next;
  my $ocn = $row->[3];
  my $key = $lata . '-' . $ocn;
  push @{ $lata_ocn{$key} ||= [] },
    { npa => $row->[0],
      nxx => $row->[1],
      ratecenter => $row->[6],
      state => $row->[7]
    }
  ;
}

my $multiplier = shift;
$multiplier ||= 1;

my $parser = Spreadsheet::ParseExcel->new;
my $book = $parser->parse($file);
my $sheet = $book->worksheet('VT - US48 OCN Rates')
  or die "No 'VT - US48 OCN Rates' sheet found.\n";

my $row = 0;
for (; $row < 256; $row++) {
  if (lc($sheet->get_cell($row, 0)->value) eq 'lata') {
    last;
  }
}
die "Start of data table not found.\n" if $row == 256;

my $error;

my $granularity = 1;
# default is to charge per second; edit this if needed


my %rate;
my %classnum;
foreach (qw(INTERSTATE INTRASTATE)) {
  my $rate = qsearchs('rate', { 'ratename' => $_ });
  if (!$rate) {
    $rate = FS::rate->new({ 'ratename' => $_ });
    $error = $rate->insert;
    die $error if $error;
  }
  $rate{$_} = $rate;
  my $class = qsearchs('usage_class', { 'classname' => ucfirst($_) });
  $classnum{$_} = $class->classnum if $class;
}

$row++;
my ($lata, $ocn, $jurisdiction, $charge) = @_;
while ( $sheet->get_cell($row, 0) ) {
  ($lata, $ocn, $jurisdiction, $charge) = map {
    $sheet->get_cell($row, $_)->value
  } 0..3;

  last if !$lata;

  print join("\t", $lata, $ocn, $jurisdiction, $charge),"\n";

  my $here = '[line '.($row+1).']';

  my @regionnums; # add the rate to each of these...

  if ( $lata eq '*' ) {

    my $regionname = 'Other US';
    my $region = qsearchs('rate_region', { 'regionname' => $regionname });
    if (!$region) {
      $region = FS::rate_region->new({ 'regionname' => $regionname });
      $error = $region->insert;
      die "$here inserting region: $error\n" if $error;
    }
    my %prefix = (
      'regionnum' => $region->regionnum,
      'countrycode' => '1',
      'npa' => '',
      'nxx' => '',
    );
    my $rate_prefix = qsearchs('rate_prefix', \%prefix);
    if (!$rate_prefix) {
      $rate_prefix = FS::rate_prefix->new(\%prefix);
      $error = $rate_prefix->insert;
      die "$here inserting prefix: $error\n" if $error;
    }
    push @regionnums, $region->regionnum;

  } else {

    my $data = $lata_ocn{"$lata-$ocn"};
    if (!$data) {
      warn "$here no prefixes found for lata $lata / ocn $ocn\n";
      next;
    }

    # find prefixes corresponding to this LATA/OCN. there can be MANY.

    foreach my $prefixdata (@$data) {
      my $npa = $prefixdata->{npa}
        or die "$here no NPA found.\n";
      my $nxx = $prefixdata->{nxx}
        or die "$here no NXX found.\n";

      # show a useful regionname, but include the LATA/OCN in it to
      # prevent overlap.
      my $regionname = $prefixdata->{ratecenter} . ', ' .
                       $prefixdata->{state} .
                       " $lata-$ocn";

      my $region = qsearchs('rate_region', { 'regionname' => $regionname });
      if (!$region) {
        $region = FS::rate_region->new({ 'regionname' => $regionname });
        $error = $region->insert;
        die "$here inserting region: $error\n" if $error;
      }

      my %prefix = (
        'regionnum'   => $region->regionnum,
        'countrycode' => '1',
        'npa'         => $npa . $nxx,
      );
      my $rate_prefix = qsearchs('rate_prefix', \%prefix);
      if (!$rate_prefix) {
        # don't search on unindexed fields
        $prefix{'latanum'} = $lata;
        $prefix{'ocn'}     = $ocn;
        $prefix{'state'}   = $prefixdata->{state},
        $rate_prefix = FS::rate_prefix->new(\%prefix);
        $error = $rate_prefix->insert;
        die "$here inserting prefix: $error\n" if $error;
      }
      push @regionnums, $region->regionnum;
    } # foreach $prefixdata

  } # $lata ne '*'

  $charge =~ s/^[\s\$]*//;
  $charge = sprintf('%.05f', $charge * $multiplier);

  foreach my $regionnum (@regionnums) {
    my $rate = $rate{$jurisdiction}
      or die "$here unknown jurisdiction $jurisdiction\n";
    my %detail = (
      'ratenum'         => $rate->ratenum,
      'dest_regionnum'  => $regionnum,
      'cdrtypenum'      => '',
      'ratetimenum'     => '',
    );
    
    my $dest_detail = qsearchs('rate_detail', \%detail);
    if (!$dest_detail) {
      $dest_detail = FS::rate_detail->new({
          %detail,
          'min_included'    => 0,
          'min_charge'      => $charge,
          'sec_granularity' => $granularity,
      });
      $error = $dest_detail->insert;
    } else {
      local $FS::Record::nowarn_identical = 1;
      $dest_detail->set('min_charge' => $charge);
      $error = $dest_detail->replace;
    }
    die "$here setting rate detail: $error\n" if $error;
  }
} continue {
  $row++
}