1 <% include( 'elements/search.html',
5 'count_query' => $count_query,
6 'count_addl' => \@count_addl,
19 unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
21 my $money_char = FS::Conf->new->config('money_char') || '$';
23 my $title = 'Credit Search Results';
24 #my( $count_query, $sql_query );
26 my $unapplied = $cgi->param('unapplied');
27 $title = "Unapplied $title" if $unapplied;
29 my $cust_bill = shift;
30 $cust_bill->cust_main_custnum
31 ? [ "${p}view/cust_main.cgi?", 'custnum' ]
35 my (@header, @fields, $align, @links, @color, @style);
39 push @header, 'Amount';
40 push @fields, sub { $money_char .sprintf('%.2f', shift->amount) };
48 push @header, 'Unapplied';
49 push @fields, sub { $money_char .sprintf('%.2f', shift->unapplied_amount) };
59 FS::UI::Web::cust_header(),
61 push @fields, sub { time2str('%b %d %Y', shift->_date ) },
64 \&FS::UI::Web::cust_fields,
66 $align .= 'rll'.FS::UI::Web::cust_aligns(),
70 ( map { $_ ne 'Cust. Status' ? $clink : '' }
71 FS::UI::Web::cust_header()
77 FS::UI::Web::cust_colors(),
82 FS::UI::Web::cust_styles(),
87 if ( $cgi->param('usernum') =~ /^(\d+)$/ ) {
88 push @search, "cust_credit.usernum = $1";
91 if ( $cgi->param('agentnum') && $cgi->param('agentnum') =~ /^(\d+)$/ ) {
92 push @search, "agentnum = $1";
93 my $agent = qsearchs('agent', { 'agentnum' => $1 } );
94 die "unknown agentnum $1" unless $agent;
95 $title = $agent->agent. " $title";
99 push @search, FS::cust_credit->unapplied_sql . ' > 0';
102 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
103 push @search, "_date >= $beginning ",
106 push @search, FS::UI::Web::parse_lt_gt($cgi, 'amount' );
108 #here is the agent virtualization
109 push @search, $FS::CurrentUser::CurrentUser->agentnums_sql;
113 'cust_main.custnum as cust_main_custnum',
114 FS::UI::Web::cust_sql_fields(),
118 push @select, '('.FS::cust_credit->unapplied_sql .') AS unapplied_amount';
119 push @search, FS::cust_credit->unapplied_sql .' > 0';
122 my $where = 'WHERE '. join(' AND ', @search);
124 my $count_query = 'SELECT COUNT(*), SUM(amount) ';
125 $count_query .= ', SUM(' . FS::cust_credit->unapplied_sql . ') ' if $unapplied;
126 $count_query .= 'FROM cust_credit LEFT JOIN cust_main USING ( custnum ) '.
129 my @count_addl = ( $money_char.'%.2f total credited (gross)' );
130 push @count_addl, $money_char.'%.2f unapplied' if $unapplied;
133 'table' => 'cust_credit',
134 'select' => join(', ',@select),
136 'extra_sql' => $where,
137 'addl_from' => 'LEFT JOIN cust_main USING ( custnum )',