add option to limit automatic unsuspensions to a specific suspension reason type...
[freeside.git] / bin / rate-threshold_tollfree.import
1 #!/usr/bin/perl
2
3 use strict;
4 use Text::CSV;
5 use FS::Misc::Getopt;
6 use FS::Record qw( qsearch qsearchs dbh );
7 use FS::rate;
8 use FS::rate_region;
9 use FS::rate_prefix;
10
11 getopts('');
12
13 $FS::UID::AutoCommit = 0;
14 my $dbh = dbh;
15
16 my $file = shift or usage();
17 open my $in, '<', $file or die "$file: $!\n";
18 my $csv = Text::CSV->new({ binary => 1, auto_diag => 2 });
19 # set header row
20 $csv->column_names($csv->getline($in));
21
22 #my $error;
23
24 my $rate = new FS::rate {
25   'ratename' => 'Toll-Free base rates',
26 };
27 my $r_error = $rate->insert;
28 die $r_error if $r_error;
29 my $ratenum = $rate->ratenum;
30
31 my %rate_region = ();
32
33 my ($rd, $rp, $nr) = (0, 0, 0);
34
35 while (my $row = $csv->getline_hr($in)) {
36   print $csv->string;
37
38
39   #no, this creates duplicate regions/prefixes
40   #my $rate_region = new FS::rate_region {
41   #  'regionname' => $row->{'Originating Location'},
42   #};
43   #my $rr_error = $rate_region->insert;
44   #die $rr_error if $rr_error;
45
46   my $cc = $row->{'Country Code'};
47   my $npa = '';
48   if ( $row->{'World Zone'} eq '1' ) {
49     $npa = $cc;
50     $cc = '1';
51   }
52
53   my @rate_prefix = ();
54   if ( length($npa) ) {
55     push @rate_prefix, qsearchs('rate_prefix', { countrycode=>$cc, npa=>$npa } )
56       or do {
57               my $regionname = $row->{'Originating Location'};
58
59               warn "WARNING: previously unknown countrycode/npa $cc/$npa; ".
60                    "make sure to update previous rates for new '$regionname' ".
61                    "region\n";
62
63               my $rate_region = new FS::rate_region {
64                 'regionname' => $row->{'Originating Location'},
65               };
66               my $rr_error = $rate_region->insert;
67               die $rr_error if $rr_error;
68
69               $nr++;
70
71               my $rate_prefix = new FS::rate_prefix {
72                 'regionnum'   => $rate_region->regionnum,
73                 'countrycode' => $cc,
74                 'npa'         => $npa,
75               };
76               my $rp_error = $rate_prefix->insert;
77               die $rp_error if $rp_error;
78
79               push @rate_prefix, $rate_prefix;
80
81             };
82   } else {
83     push @rate_prefix, qsearch('rate_prefix', { countrycode=>$cc } )
84       or die "unknown countrycode/npa $cc/$npa\n";
85   }
86
87   my %saw = ();
88   my @regionnum = grep !$saw{$_}++, map $_->regionnum, @rate_prefix;
89
90   foreach my $regionnum (@regionnum) {
91
92     my $rate_detail = new FS::rate_detail {
93       'ratenum'         => $ratenum,
94       'dest_regionnum'  => $regionnum,
95       'conn_charge'     => ( ( $row->{'minimum seconds'} / 60 ) * $row->{'Dedicated Carrier (in US)'} ),
96       'conn_sec'        => $row->{'minimum seconds'},
97       'min_charge'      => $row->{'Dedicated Carrier (in US)'},
98       'sec_granularity' => $row->{"add'l sec increment"},
99       'min_included'    => 0,
100     };
101     my $rd_error = $rate_detail->insert;
102     die $rd_error if $rd_error;
103
104     $rd++;
105
106   }
107
108
109   #no, this creates duplicate regions/prefixes
110   #my $rate_prefix = new FS::rate_prefix {
111   #  'regionnum'   => $rate_region->regionnum,
112   #  'countrycode' => $cc,
113   #  'npa'         => $npa,
114   #};
115   #my $rp_error = $rate_prefix->insert;
116   #die $rp_error if $rp_error;
117   
118   $rp++;
119 }
120
121 dbh->commit;
122 print "Inserted $rd rates for $rp regions\n";
123 print "(Inserted $nr new regions)\n";
124
125 1;
126
127 sub usage {
128   die "Usage: rate-threshold_tollfree.import <user> <file>.csv\n\n";
129 }
130