+my $unapplied = $cgi->param('unapplied');
+my $title = '';
+$title = 'Unapplied ' if $unapplied;
+$title .= "\u$name_singular Search Results";
+
+my $link = '';
+if ( ( $curuser->access_right('View invoices') #remove in 2.5 (2.7?)
+ || ($curuser->access_right('View payments') && $table =~ /^cust_pay/)
+ || ($curuser->access_right('View refunds') && $table eq 'cust_refund')
+ )
+ && ! $opt{'disable_link'}
+ )
+{
+
+ my $key;
+ my $q = '';
+ if ( $table eq 'cust_pay_void' ) {
+ $key = 'paynum';
+ $q .= 'void=1;';
+ } elsif ( $table eq /^cust_(\w+)$/ ) {
+ $key = $1.'num';
+ }
+
+ if ( $key ) {
+ $q .= "$key=";
+ $link = [ "${p}view/$table.html?$q", $key ]
+ }
+}
+
+my $cust_link = sub {
+ my $cust_thing = shift;
+ $cust_thing->cust_main_custnum
+ ? [ "${p}view/cust_main.cgi?", 'custnum' ]
+ : '';
+};
+
+# only valid for $table == 'cust_pay' atm
+my $tax_names = '';
+if ( $cgi->param('tax_names') ) {
+ if ( dbh->{Driver}->{Name} =~ /^Pg/i ) {
+
+ $tax_names = "
+ array_to_string(
+ array(
+ SELECT itemdesc
+ FROM cust_bill_pay
+ LEFT JOIN cust_bill_pay_pkg USING ( billpaynum )
+ LEFT JOIN cust_bill_pkg USING ( billpkgnum )
+ WHERE cust_bill_pkg.pkgnum = 0
+ AND cust_bill_pay.paynum = cust_pay.paynum
+ ), '|'
+ ) AS tax_names"
+ ;
+
+ } elsif ( dbh->{Driver}->{Name} =~ /^mysql/i ) {
+
+ $tax_names = "GROUP_CONCAT(itemdesc SEPARATOR '|') AS tax_names";
+
+ } else {
+
+ warn "warning: unknown database type ". dbh->{Driver}->{Name}.
+ "omitting tax name information from report.";
+
+ }
+}
+
+my @header;
+my @fields;
+my @sort_fields;
+my $align = '';
+my @links;
+my @link_onclicks;
+if ( $opt{'pre_header'} ) {
+ push @header, @{ $opt{'pre_header'} };
+ $align .= 'c' x scalar(@{ $opt{'pre_header'} });
+ push @links, map '', @{ $opt{'pre_header'} };
+ push @fields, @{ $opt{'pre_fields'} };
+ push @sort_fields, @{ $opt{'pre_fields'} };
+}
+
+my $sub_receipt = sub {
+ my $obj = shift;
+ my $objnum = $obj->primary_key . '=' . $obj->get($obj->primary_key);
+
+ include('/elements/popup_link_onclick.html',
+ 'action' => $p.'view/cust_pay.html?link=popup;'.$objnum,
+ 'actionlabel' => emt('Payment Receipt'),
+ );
+};
+
+push @header, "\u$name_singular",
+ 'Amount',
+;
+$align .= 'rr';
+push @links, '', '';
+push @fields, 'payby_payinfo_pretty',
+ sub { sprintf($money, shift->$amount_field() ) },
+;
+push @link_onclicks, $sub_receipt, '';
+push @sort_fields, 'paysort', $amount_field;
+
+if ($opt{'show_card_type'}) {
+ push @header, emt('Card Type');
+ $align .= 'r';
+ push @links, '';
+ push @fields, sub {
+ (($_[0]->payby eq 'CARD') && ($_[0]->paymask !~ /N\/A/)) ? cardtype($_[0]->paymask) : ''
+ };
+ push @sort_fields, '';
+}
+
+if ( $unapplied ) {
+ push @header, emt('Unapplied');
+ $align .= 'r';
+ push @links, '';
+ push @fields, sub { sprintf($money, shift->unapplied_amount) };
+ push @sort_fields, '';
+}
+
+push @header, emt('Date');
+$align .= 'r';
+push @links, '';
+push @fields, sub { time2str('%b %d %Y', shift->_date ) };
+push @sort_fields, '_date';
+
+if ($opt{'show_order_number'}) {
+ push @header, emt('Order Number');
+ $align .= 'r';
+ push @links, '';
+ push @fields, 'order_number';
+ push @sort_fields, 'order_number';
+}
+
+unless ( $opt{'disable_by'} ) {
+ push @header, emt('By');
+ $align .= 'c';
+ push @links, '';
+ push @fields, sub { my $o = shift->otaker;
+ $o = 'auto billing' if $o eq 'fs_daily';
+ $o = 'customer self-service' if $o eq 'fs_selfservice';
+ $o;
+ };
+ push @sort_fields, '';
+}
+
+if ( $tax_names ) {
+ push @header, (emt('Tax names'), emt('Tax province'));
+ $align .= 'cc';
+ push @links, ('','');
+ push @fields, sub { join (' + ', map { /^(.*?)(, \w\w)?$/; $1 }
+ split('\|', shift->tax_names)
+ );
+ };
+ push @fields, sub { join (' + ', map { if (/^(?:.*)(?:, )(\w\w)$/){ $1 }
+ else { () }
+ }
+ split('\|', shift->tax_names)
+ );
+ };
+ push @sort_fields, '', '';
+}
+
+push @header, FS::UI::Web::cust_header();
+$align .= FS::UI::Web::cust_aligns();
+push @links, map { $_ ne 'Cust. Status' ? $cust_link : '' }
+ FS::UI::Web::cust_header();
+my @color = ( ( map '', @fields ), FS::UI::Web::cust_colors() );
+my @style = ( ( map '', @fields ), FS::UI::Web::cust_styles() );
+push @fields, \&FS::UI::Web::cust_fields;
+push @sort_fields, FS::UI::Web::cust_sort_fields;
+
+push @header, @{ $opt{'addl_header'} }
+ if $opt{'addl_header'};
+push @fields, @{ $opt{'addl_fields'} }
+ if $opt{'addl_fields'};
+push @sort_fields, @{ $opt{'addl_sort_fields'} }
+ if $opt{'addl_sort_fields'};
+
+my( $count_query, $sql_query, @count_addl );