rate cost fields, RT#27555
[freeside.git] / httemplate / edit / elements / rate_detail.html
1 <%doc>
2 <% include('/edit/elements/rate_detail.html',
3     # required
4     'ratenum'     => '1',
5     
6     # optional
7     'regionnum'   => '25',
8     # or
9     'countrycode' => '237',
10 ) %>
11
12 If regionnum is specified, this produces column headers plus 
13 one row of rate details for that region (in all time periods).
14 Otherwise, there's one row for each region in the specified 
15 countrycode (or each region anywhere, if there is no countrycode),
16 with row headers showing the region name and prefixes.
17
18 </%doc>
19 <% include('/elements/table-grid.html') %>
20 <TR>
21 % my $col = 0;
22 % foreach (@header) {
23 %   my $hlink = $hlinks[$col];
24   <TH CLASS   = "grid",
25       BGCOLOR = "#cccccc">
26   <% $hlink ? qq!<A HREF="$hlink">$_</A>! : $_ %>
27   </TH>
28 %   $col++;
29 % } #foreach @header
30 </TR><TR>
31 % my $row = 0;
32 % foreach my $r (@rows) {
33 %   $col = 0;
34 %   if ( !$opt{'regionnum'} ) {
35 %     $region = $r;
36 %     foreach ($r->regionname, $r->prefixes_short) {
37   <TD>
38     <A HREF="<% $p.'edit/rate_region.cgi?regionnum='.$r->regionnum %>"><% $_ %></A>
39   </TD>
40 %     }
41 %   }
42 %   elsif ( !$opt{'ratenum'} ) {
43 %     $rate = $r;
44   <TD>
45     <A HREF="<% $p.'edit/rate.cgi?ratenum='.$r->ratenum %>"><% $r->ratename %></A>
46   </TD>
47 %   }
48 %   foreach my $rate_time (@rate_time, '') {
49   <TD>
50 %     my $detail = $details[$row][$col];
51 %     if($detail) {
52       <TABLE CLASS="inv" STYLE="border:none">
53       <TR><TD><% edit_link($detail) %><% $money_char.$detail->min_charge %>
54               <% $detail->sec_granularity ? ' / minute':' / call' %>
55 %             if ( $detail->min_cost ) {
56                 (<% $money_char.$detail->min_charge %> cost)
57 %             }
58       <% $edit_hint %></A>
59       </TD></TR>
60       <% granularity_detail($detail) %>
61       <% min_included_detail($detail) %>
62       <% conn_charge_detail($detail) %>
63       <TR><TD><% ( $rate_time || $cdrtypenum ) ? delete_link($detail) : '' %>
64       </TD></TR>
65     </TABLE>
66 %     }
67 %     else { #!$detail
68     <% add_link($rate, $region, $rate_time, $cdrtypenum) %>
69 %     }
70 %     $col++;
71   </TD>
72 %   } # foreach @rate_time
73 </TR>
74 %   $row++;
75 % }# foreach @rate_region
76 </TABLE>
77
78 <%once>
79
80 tie my %granularity, 'Tie::IxHash', FS::rate_detail::granularities();
81 tie my %conn_secs,   'Tie::IxHash', FS::rate_detail::conn_secs();
82
83 my $conf = new FS::Conf;
84 my $money_char = $conf->config('money_char') || '$';
85
86 sub small {
87   '<FONT SIZE="-1">'.shift.'</FONT>'
88 }
89 my $edit_hint = small('(edit)');
90
91 sub edit_link {
92   my $rate_detail = shift;
93   my $ratedetailnum = $rate_detail->ratedetailnum;
94   '<A HREF="javascript:void(0);" onclick="'.
95   include( '/elements/popup_link_onclick.html',
96              'action'      => "${p}edit/rate_detail.html?$ratedetailnum",
97              'actionlabel' => 'Edit rate',
98              'height'      => 460,
99              #default# 'width'       => 540,
100              #default# 'color'       => '#333399',
101          ) . '">'
102 }
103
104 sub add_link {
105   my ($rate, $region, $rate_time, $cdrtypenum) = @_;
106   '<A HREF="javascript:void(0);" onclick="'.
107   include( '/elements/popup_link_onclick.html',
108              'action'      => "${p}edit/rate_detail.html?ratenum=".
109                                   $rate->ratenum.
110                                ';dest_regionnum='.
111                                   $region->regionnum.
112                                ';ratetimenum='.
113                                  ($rate_time ? $rate_time->ratetimenum : '').
114                                ";cdrtypenum=$cdrtypenum",
115              'actionlabel' => 'Add rate',
116              'height'      => 460,
117              ).'">'.small('(add)').'</A>'
118 }
119
120 sub delete_link {
121   my $rate_detail = shift;
122   my $ratedetailnum = $rate_detail->ratedetailnum;
123   my $onclick = include( '/elements/popup_link_onclick.html',
124              'action'      => "${p}misc/delete-rate_detail.html?$ratedetailnum",
125              'actionlabel' => 'Delete rate',
126              'width'       => 510,
127              'height'      => 315,
128              'frame'       => 'top',
129              );
130   $onclick = "if(confirm('Delete this rate?')) { $onclick }";
131   qq!<A HREF="javascript:void(0);" onclick="$onclick">!.small('(delete)').'</A>'
132 }
133
134 sub granularity_detail {
135   my $rate_detail = shift;
136   if($rate_detail->sec_granularity != 60 && $rate_detail->sec_granularity > 0) {
137     '<TR><TD>'.
138     small('in '.$granularity{$rate_detail->sec_granularity}.' increments').
139     '</TD></TR>';
140   }
141   else { '' }
142 }
143
144 sub min_included_detail {
145   my $rate_detail = shift;
146   if($rate_detail->min_included) {
147     '<TR><TD>'.
148     small( $rate_detail->min_included . 
149             ($rate_detail->sec_granularity ? 
150              ' minutes included' : 
151              ' calls included') ).
152     '</TD></TR>'
153   }
154   else { '' }
155 }
156
157 sub conn_charge_detail {
158   my $rate_detail = shift;
159   if($rate_detail->conn_charge > 0) {
160   #return '' unless $rate_detail->conn_charge > 0 || $rate_detail->conn_sec;
161     '<TR><TD>'.
162     small( $money_char. $rate_detail->conn_charge.
163              ( $rate_detail->conn_cost
164                  ? ' ('. $money_char.$rate_detail->conn_cost. ' cost)'
165                  : ''
166              ).
167            ' for '. $conn_secs{$rate_detail->conn_sec}
168     ).
169     '</TD></TR>'
170   }
171   else { '' }
172 }
173
174 </%once>
175 <%init>
176
177 die "access denied"
178   unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
179
180 my %opt = @_;
181 my $ratenum = $opt{'ratenum'} || '';
182 my $regionnum = $opt{'regionnum'} || '';
183 my $cdrtypenum = $opt{'cdrtypenum'} || '';
184
185 # either of these, if the $opt isn't passed, will be set to the 
186 # correct object when generating each row.
187 my $rate = qsearchs('rate', { 'ratenum' => $ratenum } ) if $ratenum;
188 my $region = qsearchs('rate_region', { 'regionnum' => $regionnum }) if $regionnum;
189
190 my @rate_time = qsearch('rate_time', {});
191 my @header = (
192   map( { $_->ratetimename } @rate_time ),
193   'Default rate');
194 my @hlinks = map( {$p.'edit/rate_time.cgi?'.$_->ratetimenum} @rate_time ), '';
195 my @rtns = ( map( { $_->ratetimenum } @rate_time ), '' );
196
197 my @details;
198 my @rows;
199 if ( $ratenum ) {
200   if ( $regionnum ) {
201     @rows = qsearch('rate_region', 
202                       { ratenum => $ratenum, regionnum => $regionnum });
203   }
204   else {
205     my $where = '';
206     if ( $opt{'countrycode'} ) {
207       $where = "WHERE 0 < ( 
208                     SELECT COUNT(*) FROM rate_prefix
209                     WHERE rate_prefix.regionnum = rate_region.regionnum
210                     AND countrycode = '$opt{countrycode}' 
211                 )";
212     }
213     @rows = qsearch({ table     => 'rate_region',
214                       hashref   => {},
215                       extra_sql => $where,
216                     });
217     #die "no region found" if !@rows;
218
219     unshift @header, 'Region', 'Prefix(es)';
220     unshift @hlinks, '', '';
221   }
222   foreach my $region (@rows) {
223      push @details, [ map { qsearchs('rate_detail', 
224                                       { 'ratenum'        => $ratenum,
225                                         'dest_regionnum' => $region->regionnum,
226                                         'cdrtypenum'     => $cdrtypenum,
227                                         'ratetimenum'    => $_ } ) or ''
228                           } @rtns
229                     ];
230   }
231 }
232 elsif ( $regionnum ) {
233   @rows = qsearch('rate', {}) or die "no rate plans found";
234   unshift @header, 'Rate plan';
235   unshift @hlinks, '';
236   foreach my $rate (@rows) {
237     push @details, [ map { qsearchs('rate_detail',
238                                      { 'ratenum'        => $rate->ratenum,
239                                        'dest_regionnum' => $regionnum,
240                                        'cdrtypenum'     => $cdrtypenum,
241                                        'ratetimenum'    => $_ } ) or ''
242                          } @rtns
243                    ];
244   }
245 }
246 else {
247   die "no ratenum or regionnum specified";
248 }
249
250 </%init>