1 <% include( 'elements/search.html',
5 'count_query' => $count_query,
6 'count_addl' => \@count_addl,
10 'sort_fields' => \@sort_fields,
20 unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
22 my $money_char = FS::Conf->new->config('money_char') || '$';
24 my $title = 'Credit Search Results';
25 #my( $count_query, $sql_query );
27 my $unapplied = $cgi->param('unapplied');
28 $title = "Unapplied $title" if $unapplied;
30 my $cust_bill = shift;
31 $cust_bill->cust_main_custnum
32 ? [ "${p}view/cust_main.cgi?", 'custnum' ]
36 my (@header, @fields, @sort_fields, $align, @links, @color, @style);
40 push @header, 'Amount';
41 push @fields, sub { $money_char .sprintf('%.2f', shift->amount) };
42 push @sort_fields, 'amount';
50 push @header, 'Unapplied';
51 push @fields, sub { $money_char .sprintf('%.2f', shift->unapplied_amount) };
52 push @sort_fields, '';
62 FS::UI::Web::cust_header(),
64 push @fields, sub { time2str('%b %d %Y', shift->_date ) },
67 \&FS::UI::Web::cust_fields,
69 push @sort_fields, '_date', 'otaker', 'reason';
70 $align .= 'rll'.FS::UI::Web::cust_aligns(),
74 ( map { $_ ne 'Cust. Status' ? $clink : '' }
75 FS::UI::Web::cust_header()
81 FS::UI::Web::cust_colors(),
86 FS::UI::Web::cust_styles(),
91 if ( $cgi->param('usernum') =~ /^(\d+)$/ ) {
92 push @search, "cust_credit.usernum = $1";
95 if ( $cgi->param('agentnum') && $cgi->param('agentnum') =~ /^(\d+)$/ ) {
96 push @search, "agentnum = $1";
97 my $agent = qsearchs('agent', { 'agentnum' => $1 } );
98 die "unknown agentnum $1" unless $agent;
99 $title = $agent->agent. " $title";
103 push @search, FS::cust_credit->unapplied_sql . ' > 0';
106 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
107 push @search, "_date >= $beginning ",
110 push @search, FS::UI::Web::parse_lt_gt($cgi, 'amount' );
112 #here is the agent virtualization
113 push @search, $FS::CurrentUser::CurrentUser->agentnums_sql;
117 'cust_main.custnum as cust_main_custnum',
118 FS::UI::Web::cust_sql_fields(),
122 push @select, '('.FS::cust_credit->unapplied_sql .') AS unapplied_amount';
123 push @search, FS::cust_credit->unapplied_sql .' > 0';
126 my $where = 'WHERE '. join(' AND ', @search);
128 my $count_query = 'SELECT COUNT(*), SUM(amount) ';
129 $count_query .= ', SUM(' . FS::cust_credit->unapplied_sql . ') ' if $unapplied;
130 $count_query .= 'FROM cust_credit LEFT JOIN cust_main USING ( custnum ) '.
133 my @count_addl = ( $money_char.'%.2f total credited (gross)' );
134 push @count_addl, $money_char.'%.2f unapplied' if $unapplied;
137 'table' => 'cust_credit',
138 'select' => join(', ',@select),
140 'extra_sql' => $where,
141 'addl_from' => 'LEFT JOIN cust_main USING ( custnum )',