X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=blobdiff_plain;f=bin%2Fcdr-netsapiens.import;h=1cce461e28ae89017392b082f87d2643d0555b32;hp=80922556aaa57b415db9708bc35a3609a8b148e0;hb=ff27c3f36240aee48ed50153dd5d8fe3ac3f2443;hpb=650c968bf1493c601dcf1b57d98ef0bdb1a2caa6 diff --git a/bin/cdr-netsapiens.import b/bin/cdr-netsapiens.import index 80922556a..1cce461e2 100755 --- a/bin/cdr-netsapiens.import +++ b/bin/cdr-netsapiens.import @@ -4,13 +4,14 @@ use strict; use vars qw( $DEBUG ); +use Date::Format; use REST::Client; -use FS::UID qw(adminsuidsetup); +use FS::UID qw(adminsuidsetup dbh); use FS::Record qw(qsearchs); use FS::part_export; use FS::cdr; -$DEBUG = 2; +$DEBUG = 1; my $user = shift or die &usage; adminsuidsetup $user; @@ -19,56 +20,86 @@ my $exportnum = shift or die &usage; my $part_export = qsearchs('part_export', { 'exportnum' => $exportnum } ) or die "unknown exportnum $exportnum\n"; -#find max time_release -#SELECT MAX( -#2009-02-19 20:17:37 -my $time_release = '2009-01-01 00:00:00'; +my $cdrbatch = 'NetSapiens import '. time2str('%Y-%m-%d %x', $^T); -#retreive CDRs >= this time -#XXX (in pages, this tops out at 20) _start=>0, _limit=>20 +my $cdrs = 0; -my $ns = $part_export->ns_command( 'GET', '/cdr/', - 'time_release' => '$time_release,', - '_sort' => '+time_release', - ); +do { -#loop over them, double check duplicates, insert the rest + #find max time_release + my $sth = dbh->prepare('SELECT MAX(enddate) FROM cdr') #XXX and imported from this netsapens switch + or die dbh->errstr; -my $content = $ns->responseContent; + $sth->execute or die $sth->errstr; + my $time_release = time2str('%Y-%m-%d %X', $sth->fetchrow_arrayref->[0]); + #retreive CDRs >= this time -#20090219201719000016@SkyNet360.Com -#
-while ( $content =~ - s/^.*?//is ) -{ + my $ns = $part_export->ns_command( 'GET', '/cdr/', + 'time_release' => "$time_release,", + '_sort' => '+time_release', + '_limit' => '500', + ); - my $accountcode = $1; - warn "$accountcode\n" if $DEBUG; + #loop over them, double check duplicates, insert the rest - $content =~ s/(.*?)<\/form>//is; - my $cdr_content = $1; + my $content = $ns->responseContent; - while ( $cdr_content =~ - s/.*?//is ) + $cdrs = 0; + + #20090219201719000016@SkyNet360.Com + # + while ( $content =~ + s/^.*?//is ) { - warn " $1 => $2\n" if $DEBUG > 1; + + my $cdrid = ($1-1900). $2; #2009 -> 109 so we fit in a bigint + + unless ( $cdrs ) { #inefficient + my $dsth = dbh->prepare( + 'SELECT cdrid FROM cdr WHERE cdrid IS NOT NULL AND cdrid = ?' + ) or die dbh->errstr; + $dsth->execute($cdrid) or die $dsth->errstr; + my $row = $dsth->fetchrow_arrayref; + if ( $row && $row->[0] eq $cdrid ) { # == w/ 8 byte int? + warn "$cdrid (dup)\n" if $DEBUG > 1; + next; + } + } + warn "$cdrid\n" if $DEBUG > 1; + + $content =~ s/(.*?)<\/form>//is; + my $cdr_content = $1; + + my %cdr = (); + while ( $cdr_content =~ + s/.*?//is ) + { + warn " $1 => $2\n" if $DEBUG > 2; + $cdr{$1} = $2; + } + + $cdrs++; my $cdr = new FS::cdr { - #'src' => - #'dst' => -# 'startdate' => 'time_start', #XXX needs parsing -# 'enddate' => 'time_release', #XXX needs parsing - 'duration' => 'duration', - 'billsec' => 'time_talking', + 'src' => $cdr{'orig_from_user'}, #orig_sub + 'dst' => $cdr{'orig_to_user'}, #term_sub? + 'startdate' => FS::cdr::_cdr_date_parse($cdr{'time_start'}), + 'enddate' => FS::cdr::_cdr_date_parse($cdr{'time_release'}), + 'duration' => $cdr{'duration'}, + 'billsec' => $cdr{'time_talking'}, #'disposition' => - 'accountcode' => $accountcode, + #'accountcode' => #'charged_party' + 'cdrid' => $cdrid, + 'cdrbatch' => $cdrbatch, }; - + + my $error = $cdr->insert; + die $error if $error; } -} +} while $cdrs; sub usage { "Usage: \n cdr-netsapiens.import user exportnum\n";