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