add per-customer "Invoice reports" link, RT#13802
[freeside.git] / FS / FS / cust_bill.pm
index 6e4eba4..a6a782a 100644 (file)
@@ -1520,14 +1520,36 @@ isn't an open batch, one will be created.
 
 sub batch_invoice {
   my ($self, $opt) = @_;
-  my $batch = FS::bill_batch->get_open_batch;
+  my $bill_batch = $self->get_open_bill_batch;
   my $cust_bill_batch = FS::cust_bill_batch->new({
-      batchnum => $batch->batchnum,
+      batchnum => $bill_batch->batchnum,
       invnum   => $self->invnum,
   });
   return $cust_bill_batch->insert($opt);
 }
 
+=item get_open_batch
+
+Returns the currently open batch as an FS::bill_batch object, creating a new
+one if necessary.  (A per-agent batch if invoice_print_pdf-spoolagent is
+enabled)
+
+=cut
+
+sub get_open_bill_batch {
+  my $self = shift;
+  my $hashref = { status => 'O' };
+  $hashref->{'agentnum'} = $conf->exists('invoice_print_pdf-spoolagent')
+                             ? $self->cust_main->agentnum
+                             : '';
+  my $batch = qsearchs('bill_batch', $hashref);
+  return $batch if $batch;
+  $batch = FS::bill_batch->new($hashref);
+  my $error = $batch->insert;
+  die $error if $error;
+  return $batch;
+}
+
 =item ftp_invoice [ TEMPLATENAME ] 
 
 Sends this invoice data via FTP.
@@ -2329,7 +2351,6 @@ notice_name - overrides "Invoice" as the name of the sent document (templates fr
 # (alignment in text invoice?) problems to change them all to '%.2f' ?
 # yes: fixed width (dot matrix) text printing will be borked
 sub print_generic {
-
   my( $self, %params ) = @_;
   my $today = $params{today} ? $params{today} : time;
   warn "$me print_generic called on $self with suffix $params{template}\n"
@@ -4200,21 +4221,25 @@ sub _items_accountcode_cdr {
                     quantity    => '',
                     product_code => 'N/A',
                     section     => $section,
-                    ext_description => [],
+                    ext_description => [ $section->{'header'} ],
+                    detail_temp => [],
             };
 
             $section->{'amount'} += $amount;
             $accountcodes{$accountcode}{'amount'} += $amount;
             $accountcodes{$accountcode}{calls}++;
             $accountcodes{$accountcode}{duration} += $detail->duration;
-            push @{$accountcodes{$accountcode}{ext_description}},
-                $detail->formatted('format' => $format);
+            push @{$accountcodes{$accountcode}{detail_temp}}, $detail;
         }
     }
 
     foreach my $l ( values %accountcodes ) {
         $l->{amount} = sprintf( "%.2f", $l->{amount} );
-        unshift @{$l->{ext_description}}, $section->{'header'};
+        my @sorted_detail = sort { $a->startdate <=> $b->startdate } @{$l->{detail_temp}};
+        foreach my $sorted_detail ( @sorted_detail ) {
+            push @{$l->{ext_description}}, $sorted_detail->formatted('format'=>$format);
+        }
+        delete $l->{detail_temp};
         push @lines, $l;
     }
 
@@ -5196,6 +5221,11 @@ sub search_sql_where {
     push @search, "cust_main.agentnum = $1";
   }
 
+  #agentnum
+  if ( $param->{'custnum'} =~ /^(\d+)$/ ) {
+    push @search, "cust_bill.custnum = $1";
+  }
+
   #_date
   if ( $param->{_date} ) {
     my($beginning, $ending) = @{$param->{_date}};