5 freeside-vitelity-cdrimport [ -v ] [ -k ]
10 Download CDRs using the Vitelity API.
14 -k: Keep the .csv file for debugging purposes, instead of deleting it.
16 -s date: Import only records on or after 'date' Now required as the Vitelity
19 -e date: Import only records before 'date'. Now required as the Vitelity API
22 username: a Freeside user
24 exportnum: Run only for that export. If not specified, this will run for
30 use FS::UID qw(adminsuidsetup dbh);
31 use FS::Record qw(qsearchs qsearch);
36 use Date::Format 'time2str';
37 use Date::Parse 'str2time';
40 getopts('vks:e:', \%opt);
42 my $user = shift or die &usage;
43 my $exportnum = shift;
44 my $dbh = adminsuidsetup $user;
46 my $start = $opt{'s'} ? str2time($opt{'s'}) : die &usage('-s is now required');
47 my $end = $opt{'e'} ? str2time($opt{'e'}) : die &usage('-e is now required');
49 local $FS::UID::AutoCommit = 0;
53 @part_exports = ( qsearchs('part_export', { 'exportnum' => $exportnum }) )
54 or die "exportnum $exportnum not found\n";
57 @part_exports = qsearch('part_export', { 'exporttype' => 'vitelity' })
58 or die "no Vitelity exports found\n";
61 foreach my $export (@part_exports) {
62 my $exportnum = $export->exportnum;
63 print "Processing exportnum $exportnum.\n" if $opt{'v'};
64 $export->isa('FS::part_export::vitelity')
65 or die "exportnum $exportnum is not a Vitelity export\n";
67 my $dir = $FS::UID::cache_dir . "/cache.". $FS::UID::datasrc;
68 my $temp = new File::Temp ( TEMPLATE => 'download.XXXXXXXX',
71 UNLINK => !$opt{'k'} )
72 or die "can't open temporary file to store download: $!\n";
73 print "Downloading to ".$temp->filename."\n" if $opt{'v'};
75 print "Sending API request..." if $opt{'v'};
77 my $s = time2str('%m-%d-%Y', $start);
78 my $e = time2str('%m-%d-%Y', $end);
80 my @records = eval { $export->vitelity_command('getcdr',
86 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 while (my $rec = shift @records) {
99 print $temp $rec, "\n";
103 my $format = 'vitelity';
104 my $batchname = "vitelity-$exportnum-".time2str('%Y/%m/%d-%T',time);
106 print "Importing batch..." if $opt{'v'};
107 my $error = FS::cdr::batch_import( {
108 'file' => $temp->filename,
110 'batch_namevalue' => $batchname,
115 print "error: $error";
124 my $err = @_ ? shift."\n\n" : '';
126 freeside-vitelity-cdrimport [ -v ] [ -k ]