87a67456686eac08f32d58f5d7eb77d3f3fae961
[freeside.git] / httemplate / misc / process / copy-rate_detail.html
1 %# if ( $error ) {
2 %# <% $cgi->redirect(popurl(2).'copy-rate_detail.html?'. $cgi->query_string ) %>
3 %# } else {
4 <% include('/elements/header.html', 'Rates copied',
5               menubar( 'View all rate plans' => popurl(3).'browse/rate.cgi' ),
6  ) %>
7 %# }
8 <%init>
9
10 die "access denied"
11   unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
12
13 $cgi->param('src_ratenum') =~ /^(\d+)$/ or die 'Illegal src_ratenum';
14 my $src_ratenum = $1;
15
16 $cgi->param('dst_ratenum') =~ /^(\d+)$/ or die 'Illegal src_ratenum';
17 my $dst_ratenum = $1;
18
19 my @countrycodes = map  { /^countrycode(\d+)$/ or die; $1 }
20                    grep { /^countrycode(\d+)$/ && $cgi->param($_) }
21                         $cgi->param;
22
23 foreach my $countrycode ( @countrycodes ) {
24
25   my @src_rate_detail = qsearch({
26     'table'     => 'rate_detail',
27     'addl_from' => 'JOIN rate_region'.
28                    ' ON ( rate_detail.dest_regionnum = rate_region.regionnum )',
29     'hashref' => { 'ratenum' => $src_ratenum },
30     'extra_sql' =>
31       "AND 0 < ( SELECT COUNT(*) FROM rate_prefix
32                    WHERE rate_prefix.regionnum = rate_region.regionnum
33                      AND countrycode = '$countrycode'
34                )
35       ",
36   });
37
38   foreach my $src_rate_detail ( @src_rate_detail ) {
39
40     my %hash = (
41         'ratenum' => $dst_ratenum,
42         map { $_ => $src_rate_detail->get($_) }
43             qw( orig_regionnum dest_regionnum )
44       );
45
46     my $dst_rate_detail = qsearchs( 'rate_detail', \%hash)
47                           || new FS::rate_detail   \%hash;
48
49     $dst_rate_detail->$_( $src_rate_detail->get($_) )
50       foreach qw( min_included min_charge sec_granularity classnum );
51
52     my $method = $dst_rate_detail->ratedetailnum ? 'replace' : 'insert';
53
54     my $error = $dst_rate_detail->$method();
55
56     die $error if $error; # "shouldn't" happen
57
58   }
59 }
60
61 </%init>