5 cdr-vitelity.import [ -v ] [ -k ] [ -r ]
6 [ -s date ] [ -e date ]
10 Download CDRs using the Vitelity API.
14 -k: Keep the .csv file for debugging purposes, instead of deleting it.
16 -r: Reset the CDR counter before fetching. This will download previously
19 -s date: Import only records on or after 'date'. If not specified, the
20 script will import the entire history (if -r is specified) or everything
21 since the last import (if not). Note that the API request doesn't have any
22 limits, so the entire data file will be downloaded regardless, but parsing
23 and importing the records is considerably slower than downloading them.
25 -e date: Import only records before 'date'. See above.
27 username: a Freeside user
29 exportnum: Run only for that export. If not specified, this will run for
35 use FS::UID qw(adminsuidsetup dbh);
36 use FS::Record qw(qsearchs qsearch);
41 use Date::Format 'time2str';
42 use Date::Parse 'str2time';
45 getopts('vrkas:e:', \%opt);
47 my $user = shift or die &usage;
48 my $exportnum = shift;
49 my $dbh = adminsuidsetup $user;
50 local $FS::UID::AutoCommit = 0;
54 @part_exports = ( qsearchs('part_export', { 'exportnum' => $exportnum }) )
55 or die "exportnum $exportnum not found\n";
58 @part_exports = qsearch('part_export', { 'exporttype' => 'vitelity' })
59 or die "no Vitelity exports found\n";
62 foreach my $export (@part_exports) {
63 my $exportnum = $export->exportnum;
64 print "Processing exportnum $exportnum.\n" if $opt{'v'};
65 $export->isa('FS::part_export::vitelity')
66 or die "exportnum $exportnum is not a Vitelity export\n";
69 my $result = $export->vitelity_command('resetcdrlist');
70 if ( $result ne 'ok' ) {
72 die "Failed to reset CDR list: $result\n";
76 my $dir = $FS::UID::cache_dir . "/cache.". $FS::UID::datasrc;
77 my $temp = new File::Temp ( TEMPLATE => 'download.XXXXXXXX',
80 UNLINK => !$opt{'k'} )
81 or die "can't open temporary file to store download: $!\n";
82 print "Downloading to ".$temp->filename."\n" if $opt{'v'};
84 print "Sending API request..." if $opt{'v'};
85 my @records = eval { $export->vitelity_command('cdrlist') };
87 print "download error: $@\n";
90 print "received ".scalar(@records)." records\n" if $opt{'v'};
92 print "No records to process.\n" if $opt{'v'};
96 print $temp "Date,Source,Destination,Seconds,CallerID,Disposition,Cost\n";
98 my $regex = qr/^(\d{4})-(\d{2})-(\d{2})/;
99 my $start = $opt{'s'} ? str2time($opt{'s'}) : '';
100 my $s = time2str('%Y-%m-%d', $start) if $start;
101 my $end = $opt{'e'} ? str2time($opt{'e'}) : '';
102 my $e = time2str('%Y-%m-%d', $end) if $end;
104 while (my $rec = shift @records) {
105 my $date = substr($rec, 0, 10);
106 next if ($start and $s gt $date);
107 next if ($end and $e le $date);
108 print $temp $rec, "\n";
112 print "Selected $count records in date range." if $opt{'v'} and ($s or $e);
114 my $format = 'vitelity';
115 my $batchname = "vitelity-$exportnum-".time2str('%Y/%m/%d-%T',time);
117 print "Importing batch..." if $opt{'v'};
118 my $error = FS::cdr::batch_import( {
119 'file' => $temp->filename,
121 'batch_namevalue' => $batchname,
126 print "error: $error";
136 cdr-vitelity.import [ -v ] [ -k ] [ -r ]
137 [ -s date ] [ -e date ]