- my @dirass = ();
- if ( $self->option('411_rewrite') ) {
- my $dirass = $self->option('411_rewrite');
- $dirass =~ s/\s//g;
- @dirass = split(',', $dirass);
- }
-
- my %interval_cache = (); # for timed rates
-
- #for check_chargable, so we don't keep looking up options inside the loop
- my %opt_cache = ();
-
- eval "use Text::CSV_XS;";
- die $@ if $@;
- my $csv = new Text::CSV_XS;
-
- my($svc_table, $svc_field) = split('\.', $cdr_svc_method);
-
- foreach my $cust_svc (
- grep { $_->part_svc->svcdb eq $svc_table } $cust_pkg->cust_svc
- ) {
-
- my $svc_x = $cust_svc->svc_x;
- foreach my $cdr (
- $svc_x->get_cdrs(
- 'disable_src' => $self->option('disable_src'),
- 'default_prefix' => $self->option('default_prefix'),
- 'status' => '',
- 'for_update' => 1,
- ) # $last_bill, $$sdate )
- ) {
- if ( $DEBUG > 1 ) {
- warn "rating CDR $cdr\n".
- join('', map { " $_ => ". $cdr->{$_}. "\n" } keys %$cdr );
- }
-
- 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 = $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')
- : '';
- $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,
- });
-
- 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' ) { #XXX this was convergent, not currently used. very much becoming the odd one out. remove?
-#
-# if ( $cdr->cdrtypenum == 1 ) { #rate based on upstream rateid
-#
-# $rate_detail = $cdr->cdr_upstream_rate->rate_detail;
-#
-# $regionnum = $rate_detail->dest_regionnum;
-# $rate_region = $rate_detail->dest_region;
-#
-# $pretty_destnum = $cdr->dst;
-#
-# warn " found rate for regionnum $regionnum and ".
-# "rate detail $rate_detail\n"
-# if $DEBUG;
-#
-# } else { #pass upstream price through
-#
-# $charge = sprintf('%.2f', $cdr->upstream_price);
-# warn "Incrementing \$charges by $charge. Now $charges\n" if $DEBUG;
-# $charges += $charge;
-#
-# @call_details = (
-# #time2str("%Y %b %d - %r", $cdr->calldate_unix ),
-# time2str("%c", $cdr->calldate_unix), #XXX this should probably be a config option dropdown so they can select US vs- rest of world dates or whatnot
-# 'N/A', #minutes...
-# '$'.$charge,
-# #$pretty_destnum,
-# $cdr->description, #$rate_region->regionname,
-# );
-#
-# }
-
- } elsif ( $rating_method eq 'upstream_simple' ) {