faf11f8447e69985100cf8b6f9f068652db0ae53
[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?'.$r->regionnum %>"><% $_ %></A>
39   </TD>
40 %     }
41 %   }
42 %   elsif ( !$opt{'ratenum'} ) {
43 %     $rate = $r;
44   <TD>
45     <A HREF="<% $p.'edit/rate.cgi?'.$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       <% $edit_hint %></A>
56       </TD></TR>
57       <% granularity_detail($detail) %>
58       <% min_included_detail($detail) %>
59       <% conn_charge_detail($detail) %>
60       <TR><TD><% $rate_time ? delete_link($detail) : '' %></TD></TR>
61     </TABLE>
62 %     }
63 %     else { #!$detail
64     <% add_link($rate, $region, $rate_time) %>
65 %     }
66 %     $col++;
67   </TD>
68 %   } # foreach @rate_time
69 </TR>
70 %   $row++;
71 % }# foreach @rate_region
72 </TABLE>
73
74 <%once>
75
76 tie my %granularity, 'Tie::IxHash', FS::rate_detail::granularities();
77 tie my %conn_secs,   'Tie::IxHash', FS::rate_detail::conn_secs();
78
79 my $conf = new FS::Conf;
80 my $money_char = $conf->config('money_char') || '$';
81
82 sub small {
83   '<FONT SIZE="-1">'.shift.'</FONT>'
84 }
85 my $edit_hint = small('(edit)');
86
87 sub edit_link {
88   my $rate_detail = shift;
89   my $ratedetailnum = $rate_detail->ratedetailnum;
90   '<A HREF="javascript:void(0);" onclick="'.
91   include( '/elements/popup_link_onclick.html',
92              'action'      => "${p}edit/rate_detail.html?$ratedetailnum",
93              'actionlabel' => 'Edit rate',
94              'height'      => 420,
95              #default# 'width'       => 540,
96              #default# 'color'       => '#333399',
97          ) . '">'
98 }
99
100 sub add_link {
101   my ($rate, $region, $rate_time) = @_;
102   '<A HREF="javascript:void(0);" onclick="'.
103   include( '/elements/popup_link_onclick.html',
104              'action'      => "${p}edit/rate_detail.html?ratenum=".
105                                   $rate->ratenum.
106                                ';dest_regionnum='.
107                                   $region->regionnum.
108                                ($rate_time ? 
109                                  ';ratetimenum='.$rate_time->ratetimenum :
110                                  ''),
111              'actionlabel' => 'Add rate',
112              'height'      => 420,
113              ).'">'.small('(add)').'</A>'
114 }
115
116 sub delete_link {
117   my $rate_detail = shift;
118   my $ratedetailnum = $rate_detail->ratedetailnum;
119   my $onclick = include( '/elements/popup_link_onclick.html',
120              'action'      => "${p}misc/delete-rate_detail.html?$ratedetailnum",
121              'actionlabel' => 'Delete rate',
122              'width'       => 510,
123              'height'      => 315,
124              'frame'       => 'top',
125              );
126   $onclick = "if(confirm('Delete this rate?')) { $onclick }";
127   qq!<A HREF="javascript:void(0);" onclick="$onclick">!.small('(delete)').'</A>'
128 }
129
130 sub granularity_detail {
131   my $rate_detail = shift;
132   if($rate_detail->sec_granularity != 60 && $rate_detail->sec_granularity > 0) {
133     '<TR><TD>'.
134     small('in '.$granularity{$rate_detail->sec_granularity}.' increments').
135     '</TD></TR>';
136   }
137   else { '' }
138 }
139
140 sub min_included_detail {
141   my $rate_detail = shift;
142   if($rate_detail->min_included) {
143     '<TR><TD>'.
144     small( $rate_detail->min_included . 
145             ($rate_detail->sec_granularity ? 
146              ' minutes included' : 
147              ' calls included') ).
148     '</TD></TR>'
149   }
150   else { '' }
151 }
152
153 sub conn_charge_detail {
154   my $rate_detail = shift;
155   if($rate_detail->conn_charge > 0) {
156   #return '' unless $rate_detail->conn_charge > 0 || $rate_detail->conn_sec;
157     '<TR><TD>'.
158     small( $money_char. $rate_detail->conn_charge.
159       ' for '.$conn_secs{$rate_detail->conn_sec}
160     ).
161     '</TD></TR>'
162   }
163   else { '' }
164 }
165
166 </%once>
167 <%init>
168
169 die "access denied"
170   unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
171
172 my %opt = @_;
173 my $ratenum = $opt{'ratenum'} || '';
174 my $regionnum = $opt{'regionnum'} || '';
175
176 # either of these, if the $opt isn't passed, will be set to the 
177 # correct object when generating each row.
178 my $rate = qsearchs('rate', { 'ratenum' => $ratenum } ) if $ratenum;
179 my $region = qsearchs('rate_region', { 'regionnum' => $regionnum }) if $regionnum;
180
181 my @rate_time = qsearch('rate_time', {});
182 my @header = (
183   map( { $_->ratetimename } @rate_time ),
184   'Default rate');
185 my @hlinks = map( {$p.'edit/rate_time.cgi?'.$_->ratetimenum} @rate_time ), '';
186 my @rtns = ( map( { $_->ratetimenum } @rate_time ), '' );
187
188 my @details;
189 my @rows;
190 if ( $ratenum ) {
191   if ( $regionnum ) {
192     @rows = qsearch('rate_region', 
193                       { ratenum => $ratenum, regionnum => $regionnum });
194   }
195   else {
196     my $where = '';
197     if ( $opt{'countrycode'} ) {
198       $where = "WHERE 0 < ( 
199                     SELECT COUNT(*) FROM rate_prefix
200                     WHERE rate_prefix.regionnum = rate_region.regionnum
201                     AND countrycode = '$opt{countrycode}' 
202                 )";
203     }
204     @rows = qsearch({ table     => 'rate_region',
205                       hashref   => { },
206                       extra_sql => $where,
207                     });
208     die "no region found" if !@rows;
209
210     unshift @header, 'Region', 'Prefix(es)';
211     unshift @hlinks, '', '';
212   }
213   foreach my $region (@rows) {
214      push @details, [ map { qsearchs('rate_detail', 
215                                       { 'ratenum'        => $ratenum,
216                                         'dest_regionnum' => $region->regionnum,
217                                         'ratetimenum'    => $_ } ) or ''
218                           } @rtns
219                     ];
220   }
221 }
222 elsif ( $regionnum ) {
223   @rows = qsearch('rate', {}) or die "no rate plans found";
224   unshift @header, 'Rate plan';
225   unshift @hlinks, '';
226   foreach my $rate (@rows) {
227     push @details, [ map { qsearchs('rate_detail',
228                                      { 'ratenum'        => $rate->ratenum,
229                                        'dest_regionnum' => $regionnum,
230                                        'ratetimenum'    => $_ } ) or ''
231                          } @rtns
232                    ];
233   }
234 }
235 else {
236   die "no ratenum or regionnum specified";
237 }
238
239 </%init>