4a4b70ea32a3f521f6a5b2382145c973b9d7c558
[freeside.git] / httemplate / edit / rate.cgi
1 <%
2
3 my($rate, $error);
4
5 if ( $cgi->param('magic') eq 'process' ) {
6
7   my $ratenum = $cgi->param('ratenum');
8   
9   my $old = qsearchs('rate', { 'ratenum' => $ratenum } ) if $ratenum;
10   
11   my @rate_detail = map {
12     my $regionnum = $_->regionnum;
13     if ( $cgi->param("sec_granularity$regionnum") ) {
14       new FS::rate_detail {
15         'dest_regionnum'  => $regionnum,
16         map { $_ => scalar($cgi->param("$_$regionnum")) }
17             qw( min_included min_charge sec_granularity )
18       };
19     } else {
20       new FS::rate_detail {
21         'dest_regionnum'  => $regionnum,
22         'min_included'    => 0,
23         'min_charge'      => 0,
24         'sec_granularity' => '60'
25       };
26     }
27   } qsearch('rate_region', {} );
28   
29   $rate = new FS::rate ( {
30     map {
31       $_, scalar($cgi->param($_));
32     } fields('rate')
33   } );
34   
35   if ( $ratenum ) {
36     warn "$rate replacing $old ($ratenum)\n";
37     $error = $rate->replace($old, 'rate_detail' => \@rate_detail );
38   } else {
39     warn "inserting $rate\n";
40     $error = $rate->insert( 'rate_detail' => \@rate_detail );
41     $ratenum = $rate->getfield('ratenum');
42   }
43
44   unless ( $error ) {
45     print $cgi->redirect("${p}browse/rate.cgi");
46     myexit;
47   }
48   
49 } elsif ( $cgi->keywords ) {
50   my($query) = $cgi->keywords;
51   $query =~ /^(\d+)$/;
52   $rate = qsearchs( 'rate', { 'ratenum' => $1 } );
53 } else { #adding
54   $rate = new FS::rate {};
55 }
56 my $action = $rate->ratenum ? 'Edit' : 'Add';
57
58 my $p1 = popurl(1);
59
60 my %granularity = (
61   '6'  => '6 second',
62   '60' => 'minute',
63 );
64
65 my $nous = <<END;
66   WHERE 0 < ( SELECT COUNT(*) FROM rate_prefix
67                WHERE rate_region.regionnum = rate_prefix.regionnum
68                  AND countrycode != '1'
69             )
70 END
71
72 %>
73
74 <%= header("$action Rate plan", menubar(
75       'Main Menu' => $p,
76       'View all rate plans' => "${p}browse/rate.cgi",
77     ))
78 %>
79
80 <% if ( $error ) { %>
81 <FONT SIZE="+1" COLOR="#ff0000">Error: <%= $error %></FONT><BR>
82 <% } %>
83
84 <FORM ACTION="<%=$p1%>rate.cgi" NAME="OneTrueForm" METHOD=POST onSubmit="document.OneTrueForm.submit.disabled=true">
85 <INPUT TYPE="hidden" NAME="magic" VALUE="process">
86 <INPUT TYPE="hidden" NAME="ratenum" VALUE="<%= $rate->ratenum %>">
87
88 Rate plan
89 <INPUT TYPE="text" NAME="ratename" SIZE=32 VALUE="<%= $rate->ratename %>">
90 <BR><BR>
91
92 <%= table() %>
93 <TR>
94   <TH>Region</TH>
95   <TH>Prefix(es)</TH>
96   <TH><FONT SIZE=-1>Included<BR>minutes</FONT></TH>
97   <TH><FONT SIZE=-1>Charge per<BR>minute</FONT></TH>
98   <TH><FONT SIZE=-1>Granularity</FONT></TH>
99 </TR>
100
101 <% foreach my $rate_region (
102      qsearch( 'rate_region',
103               {},
104               '',
105               "$nous ORDER BY regionname",
106             )
107    ) {
108      my $n = $rate_region->regionnum;
109      my $rate_detail =
110        $rate->dest_detail($rate_region)
111        || new FS::rate_detail { 'min_included'    => 0,
112                                 'min_charge'      => 0,
113                                 'sec_granularity' => '60'
114                               };
115 %>
116   <TR>
117     <TD><A HREF="<%=$p%>edit/rate_region.cgi?<%= $rate_region->regionnum %>"><%= $rate_region->regionname %></A></TD>
118     <TD><%= $rate_region->prefixes_short %></TD>
119     <TD><INPUT TYPE="text" SIZE=5 NAME="min_included<%=$n%>" VALUE="<%= $cgi->param("min_included$n") || $rate_detail->min_included %>"></TD>
120     <TD>$<INPUT TYPE="text" SIZE=4 NAME="min_charge<%=$n%>" VALUE="<%= sprintf('%.2f', $cgi->param("min_charge$n") || $rate_detail->min_charge ) %>"></TD>
121     <TD>
122       <SELECT NAME="sec_granularity<%=$n%>">
123         <% foreach my $granularity ( keys %granularity ) { %>
124           <OPTION VALUE="<%=$granularity%>"<%= $granularity == ( $cgi->param("sec_granularity$n") || $rate_detail->sec_granularity ) ? ' SELECTED' : '' %>><%=$granularity{$granularity}%>
125         <% } %>
126       </SELECT>
127   </TR>
128 <% } %>
129
130 <TR>
131   <TD COLSPAN=5 ALIGN="center">
132     <A HREF="<%=$p%>edit/rate_region.cgi"><I>Add a region</I></A>
133   </TD>
134 </TR>
135
136 </TABLE>
137
138 <BR><INPUT NAME="submit" TYPE="submit" VALUE="<%= 
139   $rate->ratenum ? "Apply changes" : "Add rate plan"
140 %>">
141 Please be patient, <%= $rate->ratenum ? 'editing' : 'adding' %>
142 a rate plan can take a few minutes...
143
144     </FORM>
145   </BODY>
146 </HTML>
147