default to a session cookie instead of setting an explicit timeout, weird timezone...
[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   if ((@bytes_svcs > 1) and (grep { $bytes_show{$_.'_used'} } qw(seconds upbytes downbytes totalbytes) )) {
79     $OUT .= '<TR>';
80     $OUT .= '<TH align="left">Total Used</TH>';
81     foreach my $field (@bytes_cols) {
82       if ($bytes_show{$field}) {
83         $OUT .= '<TD ALIGN="right">';
84         $OUT .= $bytes_used_total{$field} || '0' if $field =~ /_used$/;
85         $OUT .= '</TD>';
86       }
87     }
88     $OUT .= '</TR>';
89   }    
90 %>
91
92 <%= scalar(@bytes_svcs) ? '</TABLE><BR><BR>' : '' %>
93
94 <%= if ( @svc_phone or @svc_pbx ) {
95       %any = ();
96       for my $dir (qw(outbound inbound)) {
97         $any{$dir} = grep { $_->{$dir} } (@svc_phone, @svc_pbx);
98       }
99       $OUT.= '<FONT SIZE="4">Call usage</FONT><BR><BR>
100               <TABLE BGCOLOR="#cccccc" STYLE="display:inline-block">
101                 <TR>
102                   <TH ALIGN="left">Number</TH>';
103       if ( $any{outbound} ) {
104         $OUT .= '
105                   <TH>Dialed</TH>';
106       }
107       if ( $any{inbound} ) {
108         $OUT .= '
109                   <TH>Received</TH>';
110       }
111       $OUT .= '</TR>';
112     } else {
113       $OUT .= '';
114     }
115 %>
116
117 <%= foreach my $svc_x ( @svc_phone, @svc_pbx ) {
118   my $link = $url . 'view_cdr_details;' .
119           'svcnum='.$svc_x->{'svcnum'}.
120           ';beginning=0;ending=0';
121   $OUT .= '<TR><TD>'. $svc_x->{'label'}. ': '. $svc_x->{'value'};
122   $OUT .= '</TD>';
123   # usage summary w/ links
124   for my $dir (qw(outbound inbound)) {
125     if ( $dir eq 'inbound' ) {
126       $link .= ';inbound=1';
127     }
128     if ( $svc_x->{$dir} ) {
129       $OUT .= '<TD ALIGN="right">'.qq!<A HREF="$link">! .
130         sprintf('%d calls (%.0f minutes)',
131           $svc_x->{$dir}->{'count'},
132           $svc_x->{$dir}->{'duration'} / 60
133         ) .
134         '</A></TD>';
135     } elsif ( $any{$dir} )  {
136       $OUT .= '<TD></TD>';
137     }
138   }
139   $OUT .= '</TR>';
140 }
141 '';
142 %>
143
144 <%= if ( @usage_pools ) {
145   $OUT .= '</TABLE>
146   <TABLE BGCOLOR="#cccccc" STYLE="display: inline-block">
147     <TR><TH COLSPAN=4>Remaining minutes</TH></TR>
148     ';
149   my $any_shared = 0;
150   foreach my $usage (@usage_pools) {
151     # false laziness with the back office side
152     my ($description, $remain, $total, $shared) = @$usage;
153     if ( $shared ) {
154       $any_shared = 1;
155       $description .= '*';
156     }
157     my $ratio = 255 * ($remain/$total);
158     $ratio = 255 if $color > 255;
159     my $color = 
160       sprintf('STYLE="font-weight: bold; color: #%02x%02x00"',
161         255 - $ratio, $ratio);
162     $OUT .=
163     qq!<TR>
164       <TD ALIGN="right">$description</TD>
165       <TD $color ALIGN="right">$remain</TD>
166       <TD $color> / </TD>
167       <TD $color>$total</TD>
168     </TR>!;
169   }
170   if ( $any_shared ) {
171     $OUT .= '<TR STYLE="font-size: 80%; font-style: italic">'.
172             '<TD COLSPAN=4>* shared among all your phone plans</TD></TR>';
173   }
174 }
175 if ( @svc_phone or @svc_pbx or @usage_pools ) {
176   $OUT .= '</TABLE><BR><BR>';
177 }
178 '';
179 %>
180
181 <%= if ( @svc_port ) {
182       $OUT.= '<FONT SIZE="4">Bandwidth Graphs</FONT><BR><BR>
183                 <script type="text/javascript">
184                     function preset_range(start,end,prefix){
185                         document.getElementById(prefix+\'_start\').value = start;
186                         document.getElementById(prefix+\'_end\').value = end;
187                     }
188                   </script>
189               <TABLE BGCOLOR="#cccccc">
190                 <TR>
191                   <TH ALIGN="left">Service</TH>
192                   <TH ALIGN="right">
193                   </TH>
194                 </TR>';
195     } 
196     $OUT .= '';
197 %>
198
199 <%=
200
201 sub preset_range {
202     my($start,$end,$label,$date_format,$prefix) = (shift,shift,shift,shift,shift);
203     $start = Date::Format::time2str($date_format,$start);
204     $end = Date::Format::time2str($date_format,$end);
205     return '<A HREF="javascript:void(0);" onclick="preset_range(\''
206             .$start.'\',\''.$end.'\',\''.$prefix.'\')">'.$label.'</A>';
207 }
208
209 foreach my $svc_port ( @svc_port ) {
210   $svcnum = $svc_port->{'svcnum'}; 
211   $default_end = time;
212   $default_start = $default_end-86400;
213
214   $OUT .= '<TR><TD>'. $svc_port->{'label'}. ': '. $svc_port->{'value'}.'</TD>';
215   $OUT .= qq! <TD><FORM ACTION="$url" METHOD="GET">
216                 <INPUT TYPE="hidden" name="svcnum" value="$svcnum">
217                 <INPUT TYPE="hidden" name="action" value="view_port_graph"> !; 
218   $OUT .= preset_range($default_start,$default_end,'Last Day',$date_format,$svcnum)
219         .' | '.preset_range($default_end-86400*7,$default_end,'Last Week',$date_format,$svcnum)
220         .' | '.preset_range($default_end-86400*30,$default_end,'Last Month',$date_format,$svcnum)
221         .' | '.preset_range($default_end-86400*365,$default_end,'Last Year',$date_format,$svcnum);
222   
223   $OUT .= qq! <BR>
224             Start Date <INPUT TYPE="TEXT" id="${svcnum}_start" name="${svcnum}_start" SIZE="10" MAXLENGTH="10">
225             End Date <INPUT TYPE="TEXT" id="${svcnum}_end" name="${svcnum}_end" SIZE="10" MAXLENGTH="10">
226             <BR>
227             <INPUT TYPE="submit" value="Display"> !;
228
229   $OUT .= '</FORM></TD></TR>';
230 }
231 %>
232
233 <%= scalar(@svc_port) ? '</TABLE><BR><BR>' : '' %>
234
235
236 </TD></TR></TABLE>
237 <%= include('footer') %>
238