+my $unapplied = $cgi->param('unapplied');
+my $title = '';
+$title = 'Unapplied ' if $unapplied;
+$title .= "\u$name_singular Search Results";
+
+###NOT USED???
+#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 = $opt{'disable_link'} ? '' : sub {
+ my $obj = shift;
+ my $objnum = $obj->primary_key . '=' . $obj->get($obj->primary_key);
+ my $table = $obj->table;
+ my $void = '';
+ if ($table eq 'cust_pay_void') {
+ $table = 'cust_pay';
+ $void = ';void=1';
+ }
+
+ include('/elements/popup_link_onclick.html',
+ 'action' => $p.'view/'.$table.'.html?link=popup;'.$objnum.$void,
+ '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, 'paycardtype';
+ push @sort_fields, 'paycardtype';
+}
+
+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 ($cgi->param('show_order_number')) {
+ push @header, emt('Order Number');
+ $align .= 'r';
+ push @links, '';
+ push @fields, 'order_number';
+ push @sort_fields, 'order_number';
+}