'name'           => 'Default with description field as destination',
     'invoice_header' => 'Caller,Date,Time,Number,Destination,Duration,Price',
   },
+  'sum_duration' => {
+    'name'           => 'Summary (one line per service, with duration)',
+    'invoice_header' => 'Caller,Calls,Minutes,Price',
+  },
+  'sum_count' => {
+    'name'           => 'Summary (one line per service, with count)',
+    'invoice_header' => 'Caller,Messages,Price',
+  },
 );
 
 my %export_formats = ();
       #sub { sprintf('%.3f', shift->upstream_price ) }, #PRICE
       sub { my($cdr, %opt) = @_; $opt{money_char}. $opt{charge}; }, #PRICE
     ],
+    'sum_duration' => [ 
+      # for summary formats, the CDR is a fictitious object containing the 
+      # total billsec and the phone number of the service
+      'src',
+      sub { my($cdr, %opt) = @_; $opt{count} },
+      sub { my($cdr, %opt) = @_; int($opt{seconds}/60).'m' },
+      sub { my($cdr, %opt) = @_; $opt{money_char}. $opt{charge}; },
+    ],
+    'sum_count' => [
+      'src',
+      sub { my($cdr, %opt) = @_; $opt{count} },
+      sub { my($cdr, %opt) = @_; $opt{money_char}. $opt{charge}; },
+    ],
     'basic' => [
       sub { time2str('%d %b - %I:%M %p', shift->calldate_unix) },
       'dst',
 
 =item downstream_csv OPTION => VALUE ...
 
+Returns a string of formatted call details for display on an invoice.
+
 Options:
 
 format
 
     my %options = (
         'disable_src'    => $self->option('disable_src'),
         'default_prefix' => $self->option('default_prefix'),
+        'cdrtypenum'     => $self->option('use_cdrtypenum'),
         'status'         => '',
         'for_update'     => 1,
       );  # $last_bill, $$sdate )
       if ( $rating_method eq 'prefix' ) {
 
         my $da_rewrote = 0;
+        # this will result in those CDRs being marked as done... is that 
+        # what we want?
         if ( length($cdr->dst) && grep { $cdr->dst eq $_ } @dirass ){
           $cdr->dst('411');
           $da_rewrote = 1;
 
           warn "not charging for CDR ($reason)\n" if $DEBUG;
           $charge = 0;
+          # this will result in those CDRs being marked as done... is that 
+          # what we want?
 
         } else {
           
           warn "Incrementing \$charges by $charge.  Now $charges\n" if $DEBUG;
           $charges += $charge;
 
-          @call_details = (
-            $cdr->downstream_csv( 'format'         => $output_format,
-                                  'granularity'    => $rate_detail->sec_granularity, 
-                                  'seconds'        => ($use_duration ?
-                                                       $cdr->duration :
-                                                       $cdr->billsec),
-                                  'charge'         => $charge,
-                                  'pretty_dst'     => $pretty_destnum,
-                                  'dst_regionname' => $regionname,
-                                )
-          );
+          if ( !$self->sum_usage ) {
+            @call_details = (
+              $cdr->downstream_csv( 'format'         => $output_format,
+                                    'granularity'    => $rate_detail->sec_granularity, 
+                                    'seconds'        => ($use_duration ?
+                                                         $cdr->duration :
+                                                         $cdr->billsec),
+                                    'charge'         => $charge,
+                                    'pretty_dst'     => $pretty_destnum,
+                                    'dst_regionname' => $regionname,
+                                  )
+            );
+          }
         } #if(there is a rate_detail)
  
 
                 regionname  => $regionname,
               };
           } else { #only used for $rating_method eq 'upstream' now
+                   # and for sum_ formats
             $csv->combine(@call_details);
             $call_details =
               { format      => 'C',
                 regionname  => $regionname,
               };
           }
-          #warn "  adding details on charge to invoice: [ ".
-          #    join(', ', @{$call_details} ). " ]"
-          #  if ( $DEBUG && ref($call_details) );
           push @invoice_details_sort, [ $call_details, $cdr->calldate_unix ];
         }
 
       }
 
     } # $cdr
- 
-    my @sorted_invoice_details = sort { @{$a}[1] <=> @{$b}[1] } @invoice_details_sort;
-    foreach my $sorted_call_detail ( @sorted_invoice_details ) {
+
+    if ( !$self->sum_usage ) {
+      #sort them
+      my @sorted_invoice_details = 
+        sort { @{$a}[1] <=> @{$b}[1] } @invoice_details_sort;
+      foreach my $sorted_call_detail ( @sorted_invoice_details ) {
         push @$details, @{$sorted_call_detail}[0];
+      }
     }
+    else { #$self->sum_usage
+      my $sum_detail = {
+        amount    => 0,
+        format    => 'C',
+        classnum  => '', #XXX
+        duration  => 0,
+        phonenum  => $svc_x->phonenum,
+        accountcode => '', #XXX
+        startdate => '', #XXX
+        regionnam => '',
+      };
+      # combine the entire set of CDRs
+      foreach ( @invoice_details_sort ) {
+        $sum_detail->{amount} += $_->[0]{amount};
+        $sum_detail->{duration} += $_->[0]{duration};
+      }
+      my $total_cdr = FS::cdr->new({
+          'billsec' => $sum_detail->{duration},
+          'src'     => $sum_detail->{phonenum},
+      });
+      $sum_detail->{detail} = $total_cdr->downstream_csv(
+        format    => $output_format,
+        seconds   => $sum_detail->{duration},
+        charge    => sprintf('%.2f',$sum_detail->{amount}),
+        phonenum  => $sum_detail->{phonenum},
+        count     => scalar(@invoice_details_sort),
+      );
+      push @$details, $sum_detail;
+    } #if $self->sum_usage
 
   } # $cust_svc
 
     && $cdr->carrierid ne $opt{'use_carrierid'} #ne otherwise 0 matches ''
     && ! $flags{'da_rewrote'};
 
+  # unlike everything else, use_cdrtypenum is applied in FS::svc_x::get_cdrs.
   return "cdrtypenum != $opt{'use_cdrtypenum'}"
     if length($opt{'use_cdrtypenum'})
     && $cdr->cdrtypenum ne $opt{'use_cdrtypenum'}; #ne otherwise 0 matches ''
   $count;
 }
 
+# tells whether cust_bill_pkg_detail should return a single line for 
+# each phonenum
+sub sum_usage {
+  my $self = shift;
+  $self->option('output_format') =~ /^sum_/;
+}
+
+# and whether cust_bill should show a detail line for the service label 
+# (separate from usage details)
+sub hide_svc_detail {
+  my $self = shift;
+  $self->option('output_format') =~ /^sum_/;
+}
+
+
 1;