+sub _items_nontax {
+ my $self = shift;
+ # The order of these is important. Bundled line items will be merged into
+ # the most recent non-hidden item, so it needs to be the one with:
+ # - the same pkgnum
+ # - the same start date
+ # - no pkgpart_override
+ #
+ # So: sort by pkgnum,
+ # then by sdate
+ # then sort the base line item before any overrides
+ # then sort hidden before non-hidden add-ons
+ # then sort by override pkgpart (for consistency)
+ sort { $a->pkgnum <=> $b->pkgnum or
+ $a->sdate <=> $b->sdate or
+ ($a->pkgpart_override ? 0 : -1) or
+ ($b->pkgpart_override ? 0 : 1) or
+ $b->hidden cmp $a->hidden or
+ $a->pkgpart_override <=> $b->pkgpart_override
+ }
+ # and of course exclude taxes and fees
+ grep { $_->pkgnum > 0 } $self->cust_bill_pkg;
+}
+
+sub _items_fee {
+ my $self = shift;
+ my %options = @_;
+ my @cust_bill_pkg = grep { $_->feepart } $self->cust_bill_pkg;
+ my @items;
+ foreach my $cust_bill_pkg (@cust_bill_pkg) {
+ # cache this, so we don't look it up again in every section
+ my $part_fee = $cust_bill_pkg->get('part_fee')
+ || $cust_bill_pkg->part_fee;
+ $cust_bill_pkg->set('part_fee', $part_fee);
+ if (!$part_fee) {
+ #die "fee definition not found for line item #".$cust_bill_pkg->billpkgnum."\n"; # might make more sense
+ warn "fee definition not found for line item #".$cust_bill_pkg->billpkgnum."\n";
+ next;
+ }
+ if ( exists($options{section}) and exists($options{section}{category}) )
+ {
+ my $categoryname = $options{section}{category};
+ # then filter for items that have that section
+ if ( $part_fee->categoryname ne $categoryname ) {
+ warn "skipping fee '".$part_fee->itemdesc."'--not in section $categoryname\n" if $DEBUG;
+ next;
+ }
+ } # otherwise include them all in the main section
+ # XXX what to do when sectioning by location?
+
+ my @ext_desc;
+ my %base_invnums; # invnum => invoice date
+ foreach ($cust_bill_pkg->cust_bill_pkg_fee) {
+ if ($_->base_invnum) {
+ my $base_bill = FS::cust_bill->by_key($_->base_invnum);
+ my $base_date = $self->time2str_local('short', $base_bill->_date)
+ if $base_bill;
+ $base_invnums{$_->base_invnum} = $base_date || '';
+ }
+ }
+ foreach (sort keys(%base_invnums)) {
+ next if $_ == $self->invnum;
+ push @ext_desc,
+ $self->mt('from invoice \\#[_1] on [_2]', $_, $base_invnums{$_});
+ }
+ push @items,
+ { feepart => $cust_bill_pkg->feepart,
+ amount => sprintf('%.2f', $cust_bill_pkg->setup + $cust_bill_pkg->recur),
+ description => $part_fee->itemdesc_locale($self->cust_main->locale),
+ ext_description => \@ext_desc
+ # sdate/edate?
+ };
+ }
+ @items;
+}
+