+my $conf = new FS::Conf;
+
+my $money_char = $conf->config('money_char') || '$';
+
+my $title = emt('Credit Search Results');
+
+my $unapplied = $cgi->param('unapplied');
+$title = emt("Unapplied $title") if $unapplied;
+my $clink = sub {
+ my $cust_bill = shift;
+ $cust_bill->cust_main_custnum
+ ? [ "${p}view/cust_main.cgi?", 'custnum' ]
+ : '';
+};
+
+# form checkbox for show_voided_credits:
+# - value=unset : do not show voided credits
+# - value="1" : show voided credits
+my $show_voided_credits;
+$show_voided_credits = $cgi->param('show_voided_credits')
+ if $cgi->param('show_voided_credits');
+
+my (@header, @fields, @sort_fields, $align, @links, @color, @style);
+$align = '';
+
+# Report Column: crednum
+push @header, emt('Ref No');
+push @fields, sub { shift->crednum };
+push @sort_fields, '';
+$align .= 'l';
+push @links, '';
+push @color, '';
+push @style, '';
+
+# Report Column: Amount
+push @header, emt('Amount');
+push @fields, sub { $money_char .sprintf('%.2f', shift->amount) };
+push @sort_fields, 'amount';
+$align .= 'r';
+push @links, '';
+push @color, '';
+push @style, '';
+
+# Report Column: Unapplied Amount
+if ($unapplied) {
+ push @header, emt('Unapplied');
+ push @fields, sub { $money_char .sprintf('%.2f', shift->unapplied_amount) };
+ push @sort_fields, '';
+ $align .= 'r';
+ push @links, '';
+ push @color, '';
+ push @style, '';
+}
+
+# Report Columns: Date, By, Reason, Info
+push @header, emt('Date'),
+ emt('By'),
+ emt('Reason'),
+ emt('Info'),
+ ;
+push @fields, sub { time2str('%b %d %Y', shift->_date ) },
+ 'otaker',
+ 'reason_only',
+ 'addlinfo',
+ ;
+push @sort_fields, '_date', 'otaker', 'reasonnum', 'addlinfo';
+$align .= 'rlll';
+push @links, '',
+ '',
+ '',
+ '',
+ ;
+push @color, '',
+ '',
+ '',
+ '',
+ ;
+push @style, '',
+ '',
+ '',
+ '',
+ ;
+
+# insert customer email after 'Reason' if this is a commission report
+if ( $cgi->param('commission_salesnum') or $cgi->param('commission_agentnum') ) {
+ my $sub_customer_email = sub {
+ my $cust_credit = shift;
+ my $cust_event = $cust_credit->cust_event or return '';
+ my $cust_X = $cust_event->cust_X or return '';
+ my $cust_main = $cust_X->cust_main or return '';
+ my ($email) = $cust_main->invoicing_list_emailonly;
+ return encode_entities("<$email>") if length($email);
+ '';
+ };
+ push @header, '';
+ push @fields, $sub_customer_email;
+ push @sort_fields, '';
+ $align .= 'l';
+ push @links, '';
+ push @color, '';
+ push @style, '';
+}
+
+# cust fields
+push @header, FS::UI::Web::cust_header();
+push @fields, \&FS::UI::Web::cust_fields;
+push @sort_fields, FS::UI::Web::cust_sort_fields;
+$align .= FS::UI::Web::cust_aligns();
+push @links, map { $_ ne 'Cust. Status' ? $clink : '' }
+ FS::UI::Web::cust_header();
+push @color, FS::UI::Web::cust_colors();
+push @style, FS::UI::Web::cust_styles();
+
+if ( $show_voided_credits ) {
+
+ # Report Column: Void By:
+ push @header, emt('Void By');
+ push @fields, sub {
+ my $rec = shift;
+ return $rec->void_username
+ if $rec->isa('FS::cust_credit_void');
+ return '';
+ };
+ push @sort_fields, '';
+ $align .= 'l';
+ push @links, '';
+ push @color, '';
+ push @style, '';
+
+ # Report Column: Void Date:
+ push @header, emt('Void Date');
+ push @fields, sub {
+ my $rec = shift;
+ return time2str('%b %d %Y', $rec->void_date )
+ if $rec->isa('FS::cust_credit_void');
+ return '';
+ };
+ push @sort_fields, '';
+ $align .= 'l';
+ push @links, '';
+ push @color, '';
+ push @style, '';
+
+ # Report Column: Void Reason:
+ push @header, emt('Void Reason');
+ push @fields, sub {
+ my $rec = shift;
+ return $rec->void_reason_text
+ if $rec->isa('FS::cust_credit_void');
+ return '';
+ };
+ push @sort_fields, '';
+ $align .= 'l';
+ push @links, '';
+ push @color, '';
+ push @style, '';
+}
+