communigate provisioning phase 2: Domain:Account Defaults:Settings: RulesAllowed...
[freeside.git] / FS / FS / cdr.pm
index 3735274..b9a4e41 100644 (file)
@@ -1,7 +1,7 @@
 package FS::cdr;
 
 use strict;
-use vars qw( @ISA @EXPORT_OK $DEBUG );
+use vars qw( @ISA @EXPORT_OK $DEBUG $me );
 use Exporter;
 use Tie::IxHash;
 use Date::Parse;
@@ -13,11 +13,13 @@ use FS::Record qw( qsearch qsearchs );
 use FS::cdr_type;
 use FS::cdr_calltype;
 use FS::cdr_carrier;
+use FS::cdr_batch;
 
 @ISA = qw(FS::Record);
 @EXPORT_OK = qw( _cdr_date_parser_maker _cdr_min_parser_maker );
 
 $DEBUG = 0;
+$me = '[FS::cdr]';
 
 =head1 NAME
 
@@ -193,7 +195,8 @@ sub table_info {
         'svcnum'                => 'Freeside service',
         'freesidestatus'        => 'Freeside status',
         'freesiderewritestatus' => 'Freeside rewrite status',
-        'cdrbatch'              => 'Batch',
+        'cdrbatch'              => 'Legacy batch',
+        'cdrbatchnum'           => 'Batch',
     },
 
   };
@@ -375,7 +378,7 @@ sub set_charged_party {
 
 }
 
-=item set_status_and_rated_price STATUS [ RATED_PRICE ]
+=item set_status_and_rated_price STATUS [ RATED_PRICE [ SVCNUM ] ]
 
 Sets the status to the provided string.  If there is an error, returns the
 error, otherwise returns false.
@@ -383,9 +386,10 @@ error, otherwise returns false.
 =cut
 
 sub set_status_and_rated_price {
-  my($self, $status, $rated_price) = @_;
+  my($self, $status, $rated_price, $svcnum) = @_;
   $self->freesidestatus($status);
   $self->rated_price($rated_price);
+  $self->svcnum($svcnum) if $svcnum;
   $self->replace();
 }
 
@@ -504,7 +508,9 @@ my $duration_sub = sub {
   if ( $opt{minutes} ) {
     $opt{minutes}. ( $opt{granularity} ? 'm' : ' call' );
   } else {
-    sprintf('%.2fm', $cdr->billsec / 60 );
+    #config if anyone really wants decimal minutes back
+    #sprintf('%.2fm', $cdr->billsec / 60 );
+    int($cdr->billsec / 60).'m '. ($cdr->billsec % 60).'s';
   }
 };
 
@@ -715,6 +721,9 @@ sub _cdr_date_parse {
     ($year, $mon, $day, $hour, $min, $sec) = ( $1, $2, $3, $4, $5, $6 );
   } elsif ( $date  =~ /^\s*(\d{1,2})\D(\d{1,2})\D(\d{4})\s+(\d{1,2})\D(\d{1,2})\D(\d{1,2})(\D|$)/ ) {
     ($mon, $day, $year, $hour, $min, $sec) = ( $1, $2, $3, $4, $5, $6 );
+  } elsif ( $date  =~ /^\s*(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d+\.\d+)(\D|$)/ ) {
+    # broadsoft: 20081223201938.314
+    ($year, $mon, $day, $hour, $min, $sec) = ( $1, $2, $3, $4, $5, $6);
   } else {
      die "unparsable date: $date"; #maybe we shouldn't die...
   }
@@ -754,7 +763,11 @@ Set true to prevent throwing an error on empty imports
 =cut
 
 my %import_options = (
-  'table'   => 'cdr',
+  'table'         => 'cdr',
+
+  'batch_keycol'  => 'cdrbatchnum',
+  'batch_table'   => 'cdr_batch',
+  'batch_namecol' => 'cdrbatch',
 
   'formats' => { map { $_ => $cdr_info{$_}->{'import_fields'}; }
                      keys %cdr_info
@@ -801,7 +814,7 @@ sub process_batch_import {
   my $job = shift;
 
   my $opt = _import_options;
-  $opt->{'params'} = [ 'format', 'cdrbatch' ];
+#  $opt->{'params'} = [ 'format', 'cdrbatch' ];
 
   FS::Record::process_batch_import( $job, $opt, @_ );
 
@@ -810,6 +823,37 @@ sub process_batch_import {
 #    @columns = map { s/^ +//; $_; } @columns;
 #  }
 
+# _ upgrade_data
+#
+# Used by FS::Upgrade to migrate to a new database.
+
+sub _upgrade_data {
+  my ($class, %opts) = @_;
+
+  warn "$me upgrading $class\n" if $DEBUG;
+
+  my $sth = dbh->prepare(
+    'SELECT DISTINCT(cdrbatch) FROM cdr WHERE cdrbatch IS NOT NULL'
+  ) or die dbh->errstr;
+
+  $sth->execute or die $sth->errstr;
+
+  my %cdrbatchnum = ();
+  while (my $row = $sth->fetchrow_arrayref) {
+    my $cdr_batch = new FS::cdr_batch { 'cdrbatch' => $row->[0] };
+    my $error = $cdr_batch->insert;
+    die $error if $error;
+    $cdrbatchnum{$row->[0]} = $cdr_batch->cdrbatchnum;
+  }
+
+  $sth = dbh->prepare('UPDATE cdr SET cdrbatch = NULL, cdrbatchnum = ? WHERE cdrbatch IS NOT NULL AND cdrbatch = ?') or die dbh->errstr;
+
+  foreach my $cdrbatch (keys %cdrbatchnum) {
+    $sth->execute($cdrbatchnum{$cdrbatch}, $cdrbatch) or die $sth->errstr;
+  }
+
+}
+
 =back
 
 =head1 BUGS