+ my @summary_subtotals;
+ # the templates say "$_->{tax_section} || !$_->{summarized}"
+ # except 'summarized' is only true when tax_section is true, so this
+ # is always true, so what's the deal?
+ foreach my $s (@sections) {
+ # not to include in the "summary of new charges" block:
+ # finance charges, adjustments, previous charges,
+ # and itemized phone usage sections
+ if ( $s eq $adjust_section or
+ ($s eq $previous_section and $s ne $default_section) or
+ ($invoice_data{'finance_section'} and
+ $invoice_data{'finance_section'} eq $s->{description}) or
+ $s->{'description'} =~ /^\d+ $/ ) {
+ next;
+ }
+ push @summary_subtotals, $s;
+ }
+ $invoice_data{summary_subtotals} = \@summary_subtotals;
+
+ # usage subtotals
+ if ( $conf->exists('usage_class_summary')
+ and $self->can('_items_usage_class_summary') ) {
+ my @usage_subtotals = $self->_items_usage_class_summary(escape => $escape_function);
+ if ( @usage_subtotals ) {
+ unshift @sections, $usage_subtotals[0]->{section};
+ unshift @detail_items, @usage_subtotals;
+ }
+ }
+
+ # invoice history "section" (not really a section)
+ # not to be included in any subtotals, completely independent of
+ # everything...
+ if ( $conf->exists('previous_invoice_history') ) {
+ my %history;
+ my %monthorder;
+ foreach my $cust_bill ( $cust_main->cust_bill ) {
+ # XXX hardcoded format, and currently only 'charged'; add other fields
+ # if they become necessary
+ my $date = $self->time2str_local('%b %Y', $cust_bill->_date);
+ $history{$date} ||= 0;
+ $history{$date} += $cust_bill->charged;
+ # just so we have a numeric sort key
+ $monthorder{$date} ||= $cust_bill->_date;
+ }
+ my @sorted_months = sort { $monthorder{$a} <=> $monthorder{$b} }
+ keys %history;
+ my @sorted_amounts = map { sprintf('%.2f', $history{$_}) } @sorted_months;
+ $invoice_data{monthly_history} = [ \@sorted_months, \@sorted_amounts ];
+ }
+
+ # service locations: another option for template customization
+ my %location_info;
+ foreach my $item (@detail_items) {
+ if ( $item->{locationnum} ) {
+ $location_info{ $item->{locationnum} } ||= {
+ FS::cust_location->by_key( $item->{locationnum} )->location_hash
+ };
+ }
+ }
+ $invoice_data{location_info} = \%location_info;
+
+ # debugging hook: call this with 'diag' => 1 to just get a hash of
+ # the invoice variables
+ return \%invoice_data if ( $params{'diag'} );
+