Vitelity API: Use new getcdr command, RT#31037
[freeside.git] / bin / cdr-vitelity.import
1 #!/usr/bin/perl
2
3 =pod
4
5 cdr-vitelity.import [ -v ] [ -k ]
6                     -s date -e date
7                     username
8                     [ exportnum ]
9
10 Download CDRs using the Vitelity API.
11
12 -v: Be verbose.
13
14 -k: Keep the .csv file for debugging purposes, instead of deleting it.
15
16 -s date: Import only records on or after 'date'  Now required as the Vitelity
17 API has changed.
18
19 -e date: Import only records before 'date'.  Now required as the Vitelity API
20 has changed.
21
22 username: a Freeside user
23
24 exportnum: Run only for that export.  If not specified, this will run for 
25 all Vitelity exports.
26
27 =cut
28
29 use strict;
30 use FS::UID qw(adminsuidsetup dbh);
31 use FS::Record qw(qsearchs qsearch);
32 use FS::cdr;
33 use FS::part_export;
34 use Getopt::Std;
35 use File::Temp;
36 use Date::Format 'time2str';
37 use Date::Parse 'str2time';
38
39 my %opt;
40 getopts('vks:e:', \%opt);
41
42 my $user = shift or die &usage;
43 my $exportnum = shift;
44 my $dbh = adminsuidsetup $user;
45
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');
48
49 local $FS::UID::AutoCommit = 0;
50
51 my @part_exports;
52 if ( $exportnum ) {
53   @part_exports = ( qsearchs('part_export', { 'exportnum' => $exportnum }) )
54     or die "exportnum $exportnum not found\n";
55 }
56 else {
57   @part_exports = qsearch('part_export', { 'exporttype' => 'vitelity' })
58     or die "no Vitelity exports found\n";
59 }
60
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";
66
67   my $dir = $FS::UID::cache_dir . "/cache.". $FS::UID::datasrc;
68   my $temp = new File::Temp ( TEMPLATE => 'download.XXXXXXXX',
69                               SUFFIX   => '.csv',
70                               DIR      => $dir,
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'};
74
75   print "Sending API request..." if $opt{'v'};
76   my @records = eval { $export->vitelity_command('getcdr') };
77   if ( $@ ) {
78     print "download error: $@\n";
79     exit(-1);
80   }
81   print "received ".scalar(@records)." records\n" if $opt{'v'};
82   if ( !@records ) {
83     print "No records to process.\n" if $opt{'v'};
84     exit(1);
85   }
86
87   print $temp "Date,Source,Destination,Seconds,CallerID,Disposition,Cost\n";
88
89   my $regex = qr/^(\d{4})-(\d{2})-(\d{2})/;
90   my $s = time2str('%m-%d-%Y', $start);
91   my $e = time2str('%m-%d-%Y', $end);
92   my $count = 0;
93   while (my $rec = shift @records) {
94     my $date = substr($rec, 0, 10);
95     next if ($start and $s gt $date);
96     next if ($end and $e le $date);
97     print $temp $rec, "\n";
98     $count++;
99   }
100   close $temp;
101   print "Selected $count records in date range." if $opt{'v'};
102
103   my $format = 'vitelity';
104   my $batchname = "vitelity-$exportnum-".time2str('%Y/%m/%d-%T',time);
105
106   print "Importing batch..." if $opt{'v'};
107   my $error = FS::cdr::batch_import( {
108     'file'            => $temp->filename,
109     'format'          => $format,
110     'batch_namevalue' => $batchname,
111   } );
112
113   if ( $error ) {
114     $dbh->rollback;
115     print "error: $error";
116     exit(-2);
117   }
118 }
119 $dbh->commit;
120 print "done.\n";
121 exit(0);
122
123 sub usage {
124   my $err = @_ ? shift."\n\n" : '';
125 $err."Usage: 
126 cdr-vitelity.import [ -v ] [ -k ]
127                     -s date -e date
128                     username
129                     [ exportnum ]
130 ";
131 }
132