rewrite CDRs for forwarded Asterisk calls to be billable, RT#3196
[freeside.git] / FS / FS / cdr.pm
index c2a3d00..8307b28 100644 (file)
@@ -130,6 +130,8 @@ following fields are currently supported:
 
 =item freesidestatus - NULL, done (or something)
 
+=item freesiderewritestatus - NULL, done (or something)
+
 =item cdrbatch
 
 =back
@@ -228,6 +230,7 @@ sub check {
 #    || $self->ut_numbern('upstream_rateid')
 #    || $self->ut_numbern('svcnum')
 #    || $self->ut_textn('freesidestatus')
+#    || $self->ut_textn('freesiderewritestatus')
 #  ;
 #  return $error if $error;
 
@@ -436,11 +439,11 @@ my %export_names = (
   },
   'default' => {
     'name'           => 'Default',
-    'invoice_header' => 'Date,Time,Duration,Price,Number,Destination',
+    'invoice_header' => 'Date,Time,Number,Destination,Duration,Price',
   },
   'source_default' => {
     'name'           => 'Default with source',
-    'invoice_header' => 'Caller,Date,Time,Duration,Number,Destination,Price',
+    'invoice_header' => 'Caller,Date,Time,Number,Destination,Duration,Price',
   },
 );
 
@@ -466,7 +469,8 @@ my %export_formats = (
     'userfield',                                     #USER
     'dst',                                           #NUMBER_DIALED
     sub { sprintf('%.2fm', shift->billsec / 60 ) },  #DURATION
-    sub { sprintf('%.3f', shift->upstream_price ) }, #PRICE
+    #sub { sprintf('%.3f', shift->upstream_price ) }, #PRICE
+    sub { my($cdr, %opt) = @_; $opt{money_char}. $opt{charge}; }, #PRICE
   ],
   'simple2' => [
     sub { time2str('%D', shift->calldate_unix ) },   #DATE
@@ -475,7 +479,8 @@ my %export_formats = (
     'dst',                                           #NUMBER_DIALED
     'src',                                           #called from
     sub { sprintf('%.2fm', shift->billsec / 60 ) },  #DURATION
-    sub { sprintf('%.3f', shift->upstream_price ) }, #PRICE
+    #sub { sprintf('%.3f', shift->upstream_price ) }, #PRICE
+    sub { my($cdr, %opt) = @_; $opt{money_char}. $opt{charge}; }, #PRICE
   ],
   'default' => [
 
@@ -487,6 +492,12 @@ my %export_formats = (
     sub { time2str('%r', shift->calldate_unix ) },
           # time2str("%c", $cdr->calldate_unix),  #XXX this should probably be a config option dropdown so they can select US vs- rest of world dates or whatnot
 
+    #DEST ("Number")
+    sub { my($cdr, %opt) = @_; $opt{pretty_dst} || $cdr->dst; },
+
+    #REGIONNAME ("Destination")
+    sub { my($cdr, %opt) = @_; $opt{dst_regionname}; },
+
     #DURATION
     sub { my($cdr, %opt) = @_;
           $opt{minutes}. ( $opt{granularity} ? 'm' : ' call' );
@@ -495,19 +506,9 @@ my %export_formats = (
     #PRICE
     sub { my($cdr, %opt) = @_; $opt{money_char}. $opt{charge}; },
 
-    #DEST ("Number")
-    sub { my($cdr, %opt) = @_; $opt{pretty_dst} || $cdr->dst; },
-
-    #REGIONNAME ("Destination")
-    sub { my($cdr, %opt) = @_; $opt{dst_regionname}; },
-
   ],
 );
-$export_formats{'source_default'} = [ 'src',
-                                      @{ $export_formats{'default'} }[0..2],
-                                      @{ $export_formats{'default'} }[4..5],
-                                      @{ $export_formats{'default'} }[3],
-                                    ];
+$export_formats{'source_default'} = [ 'src', @{ $export_formats{'default'} }, ];
 
 sub downstream_csv {
   my( $self, %opt ) = @_;
@@ -634,11 +635,12 @@ sub _cdr_min_parse {
 
 sub _cdr_date_parser_maker {
   my $field = shift;
+  my @fields = ref($field) ? @$field : ($field);
   return sub {
-    my( $cdr, $date ) = @_;
-    #$cdr->$field( _cdr_date_parse($date) );
-    eval { $cdr->$field( _cdr_date_parse($date) ); };
-    die "error parsing date for $field from $date: $@\n" if $@;
+    my( $cdr, $datestring ) = @_;
+    my $unixdate = eval { _cdr_date_parse($datestring) };
+    die "error parsing date for @fields from $datestring: $@\n" if $@;
+    $cdr->$_($unixdate) foreach @fields;
   };
 }
 
@@ -672,43 +674,73 @@ Imports CDR records.  Available options are:
 
 =over 4
 
-=item filehandle
+=item file
+
+Filename
 
 =item format
 
+=item params
+
+Hash reference of preset fields, typically cdrbatch
+
+=item empty_ok
+
+Set true to prevent throwing an error on empty imports
+
 =back
 
 =cut
 
-sub process_batch_import {
-  my $job = shift;
+my %import_options = (
+  'table'   => 'cdr',
 
-  my $opt = {
-    'table'   => 'cdr',
-    'params'  => [ 'format', 'cdrbatch' ],
+  'formats' => { map { $_ => $cdr_info{$_}->{'import_fields'}; }
+                     keys %cdr_info
+               },
 
-    'formats' => { map { $_ => $cdr_info{$_}->{'import_fields'}; }
-                       keys %cdr_info
-                 },
+                          #drop the || 'csv' to allow auto xls for csv types?
+  'format_types' => { map { $_ => ( lc($cdr_info{$_}->{'type'}) || 'csv' ); }
+                          keys %cdr_info
+                    },
 
-                            #drop the || 'csv' to allow auto xls for csv types?
-    'format_types' => { map { $_ => ( lc($cdr_info{$_}->{'type'}) || 'csv' ); }
+  'format_headers' => { map { $_ => ( $cdr_info{$_}->{'header'} || 0 ); }
                             keys %cdr_info
                       },
 
-    'format_headers' => { map { $_ => ( $cdr_info{$_}->{'header'} || 0 ); }
+  'format_sep_chars' => { map { $_ => $cdr_info{$_}->{'sep_char'}; }
                               keys %cdr_info
                         },
 
-    'format_sep_chars' => { map { $_ => $cdr_info{$_}->{'sep_char'}; }
-                                keys %cdr_info
-                          },
+  'format_fixedlength_formats' =>
+    { map { $_ => $cdr_info{$_}->{'fixedlength_format'}; }
+          keys %cdr_info
+    },
+);
 
-    'format_fixedlength_formats' =>
-      { map { $_ => $cdr_info{$_}->{'fixedlength_format'}; }
-            keys %cdr_info
-      },
-  };
+sub _import_options {
+  \%import_options;
+}
+
+sub batch_import {
+  my $opt = shift;
+
+  my $iopt = _import_options;
+  $opt->{$_} = $iopt->{$_} foreach keys %$iopt;
+
+  FS::Record::batch_import( $opt );
+
+}
+
+=item process_batch_import
+
+=cut
+
+sub process_batch_import {
+  my $job = shift;
+
+  my $opt = _import_options;
+  $opt->{'params'} = [ 'format', 'cdrbatch' ];
 
   FS::Record::process_batch_import( $job, $opt, @_ );