enable CardFortress in test database, #71513
[freeside.git] / FS / bin / freeside-vitelity-cdrimport
1 #!/usr/bin/perl
2
3 =pod
4
5 freeside-vitelity-cdrimport [ -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
77   my $s = time2str('%m-%d-%Y', $start);
78   my $e = time2str('%m-%d-%Y', $end);
79
80   my @records = eval { $export->vitelity_command('getcdr',
81                                                    'startdate' => $s,
82                                                    'enddate'   => $e,
83                                                 );
84                      };
85   if ( $@ ) {
86     print "download error: $@\n";
87     exit(-1);
88   }
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   while (my $rec = shift @records) {
99     print $temp $rec, "\n";
100   }
101   close $temp;
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 freeside-vitelity-cdrimport [ -v ] [ -k ]
127                     -s date -e date
128                     username
129                     [ exportnum ]
130 ";
131 }
132