third time's a charm - fix a small bug and make more analogous to HEAD - RT#8605
[freeside.git] / FS / FS / part_pkg / voip_cdr.pm
index eccf2c1..e2408a3 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',
@@ -324,6 +324,8 @@ sub calc_usage {
       my( $rate_region, $regionnum );
       my $pretty_destnum;
       my $charge = '';
+      my $seconds = '';
+      my $regionname = '';
       my $classnum = '';
       my @call_details = ();
       if ( $rating_method eq 'prefix' ) {
@@ -397,6 +399,8 @@ sub calc_usage {
 
           warn "rating call $to_or_from +$countrycode $number\n" if $DEBUG;
           $pretty_destnum = "+$countrycode $number";
+          #asterisks here causes inserting the detail to barf, so:
+          $pretty_destnum =~ s/\*//g;
 
           my $rate = qsearchs('rate', { 'ratenum' => $ratenum })
             or die "ratenum $ratenum not found!";
@@ -409,6 +413,7 @@ sub calc_usage {
 
             $rate_region = $rate_detail->dest_region;
             $regionnum = $rate_region->regionnum;
+            $regionname = $rate_region->regionname;
             warn "  found rate for regionnum $regionnum ".
                  "and rate detail $rate_detail\n"
               if $DEBUG;
@@ -447,6 +452,7 @@ sub calc_usage {
 #        } else { #pass upstream price through
 #
 #          $charge = sprintf('%.2f', $cdr->upstream_price);
+#          warn "Incrementing \$charges by $charge.  Now $charges\n" if $DEBUG;
 #          $charges += $charge;
 # 
 #          @call_details = (
@@ -465,6 +471,7 @@ sub calc_usage {
         #XXX $charge = sprintf('%.2f', $cdr->upstream_price);
         $charge = sprintf('%.3f', $cdr->upstream_price);
         $charges += $charge;
+        warn "Incrementing \$charges by $charge.  Now $charges\n" if $DEBUG;
 
         @call_details = ($cdr->downstream_csv( 'format' => $output_format,
                                                'charge' => $charge,
@@ -481,7 +488,7 @@ sub calc_usage {
                             : 60;
 
                     # length($cdr->billsec) ? $cdr->billsec : $cdr->duration;
-        my $seconds = $use_duration ? $cdr->duration : $cdr->billsec;
+        $seconds = $use_duration ? $cdr->duration : $cdr->billsec;
 
         $seconds += $granularity - ( $seconds % $granularity )
           if $seconds      # don't granular-ize 0 billsec calls (bills them)
@@ -495,6 +502,7 @@ sub calc_usage {
         $charge = sprintf('%.4f', ( $self->option('min_charge') * $minutes )
                                   + 0.0000000001 ); #so 1.00005 rounds to 1.0001
 
+        warn "Incrementing \$charges by $charge.  Now $charges\n" if $DEBUG;
         $charges += $charge;
 
         @call_details = ($cdr->downstream_csv( 'format' => $output_format,
@@ -530,27 +538,44 @@ sub calc_usage {
           my $granularity = $rate_detail->sec_granularity;
 
                       # length($cdr->billsec) ? $cdr->billsec : $cdr->duration;
-          my $seconds = $use_duration ? $cdr->duration : $cdr->billsec;
+          my $charge_sec = $use_duration ? $cdr->duration : $cdr->billsec;
+          $seconds = ( $charge_sec <= $rate_detail->conn_sec ) # min()
+                       ? $charge_sec
+                       : $rate_detail->conn_sec;
 
-          $seconds += $granularity - ( $seconds % $granularity )
-            if $seconds      # don't granular-ize 0 billsec calls (bills them)
+          $charge_sec -= $rate_detail->conn_sec;
+          $charge_sec = 0 if $charge_sec < 0;
+
+          $charge_sec += $granularity - ( $charge_sec % $granularity )
+            if $charge_sec   # don't granular-ize 0 billsec calls (bills them)
             && $granularity; # 0 is per call
-          my $minutes = sprintf("%.1f", $seconds / 60);
+          $seconds += $charge_sec;
+
+          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 ) {
             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;
+            $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;
 
           # this is why we need regionnum/rate_region....
           warn "  (rate region $rate_region)\n" if $DEBUG;
@@ -558,10 +583,10 @@ 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' => $rate_region->regionname,
+                                 'dst_regionname' => $regionname,
                                )
           );
 
@@ -577,11 +602,25 @@ sub calc_usage {
           #if ( $self->option('rating_method') eq 'upstream_simple' ) {
           if ( scalar(@call_details) == 1 ) {
             $call_details =
-              [ 'C', $call_details[0], $charge, $classnum, $phonenum ];
+              [ 'C',
+                $call_details[0],
+                $charge,
+                $classnum,
+                $phonenum,
+                $seconds,
+                $regionname,
+              ];
           } else { #only used for $rating_method eq 'upstream' now
             $csv->combine(@call_details);
             $call_details =
-              [ 'C', $csv->string, $charge, $classnum, $phonenum ];
+              [ 'C',
+                $csv->string,
+                $charge,
+                $classnum,
+                $phonenum,
+                $seconds,
+                $regionname,
+              ];
           }
           warn "  adding details on charge to invoice: [ ".
               join(', ', @{$call_details} ). " ]"
@@ -607,7 +646,14 @@ sub calc_usage {
 
   } # $cust_svc
 
-  unshift @$details, [ 'C', FS::cdr::invoice_header($output_format) ]
+  unshift @$details, [ 'C',
+                       FS::cdr::invoice_header($output_format),
+                       '',
+                       '',
+                       '',
+                       '',
+                       '',
+                     ]
     if @$details && $rating_method ne 'upstream';
 
 #  if ( $spool_cdr && length($downstream_cdr) ) {