input and output on data volume fields specified with k,m,g,or t
[freeside.git] / FS / FS / UI / Web.pm
index 18afc3d..a05a667 100644 (file)
@@ -4,6 +4,7 @@ use strict;
 use vars qw($DEBUG $me);
 use FS::Conf;
 use FS::Record qw(dbdef);
+use Number::Format;
 
 #use vars qw(@ISA);
 #use FS::UI
@@ -18,19 +19,20 @@ $me = '[FS::UID::Web]';
 
 use Date::Parse;
 sub parse_beginning_ending {
-  my($cgi) = @_;
+  my($cgi, $prefix) = @_;
+  $prefix .= '_' if $prefix;
 
   my $beginning = 0;
-  if ( $cgi->param('begin') =~ /^(\d+)$/ ) {
+  if ( $cgi->param($prefix.'begin') =~ /^(\d+)$/ ) {
     $beginning = $1;
-  } elsif ( $cgi->param('beginning') =~ /^([ 0-9\-\/]{1,64})$/ ) {
+  } elsif ( $cgi->param($prefix.'beginning') =~ /^([ 0-9\-\/]{1,64})$/ ) {
     $beginning = str2time($1) || 0;
   }
 
   my $ending = 4294967295; #2^32-1
-  if ( $cgi->param('end') =~ /^(\d+)$/ ) {
+  if ( $cgi->param($prefix.'end') =~ /^(\d+)$/ ) {
     $ending = $1 - 1;
-  } elsif ( $cgi->param('ending') =~ /^([ 0-9\-\/]{1,64})$/ ) {
+  } elsif ( $cgi->param($prefix.'ending') =~ /^([ 0-9\-\/]{1,64})$/ ) {
     #probably need an option to turn off the + 86399
     $ending = str2time($1) + 86399;
   }
@@ -185,6 +187,31 @@ sub bytecount_unexact {
   return(sprintf("%.2f Gbytes", $bc/1000000000));
 }
 
+sub parse_bytecount {
+  my $bc = shift;
+  return $bc if (($bc =~ tr/.//) > 1);
+  $bc =~ /^\s*([\d.]*)\s*([kKmMgGtT]?)[bB]?\s*$/ or return $bc;
+  my $base = $1;
+  return $bc unless length $base;
+  my $exponent = index ' kmgt', lc($2);
+  return $bc if ($exponent < 0 && $2);
+  $exponent = 0 if ($exponent < 0);
+  return $base * 1024 ** $exponent;
+}
+
+sub display_bytecount {
+  my $bc = shift;
+  return $bc unless ($bc =~ /^(\d+)$/);
+  my $conf = new FS::Conf;
+  my $f = new Number::Format;
+  my $precision = $conf->exists('datavolume-significantdigits')
+                ? $conf->config('datavolume-significantdigits')
+                : 3;
+  my $unit = $conf->exists('datavolume-forcemegabytes') ? 'M' : 'A';
+
+  return $f->format_bytes($bc, precision => $precision, unit => $unit);
+}
+
 ###
 # cust_main report subroutines
 ###