summaryrefslogtreecommitdiff
path: root/httemplate/edit/rate.cgi
blob: 4a4b70ea32a3f521f6a5b2382145c973b9d7c558 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<%

my($rate, $error);

if ( $cgi->param('magic') eq 'process' ) {

  my $ratenum = $cgi->param('ratenum');
  
  my $old = qsearchs('rate', { 'ratenum' => $ratenum } ) if $ratenum;
  
  my @rate_detail = map {
    my $regionnum = $_->regionnum;
    if ( $cgi->param("sec_granularity$regionnum") ) {
      new FS::rate_detail {
        'dest_regionnum'  => $regionnum,
        map { $_ => scalar($cgi->param("$_$regionnum")) }
            qw( min_included min_charge sec_granularity )
      };
    } else {
      new FS::rate_detail {
        'dest_regionnum'  => $regionnum,
        'min_included'    => 0,
        'min_charge'      => 0,
        'sec_granularity' => '60'
      };
    }
  } qsearch('rate_region', {} );
  
  $rate = new FS::rate ( {
    map {
      $_, scalar($cgi->param($_));
    } fields('rate')
  } );
  
  if ( $ratenum ) {
    warn "$rate replacing $old ($ratenum)\n";
    $error = $rate->replace($old, 'rate_detail' => \@rate_detail );
  } else {
    warn "inserting $rate\n";
    $error = $rate->insert( 'rate_detail' => \@rate_detail );
    $ratenum = $rate->getfield('ratenum');
  }

  unless ( $error ) {
    print $cgi->redirect("${p}browse/rate.cgi");
    myexit;
  }
  
} elsif ( $cgi->keywords ) {
  my($query) = $cgi->keywords;
  $query =~ /^(\d+)$/;
  $rate = qsearchs( 'rate', { 'ratenum' => $1 } );
} else { #adding
  $rate = new FS::rate {};
}
my $action = $rate->ratenum ? 'Edit' : 'Add';

my $p1 = popurl(1);

my %granularity = (
  '6'  => '6 second',
  '60' => 'minute',
);

my $nous = <<END;
  WHERE 0 < ( SELECT COUNT(*) FROM rate_prefix
               WHERE rate_region.regionnum = rate_prefix.regionnum
                 AND countrycode != '1'
            )
END

%>

<%= header("$action Rate plan", menubar(
      'Main Menu' => $p,
      'View all rate plans' => "${p}browse/rate.cgi",
    ))
%>

<% if ( $error ) { %>
<FONT SIZE="+1" COLOR="#ff0000">Error: <%= $error %></FONT><BR>
<% } %>

<FORM ACTION="<%=$p1%>rate.cgi" NAME="OneTrueForm" METHOD=POST onSubmit="document.OneTrueForm.submit.disabled=true">
<INPUT TYPE="hidden" NAME="magic" VALUE="process">
<INPUT TYPE="hidden" NAME="ratenum" VALUE="<%= $rate->ratenum %>">

Rate plan
<INPUT TYPE="text" NAME="ratename" SIZE=32 VALUE="<%= $rate->ratename %>">
<BR><BR>

<%= table() %>
<TR>
  <TH>Region</TH>
  <TH>Prefix(es)</TH>
  <TH><FONT SIZE=-1>Included<BR>minutes</FONT></TH>
  <TH><FONT SIZE=-1>Charge per<BR>minute</FONT></TH>
  <TH><FONT SIZE=-1>Granularity</FONT></TH>
</TR>

<% foreach my $rate_region (
     qsearch( 'rate_region',
              {},
              '',
              "$nous ORDER BY regionname",
            )
   ) {
     my $n = $rate_region->regionnum;
     my $rate_detail =
       $rate->dest_detail($rate_region)
       || new FS::rate_detail { 'min_included'    => 0,
                                'min_charge'      => 0,
                                'sec_granularity' => '60'
                              };
%>
  <TR>
    <TD><A HREF="<%=$p%>edit/rate_region.cgi?<%= $rate_region->regionnum %>"><%= $rate_region->regionname %></A></TD>
    <TD><%= $rate_region->prefixes_short %></TD>
    <TD><INPUT TYPE="text" SIZE=5 NAME="min_included<%=$n%>" VALUE="<%= $cgi->param("min_included$n") || $rate_detail->min_included %>"></TD>
    <TD>$<INPUT TYPE="text" SIZE=4 NAME="min_charge<%=$n%>" VALUE="<%= sprintf('%.2f', $cgi->param("min_charge$n") || $rate_detail->min_charge ) %>"></TD>
    <TD>
      <SELECT NAME="sec_granularity<%=$n%>">
        <% foreach my $granularity ( keys %granularity ) { %>
          <OPTION VALUE="<%=$granularity%>"<%= $granularity == ( $cgi->param("sec_granularity$n") || $rate_detail->sec_granularity ) ? ' SELECTED' : '' %>><%=$granularity{$granularity}%>
        <% } %>
      </SELECT>
  </TR>
<% } %>

<TR>
  <TD COLSPAN=5 ALIGN="center">
    <A HREF="<%=$p%>edit/rate_region.cgi"><I>Add a region</I></A>
  </TD>
</TR>

</TABLE>

<BR><INPUT NAME="submit" TYPE="submit" VALUE="<%= 
  $rate->ratenum ? "Apply changes" : "Add rate plan"
%>">
Please be patient, <%= $rate->ratenum ? 'editing' : 'adding' %>
a rate plan can take a few minutes...

    </FORM>
  </BODY>
</HTML>