RT# 82949 - changes section name from fees to pricing, better opiton
[freeside.git] / httemplate / browse / rate_region.html
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,
7      'query'          => {
8                            'select'    => $select,
9                            'table'     => 'rate_region',
10                            'addl_from' => $join,
11                            'extra_sql' => $extra_sql, 
12                            'order_by'  => 'ORDER BY LOWER(regionname)',
13                          },
14      'count_query'    => $count_query,
15      'header'         => \@header,
16      'fields'         => \@fields,
17      'links'          => \@links,
18      'align'          => \@align,
19      'xls_format'     => \@xls_format,
20    )
21 %>
22 <%once>
23
24 my $edit_url = $p.'edit/rate_region.cgi';
25
26 my $link = [ "$edit_url?regionnum=", 'regionnum' ];
27
28 my $html_init =
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>);
31
32 #not quite right for the shouldn't-happen multiple countrycode per region case
33 my $select = 'rate_region.*, ';
34 my $join = '';
35 my $group_sql = '';
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 = '' ".
40                    "   THEN npa ".
41                    "   ELSE npa || '-' || nxx ".
42                    " END";
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';
51 } else {
52   die 'unknown database '. driver_name;
53 }
54
55 my $base_count_sql = 'SELECT COUNT(*) FROM rate_region';
56
57 tie my %granularity, 'Tie::IxHash', FS::rate_detail::granularities();
58
59 </%once>
60 <%init>
61
62 die "access denied"
63   unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
64
65 my $sub_prefixes = sub {
66   my $region = shift;
67   $region->prefixes .
68   ($region->exact_match ? ' <I>(exact match only)</I>' : '');
69 };
70
71 my @header     = ( '#',         'Region',  'Country code', 'Prefixes' );
72 my @fields     = ( 'regionnum', 'regionname',   'ccode',   $sub_prefixes );
73 my @links      = ( ($link) x 4 );
74 my @align      = ( 'right', 'left', 'right', 'left' );
75 my @xls_format = ( ({ locked=>1, bg_color=>22 }) x 4 );
76
77 my $countrycode_filter_change =
78   "window.location = '".
79   $cgi->url. "?countrycode=' + this.options[this.selectedIndex].value;";
80
81 my $countrycode = '';
82 my $extra_sql = $group_sql;
83 my $count_query = $base_count_sql;
84 if ( $cgi->param('countrycode') =~ /^(\d+)$/ ) {
85   $countrycode = $1;
86   my $ccode_sql = '( SELECT countrycode FROM rate_prefix
87                        WHERE rate_prefix.regionnum = rate_region.regionnum
88                        LIMIT 1
89                    )';
90   $extra_sql = " WHERE $ccode_sql = '$1' $extra_sql";
91   $count_query .= " WHERE $ccode_sql = '$1'";
92 }
93
94 sub _rate_detail_factory {
95   my( $rate, $field ) = @_;
96   return sub {
97     my $rate_detail = $rate->dest_detail(shift)
98                       || new FS::rate_detail { 'min_included'    => 0,
99                                                'min_charge'      => 0,
100                                                'min_cost'        => 0,
101                                                'sec_granularity' => 0,
102                                              };
103     my $value = $rate_detail->$field();
104     $field eq 'sec_granularity' ? $granularity{$value} : $value;
105   };
106 }
107
108 if ( $cgi->param('show_rates') ) {
109   foreach my $rate ( qsearch('rate', {}) ) {
110
111     my $label = $rate->ratenum.': '. $rate->ratename;
112     push @header, "$label: Included minutes/calls",
113                   "$label: Charge per minute/call",
114                   "$label: Granularity",
115                   "$label: Usage class";
116
117     #closure me harder
118     push @fields, _rate_detail_factory($rate, 'min_included'),
119                   _rate_detail_factory($rate, 'min_charge'),
120                   _rate_detail_factory($rate, 'sec_granularity'),
121                   _rate_detail_factory($rate, 'classnum');
122
123     push @links, ( ('') x 4 );
124     push @xls_format, ( ({}) x 4 );
125
126   }
127
128 }
129
130 sub countrycode2country {
131   my $cc = shift;
132
133   #exceptions to the below
134   return 'North America (NANPA)'                     if $cc eq '1';
135   #return 'Algeria'                                   if $cc eq '214';
136   return 'Ascension Island'                          if $cc eq '247';
137   return 'Satellite / Maritime / Universal Personal' if $cc eq '87'; #? or 870..
138   return 'Inmarsat'                                  if $cc eq '870';
139   return 'Global Mobile Satellite System'            if $cc eq '881';
140   return 'International Networks'                    if $cc eq '882';
141   return 'International Networks'                    if $cc eq '883';
142
143   code2country(
144     Number::Phone::Country::phone2country("+$cc")
145   );
146 }
147
148 my $html_posttotal = 
149   '(show country code: '.
150   qq(<SELECT NAME="countrycode" onChange="$countrycode_filter_change">).
151   qq(<OPTION VALUE="">(all)).
152   join("\n", map { qq(<OPTION VALUE="$_").
153                      ($_ eq $countrycode ? ' SELECTED' : '' ).
154                      ">+$_ - ". encode_entities( countrycode2country($_) ),
155                  }
156                  FS::rate_prefix->all_countrycodes
157       ).
158   '</SELECT>)';
159
160 </%init>