summaryrefslogtreecommitdiff
path: root/FS/bin/freeside-cdr-import
diff options
context:
space:
mode:
authorIvan Kohler <ivan@freeside.biz>2015-04-21 10:48:50 -0700
committerIvan Kohler <ivan@freeside.biz>2015-04-21 10:48:50 -0700
commitb54dbcd5bf26986115279680e12668cf9a0b51b7 (patch)
tree3516e1755e3767982c2227a60f2ad7f4cc2b166e /FS/bin/freeside-cdr-import
parent5b832f4528bbf7f1349fc9b71077a062b1bde78c (diff)
parent6d1d5e97768be940da90dc493dfd1748c839660a (diff)
Merge branch 'FREESIDE_3_BRANCH' of git.freeside.biz:/home/git/freeside into FREESIDE_3_BRANCH
Diffstat (limited to 'FS/bin/freeside-cdr-import')
-rw-r--r--FS/bin/freeside-cdr-import71
1 files changed, 71 insertions, 0 deletions
diff --git a/FS/bin/freeside-cdr-import b/FS/bin/freeside-cdr-import
new file mode 100644
index 000000000..55a007008
--- /dev/null
+++ b/FS/bin/freeside-cdr-import
@@ -0,0 +1,71 @@
+#!/usr/bin/perl
+
+use strict;
+use FS::Misc::Getopt;
+use FS::cdr_batch;
+use FS::part_export; # yes, they're really "imports" in this context
+use FS::Record qw(qsearch qsearchs dbh);
+use Date::Format 'time2str';
+
+###
+# parse command line
+###
+
+our %opt;
+getopts('');
+
+$FS::UID::AutoCommit = 0;
+
+my @exports = grep { $_->can('import_cdrs') } qsearch('part_export');
+if (!@exports) {
+ die "There are no CDR exports configured.\n";
+}
+
+foreach my $part_export (@exports) {
+ debug $part_export->label;
+
+ if (!$opt{start}) {
+ # find the most recently downloaded batch
+ my $prefix = $part_export->exporttype . '-' . $part_export->exportnum
+ . '-%';
+ my $most_recent = qsearchs({
+ 'table' => 'cdr_batch',
+ 'hashref' => { 'cdrbatch' => {op=>'like', value=>$prefix}
+ },
+ 'order_by' => 'ORDER BY _date DESC LIMIT 1',
+ });
+ if ( $most_recent ) {
+ $most_recent->cdrbatch =~ /-(\d+)$/; # extract the end timestamp
+ $opt{start} = $1;
+ debug "Downloading records since most recent batch: ".
+ time2str('%Y-%m-%d', $opt{start});
+ } else {
+ $opt{start} = 1262332800;
+ debug "Downloading records since January 2010.";
+ }
+ }
+
+ $opt{end} ||= time;
+
+ my $error_or_batch = $part_export->import_cdrs( $opt{start}, $opt{end} );
+ if ( ref $error_or_batch ) {
+ debug "Created batch #".$error_or_batch->cdrbatchnum;
+ dbh->commit;
+ } elsif ( $error_or_batch ) {
+ warn $error_or_batch;
+ dbh->rollback;
+ } else {
+ debug "No CDRs found."
+ }
+}
+
+sub usage {
+ "Usage: \n freeside-cdr-import [ options ] user
+ Imports CDRs from certain svc_phone exports that are capable of it.
+ Options:
+ -v: be verbose
+ -s date: start date (defaults to the most recent batch date)
+ -e date: end date
+";
+}
+