+ } else { # there *is* a rate_detail (or call_details), proceed...
+
+ unless ( @call_details || ( $charge ne '' && $charge == 0 ) ) {
+
+ $included_min{$regionnum} = $rate_detail->min_included
+ unless exists $included_min{$regionnum};
+
+ my $granularity = $rate_detail->sec_granularity;
+
+ # length($cdr->billsec) ? $cdr->billsec : $cdr->duration;
+ my $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
+ my $minutes = sprintf("%.1f", $seconds / 60);
+ $minutes =~ s/\.0$// if $granularity == 60;
+
+ # per call rather than per minute
+ $minutes = 1 unless $granularity;
+
+ $included_min{$regionnum} -= $minutes;
+
+ if ( $included_min{$regionnum} < 0 ) {
+ my $charge_min = 0 - $included_min{$regionnum}; #XXX should preserve
+ #(display?) this
+ $included_min{$regionnum} = 0;
+ $charge = sprintf('%.2f', ( $rate_detail->min_charge * $charge_min )
+ + 0.00000001 ); #so 1.005 rounds to 1.01
+ $charges += $charge;
+ }
+
+ # this is why we need regionnum/rate_region....
+ warn " (rate region $rate_region)\n" if $DEBUG;
+
+ @call_details = (
+ $cdr->downstream_csv( 'format' => $output_format,
+ 'granularity' => $granularity,
+ 'minutes' => $minutes,
+ 'charge' => $charge,
+ 'pretty_dst' => $pretty_destnum,
+ 'dst_regionname' => $rate_region->regionname,
+ )
+ );
+
+ $classnum = $rate_detail->classnum;
+
+ }
+
+ if ( $charge > 0 ) {
+ #just use FS::cust_bill_pkg_detail objects?
+ my $call_details;
+
+ #if ( $self->option('rating_method') eq 'upstream_simple' ) {
+ if ( scalar(@call_details) == 1 ) {
+ $call_details = [ 'C', $call_details[0], $charge, $classnum ];
+ } else { #only used for $rating_method eq 'upstream' now
+ $csv->combine(@call_details);
+ $call_details = [ 'C', $csv->string, $charge, $classnum ];
+ }
+ warn " adding details on charge to invoice: [ ".
+ join(', ', @{$call_details} ). " ]"
+ if ( $DEBUG && ref($call_details) );
+ push @$details, $call_details; #\@call_details,
+ }
+
+ # if the customer flag is on, call "downstream_csv" or something
+ # like it to export the call downstream!
+ # XXX price plan option to pick format, or something...
+ #$downstream_cdr .= $cdr->downstream_csv( 'format' => 'XXX format' )
+ # if $spool_cdr;
+
+ my $error = $cdr->set_status_and_rated_price('done', $charge);
+ die $error if $error;