This commit was manufactured by cvs2svn to create tag 'freeside_2_1_0'.
[freeside.git] / FS / FS / cdr.pm
index 3735274..2426f41 100644 (file)
@@ -1,7 +1,7 @@
 package FS::cdr;
 
 use strict;
 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;
 use Exporter;
 use Tie::IxHash;
 use Date::Parse;
@@ -13,11 +13,14 @@ use FS::Record qw( qsearch qsearchs );
 use FS::cdr_type;
 use FS::cdr_calltype;
 use FS::cdr_carrier;
 use FS::cdr_type;
 use FS::cdr_calltype;
 use FS::cdr_carrier;
+use FS::cdr_batch;
+use FS::cdr_termination;
 
 @ISA = qw(FS::Record);
 @EXPORT_OK = qw( _cdr_date_parser_maker _cdr_min_parser_maker );
 
 $DEBUG = 0;
 
 @ISA = qw(FS::Record);
 @EXPORT_OK = qw( _cdr_date_parser_maker _cdr_min_parser_maker );
 
 $DEBUG = 0;
+$me = '[FS::cdr]';
 
 =head1 NAME
 
 
 =head1 NAME
 
@@ -193,7 +196,8 @@ sub table_info {
         'svcnum'                => 'Freeside service',
         'freesidestatus'        => 'Freeside status',
         'freesiderewritestatus' => 'Freeside rewrite status',
         'svcnum'                => 'Freeside service',
         'freesidestatus'        => 'Freeside status',
         'freesiderewritestatus' => 'Freeside rewrite status',
-        'cdrbatch'              => 'Batch',
+        'cdrbatch'              => 'Legacy batch',
+        'cdrbatchnum'           => 'Batch',
     },
 
   };
     },
 
   };
@@ -375,7 +379,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.
 
 Sets the status to the provided string.  If there is an error, returns the
 error, otherwise returns false.
@@ -383,10 +387,33 @@ error, otherwise returns false.
 =cut
 
 sub set_status_and_rated_price {
 =cut
 
 sub set_status_and_rated_price {
-  my($self, $status, $rated_price) = @_;
-  $self->freesidestatus($status);
-  $self->rated_price($rated_price);
-  $self->replace();
+  my($self, $status, $rated_price, $svcnum, %opt) = @_;
+  if($opt{'inbound'}) {
+    my $term = qsearchs('cdr_termination', {
+        acctid   => $self->acctid, 
+        termpart => 1 # inbound
+    });
+    my $error;
+    if($term) {
+      warn "replacing existing cdr status (".$self->acctid.")\n" if $term;
+      $error = $term->delete;
+      return $error if $error;
+    }
+    $term = FS::cdr_termination->new({
+        acctid      => $self->acctid,
+        termpart    => 1,
+        rated_price => $rated_price,
+        status      => $status,
+        svcnum      => $svcnum,
+    });
+    return $term->insert;
+  }
+  else {
+    $self->freesidestatus($status);
+    $self->rated_price($rated_price);
+    $self->svcnum($svcnum) if $svcnum;
+    return $self->replace();
+  }
 }
 
 =item calldate_unix 
 }
 
 =item calldate_unix 
@@ -504,7 +531,9 @@ my $duration_sub = sub {
   if ( $opt{minutes} ) {
     $opt{minutes}. ( $opt{granularity} ? 'm' : ' call' );
   } else {
   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 +744,12 @@ 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 );
     ($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 );
+  } elsif ( $date  =~ /^\s*(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/ ) {
+    # WIP: 20100329121420
+    ($year, $mon, $day, $hour, $min, $sec) = ( $1, $2, $3, $4, $5, $6 );
   } else {
      die "unparsable date: $date"; #maybe we shouldn't die...
   }
   } else {
      die "unparsable date: $date"; #maybe we shouldn't die...
   }
@@ -754,7 +789,11 @@ Set true to prevent throwing an error on empty imports
 =cut
 
 my %import_options = (
 =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
 
   'formats' => { map { $_ => $cdr_info{$_}->{'import_fields'}; }
                      keys %cdr_info
@@ -801,7 +840,7 @@ sub process_batch_import {
   my $job = shift;
 
   my $opt = _import_options;
   my $job = shift;
 
   my $opt = _import_options;
-  $opt->{'params'} = [ 'format', 'cdrbatch' ];
+#  $opt->{'params'} = [ 'format', 'cdrbatch' ];
 
   FS::Record::process_batch_import( $job, $opt, @_ );
 
 
   FS::Record::process_batch_import( $job, $opt, @_ );
 
@@ -810,6 +849,37 @@ sub process_batch_import {
 #    @columns = map { s/^ +//; $_; } @columns;
 #  }
 
 #    @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
 =back
 
 =head1 BUGS