summaryrefslogtreecommitdiff
path: root/bin/cdr.reimport
diff options
context:
space:
mode:
Diffstat (limited to 'bin/cdr.reimport')
-rw-r--r--bin/cdr.reimport75
1 files changed, 75 insertions, 0 deletions
diff --git a/bin/cdr.reimport b/bin/cdr.reimport
new file mode 100644
index 000000000..23060387d
--- /dev/null
+++ b/bin/cdr.reimport
@@ -0,0 +1,75 @@
+#!/usr/bin/perl
+#
+# Usage:
+# cdr.reimport user format filename
+#
+
+use strict;
+use FS::UID qw(adminsuidsetup);
+use FS::Record qw(qsearch);
+use FS::cdr;
+
+my $user = shift or die &usage;
+adminsuidsetup $user;
+
+my $format = shift or die &usage;
+
+my $file = shift;
+
+my($new, $rep, $skip) = (0, 0, 0);
+
+#this is what makes it a reimport and should probably be moved to cdr.pm
+my $cb = sub {
+ my($cdr, $param) = @_;
+
+ my @exists = qsearch('cdr', {
+ map { $_ => $cdr->$_() }
+ qw( uniqueid startdate enddate src dst charged_party )
+ });
+
+ unless ( scalar(@exists) ) {
+ $new++;
+ return;
+ }
+
+ if ( scalar(@exists) == 2 ) {
+ if ( $exists[0]->freesidestatus || $exists[1]->freesidestatus ) {
+ return "processed double record for uniqueid ". $cdr->uniqueid. "\n";
+ }
+ warn "deleting double record for uniqueid ". $cdr->uniqueid. "\n";
+ my $extra = shift @exists;
+ my $error = $extra->delete;
+ return $error if $error;
+ }
+
+ return "too many matches (". scalar(@exists). ") found!"
+ if scalar(@exists) > 1;
+
+ my $exists = $exists[0];
+ if ( $exists->freesidestatus ) {
+ $skip++;
+ $param->{skiprow} = 1;
+ } else {
+ $rep++;
+ my $error = $exists->delete;
+ return $error if $error;
+ }
+
+ return '';
+
+};
+
+my $error = FS::cdr::batch_import( {
+ 'file' => $file,
+ 'format' => $format,
+ 'batch_namevalue' => $file."-REIMPORT$$",
+ 'preinsert_callback' => $cb,
+} );
+die $error if $error;
+
+warn "$skip skipped, $rep replaced, $new new\n";
+
+sub usage {
+ "Usage: \n cdr.reimport user format filename\n";
+}
+