invoice batch download fix, #11871
[freeside.git] / FS / FS / cust_bill.pm
index 3f94796..fd3b638 100644 (file)
@@ -245,6 +245,7 @@ sub delete {
     cust_pay_batch
     cust_bill_pay_batch
     cust_bill_pkg
+    cust_bill_batch
   )) {
 
     foreach my $linked ( $self->$table() ) {
@@ -733,6 +734,17 @@ sub cust_credit_bill_pkg {
 
 }
 
+=item cust_bill_batch
+
+Returns all invoice batch records (L<FS::cust_bill_batch>) for this invoice.
+
+=cut
+
+sub cust_bill_batch {
+  my $self = shift;
+  qsearch('cust_bill_batch', { 'invnum' => $self->invnum });
+}
+
 =item tax
 
 Returns the tax amount (see L<FS::cust_bill_pkg>) for this invoice.
@@ -2832,8 +2844,10 @@ sub print_generic {
     if ($conf->exists('voip-cust_accountcode_cdr') && $cust_main->accountcode_cdr) {
       my ($accountcode_section, $accountcode_lines) =
         $self->_items_accountcode_cdr($escape_function_nonbsp,$format);
-      push @{$late_sections}, $accountcode_section;
-      push @detail_items, @$accountcode_lines;
+      if ( scalar(@$accountcode_lines) ) {
+          push @{$late_sections}, $accountcode_section;
+          push @detail_items, @$accountcode_lines;
+      }
     }
   }else{
     push @sections, { 'description' => '', 'subtotal' => '' };
@@ -4149,7 +4163,6 @@ sub _items_accountcode_cdr {
                     'description'   => 'Usage by Account Code',
                     'post_total'    => '',
                     'summarized'    => '',
-                    'total_generator' => sub { '' },
                     'header'        => '',
                   };
     my @lines;
@@ -4186,6 +4199,7 @@ sub _items_accountcode_cdr {
                     ext_description => [],
             };
 
+            $section->{'amount'} += $amount;
             $accountcodes{$accountcode}{'amount'} += $amount;
             $accountcodes{$accountcode}{calls}++;
             $accountcodes{$accountcode}{duration} += $detail->duration;
@@ -4200,7 +4214,9 @@ sub _items_accountcode_cdr {
         push @lines, $l;
     }
 
-    return ($section,\@lines);
+    my @sorted_lines = sort { $a->{'description'} <=> $b->{'description'} } @lines;
+
+    return ($section,\@sorted_lines);
 }
 
 sub _items_svc_phone_sections {