inter/intra-state phone billing and custom rate import script, RT13112
[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 # Assumption: 1-to-1 relationship between rate_region and rate_prefix, with
12 # two rate_detail per rate_region: one for interstate; one for intrastate
13 #
14 # Create interstate and intrastate rate plans - run the script once
15 # per spreadsheet, setting the appropriate values below.
16 ####### SET THESE! ####################
17 my $ratenum = 3;
18 my $file = "/home/levinse/domestic_interstate.xls";
19 my $sheet_name = 'domestic_interstate';
20 #######################################
21
22 my $user = shift or die "no user specified";
23 adminsuidsetup $user;
24
25 local $SIG{HUP} = 'IGNORE';
26 local $SIG{INT} = 'IGNORE';
27 local $SIG{QUIT} = 'IGNORE';
28 local $SIG{TERM} = 'IGNORE';
29 local $SIG{TSTP} = 'IGNORE';
30 local $SIG{PIPE} = 'IGNORE';
31
32 my $oldAutoCommit = $FS::UID::AutoCommit;
33 local $FS::UID::AutoCommit = 0;
34 my $dbhfs = dbh;
35
36 my $dbh = DBI->connect("DBI:Excel:file=$file")
37   or die "can't connect: $DBI::errstr";
38
39 my $sth = $dbh->prepare("select * from $sheet_name")
40   or die "can't prepare: ". $dbh->errstr;
41 $sth->execute
42   or die "can't execute: ". $sth->errstr;
43
44 my @rp_cache = qsearch('rate_prefix', {} ) or die "can't cache rate_prefix";
45 my %rp_cache = map { $_->npa => $_ } @rp_cache;
46
47 sub fatal {
48     my $msg = shift;
49     $dbhfs->rollback if $oldAutoCommit;
50     die $msg;
51 }
52
53 while ( my $row = $sth->fetchrow_hashref ) {
54   my $lata = $row->{'lata'};
55   my $ocn = $row->{'ocn'};
56   my $state = $row->{'state'};
57   my $rate = $row->{'rate'};
58   my $npanxx = $row->{'lrn'};
59   my $error = '';
60
61   my $rp;
62   if ( $rp_cache{$npanxx} ) {
63       $rp = $rp_cache{$npanxx};
64   } 
65   else {
66      my $rr = new FS::rate_region { 'regionname' => $state };
67      $error = $rr->insert;
68      fatal("can't insert rr") if $error;
69
70      $rp = new FS::rate_prefix {   'countrycode'   => '1',
71                                    'npa'           => $npanxx,
72                                    'ocn'           => $ocn,
73                                    'state'         => $state,
74                                    'latanum'       => $lata,
75                                    'regionnum'     => $rr->regionnum,
76                                }; 
77      $error = $rp->insert;
78      fatal("can't insert rp") if $error;
79      $rp_cache{$npanxx} = $rp;
80   }
81
82   my $rd = new FS::rate_detail {    'ratenum'         => $ratenum,
83                                     'min_included'    => 0,
84                                     'min_charge'      => $rate,
85                                     'sec_granularity' => 60,
86                                     'dest_regionnum'  => $rp->regionnum,
87                                 };
88   $error = $rd->insert;
89   fatal("can't insert rd") if $error;
90 }
91
92 $dbhfs->commit or die $dbhfs->errstr if $oldAutoCommit;