use strict;
use vars qw( @ISA @EXPORT_OK $DEBUG $me
$conf $cdr_prerate %cdr_prerate_cdrtypenums
+ $use_lrn $support_key
);
use Exporter;
use List::Util qw(first min);
use FS::rate_prefix;
use FS::rate_detail;
+# LRN lookup
+use LWP::UserAgent;
+use HTTP::Request::Common qw(POST);
+use IO::Socket::SSL;
+use JSON::XS qw(decode_json);
+
@ISA = qw(FS::Record);
-@EXPORT_OK = qw( _cdr_date_parser_maker _cdr_min_parser_maker );
+@EXPORT_OK = qw( _cdr_date_parser_maker _cdr_min_parser_maker _cdr_date_parse );
$DEBUG = 0;
$me = '[FS::cdr]';
@cdr_prerate_cdrtypenums = $conf->config('cdr-prerate-cdrtypenums')
if $cdr_prerate;
%cdr_prerate_cdrtypenums = map { $_=>1 } @cdr_prerate_cdrtypenums;
+
+ $support_key = $conf->config('support-key');
+ $use_lrn = $conf->exists('cdr-lrn_lookup');
+
});
=head1 NAME
'upstream_price' => 'Upstream price',
#'upstream_rateplanid' => '',
#'ratedetailnum' => '',
+ 'src_lrn' => 'Source LRN',
+ 'dst_lrn' => 'Dest. LRN',
'rated_price' => 'Rated price',
'rated_cost' => 'Rated cost',
#'distance' => '',
rated_price => $rated_price,
status => $status,
});
- $term->rated_seconds($opt{rated_seconds}) if exists($opt{rated_seconds});
- $term->rated_minutes($opt{rated_minutes}) if exists($opt{rated_minutes});
+ foreach (qw(rated_seconds rated_minutes rated_granularity)) {
+ $term->set($_, $opt{$_}) if exists($opt{$_});
+ }
$term->svcnum($svcnum) if $svcnum;
return $term->insert;
my $field = $options{column} || 'dst';
my $intl = $options{international_prefix} || '011';
+ # Still, don't break anyone's CDR rating if they have an empty string in
+ # there. Require an explicit statement that there's no prefix.
+ $intl = '' if lc($intl) eq 'none';
my $countrycode = '';
my $number = $self->$field();
}
}
-
-
-
###
# look up rate details based on called station id
# (or calling station id for toll free calls)
domestic_prefix => $part_pkg->option_cacheable('domestic_prefix'),
);
+ my $ratename = '';
+ my $intrastate_ratenum = $part_pkg->option_cacheable('intrastate_ratenum');
+
+ if ( $use_lrn and $countrycode eq '1' ) {
+
+ # then ask about the number
+ foreach my $field ('src', 'dst') {
+
+ $self->get_lrn($field);
+ if ( $field eq $column ) {
+ # then we are rating on this number
+ $number = $self->get($field.'_lrn');
+ $number =~ s/^1//;
+ # is this ever meaningful? can the LRN be outside NANP space?
+ }
+
+ } # foreach $field
+
+ }
+
warn "rating call $to_or_from +$countrycode $number\n" if $DEBUG;
my $pretty_dst = "+$countrycode $number";
#asterisks here causes inserting the detail to barf, so:
$pretty_dst =~ s/\*//g;
- my $ratename = '';
- my $intrastate_ratenum = $part_pkg->option_cacheable('intrastate_ratenum');
+ # should check $countrycode eq '1' here?
if ( $intrastate_ratenum && !$self->is_tollfree ) {
$ratename = 'Interstate'; #until proven otherwise
# this is relatively easy only because:
# -disregard private or unknown numbers
# -there is exactly one record in rate_prefix for a given NPANXX
# -default to interstate if we can't find one or both of the prefixes
+ my $dst_col = $use_lrn ? 'dst_lrn' : 'dst';
+ my $src_col = $use_lrn ? 'src_lrn' : 'src';
my (undef, $dstprefix) = $self->parse_number(
- column => 'dst',
+ column => $dst_col,
international_prefix => $part_pkg->option_cacheable('international_prefix'),
domestic_prefix => $part_pkg->option_cacheable('domestic_prefix'),
);
'npa' => $1,
}) || '';
my (undef, $srcprefix) = $self->parse_number(
- column => 'src',
+ column => $src_col,
international_prefix => $part_pkg->option_cacheable('international_prefix'),
domestic_prefix => $part_pkg->option_cacheable('domestic_prefix'),
);
# by default, set the included minutes for this region/time to
# what's in the rate_detail
- $included_min->{$regionnum}{$ratetimenum} = $rate_detail->min_included
- unless exists $included_min->{$regionnum}{$ratetimenum};
+ if (!exists( $included_min->{$regionnum}{$ratetimenum} )) {
+ $included_min->{$regionnum}{$ratetimenum} =
+ ($rate_detail->min_included * $cust_pkg->quantity || 1);
+ }
if ( $included_min->{$regionnum}{$ratetimenum} >= $minutes ) {
$charge_sec = 0;
'name' => 'Number of calls, one line per service',
'invoice_header' => 'Caller,Rate,Messages,Price',
},
+ 'sum_duration' => {
+ 'name' => 'Summary, one line per service',
+ 'invoice_header' => 'Caller,Rate,Calls,Minutes,Price',
+ },
'sum_duration_prefix' => {
'name' => 'Summary, one line per destination prefix',
'invoice_header' => 'Caller,Rate,Calls,Minutes,Price',
'name' => 'Summary, one line per usage class',
'invoice_header' => 'Caller,Class,Calls,Price',
},
+ 'sum_duration_accountcode' => {
+ 'name' => 'Summary, one line per accountcode',
+ 'invoice_header' => 'Caller,Rate,Calls,Minutes,Price',
+ },
);
my %export_formats = ();
}
+sub get_lrn {
+ my $self = shift;
+ my $field = shift;
+
+ my $ua = LWP::UserAgent->new(
+ 'ssl_opts' => {
+ verify_hostname => 0,
+ SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE,
+ },
+ );
+
+ my $url = 'https://ws.freeside.biz/get_lrn';
+
+ my %content = ( 'support-key' => $support_key,
+ 'tn' => $self->get($field),
+ );
+ my $response = $ua->request( POST $url, \%content );
+
+ die "LRN service error: ". $response->message. "\n"
+ unless $response->is_success;
+
+ local $@;
+ my $data = eval { decode_json($response->content) };
+ die "LRN service JSON error : $@\n" if $@;
+
+ if ($data->{error}) {
+ die "acctid ".$self->acctid." $field LRN lookup failed:\n$data->{error}";
+ # for testing; later we should respect ignore_unrateable
+ } elsif ($data->{lrn}) {
+ # normal case
+ $self->set($field.'_lrn', $data->{lrn});
+ } else {
+ die "acctid ".$self->acctid." $field LRN lookup returned no number.\n";
+ }
+
+ return $data; # in case it's interesting somehow
+}
+
=back
=head1 CLASS METHODS
#
# Used by FS::Upgrade to migrate to a new database.
+use FS::upgrade_journal;
sub _upgrade_data {
my ($class, %opts) = @_;
+ return if FS::upgrade_journal->is_done('cdr_cdrbatchnum');
+
warn "$me upgrading $class\n" if $DEBUG;
my $sth = dbh->prepare(
$sth->execute($cdrbatchnum{$cdrbatch}, $cdrbatch) or die $sth->errstr;
}
+ FS::upgrade_journal->set_done('cdr_cdrbatchnum');
+
}
=item ip_addr_sql FIELD RANGE