RT#42380: Show usage for broadband services in selfservice portal
[freeside.git] / fs_selfservice / FS-SelfService / cgi / view_usage.html
1 <%= $url = "$selfurl?action=";
2     %by_pkg_label = (); # not used yet, but I'm sure it will be...
3     @svc_phone = ();
4     @svc_port = ();
5     @svc_pbx = ();
6     @bytes_svcs = (); # contains svc_acct and svc_broadband
7     @bytes_cols = qw(seconds_used seconds upbytes_used upbytes downbytes_used downbytes totalbytes_used totalbytes);
8     %bytes_show = map { $_ => 0 } @bytes_cols;
9
10     foreach my $svc (@svcs) {
11       $by_pkg_label{ $svc->{pkg_label} } ||= [];
12       push @{ $by_pkg_label{ $svc->{pkg_label} } }, $svc;
13
14       if (( $svc->{svcdb} eq 'svc_acct' ) || ( $svc->{svcdb} eq 'svc_broadband' )) {
15         foreach my $field (@bytes_cols) {
16           $bytes_show{$field} = 1 if length($svc->{$field}) or (($field !~ /_used$/) && $svc->{'recharge_'.$field});
17         }
18         push @bytes_svcs, $svc;
19       } elsif ( $svc->{svcdb} eq 'svc_phone' ) {
20         push @svc_phone, $svc;
21       } elsif ( $svc->{svcdb} eq 'svc_port' ) {
22         push @svc_port, $svc;
23       } elsif ( $svc->{svcdb} eq 'svc_pbx' ) {
24         push @svc_pbx, $svc;
25       }
26     }
27     '';
28 %>
29 <%= include('header', 'Account usage') %>
30
31 <%= if ( $error ) {
32   $OUT .= qq!<FONT SIZE="+1" COLOR="#ff0000">$error</FONT><BR><BR>!;
33 } ''; %>
34
35 <%= if ( @bytes_svcs ) {
36       $OUT .= '<TABLE BGCOLOR="#cccccc"><TR>';
37       $OUT .= '<TH ALIGN="left">Account</TH>';
38       $OUT .= '<TH ALIGN="right">Time used</TH>' if $bytes_show{'seconds_used'};
39       $OUT .= '<TH ALIGN="right">Time remaining</TH>' if $bytes_show{'seconds'};
40       $OUT .= '<TH ALIGN="right">Upload used</TH>' if $bytes_show{'upbytes_used'};
41       $OUT .= '<TH ALIGN="right">Upload remaining</TH>' if $bytes_show{'upbytes'};
42       $OUT .= '<TH ALIGN="right">Download used</TH>' if $bytes_show{'downbytes_used'};
43       $OUT .= '<TH ALIGN="right">Download remaining</TH>' if $bytes_show{'downbytes'};
44       $OUT .= '<TH ALIGN="right">Total used</TH>' if $bytes_show{'totalbytes_used'};
45       $OUT .= '<TH ALIGN="right">Total remaining</TH>' if $bytes_show{'totalbytes'};
46       $OUT .= '</TR>';
47     }
48 %>
49
50 <%= foreach my $svc ( @bytes_svcs ) {
51     my $link = "${url}view_usage_details;".
52       "svcnum=$svc->{'svcnum'};beginning=0;ending=0";
53     my $username = $svc->{'value'};
54     $username =~ s/@.*?$//g if $view_usage_nodomain;
55     $OUT .= '<TR>';
56     $OUT .= '<TD>' . qq!<A HREF="$link">!. $svc->{'label'}. ': '. $username .'</A></TD>';
57     foreach my $field (@bytes_cols) {
58       $OUT .= '<TD ALIGN="right">' . $svc->{$field} . '</TD>' if $bytes_show{$field};
59     }
60     if ( $svc->{'recharge_amount'} ) {
61       my $link = "${url}process_order_recharge;".
62                  "svcnum=$svc->{'svcnum'}";
63       $OUT .= '<TR><TD ALIGN="right">';
64       $OUT .= qq!<A HREF="$link">!.'Recharge for $';
65       $OUT .= $svc->{'recharge_amount'} . '</A> with';
66       $OUT .= '</TD>';
67       foreach my $field ( qw(seconds upbytes downbytes totalbytes) ) {
68         $OUT .= '<TD></TD>' if $bytes_show{$field.'_used'};
69         if ($bytes_show{$field}) {
70           $OUT .= '<TD ALIGN="right">';
71           $OUT .= $svc->{'recharge_'.$field} if $svc->{'recharge_'.$field};
72           $OUT .= '</TD>';
73         }
74       }
75       $OUT .= '</TR>';
76     }
77   }
78 %>
79
80 <%= scalar(@bytes_svcs) ? '</TABLE><BR><BR>' : '' %>
81
82 <%= if ( @svc_phone or @svc_pbx ) {
83       %any = ();
84       for my $dir (qw(outbound inbound)) {
85         $any{$dir} = grep { $_->{$dir} } (@svc_phone, @svc_pbx);
86       }
87       $OUT.= '<FONT SIZE="4">Call usage</FONT><BR><BR>
88               <TABLE BGCOLOR="#cccccc" STYLE="display:inline-block">
89                 <TR>
90                   <TH ALIGN="left">Number</TH>';
91       if ( $any{outbound} ) {
92         $OUT .= '
93                   <TH>Dialed</TH>';
94       }
95       if ( $any{inbound} ) {
96         $OUT .= '
97                   <TH>Received</TH>';
98       }
99       $OUT .= '</TR>';
100     } else {
101       $OUT .= '';
102     }
103 %>
104
105 <%= foreach my $svc_x ( @svc_phone, @svc_pbx ) {
106   my $link = $url . 'view_cdr_details;' .
107           'svcnum='.$svc_x->{'svcnum'}.
108           ';beginning=0;ending=0';
109   $OUT .= '<TR><TD>'. $svc_x->{'label'}. ': '. $svc_x->{'value'};
110   $OUT .= '</TD>';
111   # usage summary w/ links
112   for my $dir (qw(outbound inbound)) {
113     if ( $dir eq 'inbound' ) {
114       $link .= ';inbound=1';
115     }
116     if ( $svc_x->{$dir} ) {
117       $OUT .= '<TD ALIGN="right">'.qq!<A HREF="$link">! .
118         sprintf('%d calls (%.0f minutes)',
119           $svc_x->{$dir}->{'count'},
120           $svc_x->{$dir}->{'duration'} / 60
121         ) .
122         '</A></TD>';
123     } elsif ( $any{$dir} )  {
124       $OUT .= '<TD></TD>';
125     }
126   }
127   $OUT .= '</TR>';
128 }
129 '';
130 %>
131
132 <%= if ( @usage_pools ) {
133   $OUT .= '</TABLE>
134   <TABLE BGCOLOR="#cccccc" STYLE="display: inline-block">
135     <TR><TH COLSPAN=4>Remaining minutes</TH></TR>
136     ';
137   my $any_shared = 0;
138   foreach my $usage (@usage_pools) {
139     # false laziness with the back office side
140     my ($description, $remain, $total, $shared) = @$usage;
141     if ( $shared ) {
142       $any_shared = 1;
143       $description .= '*';
144     }
145     my $ratio = 255 * ($remain/$total);
146     $ratio = 255 if $color > 255;
147     my $color = 
148       sprintf('STYLE="font-weight: bold; color: #%02x%02x00"',
149         255 - $ratio, $ratio);
150     $OUT .=
151     qq!<TR>
152       <TD ALIGN="right">$description</TD>
153       <TD $color ALIGN="right">$remain</TD>
154       <TD $color> / </TD>
155       <TD $color>$total</TD>
156     </TR>!;
157   }
158   if ( $any_shared ) {
159     $OUT .= '<TR STYLE="font-size: 80%; font-style: italic">'.
160             '<TD COLSPAN=4>* shared among all your phone plans</TD></TR>';
161   }
162 }
163 if ( @svc_phone or @svc_pbx or @usage_pools ) {
164   $OUT .= '</TABLE><BR><BR>';
165 }
166 '';
167 %>
168
169 <%= if ( @svc_port ) {
170       $OUT.= '<FONT SIZE="4">Bandwidth Graphs</FONT><BR><BR>
171                 <script type="text/javascript">
172                     function preset_range(start,end,prefix){
173                         document.getElementById(prefix+\'_start\').value = start;
174                         document.getElementById(prefix+\'_end\').value = end;
175                     }
176                   </script>
177               <TABLE BGCOLOR="#cccccc">
178                 <TR>
179                   <TH ALIGN="left">Service</TH>
180                   <TH ALIGN="right">
181                   </TH>
182                 </TR>';
183     } 
184     $OUT .= '';
185 %>
186
187 <%=
188
189 sub preset_range {
190     my($start,$end,$label,$date_format,$prefix) = (shift,shift,shift,shift,shift);
191     $start = Date::Format::time2str($date_format,$start);
192     $end = Date::Format::time2str($date_format,$end);
193     return '<A HREF="javascript:void(0);" onclick="preset_range(\''
194             .$start.'\',\''.$end.'\',\''.$prefix.'\')">'.$label.'</A>';
195 }
196
197 foreach my $svc_port ( @svc_port ) {
198   $svcnum = $svc_port->{'svcnum'}; 
199   $default_end = time;
200   $default_start = $default_end-86400;
201
202   $OUT .= '<TR><TD>'. $svc_port->{'label'}. ': '. $svc_port->{'value'}.'</TD>';
203   $OUT .= qq! <TD><FORM ACTION="$url" METHOD="GET">
204                 <INPUT TYPE="hidden" name="svcnum" value="$svcnum">
205                 <INPUT TYPE="hidden" name="action" value="view_port_graph"> !; 
206   $OUT .= preset_range($default_start,$default_end,'Last Day',$date_format,$svcnum)
207         .' | '.preset_range($default_end-86400*7,$default_end,'Last Week',$date_format,$svcnum)
208         .' | '.preset_range($default_end-86400*30,$default_end,'Last Month',$date_format,$svcnum)
209         .' | '.preset_range($default_end-86400*365,$default_end,'Last Year',$date_format,$svcnum);
210   
211   $OUT .= qq! <BR>
212             Start Date <INPUT TYPE="TEXT" id="${svcnum}_start" name="${svcnum}_start" SIZE="10" MAXLENGTH="10">
213             End Date <INPUT TYPE="TEXT" id="${svcnum}_end" name="${svcnum}_end" SIZE="10" MAXLENGTH="10">
214             <BR>
215             <INPUT TYPE="submit" value="Display"> !;
216
217   $OUT .= '</FORM></TD></TR>';
218 }
219 %>
220
221 <%= scalar(@svc_port) ? '</TABLE><BR><BR>' : '' %>
222
223
224 </TD></TR></TABLE>
225 <%= include('footer') %>
226