1 <% include( 'elements/browse.html',
2 'title' => 'Rating Regions and Prefixes',
3 'name_singular' => 'region', #'rate region',
4 'menubar' => [ 'Rate plans' => $p.'browse/rate.cgi' ],
5 'html_init' => $html_init,
6 'html_posttotal' => $html_posttotal,
9 'table' => 'rate_region',
11 'extra_sql' => $extra_sql,
12 'order_by' => 'ORDER BY LOWER(regionname)',
14 'count_query' => $count_query,
19 'xls_format' => \@xls_format,
24 my $edit_url = $p.'edit/rate_region.cgi';
26 my $link = [ "$edit_url?regionnum=", 'regionnum' ];
29 'Regions and prefixes for VoIP and call billing.<BR><BR>'.
30 qq(<A HREF="$edit_url"><I>Add a new region</I></A><BR><BR>);
32 #not quite right for the shouldn't-happen multiple countrycode per region case
33 my $select = 'rate_region.*, ';
36 if ( driver_name =~ /^Pg/ ) {
37 my $fromwhere = 'FROM rate_prefix'.
38 ' WHERE rate_prefix.regionnum = rate_region.regionnum';
39 my $prefix_sql = " CASE WHEN nxx IS NULL OR nxx = '' ".
41 " ELSE npa || '-' || nxx ".
43 my $prefixes_sql = "SELECT $prefix_sql $fromwhere AND npa IS NOT NULL ORDER BY npa";
44 $select .= "( SELECT '+'||countrycode $fromwhere LIMIT 1 ) AS ccode,
45 ARRAY_TO_STRING( ARRAY($prefixes_sql), ', ' ) AS prefixes";
46 } elsif ( driver_name =~ /^mysql/i ) {
47 $join = 'LEFT JOIN rate_prefix USING ( regionnum )';
48 $select .= "'+'||GROUP_CONCAT( DISTINCT countrycode ) AS ccode,
49 GROUP_CONCAT( npa ORDER BY npa SEPARATOR ', ' ) AS prefixes ";
50 $group_sql = 'GROUP BY regionnum, regionname';
52 die 'unknown database '. driver_name;
55 my $base_count_sql = 'SELECT COUNT(*) FROM rate_region';
57 tie my %granularity, 'Tie::IxHash', FS::rate_detail::granularities();
63 unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
65 my $sub_prefixes = sub {
68 ($region->exact_match ? ' <I>(exact match only)</I>' : '');
77 if ( FS::Record->scalar_sql('SELECT 1 FROM rate_region WHERE agent_regionid IS NOT NULL LIMIT 1') ) {
78 push @header, 'Legacy #';
79 push @fields, 'agent_regionid';
82 push @xls_format, { locked=>1, bg_color=>22};
85 push @header, 'Region', 'Country code', 'Prefixes';
86 push @fields, 'regionname', 'ccode', $sub_prefixes;
87 push @links, ($link) x 3;
88 push @align, 'left', 'right', 'left';
89 push @xls_format, ({ locked=>1, bg_color=>22 }) x 3;
91 my $countrycode_filter_change =
92 "window.location = '".
93 $cgi->url. "?countrycode=' + this.options[this.selectedIndex].value;";
96 my $extra_sql = $group_sql;
97 my $count_query = $base_count_sql;
98 if ( $cgi->param('countrycode') =~ /^(\d+)$/ ) {
100 my $ccode_sql = '( SELECT countrycode FROM rate_prefix
101 WHERE rate_prefix.regionnum = rate_region.regionnum
104 $extra_sql = " WHERE $ccode_sql = '$1' $extra_sql";
105 $count_query .= " WHERE $ccode_sql = '$1'";
108 sub _rate_detail_factory {
109 my( $rate, $field ) = @_;
111 my $rate_detail = $rate->dest_detail(shift)
112 || new FS::rate_detail { 'min_included' => 0,
115 'sec_granularity' => 0,
117 my $value = $rate_detail->$field();
118 $field eq 'sec_granularity' ? $granularity{$value} : $value;
122 if ( $cgi->param('show_rates') ) {
123 foreach my $rate ( qsearch('rate', {}) ) {
125 my $label = $rate->ratenum.': '. $rate->ratename;
126 push @header, "$label: Included minutes/calls",
127 "$label: Charge per minute/call",
128 "$label: Granularity",
129 "$label: Usage class";
132 push @fields, _rate_detail_factory($rate, 'min_included'),
133 _rate_detail_factory($rate, 'min_charge'),
134 _rate_detail_factory($rate, 'sec_granularity'),
135 _rate_detail_factory($rate, 'classnum');
137 push @links, ( ('') x 4 );
138 push @xls_format, ( ({}) x 4 );
144 sub countrycode2country {
147 #exceptions to the below
148 return 'North America (NANPA)' if $cc eq '1';
149 #return 'Algeria' if $cc eq '214';
150 return 'Ascension Island' if $cc eq '247';
151 return 'Satellite / Maritime / Universal Personal' if $cc eq '87'; #? or 870..
152 return 'Inmarsat' if $cc eq '870';
153 return 'Global Mobile Satellite System' if $cc eq '881';
154 return 'International Networks' if $cc eq '882';
155 return 'International Networks' if $cc eq '883';
158 Number::Phone::Country::phone2country("+$cc")
163 '(show country code: '.
164 qq(<SELECT NAME="countrycode" onChange="$countrycode_filter_change">).
165 qq(<OPTION VALUE="">(all)).
166 join("\n", map { qq(<OPTION VALUE="$_").
167 ($_ eq $countrycode ? ' SELECTED' : '' ).
168 ">+$_ - ". encode_entities( countrycode2country($_) ),
170 FS::rate_prefix->all_countrycodes