- my $rate_detail;
- my( $rate_region, $regionnum );
- my $rate;
- my $pretty_destnum;
- my $charge = '';
- my $seconds = '';
- my $weektime = '';
- my $regionname = '';
- my $classnum = '';
- my $countrycode;
- my $number;
-
- my @call_details = ();
- if ( $rating_method eq 'prefix' ) {
-
- my $da_rewrote = 0;
- if ( length($cdr->dst) && grep { $cdr->dst eq $_ } @dirass ){
- $cdr->dst('411');
- $da_rewrote = 1;
- }
-
- my $reason = $self->check_chargable( $cdr,
- 'da_rewrote' => $da_rewrote,
- 'option_cache' => \%opt_cache,
- );
-
- if ( $reason ) {
-
- warn "not charging for CDR ($reason)\n" if $DEBUG;
- $charge = 0;
-
- } else {
-
- ###
- # look up rate details based on called station id
- # (or calling station id for toll free calls)
- ###
-
- my( $to_or_from );
- if ( $cdr->is_tollfree && ! $disable_tollfree )
- { #tollfree call
- $to_or_from = 'from';
- $number = $cdr->src;
- } else { #regular call
- $to_or_from = 'to';
- $number = $cdr->dst;
- }
-
- warn "parsing call $to_or_from $number\n" if $DEBUG;
-
- #remove non-phone# stuff and whitespace
- $number =~ s/\s//g;
-# my $proto = '';
-# $dest =~ s/^(\w+):// and $proto = $1; #sip:
-# my $siphost = '';
-# $dest =~ s/\@(.*)$// and $siphost = $1; # @10.54.32.1, @sip.example.com
-
- #determine the country code
- $countrycode = '';
- if ( $number =~ /^$intl(((\d)(\d))(\d))(\d+)$/
- || $number =~ /^\+(((\d)(\d))(\d))(\d+)$/
- )
- {
-
- my( $three, $two, $one, $u1, $u2, $rest ) = ( $1,$2,$3,$4,$5,$6 );
- #first look for 1 digit country code
- if ( qsearch('rate_prefix', { 'countrycode' => $one } ) ) {
- $countrycode = $one;
- $number = $u1.$u2.$rest;
- } elsif ( qsearch('rate_prefix', { 'countrycode' => $two } ) ) { #or 2
- $countrycode = $two;
- $number = $u2.$rest;
- } else { #3 digit country code
- $countrycode = $three;
- $number = $rest;
- }
-
- } else {
- $countrycode = length($domestic_prefix) ? $domestic_prefix : '1';
- $number =~ s/^$countrycode//;# if length($number) > 10;
- }
-
- warn "rating call $to_or_from +$countrycode $number\n" if $DEBUG;
- $pretty_destnum = "+$countrycode $number";
- #asterisks here causes inserting the detail to barf, so:
- $pretty_destnum =~ s/\*//g;
-
- my $eff_ratenum = $cdr->is_tollfree('accountcode')
- ? $cust_pkg->part_pkg->option('accountcode_tollfree_ratenum')
- : '';
-
- my $intrastate_ratenum = $cust_pkg->part_pkg->option('accountcode_tollfree_ratenum');
- if ( $intrastate_ratenum && !$cdr->is_tollfree ) {
- # this is relatively easy only because:
- # -assume all numbers are valid NANP numbers NOT in a fully-qualified format
- # -disregard toll-free
- # -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 $dstprefix = $cdr->dst;
- $dstprefix =~ /^(\d{6})/;
- $dstprefix = qsearchs('rate_prefix', { 'countrycode' => '1',
- 'npa' => $1,
- }) || '';
- my $srcprefix = $cdr->src;
- $srcprefix =~ /^(\d{6})/;
- $srcprefix = qsearchs('rate_prefix', { 'countrycode' => '1',
- 'npa' => $1,
- }) || '';
- $eff_ratenum = $intrastate_ratenum if ($srcprefix && $dstprefix
- && $srcprefix->state && $dstprefix->state
- && $srcprefix->state eq $dstprefix->state);
- }
-
- $eff_ratenum ||= $ratenum;
- $rate = qsearchs('rate', { 'ratenum' => $eff_ratenum })
- or die "ratenum $eff_ratenum not found!";
-
- my @ltime = localtime($cdr->startdate);
- $weektime = $ltime[0] +
- $ltime[1]*60 + #minutes
- $ltime[2]*3600 + #hours
- $ltime[6]*86400; #days since sunday
- # if there's no timed rate_detail for this time/region combination,
- # dest_detail returns the default. There may still be a timed rate
- # that applies after the starttime of the call, so be careful...
- $rate_detail = $rate->dest_detail({ 'countrycode' => $countrycode,
- 'phonenum' => $number,
- 'weektime' => $weektime,
- 'cdrtypenum' => $cdr->cdrtypenum,
- });
-
- if ( $rate_detail ) {
-
- $rate_region = $rate_detail->dest_region;
- $regionnum = $rate_region->regionnum;
- $regionname = $rate_region->regionname;
- warn " found rate for regionnum $regionnum ".
- "and rate detail $rate_detail\n"
- if $DEBUG;
-
- if ( !exists($interval_cache{$regionnum}) ) {
- my @intervals = (
- sort { $a->stime <=> $b->stime }
- map { my $r = $_->rate_time; $r ? $r->intervals : () }
- $rate->rate_detail
- );
- $interval_cache{$regionnum} = \@intervals;
- warn " cached ".scalar(@intervals)." interval(s)\n"
- if $DEBUG;
- }
-
- } elsif ( $ignore_unrateable ) {
-
- $rate_region = '';
- $regionnum = '';
- #code below will throw a warning & skip
-
- } else {
-
- die "FATAL: no rate_detail found in ".
- $rate->ratenum. ":". $rate->ratename. " rate plan ".
- "for +$countrycode $number (CDR acctid ". $cdr->acctid. "); ".
- "add a rate or set ignore_unrateable flag on the package def\n";
- }
-
- }
-
- } elsif ( $rating_method eq 'upstream_simple' ) {
-
- #XXX $charge = sprintf('%.2f', $cdr->upstream_price);
- $charge = sprintf('%.3f', $cdr->upstream_price);
- $charges += $charge;
- warn "Incrementing \$charges by $charge. Now $charges\n" if $DEBUG;
-
- @call_details = ($cdr->downstream_csv( 'format' => $output_format,
- 'charge' => $charge,
- )
- );
- $classnum = $cdr->calltypenum;
-
- } elsif ( $rating_method eq 'single_price' ) {
-
- # a little false laziness w/below
- # $rate_detail = new FS::rate_detail({sec_granularity => ... }) ?
-
- my $granularity = length($self->option('sec_granularity'))
- ? $self->option('sec_granularity')
- : 60;
-
- $seconds = $use_duration ? $cdr->duration : $cdr->billsec;
-
- $seconds += $granularity - ( $seconds % $granularity )
- if $seconds # don't granular-ize 0 billsec calls (bills them)
- && $granularity # 0 is per call
- && $seconds % $granularity;
- my $minutes = $granularity ? ($seconds / 60) : 1;
- $charge = sprintf('%.4f', ( $self->option('min_charge') * $minutes )
- + 0.0000000001 ); #so 1.00005 rounds to 1.0001
-
- warn "Incrementing \$charges by $charge. Now $charges\n" if $DEBUG;
- $charges += $charge;
-
- @call_details = ($cdr->downstream_csv( 'format' => $output_format,
- 'charge' => $charge,
- 'seconds' => ($use_duration ?
- $cdr->duration :
- $cdr->billsec),
- 'granularity' => $granularity,
- )
- );