connection charge handling which comports with history of module
[freeside.git] / FS / FS / part_pkg / voip_cdr.pm
index f851b41..eb6727a 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',
@@ -163,12 +163,16 @@ tie my %granularity, 'Tie::IxHash', FS::rate_detail::granularities();
                          'default'        => 'default', #XXX test
                        },
 
-    'usage_section' => { 'name' => 'Section in which to place separate usage charges',
+    'usage_section' => { 'name' => 'Section in which to place usage charges (whether separated or not)',
                        },
 
     'summarize_usage' => { 'name' => 'Include usage summary with recurring charges when usage is in separate section',
                           'type' => 'checkbox',
                         },
+
+    'usage_mandate' => { 'name' => 'Always put usage details in separate section',
+                          'type' => 'checkbox',
+                       },
     #eofalse
 
     'bill_every_call' => { 'name' => 'Generate an invoice immediately for every call.  Useful for prepaid.',
@@ -218,7 +222,7 @@ tie my %granularity, 'Tie::IxHash', FS::rate_detail::granularities();
                        skip_dst_length_less skip_lastapp
                        use_duration
                        411_rewrite
-                       output_format summarize_usage usage_section
+                       output_format usage_mandate summarize_usage usage_section
                        bill_every_call
                        count_available_phones
                      )
@@ -320,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' ) {
@@ -393,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!";
@@ -405,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;
@@ -443,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 = (
@@ -461,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,
@@ -477,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)
@@ -491,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,
@@ -526,7 +538,10 @@ sub calc_usage {
           my $granularity = $rate_detail->sec_granularity;
 
                       # length($cdr->billsec) ? $cdr->billsec : $cdr->duration;
-          my $seconds = $use_duration ? $cdr->duration : $cdr->billsec;
+          $seconds = $use_duration ? $cdr->duration : $cdr->billsec;
+
+          $seconds -= $rate_detail->conn_sec;
+          $seconds = 0 if $seconds < 0;
 
           $seconds += $granularity - ( $seconds % $granularity )
             if $seconds      # don't granular-ize 0 billsec calls (bills them)
@@ -539,14 +554,18 @@ sub calc_usage {
 
           $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;
@@ -557,7 +576,7 @@ sub calc_usage {
                                  'minutes'        => $minutes,
                                  'charge'         => $charge,
                                  'pretty_dst'     => $pretty_destnum,
-                                 'dst_regionname' => $rate_region->regionname,
+                                 'dst_regionname' => $regionname,
                                )
           );
 
@@ -573,11 +592,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} ). " ]"
@@ -603,7 +636,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) ) {