tower tracking, RT#15538
[freeside.git] / bin / cdr-vitelity.import
1 #!/usr/bin/perl
2
3 =pod
4
5 cdr-vitelity.import [ -v ] [ -k ] [ -r ]
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 -r: Reset the CDR counter before fetching.  This will download previously 
17 imported CDRs.
18
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.
24
25 -e date: Import only records before 'date'.  See above.
26
27 username: a Freeside user
28
29 exportnum: Run only for that export.  If not specified, this will run for 
30 all Vitelity exports.
31
32 =cut
33
34 use strict;
35 use FS::UID qw(adminsuidsetup dbh);
36 use FS::Record qw(qsearchs qsearch);
37 use FS::cdr;
38 use FS::part_export;
39 use Getopt::Std;
40 use File::Temp;
41 use Date::Format 'time2str';
42 use Date::Parse 'str2time';
43
44 my %opt;
45 getopts('vrkas:e:', \%opt);
46
47 my $user = shift or die &usage;
48 my $exportnum = shift;
49 my $dbh = adminsuidsetup $user;
50 local $FS::UID::AutoCommit = 0;
51
52 my @part_exports;
53 if ( $exportnum ) {
54   @part_exports = ( qsearchs('part_export', { 'exportnum' => $exportnum }) )
55     or die "exportnum $exportnum not found\n";
56 }
57 else {
58   @part_exports = qsearch('part_export', { 'exporttype' => 'vitelity' })
59     or die "no Vitelity exports found\n";
60 }
61
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";
67
68   if ( $opt{'r'} ) {
69     my $result = $export->vitelity_command('resetcdrlist');
70     if ( $result ne 'ok' ) {
71       $dbh->rollback;
72       die "Failed to reset CDR list: $result\n";
73     }
74   }
75
76   my $dir = $FS::UID::cache_dir . "/cache.". $FS::UID::datasrc;
77   my $temp = new File::Temp ( TEMPLATE => 'download.XXXXXXXX',
78                               SUFFIX   => '.csv',
79                               DIR      => $dir,
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'};
83
84   print "Sending API request..." if $opt{'v'};
85   my @records = eval { $export->vitelity_command('cdrlist') };
86   if ( $@ ) {
87     print "download error: $@\n";
88     exit(-1);
89   }
90   print "received ".scalar(@records)." records\n" if $opt{'v'};
91   if ( !@records ) {
92     print "No records to process.\n" if $opt{'v'};
93     exit(1);
94   }
95
96   print $temp "Date,Source,Destination,Seconds,CallerID,Disposition,Cost\n";
97
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;
103   my $count = 0;
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";
109     $count++;
110   }
111   close $temp;
112   print "Selected $count records in date range." if $opt{'v'} and ($s or $e);
113
114   my $format = 'vitelity';
115   my $batchname = "vitelity-$exportnum-".time2str('%Y/%m/%d-%T',time);
116
117   print "Importing batch..." if $opt{'v'};
118   my $error = FS::cdr::batch_import( {
119     'file'            => $temp->filename,
120     'format'          => $format,
121     'batch_namevalue' => $batchname,
122   } );
123
124   if ( $error ) {
125     $dbh->rollback;
126     print "error: $error";
127     exit(-2);
128   }
129 }
130 $dbh->commit;
131 print "done.\n";
132 exit(0);
133
134 sub usage {
135 "Usage: 
136 cdr-vitelity.import [ -v ] [ -k ] [ -r ]
137                     [ -s date ] [ -e date ]
138                     username
139                     [ exportnum ]
140 "
141 }
142