diff options
| author | ivan <ivan> | 2008-02-29 04:31:42 +0000 | 
|---|---|---|
| committer | ivan <ivan> | 2008-02-29 04:31:42 +0000 | 
| commit | b1c3fd8ab89492b5d4429b3898cbc8c1b8ea1afd (patch) | |
| tree | 6c0b1c2f0f1bb1f49f846368406d641571064e83 /FS | |
| parent | a179683a47a5dbb86ae4bfc98e1d64d50283e39d (diff) | |
working asterisk CDR CSV import (not just direct DB)
Diffstat (limited to 'FS')
| -rw-r--r-- | FS/FS/cdr.pm | 48 | 
1 files changed, 40 insertions, 8 deletions
| diff --git a/FS/FS/cdr.pm b/FS/FS/cdr.pm index bddd1bf51..5078ff610 100644 --- a/FS/FS/cdr.pm +++ b/FS/FS/cdr.pm @@ -446,6 +446,38 @@ sub downstream_csv {  my($tmp_mday, $tmp_mon, $tmp_year); +sub _cdr_date_parser_maker { +  my $field = shift; +  return sub { +    my( $cdr, $date ) = @_; +    $cdr->$field( _cdr_date_parse($date) ); +  }; +} + +sub _cdr_date_parse { +  my $date = shift; + +  return '' unless length($date); #that's okay, it becomes NULL + +  #$date =~ /^\s*(\d{4})[\-\/]\(\d{1,2})[\-\/](\d{1,2})\s+(\d{1,2}):(\d{1,2}):(\d{1,2})\s*$/ +  $date =~ /^\s*(\d{4})\D(\d{1,2})\D(\d{1,2})\s+(\d{1,2})\D(\d{1,2})\D(\d{1,2})\s*$/ +    or die "unparsable date: $date"; #maybe we shouldn't die... +  my($year, $mon, $day, $hour, $min, $sec) = ( $1, $2, $3, $4, $5, $6 ); + +  timelocal($sec, $min, $hour, $day, $mon-1, $year); +} + +#http://www.the-asterisk-book.com/unstable/funktionen-cdr.html +my %amaflags = ( +  DEFAULT       => 0, +  OMIT          => 1, #asterisk 1.4+ +  IGNORE        => 1, #asterisk 1.2 +  BILLING       => 2, #asterisk 1.4+ +  BILL          => 2, #asterisk 1.2 +  DOCUMENTATION => 3, +  #? '' => 0, +); +  my %import_formats = (    'asterisk' => [      'accountcode', @@ -457,13 +489,13 @@ my %import_formats = (      'dstchannel',      'lastapp',      'lastdata', -    'startdate', # XXX will need massaging -    'answer',    # XXX same -    'end',       # XXX same +    _cdr_date_parser_maker('startdate'), +    _cdr_date_parser_maker('answerdate'), +    _cdr_date_parser_maker('enddate'),      'duration',      'billsec',      'disposition', -    'amaflags', +    sub { my($cdr, $amaflags) = @_; $cdr->amaflags($amaflags{$amaflags}); },      'uniqueid',      'userfield',    ], @@ -492,7 +524,7 @@ my %import_formats = (      'carrierid',      'upstream_rateid',    ], -  'ams' => [ +  'simple' => [      # Date      sub { my($cdr, $date) = @_; @@ -556,7 +588,7 @@ sub batch_import {    local $FS::UID::AutoCommit = 0;    my $dbh = dbh; -  if ( $format eq 'ams' ) { # and other formats with a header too? +  if ( $format eq 'simple' ) { # and other formats with a header too?    } @@ -565,7 +597,7 @@ sub batch_import {    while ( defined($line=<$fh>) ) {      #skip header... -    if ( ! $body++ && $format eq 'ams' && $line =~ /^[\w\, ]+$/ ) { +    if ( ! $body++ && $format eq 'simple' && $line =~ /^[\w\, ]+$/ ) {        next;      } @@ -577,7 +609,7 @@ sub batch_import {      my @columns = $csv->fields();      #warn join('-',@columns); -    if ( $format eq 'ams' ) { +    if ( $format eq 'simple' ) {        @columns = map { s/^ +//; $_; } @columns;      } | 
