when previous invoice due dates are shown on invoice, change placement of current...
[freeside.git] / FS / FS / cust_bill.pm
index 7d1488f..df9711c 100644 (file)
@@ -448,16 +448,20 @@ followed by the previous outstanding invoices (as FS::cust_bill objects also).
 
 sub previous {
   my $self = shift;
-  my $total = 0;
-  my @cust_bill = sort { $a->_date <=> $b->_date }
-    grep { $_->owed != 0 }
-      qsearch( 'cust_bill', { 'custnum' => $self->custnum,
-                              #'_date'   => { op=>'<', value=>$self->_date },
-                              'invnum'   => { op=>'<', value=>$self->invnum },
-                            } ) 
-  ;
-  foreach ( @cust_bill ) { $total += $_->owed; }
-  $total, @cust_bill;
+  # simple memoize; we use this a lot
+  if (!$self->get('previous')) {
+    my $total = 0;
+    my @cust_bill = sort { $a->_date <=> $b->_date }
+      grep { $_->owed != 0 }
+        qsearch( 'cust_bill', { 'custnum' => $self->custnum,
+                                #'_date'   => { op=>'<', value=>$self->_date },
+                                'invnum'   => { op=>'<', value=>$self->invnum },
+                              } ) 
+    ;
+    foreach ( @cust_bill ) { $total += $_->owed; }
+    $self->set('previous', [$total, @cust_bill]);
+  }
+  return @{ $self->get('previous') };
 }
 
 =item enable_previous
@@ -2216,7 +2220,7 @@ sub _items_extra_usage_sections {
   my %classnums = ();
   my %lines = ();
 
-  my $maxlength = $conf->config('cust_bill-latex_lineitem_maxlength') || 50;
+  my $maxlength = $conf->config('cust_bill-latex_lineitem_maxlength') || 40;
 
   my %usage_class =  map { $_->classnum => $_ } qsearch( 'usage_class', {} );
   foreach my $cust_bill_pkg ( $self->cust_bill_pkg ) {
@@ -2456,7 +2460,7 @@ sub _items_svc_phone_sections {
   my %classnums = ();
   my %lines = ();
 
-  my $maxlength = $conf->config('cust_bill-latex_lineitem_maxlength') || 50;
+  my $maxlength = $conf->config('cust_bill-latex_lineitem_maxlength') || 40;
 
   my %usage_class =  map { $_->classnum => $_ } qsearch( 'usage_class', {} );
   $usage_class{''} ||= new FS::usage_class { 'classname' => '', 'weight' => 0 };
@@ -2755,7 +2759,7 @@ sub _items_previous {
 
 sub _items_credits {
   my( $self, %opt ) = @_;
-  my $trim_len = $opt{'trim_len'} || 50;
+  my $trim_len = $opt{'trim_len'} || 40;
 
   my @b;
   #credits
@@ -2852,6 +2856,55 @@ sub _items_payments {
 
 }
 
+sub _items_total {
+  my $self = shift;
+  my $conf = $self->conf;
+
+  my @items;
+  my ($pr_total) = $self->previous;
+  if ( $conf->exists('previous_balance-exclude_from_total') ) {
+    # then return separate lines for previous balance and total new charges
+    if ( $pr_total ) {
+      push @items,
+        { total_item    => $self->mt('Previous Balance'),
+          total_amount  => sprintf('%.2f',$pr_total)
+        };
+    }
+    my $new_charges_desc = $self->mt(
+      $conf->config('previous_balance-exclude_from_total')
+       || 'Total New Charges'
+    ); # localize 'Total New Charges' or whatever's in the config
+
+    if ( $conf->exists('invoice_show_prior_due_date') ) {
+      # then the due date should be shown with Total New Charges,
+      # and should NOT be shown with the Balance Due message.
+      if ( $self->due_date ) {
+        # localize the "Please pay by" message and the date itself
+        # (grammar issues with this, yeah)
+        $new_charges_desc .= ' - ' . $self->mt('Please pay by') . ' ' .
+                             $self->due_date2str('short');
+      } elsif ( $self->terms ) {
+        # phrases like "due on receipt" should be localized
+        $new_charges_desc .= ' - ' . $self->mt($self->terms);
+      }
+    }
+
+    push @items,
+      { total_item    => $self->mt($new_charges_desc),
+        total_amount  => $self->charged
+      };
+
+  } else {
+    push @items,
+      { total_item    => $self->mt('Total Charges'),
+        total_amount  => sprintf('%.2f',$self->charged + $pr_total)
+      };
+  }
+  @items;
+}
+
+
+
 =item call_details [ OPTION => VALUE ... ]
 
 Returns an array of CSV strings representing the call details for this invoice