summaryrefslogtreecommitdiff
path: root/FS/bin/freeside-vitelity-cdrimport
blob: 83bbdd96edac1f44a8b8d6b7ff859c3e87b9a7ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/perl

=pod

freeside-vitelity-cdrimport [ -v ] [ -k ]
                    -s date -e date
                    username
                    [ exportnum ]

Download CDRs using the Vitelity API.

-v: Be verbose.

-k: Keep the .csv file for debugging purposes, instead of deleting it.

-s date: Import only records on or after 'date'  Now required as the Vitelity
API has changed.

-e date: Import only records before 'date'.  Now required as the Vitelity API
has changed.

username: a Freeside user

exportnum: Run only for that export.  If not specified, this will run for 
all Vitelity exports.

=cut

use strict;
use FS::UID qw(adminsuidsetup dbh);
use FS::Record qw(qsearchs qsearch);
use FS::cdr;
use FS::part_export;
use Getopt::Std;
use File::Temp;
use Date::Format 'time2str';
use Date::Parse 'str2time';

my %opt;
getopts('vks:e:', \%opt);

my $user = shift or die &usage;
my $exportnum = shift;
my $dbh = adminsuidsetup $user;

my $start = $opt{'s'} ? str2time($opt{'s'}) : die &usage('-s is now required');
my $end   = $opt{'e'} ? str2time($opt{'e'}) : die &usage('-e is now required');

local $FS::UID::AutoCommit = 0;

my @part_exports;
if ( $exportnum ) {
  @part_exports = ( qsearchs('part_export', { 'exportnum' => $exportnum }) )
    or die "exportnum $exportnum not found\n";
}
else {
  @part_exports = qsearch('part_export', { 'exporttype' => 'vitelity' })
    or die "no Vitelity exports found\n";
}

foreach my $export (@part_exports) {
  my $exportnum = $export->exportnum;
  print "Processing exportnum $exportnum.\n" if $opt{'v'};
  $export->isa('FS::part_export::vitelity') 
    or die "exportnum $exportnum is not a Vitelity export\n";

  my $dir = $FS::UID::cache_dir . "/cache.". $FS::UID::datasrc;
  my $temp = new File::Temp ( TEMPLATE => 'download.XXXXXXXX',
                              SUFFIX   => '.csv',
                              DIR      => $dir,
                              UNLINK   => !$opt{'k'} )
    or die "can't open temporary file to store download: $!\n";
  print "Downloading to ".$temp->filename."\n" if $opt{'v'};

  print "Sending API request..." if $opt{'v'};

  my $s = time2str('%m-%d-%Y', $start);
  my $e = time2str('%m-%d-%Y', $end);

  my @records = eval { $export->vitelity_command('getcdr',
                                                   'startdate' => $s,
                                                   'enddate'   => $e,
                                                );
                     };
  if ( $@ ) {
    print "download error: $@\n";
    exit(-1);
  }

  print "received ".scalar(@records)." records\n" if $opt{'v'};
  if ( !@records ) {
    print "No records to process.\n" if $opt{'v'};
    exit(1);
  }

  print $temp "Date,Source,Destination,Seconds,CallerID,Disposition,Cost\n";

  while (my $rec = shift @records) {
    print $temp $rec, "\n";
  }
  close $temp;

  my $format = 'vitelity';
  my $batchname = "vitelity-$exportnum-".time2str('%Y/%m/%d-%T',time);

  print "Importing batch..." if $opt{'v'};
  my $error = FS::cdr::batch_import( {
    'file'            => $temp->filename,
    'format'          => $format,
    'batch_namevalue' => $batchname,
  } );

  if ( $error ) {
    $dbh->rollback;
    print "error: $error";
    exit(-2);
  }
}
$dbh->commit;
print "done.\n";
exit(0);

sub usage {
  my $err = @_ ? shift."\n\n" : '';
$err."Usage: 
freeside-vitelity-cdrimport [ -v ] [ -k ]
                    -s date -e date
                    username
                    [ exportnum ]
";
}