-<%
- #my( $count_query, $sql_query );
-
- my @search = ();
-
- if ( $cgi->param('otaker') && $cgi->param('otaker') =~ /^([\w\.\-]+)$/ ) {
- push @search, "otaker = '$1'";
- }
-
- #false laziness with cust_pkg.cgi and cust_pay.cgi
- if ( $cgi->param('beginning')
- && $cgi->param('beginning') =~ /^([ 0-9\-\/]{1,10})$/ ) {
- my $beginning = str2time($1);
- push @search, "_date >= $beginning ";
- }
- if ( $cgi->param('ending')
- && $cgi->param('ending') =~ /^([ 0-9\-\/]{1,10})$/ ) {
- my $ending = str2time($1) + 86399;
- push @search, " _date <= $ending ";
- }
-
- if ( $cgi->param('begin')
- && $cgi->param('begin') =~ /^(\d+)$/ ) {
- push @search, "_date >= $1 ";
- }
- if ( $cgi->param('end')
- && $cgi->param('end') =~ /^(\d+)$/ ) {
- push @search, " _date < $1 ";
- }
-
- my $where = scalar(@search)
- ? 'WHERE '. join(' AND ', @search)
- : '';
-
- my $count_query = "SELECT COUNT(*), SUM(amount) FROM cust_credit $where";
- my $sql_query = {
- 'table' => 'cust_credit',
- 'hashref' => {},
- 'extra_sql' => $where,
- };
-
- my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
-
-%>
-<%= include( 'elements/search.html',
- 'title' => 'Credit Search Results',
- 'name' => 'credits',
- 'query' => $sql_query,
- 'count_query' => $count_query,
- 'count_addl' => [ '$%.2f total credited', ],
- #'redirect' => $link,
- 'header' =>
- [ qw(Amount Date), 'Cust #', 'Contact name',
- qw(Company By Reason) ],
- 'fields' => [
- #'crednum',
- sub { sprintf('$%.2f', shift->amount ) },
- sub { time2str('%b %d %Y', shift->_date ) },
- 'custnum',
- sub { my $cust_main = shift->cust_main;
- $cust_main->get('last'). ', '. $cust_main->first;
- },
- sub { my $cust_main = shift->cust_main;
- $cust_main->company;
- },
- 'otaker',
- 'reason',
- ],
- 'align' => 'rrrllll',
- 'links' => [
- '',
- '',
- $clink,
- $clink,
- $clink,
- '',
- '',
- ],
- )
-%>
+<& elements/search.html,
+ 'title' => $title,
+ 'name' => emt('credits'),
+ 'query' => $sql_query,
+ 'count_query' => $count_query,
+ 'count_addl' => \@count_addl,
+ 'header' => \@header,
+ 'fields' => \@fields,
+ 'sort_fields' => \@sort_fields,
+ 'align' => $align,
+ 'links' => \@links,
+ 'color' => \@color,
+ 'style' => \@style,
+&>
+<%init>
+
+die "access denied"
+ unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
+
+my $money_char = FS::Conf->new->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' ]
+ : '';
+};
+
+my (@header, @fields, @sort_fields, $align, @links, @color, @style);
+$align = '';
+
+#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, '';
+
+# 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, '';
+}
+
+push @header, emt('Date'),
+ emt('By'),
+ emt('Reason'),
+ FS::UI::Web::cust_header(),
+ ;
+push @fields, sub { time2str('%b %d %Y', shift->_date ) },
+ 'otaker',
+ 'reason',
+ \&FS::UI::Web::cust_fields,
+ ;
+push @sort_fields, '_date', 'otaker', 'reason';
+$align .= 'rll'.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(),
+ ;
+
+my @search = ();
+
+if ( $cgi->param('usernum') =~ /^(\d+)$/ ) {
+ push @search, "cust_credit.usernum = $1";
+}
+
+if ( $cgi->param('agentnum') && $cgi->param('agentnum') =~ /^(\d+)$/ ) {
+ push @search, "agentnum = $1";
+ my $agent = qsearchs('agent', { 'agentnum' => $1 } );
+ die "unknown agentnum $1" unless $agent;
+ $title = $agent->agent. " $title";
+}
+
+if ( $cgi->param('refnum') && $cgi->param('refnum') =~ /^(\d+)$/ ) {
+ push @search, "refnum = $1";
+ my $part_referral = qsearchs('part_referral', { 'refnum' => $1 } );
+ die "unknown refnum $1" unless $part_referral;
+ $title = $part_referral->referral. " $title";
+}
+
+
+# cust_classnum (false laziness w/ elements/cust_main_dayranges.html, elements/cust_pay_or_refund.html, prepaid_income.html, cust_bill_pay.html, cust_bill_pkg.html, cust_bill_pkg_referral.html, unearned_detail.html, cust_credit_refund.html, cust_main::Search::search_sql)
+if ( grep { $_ eq 'cust_classnum' } $cgi->param ) {
+ my @classnums = grep /^\d*$/, $cgi->param('cust_classnum');
+ push @search, 'COALESCE( cust_main.classnum, 0) IN ( '.
+ join(',', map { $_ || '0' } @classnums ).
+ ' )'
+ if @classnums;
+}
+
+if ( $unapplied ) {
+ push @search, FS::cust_credit->unapplied_sql . ' > 0';
+}
+
+my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
+push @search, "_date >= $beginning ",
+ "_date <= $ending";
+
+push @search, FS::UI::Web::parse_lt_gt($cgi, 'amount' );
+
+#here is the agent virtualization
+push @search, $FS::CurrentUser::CurrentUser->agentnums_sql;
+
+my @select = (
+ 'cust_credit.*',
+ 'cust_main.custnum as cust_main_custnum',
+ FS::UI::Web::cust_sql_fields(),
+);
+
+if ( $unapplied ) {
+ push @select, '('.FS::cust_credit->unapplied_sql .') AS unapplied_amount';
+ push @search, FS::cust_credit->unapplied_sql .' > 0';
+}
+
+my $where = 'WHERE '. join(' AND ', @search);
+
+my $count_query = 'SELECT COUNT(*), SUM(amount) ';
+$count_query .= ', SUM(' . FS::cust_credit->unapplied_sql . ') ' if $unapplied;
+$count_query .= 'FROM cust_credit'. FS::UI::Web::join_cust_main('cust_credit').
+ $where;
+
+my @count_addl = ( $money_char.'%.2f total credited (gross)' );
+push @count_addl, $money_char.'%.2f unapplied' if $unapplied;
+
+my $sql_query = {
+ 'table' => 'cust_credit',
+ 'select' => join(', ',@select),
+ 'hashref' => {},
+ 'extra_sql' => $where,
+ 'addl_from' => FS::UI::Web::join_cust_main('cust_credit')
+};
+
+</%init>