From: ivan Date: Fri, 29 Feb 2008 04:31:41 +0000 (+0000) Subject: working asterisk CDR CSV import (not just direct DB) X-Git-Tag: TRIXBOX_2_6~45 X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=commitdiff_plain;h=9e6c6f13593dd5137ea920f49d36dc4321c9e99c working asterisk CDR CSV import (not just direct DB) --- 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; } diff --git a/httemplate/misc/cdr-import.html b/httemplate/misc/cdr-import.html index 36b2e4cb0..60f619e81 100644 --- a/httemplate/misc/cdr-import.html +++ b/httemplate/misc/cdr-import.html @@ -2,9 +2,9 @@
Import a CSV file containing Call Detail Records (CDRs).

CDR Format:

Filename: