fix wa_tax_rate_update script to skip zero rates, #73226
[freeside.git] / bin / rate-level3-intl.import
1 #!/usr/bin/perl -w
2
3 use strict;
4 use Spreadsheet::ParseExcel;
5 #use DBI; # doesn't work
6 use FS::UID qw(adminsuidsetup);
7 use FS::Record qw(qsearchs);
8 use FS::rate;
9 use FS::rate_region;
10 use FS::rate_prefix;
11 use FS::rate_detail;
12 use Number::Phone::Country;
13
14 my $user = shift or usage();
15 my $file = shift or usage();
16 adminsuidsetup $user;
17
18 sub usage {
19   die "Usage:\n\n  rate-level3-us.import user rates.xls [ multiplier ]\n";
20 }
21
22 my $multiplier = shift;
23 $multiplier ||= 1;
24
25 my $parser = Spreadsheet::ParseExcel->new;
26 my $book = $parser->parse($file);
27 my $sheet = $book->worksheet('International Term')
28   or die "No 'International Term' sheet found.\n";
29
30 my $row = 0;
31 for (; $row < 256; $row++) {
32   if (lc($sheet->get_cell($row, 0)->value) eq 'terminating country') {
33     last;
34   }
35 }
36 die "Start of data table not found.\n" if $row == 256;
37
38 my $error;
39
40 my $granularity = 1;
41 # default is to charge per second; edit this if needed
42
43
44 my $rate = qsearchs('rate', { 'ratename' => 'International Termination' });
45 if (!$rate) {
46   $rate = FS::rate->new({ 'ratename' => 'International Termination' });
47   $error = $rate->insert;
48   die $error if $error;
49 }
50
51 # monkeypatch to pretend Antarctica is a country
52 $Number::Phone::Country::idd_codes{'672'} = 'AQ';
53
54 $row++;
55 my ($country, $zone, $prefix, $charge);
56 while ( $sheet->get_cell($row, 0) ) {
57   ($country, $zone, $prefix, $charge) = map {
58     $country = $sheet->get_cell($row, $_)->value
59   } 0..3;
60
61   last if !$country;
62
63   print join("\t", $country, $zone, $prefix, $charge),"\n";
64
65   my $here = '[line '.($row+1).']';
66   my ($countrycode);
67   if ($zone == 0) {
68     my $country;
69     ($country, $countrycode) =
70       Number::Phone::Country::phone2country_and_idd("+$prefix");
71
72     die "$here can't identify country prefix $prefix\n" unless $countrycode;
73     # trim countrycodes to what will fit in the field
74     $countrycode = substr($countrycode, 0, 3);
75     # and put the rest in rate_prefix.npa
76     $prefix =~ s/^$countrycode//;
77   } elsif ( $zone == 1 ) { #NANPA
78     $countrycode = '1';
79   } else {
80     die "$here unknown zone type $zone\n";
81   }
82   my $region = qsearchs('rate_region', { 'regionname' => $country });
83   if (!$region) {
84     $region = FS::rate_region->new({ 'regionname' => $country });
85     $error = $region->insert;
86     die "$here inserting region: $error\n" if $error;
87   }
88
89   my %prefix = (
90     'regionnum'   => $region->regionnum,
91     'countrycode' => $countrycode,
92     'npa'         => $prefix,
93   );
94   my $rate_prefix = qsearchs('rate_prefix', \%prefix);
95   if (!$rate_prefix) {
96     $rate_prefix = FS::rate_prefix->new(\%prefix);
97     $error = $rate_prefix->insert;
98     die "$here inserting prefix: $error\n" if $error;
99   }
100
101   # enough to identify the detail
102   my %detail = (
103     'ratenum'         => $rate->ratenum,
104     'dest_regionnum'  => $region->regionnum,
105     'cdrtypenum'      => '',
106     'ratetimenum'     => '',
107   );
108   
109   $charge =~ s/^[\s\$]*//;
110   $charge = sprintf('%.05f', $charge * $multiplier);
111
112   my $dest_detail = qsearchs('rate_detail', \%detail);
113   if (!$dest_detail) {
114     $dest_detail = FS::rate_detail->new({
115         %detail,
116         'min_included'    => 0,
117         'min_charge'      => $charge,
118         'sec_granularity' => $granularity,
119     });
120     $error = $dest_detail->insert;
121   } else {
122     local $FS::Record::nowarn_identical = 1;
123     $dest_detail->set('min_charge' => $charge);
124     $error = $dest_detail->replace;
125   }
126   die "$here setting rate detail: $error\n" if $error;
127
128   $row++
129 }
130