enable CardFortress in test database, #71513
[freeside.git] / FS / bin / freeside-cdr-import
1 #!/usr/bin/perl
2
3 use strict;
4 use FS::Misc::Getopt;
5 use FS::cdr_batch;
6 use FS::part_export; # yes, they're really "imports" in this context
7 use FS::Record qw(qsearch qsearchs dbh);
8 use Date::Format 'time2str';
9
10 ###
11 # parse command line
12 ###
13
14 our %opt;
15 getopts('');
16
17 $FS::UID::AutoCommit = 0;
18
19 my @exports = grep { $_->can('import_cdrs') } qsearch('part_export');
20 if (!@exports) {
21   die "There are no CDR exports configured.\n";
22 }
23
24 foreach my $part_export (@exports) {
25   debug $part_export->label;
26
27   if (!$opt{start}) {
28     # find the most recently downloaded batch
29     my $prefix = $part_export->exporttype . '-' . $part_export->exportnum
30                  . '-%';
31     my $most_recent = qsearchs({
32         'table'     => 'cdr_batch',
33         'hashref'   => { 'cdrbatch' => {op=>'like', value=>$prefix}
34                        },
35         'order_by'  => 'ORDER BY _date DESC LIMIT 1',
36     });
37     if ( $most_recent ) {
38       $most_recent->cdrbatch =~ /-(\d+)$/; # extract the end timestamp
39       $opt{start} = $1;
40       debug "Downloading records since most recent batch: ".
41             time2str('%Y-%m-%d', $opt{start});
42     } else {
43       $opt{start} = 1262332800;
44       debug "Downloading records since January 2010.";
45     }
46   }
47
48   $opt{end} ||= time;
49
50   my $error_or_batch = $part_export->import_cdrs( $opt{start}, $opt{end} );
51   if ( ref $error_or_batch ) {
52     debug "Created batch #".$error_or_batch->cdrbatchnum;
53     dbh->commit;
54   } elsif ( $error_or_batch ) {
55     warn $error_or_batch;
56     dbh->rollback;
57   } else {
58     debug "No CDRs found."
59   }
60 }
61
62 sub usage {
63   "Usage: \n  freeside-cdr-import [ options ] user
64   Imports CDRs from certain svc_phone exports that are capable of it.
65   Options:
66     -v: be verbose
67     -s date: start date (defaults to the most recent batch date)
68     -e date: end date
69 ";
70 }
71