rate download/edit/upload, RT#5108
[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    )
19 %>
20 <%once>
21
22 my $edit_url = $p.'edit/rate_region.cgi';
23
24 my $link = [ "$edit_url?", 'regionnum' ];
25
26 my $html_init =
27   'Regions and prefixes for VoIP and call billing.<BR><BR>'.
28   qq(<A HREF="$edit_url"><I>Add a new region</I></A><BR><BR>);
29
30 #not quite right for the shouldn't-happen multiple countrycode per region case
31 my $select = 'rate_region.*, ';
32 my $join = '';
33 my $group_sql = '';
34 if ( driver_name =~ /^Pg/ ) {
35   my $fromwhere = 'FROM rate_prefix'.
36                   ' WHERE rate_prefix.regionnum = rate_region.regionnum';
37   my $prefix_sql = " CASE WHEN nxx IS NULL OR nxx = '' ".
38                    "   THEN npa ".
39                    "   ELSE npa || '-' || nxx ".
40                    " END";
41   my $prefixes_sql = "SELECT $prefix_sql $fromwhere AND npa IS NOT NULL";
42   $select .= "( SELECT countrycode $fromwhere LIMIT 1 ) AS ccode, 
43               ARRAY_TO_STRING( ARRAY($prefixes_sql), ', ' ) AS prefixes";
44 } elsif ( driver_name =~ /^mysql/i ) {
45   $join = 'LEFT JOIN rate_prefix USING ( regionnum )';
46   $select .= "GROUP_CONCAT( DISTINCT countrycode ) AS ccode,
47               GROUP_CONCAT( npa ORDER BY npa     ) AS prefixes ";
48   $group_sql = 'GROUP BY regionnum, regionname';
49 } else {
50   die 'unknown database '. driver_name;
51 }
52
53 my $base_count_sql = 'SELECT COUNT(*) FROM rate_region';
54
55 tie my %granularity, 'Tie::IxHash', FS::rate_detail::granularities();
56
57 </%once>
58 <%init>
59
60 die "access denied"
61   unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
62
63 my @header = ( '#',         'Region',  'Country code', 'Prefixes' );
64 my @fields = ( 'regionnum', 'regionname',   'ccode',   'prefixes' );
65 my @links  = ( $link, $link, $link, $link );
66
67 $cgi->param('dummy', 1);
68 my $countrycode_filter_change =
69   "window.location = '".
70   $cgi->self_url. ";countrycode=' + this.options[this.selectedIndex].value;";
71
72 my $countrycode = '';
73 my $extra_sql = $group_sql;
74 my $count_query = $base_count_sql;
75 if ( $cgi->param('countrycode') =~ /^(\d+)$/ ) {
76   $countrycode = $1;
77   my $ccode_sql = '( SELECT countrycode FROM rate_prefix
78                        WHERE rate_prefix.regionnum = rate_region.regionnum
79                        LIMIT 1
80                    )';
81   $extra_sql = " WHERE $ccode_sql = '$1' $extra_sql";
82   $count_query .= " WHERE $ccode_sql = '$1'";
83 }
84
85 sub _rate_detail_factory {
86   my( $rate, $field ) = @_;
87   return sub {
88     my $rate_detail = $rate->dest_detail(shift)
89                       || new FS::rate_region { 'min_included'    => 0,
90                                                'min_charge'      => 0,
91                                                'sec_granularity' => 0,
92                                              };
93     my $value = $rate_detail->$field();
94     $field eq 'sec_granularity' ? $granularity{$value} : $value;
95   };
96 }
97
98 if ( $cgi->param('show_rates') ) {
99   foreach my $rate ( qsearch('rate', {}) ) {
100
101     my $label = $rate->ratenum.': '. $rate->ratename;
102     push @header, "$label: Included minutes/calls",
103                   "$label: Charge per minute/call",
104                   "$label: Granularity",
105                   "$label: Usage class";
106
107     #closure me harder
108     push @fields, _rate_detail_factory($rate, 'min_included'),
109                   _rate_detail_factory($rate, 'min_charge'),
110                   _rate_detail_factory($rate, 'sec_granularity'),
111                   _rate_detail_factory($rate, 'classnum');
112
113     push @links, '', '', '', '';
114
115   }
116
117 }
118
119 my $html_posttotal = 
120   '(show country code: '.
121   qq(<SELECT NAME="countrycode" onChange="$countrycode_filter_change">).
122   qq(<OPTION VALUE="">(all)).
123   join("\n", map { qq(<OPTION VALUE="$_").
124                      ($_ eq $countrycode ? ' SELECTED' : '' ).
125                      ">$_",
126                  }
127                  FS::rate_prefix->all_countrycodes
128       ).
129   '</SELECT>)';
130
131 </%init>