%init> my $curuser = $FS::CurrentUser::CurrentUser; die "access denied" unless $curuser->access_right('View invoices'); my ($invnum) = $cgi->keywords; my $agentnums = $curuser->agentnums_href; my $cust_bill = FS::cust_bill->by_key($invnum) || FS::cust_bill_void->by_key($invnum); if (!$cust_bill or !$agentnums->{ $cust_bill->cust_main->agentnum }) { die "invalid invnum $invnum"; } my %column_of; # taxname => @tax_amounts column index my @tax_amounts; # array of arrays my @sale_amounts; # just an array my @taxable_descs; # taxable item row headings my @itemdescs; # column headings my %taxname_of; # billpkgnum => taxname my $lineitem_table = 'cust_bill_pkg'; if ($cust_bill->isa('FS::cust_bill_void')) { $lineitem_table .= '_void'; } my (@tax, @nontax); foreach (qsearch({ 'table' => $lineitem_table, 'hashref' => { 'invnum' => $invnum }, 'order_by' => 'ORDER BY itemdesc, billpkgnum', # save on sorting the columns later }) ) { if ( $_->pkgnum or $_->feepart ) { push @nontax, $_; } else { push @tax, $_; } } my $col = 0; foreach (@tax) { $taxname_of{$_->billpkgnum} = $_->itemdesc; push @itemdescs, $_->itemdesc; $column_of{$_->itemdesc} = $col++; } foreach my $sale (@nontax) { my $this_row = []; foreach my $link_table (qw( cust_bill_pkg_tax_location cust_bill_pkg_tax_rate_location )) { $link_table .= '_void' if $cust_bill->isa('FS::cust_pkg_void'); foreach my $link (qsearch({ 'table' => $link_table, 'hashref' => { 'taxable_billpkgnum' => $sale->billpkgnum } })) { # find the itemdesc on this tax my $taxname = $taxname_of{ $link->billpkgnum }; # and accumulate in the column it belongs in ($this_row->[ $column_of{ $taxname } ] ||= 0) += $link->amount; } } # foreach $link_table # create a row if we added any taxes if ( @$this_row ) { push @tax_amounts, $this_row; push @sale_amounts, $sale->setup + $sale->recur; push @taxable_descs, $sale->desc; } } %init> <& /elements/header-popup.html &>
<% emt('Charge') %> | % for (my $col = 0; $col < scalar(@itemdescs); $col++) {<% emt($itemdescs[$col]) %> | % }|
---|---|---|
<% emt($taxable_descs[$row]) %> | <% sprintf('%.2f', $sale_amounts[$row]) %> | % for (my $col = 0; $col < scalar(@itemdescs); $col++) {<% $this_row->[$col] ? sprintf('%.2f', $this_row->[$col]) : '' %> | % }