X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=blobdiff_plain;f=FS%2FFS%2Fcdr.pm;h=c4e9c47a350286d83415a41e8d47b081fed47b63;hp=a2b9a8ccbfd18c8bc48b0fdab830c622e1c4301b;hb=a36e0f8a0f69349dafaa16d1d2d57dfb6e5dbc85;hpb=d22baa4e71bfa9e153c1fe1152ff4c748f1d935c diff --git a/FS/FS/cdr.pm b/FS/FS/cdr.pm index a2b9a8ccb..c4e9c47a3 100644 --- a/FS/FS/cdr.pm +++ b/FS/FS/cdr.pm @@ -3,6 +3,7 @@ package FS::cdr; 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); @@ -24,8 +25,14 @@ use FS::rate; use FS::rate_prefix; use FS::rate_detail; +# LRN lookup +use LWP::UserAgent; +use HTTP::Request::Common qw(POST); +use IO::Socket::SSL; +use Cpanel::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]'; @@ -39,6 +46,10 @@ FS::UID->install_callback( sub { @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 @@ -215,6 +226,8 @@ sub table_info { 'upstream_price' => 'Upstream price', #'upstream_rateplanid' => '', #'ratedetailnum' => '', + 'src_lrn' => 'Source LRN', + 'dst_lrn' => 'Dest. LRN', 'rated_price' => 'Rated price', 'rated_cost' => 'Rated cost', #'distance' => '', @@ -495,8 +508,9 @@ sub set_status_and_rated_price { 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; @@ -686,9 +700,6 @@ sub rate_prefix { } } - - - ### # look up rate details based on called station id # (or calling station id for toll free calls) @@ -722,13 +733,32 @@ sub rate_prefix { 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: @@ -737,8 +767,10 @@ sub rate_prefix { # -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'), ); @@ -747,7 +779,7 @@ sub rate_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'), ); @@ -1463,6 +1495,44 @@ sub downstream_csv { } +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