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";
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 @header = ( '#', 'Region', 'Country code', 'Prefixes' );
66 my @fields = ( 'regionnum', 'regionname', 'ccode', 'prefixes' );
67 my @links = ( ($link) x 4 );
68 my @align = ( 'right', 'left', 'right', 'left' );
69 my @xls_format = ( ({ locked=>1, bg_color=>22 }) x 4 );
71 $cgi->param('dummy', 1);
72 my $countrycode_filter_change =
73 "window.location = '".
74 $cgi->self_url. ";countrycode=' + this.options[this.selectedIndex].value;";
77 my $extra_sql = $group_sql;
78 my $count_query = $base_count_sql;
79 if ( $cgi->param('countrycode') =~ /^(\d+)$/ ) {
81 my $ccode_sql = '( SELECT countrycode FROM rate_prefix
82 WHERE rate_prefix.regionnum = rate_region.regionnum
85 $extra_sql = " WHERE $ccode_sql = '$1' $extra_sql";
86 $count_query .= " WHERE $ccode_sql = '$1'";
89 sub _rate_detail_factory {
90 my( $rate, $field ) = @_;
92 my $rate_detail = $rate->dest_detail(shift)
93 || new FS::rate_region { 'min_included' => 0,
95 'sec_granularity' => 0,
97 my $value = $rate_detail->$field();
98 $field eq 'sec_granularity' ? $granularity{$value} : $value;
102 if ( $cgi->param('show_rates') ) {
103 foreach my $rate ( qsearch('rate', {}) ) {
105 my $label = $rate->ratenum.': '. $rate->ratename;
106 push @header, "$label: Included minutes/calls",
107 "$label: Charge per minute/call",
108 "$label: Granularity",
109 "$label: Usage class";
112 push @fields, _rate_detail_factory($rate, 'min_included'),
113 _rate_detail_factory($rate, 'min_charge'),
114 _rate_detail_factory($rate, 'sec_granularity'),
115 _rate_detail_factory($rate, 'classnum');
117 push @links, ( ('') x 4 );
118 push @xls_format, ( ({}) x 4 );
125 '(show country code: '.
126 qq(<SELECT NAME="countrycode" onChange="$countrycode_filter_change">).
127 qq(<OPTION VALUE="">(all)).
128 join("\n", map { qq(<OPTION VALUE="$_").
129 ($_ eq $countrycode ? ' SELECTED' : '' ).
132 FS::rate_prefix->all_countrycodes