add package def option to show $0 recurring on invoices, RT#9777
[freeside.git] / FS / FS / cust_bill_pkg.pm
index cb070d7..2c79209 100644 (file)
@@ -10,6 +10,7 @@ use FS::part_pkg;
 use FS::cust_bill;
 use FS::cust_bill_pkg_detail;
 use FS::cust_bill_pkg_display;
+use FS::cust_bill_pkg_discount;
 use FS::cust_bill_pay_pkg;
 use FS::cust_credit_bill_pkg;
 use FS::cust_tax_exempt_pkg;
@@ -152,8 +153,9 @@ sub insert {
         'amount'     => (ref($detail) ? $detail->[2] : '' ),
         'classnum'   => (ref($detail) ? $detail->[3] : '' ),
         'phonenum'   => (ref($detail) ? $detail->[4] : '' ),
-        'duration'   => (ref($detail) ? $detail->[5] : '' ),
-        'regionname' => (ref($detail) ? $detail->[6] : '' ),
+        'accountcode' => (ref($detail) ? $detail->[5] : '' ),
+        'duration'   => (ref($detail) ? $detail->[6] : '' ),
+        'regionname' => (ref($detail) ? $detail->[7] : '' ),
       };
       $error = $cust_bill_pkg_detail->insert;
       if ( $error ) {
@@ -411,7 +413,8 @@ sub previous_cust_bill_pkg {
 
 Returns an array of detail information for the invoice line item.
 
-Currently available options are: I<format> I<escape_function>
+Currently available options are: I<format>, I<escape_function> and
+I<format_function>.
 
 If I<format> is set to html or latex then the array members are improved
 for tabular appearance in those environments if possible.
@@ -419,6 +422,12 @@ for tabular appearance in those environments if possible.
 If I<escape_function> is set then the array members are processed by this
 function before being returned.
 
+I<format_function> overrides the normal HTML or LaTeX function for returning
+formatted CDRs.  It can be set to a subroutine which returns an empty list
+to skip usage detail:
+
+  'format_function' => sub { () },
+
 =cut
 
 sub details {
@@ -480,6 +489,35 @@ sub details {
     #qsearch ( 'cust_bill_pkg_detail', { 'lineitemnum' => $self->lineitemnum });
 }
 
+=item details_header [ OPTION => VALUE ... ]
+
+Returns a list representing an invoice line item detail header, if any.
+This relies on the behavior of voip_cdr in that it expects the header
+to be the first CSV formatted detail (as is expected by invoice generation
+routines).  Returns the empty list otherwise.
+
+=cut
+
+sub details_header {
+  my $self = shift;
+  return '' unless defined dbdef->table('cust_bill_pkg_detail');
+
+  eval "use Text::CSV_XS;";
+  die $@ if $@;
+  my $csv = new Text::CSV_XS;
+
+  my @detail = 
+    qsearch ({ 'table'    => 'cust_bill_pkg_detail',
+               'hashref'  => { 'billpkgnum' => $self->billpkgnum,
+                               'format'     => 'C',
+                             },
+               'order_by' => 'ORDER BY detailnum LIMIT 1',
+            });
+  return() unless scalar(@detail);
+  $csv->parse($detail[0]->detail) or return ();
+  $csv->fields;
+}
+
 =item desc
 
 Returns a description for this line item.  For typical line items, this is the
@@ -677,7 +715,10 @@ sub disintegrate {
       $cust_bill_pkg{''}->recur( $classless );
       $cust_bill_pkg{$class} = $cust_bill_pkg_usage;
     }
-    delete $cust_bill_pkg{''} unless $cust_bill_pkg{''}->recur;
+    warn "Unexpected classless usage value: ". $cust_bill_pkg{''}->recur
+      if ($cust_bill_pkg{''}->recur && $cust_bill_pkg{''}->recur < 0);
+    delete $cust_bill_pkg{''}
+      unless ($cust_bill_pkg{''}->recur && $cust_bill_pkg{''}->recur > 0);
   }
 
 #  # sort setup,recur,'', and the rest numeric && return
@@ -838,7 +879,31 @@ sub cust_bill_pkg_detail {
   my %hash = ( 'billpkgnum' => $self->billpkgnum );
   $hash{classnum} = $classnum if $classnum;
 
-  qsearch ( 'cust_bill_pkg_detail', { %hash  } ),
+  qsearch( 'cust_bill_pkg_detail', \%hash ),
+
+}
+
+=item cust_bill_pkg_discount 
+
+Returns the list of associated cust_bill_pkg_discount objects.
+
+=cut
+
+sub cust_bill_pkg_discount {
+  my $self = shift;
+  qsearch( 'cust_bill_pkg_discount', { 'billpkgnum' => $self->billpkgnum } );
+}
+
+=item recur_show_zero
+
+=cut
+
+sub recur_show_zero {
+  my $self = shift;
+
+     $self->recur == 0
+  && $self->pkgnum
+  && $self->cust_pkg->part_pkg->recur_show_zero;
 
 }