summaryrefslogtreecommitdiff
path: root/FS/bin
diff options
context:
space:
mode:
Diffstat (limited to 'FS/bin')
-rwxr-xr-xFS/bin/freeside-cdr-a2billing-import209
-rwxr-xr-xFS/bin/freeside-daily4
-rwxr-xr-xFS/bin/freeside-fetch93
-rw-r--r--FS/bin/freeside-queued6
-rwxr-xr-xFS/bin/freeside-rbc-download9
-rw-r--r--FS/bin/freeside-reexport17
6 files changed, 231 insertions, 107 deletions
diff --git a/FS/bin/freeside-cdr-a2billing-import b/FS/bin/freeside-cdr-a2billing-import
new file mode 100755
index 000000000..a8469e744
--- /dev/null
+++ b/FS/bin/freeside-cdr-a2billing-import
@@ -0,0 +1,209 @@
+#!/usr/bin/perl
+
+use strict;
+use vars qw( $DEBUG );
+use Date::Parse 'str2time';
+use Date::Format 'time2str';
+use FS::UID qw(adminsuidsetup dbh);
+use FS::cdr;
+use DBI;
+use Getopt::Std;
+
+my %opt;
+getopts('H:U:P:D:T:s:e:c:', \%opt);
+my $user = shift or die &usage;
+
+my $dsn = 'dbi:mysql';
+$dsn .= ":database=$opt{D}" if $opt{D};
+$dsn .= ":host=$opt{H}" if $opt{H};
+
+my $mysql = DBI->connect($dsn, $opt{U}, $opt{P})
+ or die $DBI::errstr;
+
+my ($start, $end) = ('', '');
+if ( $opt{s} ) {
+ $start = str2time($opt{s}) or die "can't parse start date $opt{s}\n";
+ $start = time2str('%Y-%m-%d', $start);
+}
+if ( $opt{e} ) {
+ $end = str2time($opt{e}) or die "can't parse end date $opt{e}\n";
+ $end = time2str('%Y-%m-%d', $end);
+}
+
+adminsuidsetup $user;
+
+my $fsdbh = FS::UID::dbh;
+
+# check for existence of freesidestatus
+my $table = $opt{T} || 'cc_call';
+my $status = $mysql->selectall_arrayref("SHOW COLUMNS FROM $table WHERE Field = 'freesidestatus'");
+if( ! @$status ) {
+ print "Adding freesidestatus column...\n";
+ $mysql->do("ALTER TABLE $table ADD COLUMN freesidestatus varchar(32)")
+ or die $mysql->errstr;
+}
+else {
+ print "freesidestatus column present\n";
+}
+
+# Fields:
+# id - primary key, sequential
+# session_id - Local/<digits>-<digits> or SIP/<digits>-<digits>
+# uniqueid - a decimal number, seems to be close to the unix timestamp
+# card_id - probably the equipment port, 1 - 10
+# nasipaddress - we don't care
+# starttime, stoptime - timestamps
+# sessiontime - duration, seconds
+# calledstation - dst
+# sessionbill - upstream_price
+# id_tariffgroup - null, 0, 1
+# id_tariffplan - null, 0, 3, 4, 5, 6, 7, 8, 9
+# id_ratecard - larger numbers
+# (all of the id_* fields are foreign keys: cc_tariffgroup, cc_ratecard, etc.)
+# id_trunk - we don't care
+# sipiax - probably don't care
+# src - src. Usually a phone number, but not always.
+# id_did - always null
+# buycost - wholesale price? correlated with sessionbill
+# id_card_package_offer - no idea
+# real_sessiontime - close to sessiontime, except when it's null
+# (When sessiontime = 0, real_sessiontime is either 0 or null, and
+# sessionbill is 0. When sessiontime > 0, but real_sessiontime is null,
+# sessionbill is 0. So real_sessiontime seems to be the billable time, and
+# is null when the call is non-billable.)
+# dnid - sometimes equals calledstation, or calledstation without the leading
+# "1". But not always.
+# terminatecauseid - integer, 0 - 7
+# destination - seems to be the NPA or NPA+NXX sometimes, or "0".
+
+# terminatecauseid values:
+my %disposition = (
+ 0 => '',
+ 1 => 'ANSWER', #the only one that's billable
+ 2 => 'BUSY',
+ 3 => 'NOANSWER',
+ 4 => 'CANCEL',
+ 5 => 'CONGESTION',
+ 6 => 'CHANUNAVAIL',
+ 7 => 'DONTCALL',
+ 8 => 'TORTURE', #???
+ 9 => 'INVALIDARGS',
+);
+
+my @cols = (
+ "$table.id as id", 'cc_card.username as username',
+ qw( sessionid
+ starttime stoptime sessiontime real_sessiontime
+ terminatecauseid
+ calledstation src
+ id_tariffplan id_ratecard sessionbill
+ )
+);
+
+my $sql = 'SELECT '.join(',', @cols). " FROM $table".
+ " LEFT JOIN cc_card ON ( $table.card_id = cc_card.id ) ".
+ ' WHERE freesidestatus IS NULL' .
+ ($start && " AND starttime >= '$start'") .
+ ($end && " AND starttime < '$end'") ;
+my $sth = $mysql->prepare($sql);
+$sth->execute;
+print "Importing ".$sth->rows." records...\n";
+
+my $cdr_batch = new FS::cdr_batch({
+ 'cdrbatch' => 'mysql-import-'. time2str('%Y/%m/%d-%T',time),
+ });
+my $error = $cdr_batch->insert;
+die $error if $error;
+my $cdrbatchnum = $cdr_batch->cdrbatchnum;
+my $imports = 0;
+my $updates = 0;
+
+my $row;
+while ( $row = $sth->fetchrow_hashref ) {
+ $row->{calledstation} =~ s/^1//;
+ $row->{src} =~ s/^1//;
+ my $cdr = FS::cdr->new ({
+ uniqueid => $row->{sessionid},
+ cdrbatchnum => $cdrbatchnum,
+ startdate => time2str($row->{starttime}),
+ enddate => time2str($row->{stoptime}),
+ duration => $row->{sessiontime},
+ billsec => $row->{real_sessiontime},
+ dst => $row->{calledstation},
+ src => $row->{src},
+ charged_party => $row->{username},
+ upstream_rateplanid => $row->{id_tariffplan},
+ upstream_rateid => $row->{id_ratecard}, # I think?
+ upstream_price => $row->{sessionbill},
+ });
+ $cdr->cdrtypenum($opt{c}) if $opt{c};
+
+ my $error = $cdr->insert;
+ if($error) {
+ print "failed import: $error\n";
+ } else {
+ $imports++;
+ my $updated = $mysql->do(
+ "UPDATE $table SET freesidestatus = 'done' WHERE id = ?",
+ undef,
+ $row->{'id'}
+ );
+ $updates += $updated;
+ print "failed to set status: ".$mysql->errstr."\n" unless $updated;
+ }
+}
+print "Done.\nImported $imports CDRs, marked $updates as done in source database.\n";
+$mysql->disconnect;
+
+sub usage {
+ "Usage:
+ freeside-cdr-a2billing-import
+ [ -H host ]
+ -D database
+ -U user
+ -P password
+ [ -s start ] [ -e end ] [ -c cdrtypenum ]
+ freesideuser
+";
+}
+
+=head1 NAME
+
+freeside-cdr-a2billing-import - Download CDRs from an A2Billing MySQL database
+
+=head1 SYNOPSIS
+
+ freeside-cdr-a2billing-import [ -H host ] -D database -U user -P password
+ [ -T tablename ]
+ [ -s start ] [ -e end ] [ -c cdrtypenum ]
+ freesideuser
+
+-H: database hostname
+
+-D: database name
+
+-U: database username
+
+-P: database password
+
+-T: table to import, defaults to cc_call
+
+-s: start date, e.g. 4/20/2015
+
+-e: end date, e.g. 12/25/2015
+
+-c: cdrtypenum to set, defaults to none
+
+freesideuser: freeside username
+
+=head1 DESCRIPTION
+
+=head1 BUGS
+
+=head1 SEE ALSO
+
+L<FS::cdr>
+
+=cut
+
+1;
diff --git a/FS/bin/freeside-daily b/FS/bin/freeside-daily
index cb018d1df..6a2daf934 100755
--- a/FS/bin/freeside-daily
+++ b/FS/bin/freeside-daily
@@ -79,10 +79,6 @@ pay_batch_receive(%opt);
use FS::Cron::export_batch qw(export_batch_submit);
export_batch_submit(%opt);
-#you can skip this by not having the config
-use FS::Cron::agent_email qw(agent_email);
-agent_email(%opt);
-
#clears out cacti imports & deletes select database cache files
use FS::Cron::cleanup qw( cleanup cleanup_before_backup );
cleanup_before_backup();
diff --git a/FS/bin/freeside-fetch b/FS/bin/freeside-fetch
deleted file mode 100755
index c1ab78373..000000000
--- a/FS/bin/freeside-fetch
+++ /dev/null
@@ -1,93 +0,0 @@
-#!/usr/bin/perl -w
-
-use strict;
-use LWP::UserAgent;
-use FS::UID qw(adminsuidsetup);
-use FS::Record qw(qsearchs);
-use FS::Misc qw(send_email);
-
-my $user = shift or die &usage;
-my $employeelist = shift or die &usage;
-my $url = shift or die &usage;
-adminsuidsetup $user;
-
-my @employees = split ',', $employeelist;
-
-foreach my $employee (@employees) {
-
- $employee =~ /^(\w+)$/;
-
- my $access_user = qsearchs( 'access_user', { 'username' => $1 } );
- unless ($access_user) {
- warn "Can't find employee $employee... skipping";
- next;
- }
-
- my $email_address = $access_user->option('email_address');
- unless ($email_address) {
- warn "No email address for $employee... skipping";
- next;
- }
-
- no warnings 'redefine';
- local *LWP::UserAgent::get_basic_credentials = sub {
- return ($access_user->username, $access_user->_password);
- };
-
- my $ua = new LWP::UserAgent;
- $ua->timeout(1800); #30m, some reports can take a while
- $ua->agent("FreesideFetcher/0.1 " . $ua->agent);
-
- my $req = new HTTP::Request GET => $url;
- my $res = $ua->request($req);
-
- my $conf = new FS::Conf;
- my $subject = $conf->config('email_report-subject') || 'Freeside report';
-
- my %options = ( 'from' => $email_address,
- 'to' => $email_address,
- 'subject' => $subject,
- 'body' => $res->content,
- );
-
- $options{'content-type'} = $res->content_type
- if $res->content_type;
- $options{'content-encoding'} = $res->content_encoding
- if $res->content_encoding;
-
- if ($res->is_success) {
- send_email %options;
- }else{
- warn "fetching $url failed for $employee: " . $res->status_line;
- }
-}
-
-sub usage {
- die "Usage:\n\n freeside-fetch user employee[,employee ...] url\n\n";
-}
-
-=head1 NAME
-
-freeside-fetch - Send a freeside page to a list of employees.
-
-=head1 SYNOPSIS
-
- freeside-fetch user employee[,employee ...] url
-
-=head1 DESCRIPTION
-
- Fetches a web page specified by url as if employee and emails it to
- employee. Useful when run out of cron to send freeside web pages.
-
- user: Freeside user
-
- employee: the username of an employee to receive the emailed page. May be a comma separated list
-
- url: the web page to be received
-
-=head1 BUGS
-
- Can leak employee usernames and passwords if requested to access inappropriate urls.
-
-=cut
-
diff --git a/FS/bin/freeside-queued b/FS/bin/freeside-queued
index 7c4cf1b64..36871b295 100644
--- a/FS/bin/freeside-queued
+++ b/FS/bin/freeside-queued
@@ -219,7 +219,11 @@ while (1) {
$log->info('starting job ('.$ljob->job.')');
warn 'running "&'. $ljob->job. '('. join(', ', @args). ")\n" if $DEBUG;
local $FS::UID::AutoCommit = 0; # so that we can clean up failures
- eval $eval; #throw away return value? suppose so
+ do {
+ # switch user only if a job user is available
+ local $FS::CurrentUser::CurrentUser = $ljob->access_user || $FS::CurrentUser::CurrentUser;
+ eval $eval; #throw away return value? suppose so
+ };
if ( $@ ) {
dbh->rollback;
my %hash = $ljob->hash;
diff --git a/FS/bin/freeside-rbc-download b/FS/bin/freeside-rbc-download
index 376b839e1..3f692fa0f 100755
--- a/FS/bin/freeside-rbc-download
+++ b/FS/bin/freeside-rbc-download
@@ -10,13 +10,13 @@ use FS::Record qw(qsearch qsearchs);
use FS::pay_batch;
use FS::Conf;
-use vars qw( $opt_v $opt_a $opt_f );
-getopts('va:f:');
+use vars qw( $opt_v $opt_a $opt_f $opt_n );
+getopts('va:f:n');
#$Net::SFTP::Foreign::debug = -1;
sub usage { "
Usage:
- freeside-rbc-download [ -v ] [ -a archivedir ] [ -f filename ] user\n
+ freeside-rbc-download [ -v ] [ -n ] [ -a archivedir ] [ -f filename ] user\n
" }
sub debug {
@@ -102,6 +102,7 @@ for my $dir ( $ftp->nlst ) {
my $error = FS::pay_batch->import_results(
filehandle => $fh,
format => 'RBC',
+ no_close => ($opt_n ? 1 : 0),
);
if ( $error ) {
@@ -146,6 +147,8 @@ matching the pattern. This can be used to reprocess a specific file.
-a directory: Archive the files in the specified directory.
+-n: Do not try to close batches after applying results.
+
user: freeside username
=head1 BUGS
diff --git a/FS/bin/freeside-reexport b/FS/bin/freeside-reexport
index 54af9dd80..6b689178d 100644
--- a/FS/bin/freeside-reexport
+++ b/FS/bin/freeside-reexport
@@ -1,7 +1,7 @@
#!/usr/bin/perl -w
use strict;
-use vars qw($opt_s $opt_u $opt_p);
+use vars qw($opt_s $opt_u $opt_p $opt_e);
use Getopt::Std;
use FS::UID qw(adminsuidsetup);
use FS::Record qw(qsearch qsearchs);
@@ -22,7 +22,7 @@ if ( $export_x =~ /^(\d+)$/ ) {
or die "no exports of type $export_x found\n";
}
-getopts('s:u:p:');
+getopts('s:u:p:e:');
my @svc_x = ();
if ( $opt_s ) {
@@ -38,16 +38,20 @@ if ( $opt_s ) {
die "no services with svcpart $opt_p found\n" unless @svc_x;
}
+$opt_e ||= 'insert';
+die &usage unless grep { $_ eq $opt_e } qw( insert replace delete suspend unsuspend );
+my $method = 'export_' . $opt_e;
+
foreach my $part_export ( @part_export ) {
foreach my $svc_x ( @svc_x ) {
- my $error = $part_export->export_insert($svc_x);
+ my $error = $part_export->$method($svc_x,$svc_x);
die $error if $error;
}
}
sub usage {
- die "Usage:\n\n freeside-reexport user exportnum|exporttype [ -s svcnum | -u username | -p svcpart ]\n";
+ return "Usage:\n\n freeside-reexport user exportnum|exporttype [ -s svcnum | -u username | -p svcpart ] [ -e insert|replace|delete|suspend|unsuspend ]\n";
}
=head1 NAME
@@ -56,12 +60,13 @@ freeside-reexport - Command line tool to re-trigger export jobs for existing ser
=head1 SYNOPSIS
- freeside-reexport user exportnum|exporttype [ -s svcnum | -u username | -p svcpart ]
+ freeside-reexport user exportnum|exporttype [ -s svcnum | -u username | -p svcpart ] [ -e insert|replace|delete|suspend|unsuspend ]
=head1 DESCRIPTION
Re-queues the export job for the specified exportnum or exporttype(s) and
- specified service (selected by svcnum or username).
+ specified service (selected by svcnum, username or svcpart). Optionally
+ specify the phase of export using the -e flag (default is insert.)
=head1 SEE ALSO