882991e9ddd785353fb41fefbbe70ba128ffe0c0
[freeside.git] / httemplate / edit / process / rate_region.cgi
1 %if ( $error ) {
2 %  $cgi->param('error', $error);
3 <% $cgi->redirect(popurl(2). "rate_region.cgi?". $cgi->query_string ) %>
4 %} else { 
5 <% $cgi->redirect(popurl(3). "browse/rate_region.html") %>
6 %}
7 <%init>
8
9 my $conf = new FS::Conf;
10 die "access denied"
11   unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
12
13 my $regionnum = $cgi->param('regionnum');
14
15 my $old = qsearchs('rate_region', { 'regionnum' => $regionnum } ) if $regionnum;
16
17 my $new = new FS::rate_region ( {
18   map {
19     $_, scalar($cgi->param($_));
20   } ( fields('rate_region') )
21 } );
22
23 my $countrycode = $cgi->param('countrycode');
24 my @npa = split(/\s*,\s*/, $cgi->param('npa'));
25 $npa[0] = '' unless @npa;
26 my @rate_prefix = map {
27                         #my($npa,$nxx) = split('-', $_);
28                         s/\D//g;
29                         new FS::rate_prefix {
30                           'countrycode' => $countrycode,
31                           #'npa'         => $npa,
32                           #'nxx'         => $nxx,
33                           'npa'         => $_,
34                         }
35                       } @npa;
36
37 my @dest_detail = map {
38   my $ratenum = $_->ratenum;
39   new FS::rate_detail {
40     'ratenum'  => $ratenum,
41     map { $_ => $cgi->param("$_$ratenum") }
42         qw( min_included min_charge sec_granularity classnum )
43   };
44 } qsearch('rate', {} );
45
46
47 my $error;
48 if ( $regionnum ) {
49   $error = $new->replace($old, 'rate_prefix' => \@rate_prefix,
50                                'dest_detail' => \@dest_detail, );
51 } else {
52   $error = $new->insert( 'rate_prefix' => \@rate_prefix,
53                          'dest_detail' => \@dest_detail, );
54   $regionnum = $new->getfield('regionnum');
55 }
56
57 </%init>