show imported region and rate #s, RT#83146
[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 = ();
72 my @fields = ();
73 my @links = ();
74 my @align = ();
75 my @xls_format = ();
76
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';
80   push @links, $link;
81   push @align, 'right';
82   push @xls_format, { locked=>1, bg_color=>22};
83 }
84
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;
90
91 my $countrycode_filter_change =
92   "window.location = '".
93   $cgi->url. "?countrycode=' + this.options[this.selectedIndex].value;";
94
95 my $countrycode = '';
96 my $extra_sql = $group_sql;
97 my $count_query = $base_count_sql;
98 if ( $cgi->param('countrycode') =~ /^(\d+)$/ ) {
99   $countrycode = $1;
100   my $ccode_sql = '( SELECT countrycode FROM rate_prefix
101                        WHERE rate_prefix.regionnum = rate_region.regionnum
102                        LIMIT 1
103                    )';
104   $extra_sql = " WHERE $ccode_sql = '$1' $extra_sql";
105   $count_query .= " WHERE $ccode_sql = '$1'";
106 }
107
108 sub _rate_detail_factory {
109   my( $rate, $field ) = @_;
110   return sub {
111     my $rate_detail = $rate->dest_detail(shift)
112                       || new FS::rate_detail { 'min_included'    => 0,
113                                                'min_charge'      => 0,
114                                                'min_cost'        => 0,
115                                                'sec_granularity' => 0,
116                                              };
117     my $value = $rate_detail->$field();
118     $field eq 'sec_granularity' ? $granularity{$value} : $value;
119   };
120 }
121
122 if ( $cgi->param('show_rates') ) {
123   foreach my $rate ( qsearch('rate', {}) ) {
124
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";
130
131     #closure me harder
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');
136
137     push @links, ( ('') x 4 );
138     push @xls_format, ( ({}) x 4 );
139
140   }
141
142 }
143
144 sub countrycode2country {
145   my $cc = shift;
146
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';
156
157   code2country(
158     Number::Phone::Country::phone2country("+$cc")
159   );
160 }
161
162 my $html_posttotal = 
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($_) ),
169                  }
170                  FS::rate_prefix->all_countrycodes
171       ).
172   '</SELECT>)';
173
174 </%init>