fix invoice sub-totals, RT#6489
[freeside.git] / FS / FS / cust_bill_ApplicationCommon.pm
index 9f61f5b..7449679 100644 (file)
@@ -1,16 +1,18 @@
 package FS::cust_bill_ApplicationCommon;
 
 use strict;
-use vars qw( @ISA $DEBUG $me );
+use vars qw( @ISA $DEBUG $me $skip_apply_to_lineitems_hack );
 use List::Util qw(min);
 use FS::Schema qw( dbdef );
 use FS::Record qw( qsearch qsearchs dbh );
 
 @ISA = qw( FS::Record );
 
-$DEBUG = 1;
+$DEBUG = 0;
 $me = '[FS::cust_bill_ApplicationCommon]';
 
+$skip_apply_to_lineitems_hack = 0;
+
 =head1 NAME
 
 FS::cust_bill_ApplicationCommon - Base class for bill application classes
@@ -33,6 +35,8 @@ represent application of things to invoices, currently payments
 
 =head1 METHODS
 
+=over 4
+
 =item insert
 
 =cut
@@ -52,7 +56,7 @@ sub insert {
   my $dbh = dbh;
 
   my $error =    $self->SUPER::insert(@_)
-              || $self->apply_to_lineitems;
+              || $self->apply_to_lineitems(@_);
   if ( $error ) {
     $dbh->rollback if $oldAutoCommit;
     return $error;
@@ -109,10 +113,15 @@ Auto-applies this invoice application to specific line items, if possible.
 =cut
 
 sub apply_to_lineitems {
-  my $self = shift;
+  #my $self = shift;
+  my( $self, %options ) = @_;
+
+  return '' if $skip_apply_to_lineitems_hack;
 
   my @apply = ();
 
+  my $conf = new FS::Conf;
+
   local $SIG{HUP} = 'IGNORE';
   local $SIG{INT} = 'IGNORE';
   local $SIG{QUIT} = 'IGNORE';
@@ -125,6 +134,8 @@ sub apply_to_lineitems {
   my $dbh = dbh;
 
   my @open = $self->cust_bill->open_cust_bill_pkg; #FOR UPDATE...?
+  @open = grep { $_->pkgnum == $self->pkgnum } @open
+    if $conf->exists('pkg-balances') && $self->pkgnum;
   warn "$me ". scalar(@open). " open line items for invoice ".
        $self->cust_bill->invnum. ": ". join(', ', @open). "\n"
     if $DEBUG;
@@ -312,7 +323,7 @@ sub apply_to_lineitems {
       'sdate'      => $cust_bill_pkg->sdate,
       'edate'      => $cust_bill_pkg->edate,
     });
-    my $error = $application->insert;
+    my $error = $application->insert(%options);
     if ( $error ) {
       $dbh->rollback if $oldAutoCommit;
       return $error;
@@ -359,6 +370,18 @@ sub cust_bill {
   qsearchs( 'cust_bill', { 'invnum' => $self->invnum } );
 }
 
+=item applied_to_invoice
+
+Returns a string representing the invoice (see L<FS::cust_bill>), for example:
+"applied to Invoice #54 (3/20/2008)"
+
+=cut
+
+sub applied_to_invoice {
+  my $self = shift;
+  'applied to '. $self->cust_bill->invnum_date_pretty;
+}
+
 =item lineitem_breakdown_table 
 
 =cut