- my $rate_detail;
- my( $rate_region, $regionnum );
- my $pretty_destnum;
- my $charge = '';
- my $classnum = '';
- my @call_details = ();
- if ( $self->option('rating_method') eq 'prefix'
- || ! $self->option('rating_method')
- )
- {
-
- #should have some better way of checking these options than a long
- #if-else tree...
- my $notchg = "not charging for CDR";
-
- if ( $self->option('use_amaflags') && $cdr->amaflags != 2 ) {
-
- warn "$notchg (amaflags != 2)\n" if $DEBUG;
- $charge = 0;
-
- } elsif ( $self->option('use_disposition')
- && $cdr->disposition ne 'ANSWERED' ) {
-
- warn "$notchg (disposition != ANSWERED)\n" if $DEBUG;
- $charge = 0;
-
- } elsif ( $self->option('use_disposition_taqua')
- && $cdr->disposition != 100 ) {
-
- warn "$notchg (disposition != 100)\n" if $DEBUG;
- $charge = 0;
-
- } elsif ( $self->option('use_carrierid')
- && $cdr->carrierid != $self->option('use_carrierid') ) {
-
- warn "$notchg (carrierid != ". $self->option('use_carrierid'). ")\n"
- if $DEBUG;
- $charge = 0;
-
- } elsif ( $self->option('use_cdrtypenum')
- && $cdr->cdrtypenum != $self->option('use_cdrtypenum') ) {
-
- warn "$notchg (cdrtypenum != ". $self->option('use_cdrtypenum'). ")\n"
- if $DEBUG;
- $charge = 0;
-
- } else {
-
- ###
- # look up rate details based on called station id
- # (or calling station id for toll free calls)
- ###
-
- if ( $self->option('411_rewrite') ) {
- my @dirass = split(/\s*,\s*/, $self->option('411_rewrite'));
- $cdr->dst('411') if grep $cdr->dst eq $_, @dirass;
- }
-
- my( $to_or_from, $number );
- if ( $cdr->dst =~ /^(\+?1)?8([02-8])\1/ ) { #tollfree call
- $to_or_from = 'from';
- $number = $cdr->src;
- } else { #regular call
- $to_or_from = 'to';
- $number = $cdr->dst;
- }
-
- #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
-
- my $intl = $self->option('international_prefix') || '011';
- my @intl = ();
- if ( $intl =~ /,/ ) {
- @intl = split(/\s*,\s*/, $intl);
- } else {
- @intl = ($intl);
-
- #determine the country code
- my $countrycode;
- if ( grep { $number =~ /^$_(((\d)(\d))(\d))(\d+)$/ } @intl
- || $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 = $self->option('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";
-
- #find a rate prefix, first look at most specific (4 digits) then 3, etc.,
- # finally trying the country code only
- my $rate_prefix = '';
- for my $len ( reverse(1..6) ) {
- $rate_prefix = qsearchs('rate_prefix', {
- 'countrycode' => $countrycode,
- #'npa' => { op=> 'LIKE', value=> substr($number, 0, $len) }
- 'npa' => substr($number, 0, $len),
- } ) and last;
- }
- $rate_prefix ||= qsearchs('rate_prefix', {
- 'countrycode' => $countrycode,
- 'npa' => '',
- });
-
- #
- die "Can't find rate for call $to_or_from +$countrycode $number\n"
- unless $rate_prefix;
-
- $regionnum = $rate_prefix->regionnum;
- $rate_detail = qsearchs('rate_detail', {
- 'ratenum' => $ratenum,
- 'dest_regionnum' => $regionnum,
- } );
-
- $rate_region = $rate_prefix->rate_region;
-
- warn " found rate for regionnum $regionnum ".
- "and rate detail $rate_detail\n"
- if $DEBUG;
-
- }
-
- } elsif ( $self->option('rating_method') eq 'upstream' ) {
-
- 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);
- $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 ( $self->option('rating_method') eq 'upstream_simple' ) {
-
- #XXX $charge = sprintf('%.2f', $cdr->upstream_price);
- $charge = sprintf('%.3f', $cdr->upstream_price);
- $charges += $charge;
-
- @call_details = ($cdr->downstream_csv( 'format' => $output_format ));