multiple usage classes checkpoint
[freeside.git] / FS / FS / part_pkg / voip_cdr.pm
index a7c1903..1025705 100644 (file)
@@ -89,6 +89,13 @@ tie my %rating_method, 'Tie::IxHash',
                           'type' => 'checkbox',
                         },
 
+    'usage_section' => { 'name' => 'Section in which to place separate usage charges',
+                       },
+
+    'summarize_usage' => { 'name' => 'Include usage summary with recurring charges when usage is in separate section',
+                          'type' => 'checkbox',
+                        },
+
     #XXX also have option for an external db
 #    'cdr_location' => { 'name' => 'CDR database location'
 #                        'type' => 'select',
@@ -120,7 +127,7 @@ tie my %rating_method, 'Tie::IxHash',
                        disable_src
                        domestic_prefix international_prefix
                        use_amaflags use_disposition output_format
-                       separate_usage
+                       separate_usage summarize_usage usage_section
                      )
                   ],
   'weight' => 40,
@@ -180,6 +187,7 @@ sub calc_usage {
       my( $rate_region, $regionnum );
       my $pretty_destnum;
       my $charge = '';
+      my $classnum = '';
       my @call_details = ();
       if ( $self->option('rating_method') eq 'prefix'
            || ! $self->option('rating_method')
@@ -380,15 +388,18 @@ sub calc_usage {
             $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' ) {
-            $call_details = [ 'C', $call_details[0] ];
+            $call_details = [ 'C', $call_details[0], $charge, $classnum ];
           }else{
             $csv->combine(@call_details);
-            $call_details = [ 'C', $csv->string ];
+            $call_details = [ 'C', $csv->string, $charge, $classnum ];
           }
           warn "  adding details on charge to invoice: [ ".
               join(', ', @{$call_details} ). " ]"
@@ -470,7 +481,15 @@ sub append_cust_bill_pkgs {
   my @details = ();
   my $charges = $self->calc_usage($cust_pkg, $sdate, \@details, $param);
 
-  my $cust_bill_pkg = new FS::cust_bill_pkg {
+  return []
+    unless $charges;  # unless @details?
+
+  my @cust_bill_pkg = ();
+
+  my $want_summary = $self->option('summarize_usage', 'Hush!') &&
+                     $self->option('usage_section', 'Hush!');
+
+  push @cust_bill_pkg, new FS::cust_bill_pkg {
     'pkgnum'    => $cust_pkg->pkgnum,
     'setup'     => 0,
     'unitsetup' => 0,
@@ -480,10 +499,27 @@ sub append_cust_bill_pkgs {
     'sdate'     => $$sdate,
     'edate'     => $cust_pkg->bill,             # already fiddled
     'itemdesc'  => 'Usage charges',             # configurable?
-    'details'   => \@details,
+    'duplicate' => 'Y',
+  }
+    if $want_summary;
+
+  push @cust_bill_pkg, new FS::cust_bill_pkg {
+    'pkgnum'     => $cust_pkg->pkgnum,
+    'setup'      => 0,
+    'unitsetup ' => 0,
+    'recur'      => sprintf( "%.2f", $charges),  # hmmm
+    'unitrecur ' => 0,
+    'quantity'   => $cust_pkg->quantity,
+    'sdate'      => $$sdate,
+    'edate'      => $cust_pkg->bill,             # already fiddled
+    'itemdesc'   => 'Usage charges',             # configurable?
+    'section'    => $self->option('usage_section', 'Hush!'),
+    'details'    => \@details,
+    'post_total' => ( $want_summary ? 'Y' : '' ),
   };
 
-  return [ $cust_bill_pkg ];
+
+  return [ @cust_bill_pkg ];
 }
 
 1;