this should fix credits pushing typeset invoices off the right
[freeside.git] / FS / FS / cdr.pm
index f008227..b640bc6 100644 (file)
@@ -269,6 +269,17 @@ sub check {
   $self->SUPER::check;
 }
 
+=item is_tollfree
+
+  Returns true when the cdr represents a toll free number and false otherwise.
+
+=cut
+
+sub is_tollfree {
+  my $self = shift;
+  ( $self->dst =~ /^(\+?1)?8(8|([02-7])\3)/ ) ? 1 : 0;
+}
+
 =item set_charged_party
 
 If the charged_party field is already set, does nothing.  Otherwise:
@@ -294,7 +305,7 @@ sub set_charged_party {
 
     } else {
 
-      if ( $self->dst =~ /^(\+?1)?8[02-8]{2}/ ) {
+      if ( $self->is_tollfree ) {
         $self->charged_party($self->dst);
       } else {
         $self->charged_party($self->src);
@@ -466,10 +477,19 @@ my %export_names = (
   },
   'accountcode_default' => {
     'name'           => 'Default plus accountcode',
-    'invoice_header' => 'Caller,Date,Time,Number,Destination,Duration,Price',
+    'invoice_header' => 'Date,Time,Account,Number,Destination,Duration,Price',
   },
 );
 
+my $duration_sub = sub {
+  my($cdr, %opt) = @_;
+  if ( $opt{minutes} ) {
+    $opt{minutes}. ( $opt{granularity} ? 'm' : ' call' );
+  } else {
+    sprintf('%.2fm', $cdr->billsec / 60 );
+  }
+};
+
 my %export_formats = (
   'convergent' => [
     'carriername', #CARRIER
@@ -491,7 +511,7 @@ my %export_formats = (
     sub { time2str('%r', shift->calldate_unix ) },   #TIME
     'userfield',                                     #USER
     'dst',                                           #NUMBER_DIALED
-    sub { sprintf('%.2fm', shift->billsec / 60 ) },  #DURATION
+    $duration_sub,                                   #DURATION
     #sub { sprintf('%.3f', shift->upstream_price ) }, #PRICE
     sub { my($cdr, %opt) = @_; $opt{money_char}. $opt{charge}; }, #PRICE
   ],
@@ -501,7 +521,7 @@ my %export_formats = (
     #'userfield',                                     #USER
     'dst',                                           #NUMBER_DIALED
     'src',                                           #called from
-    sub { sprintf('%.2fm', shift->billsec / 60 ) },  #DURATION
+    $duration_sub,                                   #DURATION
     #sub { sprintf('%.3f', shift->upstream_price ) }, #PRICE
     sub { my($cdr, %opt) = @_; $opt{money_char}. $opt{charge}; }, #PRICE
   ],
@@ -522,9 +542,7 @@ my %export_formats = (
     sub { my($cdr, %opt) = @_; $opt{dst_regionname}; },
 
     #DURATION
-    sub { my($cdr, %opt) = @_;
-          $opt{minutes}. ( $opt{granularity} ? 'm' : ' call' );
-        },
+    $duration_sub,
 
     #PRICE
     sub { my($cdr, %opt) = @_; $opt{money_char}. $opt{charge}; },
@@ -663,10 +681,11 @@ sub _cdr_min_parse {
 
 sub _cdr_date_parser_maker {
   my $field = shift;
+  my %options = @_;
   my @fields = ref($field) ? @$field : ($field);
   return sub {
     my( $cdr, $datestring ) = @_;
-    my $unixdate = eval { _cdr_date_parse($datestring) };
+    my $unixdate = eval { _cdr_date_parse($datestring, %options) };
     die "error parsing date for @fields from $datestring: $@\n" if $@;
     $cdr->$_($unixdate) foreach @fields;
   };
@@ -674,6 +693,7 @@ sub _cdr_date_parser_maker {
 
 sub _cdr_date_parse {
   my $date = shift;
+  my %options = @_;
 
   return '' unless length($date); #that's okay, it becomes NULL
 
@@ -693,7 +713,11 @@ sub _cdr_date_parse {
   return '' if $year == 1900 && $mon == 1 && $day == 1
             && $hour == 0    && $min == 0 && $sec == 0;
 
-  timelocal($sec, $min, $hour, $day, $mon-1, $year);
+  if ($options{gmt}) {
+    timegm($sec, $min, $hour, $day, $mon-1, $year);
+  } else {
+    timelocal($sec, $min, $hour, $day, $mon-1, $year);
+  }
 }
 
 =item batch_import HASHREF