+ my $seconds_left = $use_duration ? $cdr->duration : $cdr->billsec;
+ # charge for the first (conn_sec) seconds
+ $seconds = min($seconds_left, $rate_detail->conn_sec);
+ $seconds_left -= $seconds;
+ $weektime += $seconds;
+ $charge = $rate_detail->conn_charge;
+
+ my $etime;
+ while($seconds_left) {
+ my $ratetimenum = $rate_detail->ratetimenum; # may be empty
+
+ # find the end of the current rate interval
+ if(@{ $interval_cache{$regionnum} } == 0) {
+ # There are no timed rates in this group, so just stay
+ # in the default rate_detail for the entire duration.
+ # Set an "end" of 1 past the end of the current call.
+ $etime = $weektime + $seconds_left + 1;
+ }
+ elsif($ratetimenum) {
+ # This is a timed rate, so go to the etime of this interval.
+ # If it's followed by another timed rate, the stime of that
+ # interval should match the etime of this one.
+ my $interval = $rate_detail->rate_time->contains($weektime);
+ $etime = $interval->etime;
+ }
+ else {
+ # This is a default rate, so use the stime of the next
+ # interval in the sequence.
+ my $next_int = first { $_->stime > $weektime }
+ @{ $interval_cache{$regionnum} };
+ if ($next_int) {
+ $etime = $next_int->stime;
+ }
+ else {
+ # weektime is near the end of the week, so decrement
+ # it by a full week and use the stime of the first
+ # interval.
+ $weektime -= (3600*24*7);
+ $etime = $interval_cache{$regionnum}->[0]->stime;
+ }
+ }
+
+ my $charge_sec = min($seconds_left, $etime - $weektime);