per-agent invoice PDF batches, RT#13727
[freeside.git] / FS / FS / cust_bill.pm
index 9d250eb..7669479 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.
@@ -2168,7 +2190,7 @@ sub print_text {
   $params{'time'} = $today if $today;
   $params{'template'} = $template if $template;
   $params{$_} = $opt{$_} 
-    foreach grep $opt{$_}, qw( unsquealch_cdr notice_name );
+    foreach grep $opt{$_}, qw( unsquelch_cdr notice_name );
 
   $self->print_generic( %params );
 }
@@ -2210,7 +2232,7 @@ sub print_latex {
   $params{'time'} = $today if $today;
   $params{'template'} = $template if $template;
   $params{$_} = $opt{$_} 
-    foreach grep $opt{$_}, qw( unsquealch_cdr notice_name );
+    foreach grep $opt{$_}, qw( unsquelch_cdr notice_name );
 
   $template ||= $self->_agent_template;
 
@@ -3997,6 +4019,8 @@ sub _items_extra_usage_sections {
   my %classnums = ();
   my %lines = ();
 
+  my $maxlength = $conf->config('cust_bill-latex_lineitem_maxlength') || 50;
+
   my %usage_class =  map { $_->classnum => $_ } qsearch( 'usage_class', {} );
   foreach my $cust_bill_pkg ( $self->cust_bill_pkg ) {
     next unless $cust_bill_pkg->pkgnum > 0;
@@ -4016,8 +4040,8 @@ sub _items_extra_usage_sections {
 
         my $desc = $detail->regionname; 
         my $description = $desc;
-        $description = substr($desc, 0, 50). '...'
-          if $format eq 'latex' && length($desc) > 50;
+        $description = substr($desc, 0, $maxlength). '...'
+          if $format eq 'latex' && length($desc) > $maxlength;
 
         $lines{$section}{$desc} ||= {
           description     => &{$escape}($description),
@@ -4100,7 +4124,7 @@ sub _did_summary {
 
            my $inserted = $h_cust_svc->date_inserted;
            my $deleted = $h_cust_svc->date_deleted;
-           my $phone_inserted = $h_cust_svc->h_svc_x($inserted);
+           my $phone_inserted = $h_cust_svc->h_svc_x($inserted+5);
            my $phone_deleted;
            $phone_deleted =  $h_cust_svc->h_svc_x($deleted) if $deleted;
            
@@ -4133,10 +4157,13 @@ sub _did_summary {
            }
 
            # increment usage minutes
-           my @cdrs = $phone_inserted->get_cdrs('begin'=>$start,'end'=>$end);
-           foreach my $cdr ( @cdrs ) {
-               $minutes += $cdr->billsec/60;
-           }
+        if ( $phone_inserted ) {
+            my @cdrs = $phone_inserted->get_cdrs('begin'=>$start,'end'=>$end,'billsec_sum'=>1);
+            $minutes = $cdrs[0]->billsec_sum if scalar(@cdrs) == 1;
+        }
+        else {
+            warn "WARNING: no matching h_svc_phone insert record for insert time $inserted, svcnum " . $h_cust_svc->svcnum;
+        }
 
            # don't look at this service again
            push @seen, $h_cust_svc->svcnum;
@@ -4227,6 +4254,8 @@ sub _items_svc_phone_sections {
   my %classnums = ();
   my %lines = ();
 
+  my $maxlength = $conf->config('cust_bill-latex_lineitem_maxlength') || 50;
+
   my %usage_class =  map { $_->classnum => $_ } qsearch( 'usage_class', {} );
   $usage_class{''} ||= new FS::usage_class { 'classname' => '', 'weight' => 0 };
 
@@ -4256,8 +4285,8 @@ sub _items_svc_phone_sections {
 
       my $desc = $detail->regionname; 
       my $description = $desc;
-      $description = substr($desc, 0, 50). '...'
-        if $format eq 'latex' && length($desc) > 50;
+      $description = substr($desc, 0, $maxlength). '...'
+        if $format eq 'latex' && length($desc) > $maxlength;
 
       $lines{$phonenum}{$desc} ||= {
         description     => &{$escape}($description),
@@ -4569,6 +4598,8 @@ sub _items_cust_bill_pkg {
   my $multisection = $opt{multisection} || '';
   my $discount_show_always = 0;
 
+  my $maxlength = $conf->config('cust_bill-latex_lineitem_maxlength') || 50;
+
   my @b = ();
   my ($s, $r, $u) = ( undef, undef, undef );
   foreach my $cust_bill_pkg ( @$cust_bill_pkgs )
@@ -4594,8 +4625,8 @@ sub _items_cust_bill_pkg {
       my $type = $display->type;
 
       my $desc = $cust_bill_pkg->desc;
-      $desc = substr($desc, 0, 50). '...'
-        if $format eq 'latex' && length($desc) > 50;
+      $desc = substr($desc, 0, $maxlength). '...'
+        if $format eq 'latex' && length($desc) > $maxlength;
 
       my %details_opt = ( 'format'          => $format,
                           'escape_function' => $escape_function,
@@ -4609,13 +4640,21 @@ sub _items_cust_bill_pkg {
  
         my $cust_pkg = $cust_bill_pkg->cust_pkg;
 
-        if ( $cust_bill_pkg->setup != 0 && (!$type || $type eq 'S') ) {
+        if (    (!$type || $type eq 'S')
+             && (    $cust_bill_pkg->setup != 0
+                  || $cust_bill_pkg->setup_show_zero
+                )
+           )
+         {
 
           warn "$me _items_cust_bill_pkg adding setup\n"
             if $DEBUG > 1;
 
           my $description = $desc;
-          $description .= ' Setup' if $cust_bill_pkg->recur != 0;
+          $description .= ' Setup'
+            if $cust_bill_pkg->recur != 0
+            || $discount_show_always
+            || $cust_bill_pkg->recur_show_zero;
 
           my @d = ();
           unless ( $cust_pkg->part_pkg->hide_svc_detail
@@ -4628,8 +4667,8 @@ sub _items_cust_bill_pkg {
 
             if ( $multilocation ) {
               my $loc = $cust_pkg->location_label;
-              $loc = substr($loc, 0, 50). '...'
-                if $format eq 'latex' && length($loc) > 50;
+              $loc = substr($loc, 0, $maxlength). '...'
+                if $format eq 'latex' && length($loc) > $maxlength;
               push @d, &{$escape_function}($loc);
             }
 
@@ -4644,6 +4683,7 @@ sub _items_cust_bill_pkg {
             push @{ $s->{ext_description} }, @d;
           } else {
             $s = {
+              _is_setup       => 1,
               description     => $description,
               #pkgpart         => $part_pkg->pkgpart,
               pkgnum          => $cust_bill_pkg->pkgnum,
@@ -4676,7 +4716,8 @@ sub _items_cust_bill_pkg {
           $description .= " (" . time2str($date_format, $cust_bill_pkg->sdate).
                           " - ". time2str($date_format, $cust_bill_pkg->edate).
                           ")"
-            unless $conf->exists('disable_line_item_date_ranges');
+            unless $conf->exists('disable_line_item_date_ranges')
+                || $cust_pkg->part_pkg->option('disable_line_item_date_ranges',1);
 
           my @d = ();
 
@@ -4707,8 +4748,8 @@ sub _items_cust_bill_pkg {
 
             if ( $multilocation ) {
               my $loc = $cust_pkg->location_label;
-              $loc = substr($loc, 0, 50). '...'
-                if $format eq 'latex' && length($loc) > 50;
+              $loc = substr($loc, 0, $maxlength). '...'
+                if $format eq 'latex' && length($loc) > $maxlength;
               push @d, &{$escape_function}($loc);
             }
 
@@ -4818,7 +4859,9 @@ sub _items_cust_bill_pkg {
         push @b, { %$_ }
           if $_->{amount} != 0
           || $discount_show_always
-          || $cust_bill_pkg->recur_show_zero;
+          || ( ! $_->{_is_setup} && $cust_bill_pkg->recur_show_zero )
+          || (   $_->{_is_setup} && $cust_bill_pkg->setup_show_zero )
+        ;
         $_ = undef;
       }
     }