input and output on data volume fields specified with k,m,g,or t
[freeside.git] / FS / FS / UI / Web.pm
index 9ddcf14..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
@@ -186,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
 ###