without screwing up usage summaries RT#8605
[freeside.git] / FS / FS / part_pkg / voip_cdr.pm
index 09dffc5..24f9fc8 100644 (file)
@@ -15,7 +15,7 @@ use FS::part_pkg::recur_Common;
 
 @ISA = qw(FS::part_pkg::recur_Common);
 
-$DEBUG = 0;
+$DEBUG = 1;
 
 tie my %rating_method, 'Tie::IxHash',
   'prefix' => 'Rate calls by using destination prefix to look up a region and rate according to the internal prefix and rate tables',
@@ -538,34 +538,44 @@ sub calc_usage {
           my $granularity = $rate_detail->sec_granularity;
 
                       # length($cdr->billsec) ? $cdr->billsec : $cdr->duration;
-          $seconds = $use_duration ? $cdr->duration : $cdr->billsec;
+          my $charge_sec = $seconds =
+            $use_duration ? $cdr->duration : $cdr->billsec;
 
-          $seconds -= $rate_detail->conn_sec;
-          $seconds = 0 if $seconds < 0;
+          $charge_sec -= $rate_detail->conn_sec;
+          $charge_sec = 0 if $charge_sec < 0;
 
-          $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);
+          if ( $seconds && $granularity ) {
+            # don't granular-ize 0 billsec calls (bills them)
+            # 0 granularity is per call
+            $seconds += $granularity - ( $seconds % $granularity );
+            $charge_sec += $granularity - ( $charge_sec % $granularity );
+          }
+
+          my $minutes = sprintf("%.1f", $charge_sec / 60);
           $minutes =~ s/\.0$// if $granularity == 60;
 
+          my $duration_minutes =  #for invoice display purposes
+            sprintf("%.1f", $seconds / 60);
+          $duration_minutes =~ s/\.0$// if $granularity == 60;
+
           # per call rather than per minute
           $minutes = 1 unless $granularity;
+          $duration_minutes = 1 unless $granularity; 
 
           $included_min{$regionnum} -= $minutes;
 
           $charge = sprintf('%.2f', $rate_detail->conn_charge);
 
-          if ( $included_min{$regionnum} <= 0 ) {
+          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
             $charge = sprintf('%.2f', $charge);
-            warn "Incrementing \$charges by $charge.  Now $charges\n" if $DEBUG;
-            $charges += $charge;
           }
+          warn "Incrementing \$charges by $charge.  Now $charges\n" if $DEBUG;
+          $charges += $charge;
 
           # this is why we need regionnum/rate_region....
           warn "  (rate region $rate_region)\n" if $DEBUG;
@@ -573,7 +583,7 @@ sub calc_usage {
           @call_details = (
            $cdr->downstream_csv( 'format'         => $output_format,
                                  'granularity'    => $granularity,
-                                 'minutes'        => $minutes,
+                                 'minutes'        => $duration_minutes,
                                  'charge'         => $charge,
                                  'pretty_dst'     => $pretty_destnum,
                                  'dst_regionname' => $regionname,