+
+ }
+}
+
+=item rate [ OPTION => VALUE ... ]
+
+Rates this CDR according and sets the status to 'rated'.
+
+Available options are: part_pkg, svcnum, single_price_included_minutes, region_group, region_group_included_minutes.
+
+part_pkg is required.
+
+If svcnum is specified, will also associate this CDR with the specified svcnum.
+
+single_price_included_minutes is requried for single_price price plans
+(otherwise unused/ignored). It should be set to a scalar reference of the
+number of included minutes and will be decremented by the rated minutes of this
+CDR.
+
+region_group_included_minutes is required for prefix price plans which have
+included minutes (otherwise unused/ignored). It should be set to a scalar
+reference of the number of included minutes and will be decremented by the
+rated minutes of this CDR.
+
+region_group_included_minutes_hashref is required for prefix price plans which
+have included minues (otehrwise unused/ignored). It should be set to an empty
+hashref at the start of a month's rating and then preserved across CDRs.
+
+=cut
+
+sub rate {
+ my( $self, %opt ) = @_;
+ my $part_pkg = $opt{'part_pkg'} or return "No part_pkg specified";
+
+ if ( $DEBUG > 1 ) {
+ warn "rating CDR $self\n".
+ join('', map { " $_ => ". $self->{$_}. "\n" } keys %$self );
+ }
+
+ my $rating_method = $part_pkg->option_cacheable('rating_method') || 'prefix';
+ my $method = "rate_$rating_method";
+ $self->$method(%opt);
+}
+
+#here?
+our %interval_cache = (); # for timed rates
+
+sub rate_prefix {
+ my( $self, %opt ) = @_;
+ my $part_pkg = $opt{'part_pkg'} or return "No part_pkg specified";
+
+ my $da_rewrote = 0;
+ # this will result in those CDRs being marked as done... is that
+ # what we want?
+ my @dirass = ();
+ if ( $part_pkg->option_cacheable('411_rewrite') ) {
+ my $dirass = $part_pkg->option_cacheable('411_rewrite');
+ $dirass =~ s/\s//g;
+ @dirass = split(',', $dirass);
+ }
+
+ if ( length($self->dst) && grep { $self->dst eq $_ } @dirass ) {
+ $self->dst('411');
+ $da_rewrote = 1;
+ }
+
+ my $reason = $part_pkg->check_chargable( $self,
+ 'da_rewrote' => $da_rewrote,
+ );
+ if ( $reason ) {
+ warn "not charging for CDR ($reason)\n" if $DEBUG;
+ return $self->set_status_and_rated_price( 'rated',
+ 0,
+ $opt{'svcnum'},
+ );
+ }
+
+
+ ###
+ # look up rate details based on called station id
+ # (or calling station id for toll free calls)
+ ###
+
+ my( $to_or_from, $number );
+ if ( $self->is_tollfree && ! $part_pkg->option_cacheable('disable_tollfree') )
+ { #tollfree call
+ $to_or_from = 'from';
+ $number = $self->src;
+ } else { #regular call
+ $to_or_from = 'to';
+ $number = $self->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
+ my $intl = $part_pkg->option_cacheable('international_prefix') || '011';
+ my $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 {
+ my $domestic_prefix = $part_pkg->option_cacheable('domestic_prefix');
+ $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;
+ my $pretty_dst = "+$countrycode $number";
+ #asterisks here causes inserting the detail to barf, so:
+ $pretty_dst =~ s/\*//g;
+
+ my $eff_ratenum = $self->is_tollfree('accountcode')
+ ? $part_pkg->option_cacheable('accountcode_tollfree_ratenum')
+ : '';
+
+ my $ratename = '';
+ my $intrastate_ratenum = $part_pkg->option_cacheable('intrastate_ratenum');
+ if ( $intrastate_ratenum && !$self->is_tollfree ) {
+ $ratename = 'Interstate'; #until proven otherwise
+ # 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 = $self->dst;
+ $dstprefix =~ /^(\d{6})/;
+ $dstprefix = qsearchs('rate_prefix', { 'countrycode' => '1',
+ 'npa' => $1,
+ }) || '';
+ my $srcprefix = $self->src;
+ $srcprefix =~ /^(\d{6})/;
+ $srcprefix = qsearchs('rate_prefix', { 'countrycode' => '1',
+ 'npa' => $1,
+ }) || '';
+ if ($srcprefix && $dstprefix
+ && $srcprefix->state && $dstprefix->state
+ && $srcprefix->state eq $dstprefix->state) {
+ $eff_ratenum = $intrastate_ratenum;
+ $ratename = 'Intrastate'; # XXX possibly just use the ratename?
+ }
+ }
+
+ $eff_ratenum ||= $part_pkg->option_cacheable('ratenum');
+ my $rate = qsearchs('rate', { 'ratenum' => $eff_ratenum })
+ or die "ratenum $eff_ratenum not found!";
+
+ my @ltime = localtime($self->startdate);
+ my $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...
+ my $rate_detail = $rate->dest_detail({ 'countrycode' => $countrycode,
+ 'phonenum' => $number,
+ 'weektime' => $weektime,
+ 'cdrtypenum' => $self->cdrtypenum,
+ });
+
+ unless ( $rate_detail ) {
+
+ if ( $part_pkg->option_cacheable('ignore_unrateable') ) {
+
+ if ( $part_pkg->option_cacheable('ignore_unrateable') == 2 ) {
+ # mark the CDR as unrateable
+ return $self->set_status_and_rated_price(
+ 'failed',
+ '',
+ $opt{'svcnum'},
+ );
+ } elsif ( $part_pkg->option_cacheable('ignore_unrateable') == 1 ) {
+ # warn and continue
+ warn "no rate_detail found for CDR.acctid: ". $self->acctid.
+ "; skipping\n";
+ return '';
+
+ } else {
+ die "unknown ignore_unrateable, pkgpart ". $part_pkg->pkgpart;
+ }
+
+ } else {
+
+ die "FATAL: no rate_detail found in ".
+ $rate->ratenum. ":". $rate->ratename. " rate plan ".
+ "for +$countrycode $number (CDR acctid ". $self->acctid. "); ".
+ "add a rate or set ignore_unrateable flag on the package def\n";
+ }
+
+ }
+
+ my $rate_region = $rate_detail->dest_region;
+ my $regionnum = $rate_region->regionnum;
+ 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 { $_->rate_time->intervals }
+ qsearch({ 'table' => 'rate_detail',
+ 'hashref' => { 'ratenum' => $rate->ratenum },
+ 'extra_sql' => 'AND ratetimenum IS NOT NULL',
+ })
+ );
+ $interval_cache{$regionnum} = \@intervals;
+ warn " cached ".scalar(@intervals)." interval(s)\n"
+ if $DEBUG;