1 <& elements/search.html,
3 'name' => emt('credits'),
5 'count_query' => $count_query,
6 'count_addl' => \@count_addl,
9 'sort_fields' => \@sort_fields,
14 'classname_from_column' => 1,
19 unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
21 my $conf = new FS::Conf;
23 my $money_char = $conf->config('money_char') || '$';
25 my $title = emt('Credit Search Results');
27 my $unapplied = $cgi->param('unapplied');
28 $title = emt("Unapplied $title") if $unapplied;
30 my $cust_bill = shift;
31 $cust_bill->cust_main_custnum
32 ? [ "${p}view/cust_main.cgi?", 'custnum' ]
36 # form checkbox for show_voided_credits:
37 # - value=unset : do not show voided credits
38 # - value="1" : show voided credits
39 my $show_voided_credits;
40 $show_voided_credits = $cgi->param('show_voided_credits')
41 if $cgi->param('show_voided_credits');
43 my (@header, @fields, @sort_fields, $align, @links, @color, @style);
46 # Report Column: Amount
47 push @header, emt('Amount');
48 push @fields, sub { $money_char .sprintf('%.2f', shift->amount) };
49 push @sort_fields, 'amount';
55 # Report Column: Unapplied Amount
57 push @header, emt('Unapplied');
58 push @fields, sub { $money_char .sprintf('%.2f', shift->unapplied_amount) };
59 push @sort_fields, '';
66 # Report Columns: Date, By, Reason, Info
67 push @header, emt('Date'),
72 push @fields, sub { time2str('%b %d %Y', shift->_date ) },
77 push @sort_fields, '_date', 'otaker', 'reasonnum', 'addlinfo';
95 # insert customer email after 'Reason' if this is a commission report
96 if ( $cgi->param('commission_salesnum') or $cgi->param('commission_agentnum') ) {
97 my $sub_customer_email = sub {
98 my $cust_credit = shift;
99 my $cust_event = $cust_credit->cust_event or return '';
100 my $cust_X = $cust_event->cust_X or return '';
101 my $cust_main = $cust_X->cust_main or return '';
102 my ($email) = $cust_main->invoicing_list_emailonly;
103 return encode_entities("<$email>") if length($email);
107 push @fields, $sub_customer_email;
108 push @sort_fields, '';
116 push @header, FS::UI::Web::cust_header();
117 push @fields, \&FS::UI::Web::cust_fields;
118 push @sort_fields, FS::UI::Web::cust_sort_fields;
119 $align .= FS::UI::Web::cust_aligns();
120 push @links, map { $_ ne 'Cust. Status' ? $clink : '' }
121 FS::UI::Web::cust_header();
122 push @color, FS::UI::Web::cust_colors();
123 push @style, FS::UI::Web::cust_styles();
125 if ( $show_voided_credits ) {
127 # Report Column: Void By:
128 push @header, emt('Void By');
131 return $rec->void_username
132 if $rec->isa('FS::cust_credit_void');
135 push @sort_fields, '';
141 # Report Column: Void Date:
142 push @header, emt('Void Date');
145 return time2str('%b %d %Y', $rec->void_date )
146 if $rec->isa('FS::cust_credit_void');
149 push @sort_fields, '';
155 # Report Column: Void Reason:
156 push @header, emt('Void Reason');
159 return $rec->void_reason_text
160 if $rec->isa('FS::cust_credit_void');
163 push @sort_fields, '';
174 if ( $cgi->param('usernum') =~ /^(\d+)$/ ) {
175 push @search, "cust_credit.usernum = $1";
178 if ( $cgi->param('agentnum') && $cgi->param('agentnum') =~ /^(\d+)$/ ) {
179 push @search, "agentnum = $1";
180 my $agent = qsearchs('agent', { 'agentnum' => $1 } );
181 die "unknown agentnum $1" unless $agent;
182 $title = $agent->agent. " $title";
185 if ( $cgi->param('credbatch') =~ /^([\w\-\/\.\:]+)$/ ) {
186 push @search, "cust_credit.credbatch = '$1'";
189 # commission_salesnum
190 if ( $cgi->param('commission_salesnum') =~ /^(\d+)$/ ) {
191 push @search, "commission_salesnum = $1";
194 # commission agentnum
195 if ( $cgi->param('commission_agentnum') =~ /^(\d+)$/ ) {
196 push @search, "commission_agentnum = $1";
199 # commission_classnum
200 if ( grep { $_ eq 'commission_classnum' } $cgi->param ) {
201 $cgi->param('commission_classnum') =~ /^(\d*)$/ or die 'guru meditation #13';
203 push @search, "part_pkg.classnum ". ( $classnum ? " = $classnum"
207 ' LEFT JOIN cust_pkg ON ( commission_pkgnum = cust_pkg.pkgnum ) '.
208 ' LEFT JOIN part_pkg USING ( pkgpart ) ';
211 if ( $cgi->param('refnum') && $cgi->param('refnum') =~ /^(\d+)$/ ) {
212 push @search, "refnum = $1";
213 my $part_referral = qsearchs('part_referral', { 'refnum' => $1 } );
214 die "unknown refnum $1" unless $part_referral;
215 $title = $part_referral->referral. " $title";
219 # 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)
220 if ( grep { $_ eq 'cust_classnum' } $cgi->param ) {
221 my @classnums = grep /^\d*$/, $cgi->param('cust_classnum');
222 push @search, 'COALESCE( cust_main.classnum, 0) IN ( '.
223 join(',', map { $_ || '0' } @classnums ).
229 push @search, FS::cust_credit->unapplied_sql . ' > 0';
232 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
233 push @search, "_date >= $beginning ",
236 push @search, FS::UI::Web::parse_lt_gt($cgi, 'amount' );
238 # Agent virtualization
239 push @search, $FS::CurrentUser::CurrentUser->agentnums_sql(table=>'cust_main');
242 "'cust_credit' as __classname",
243 qw(cust_credit.crednum
251 cust_credit.reasonnum
256 cust_credit.commission_agentnum
257 cust_credit.commission_salesnum
258 cust_credit.commission_pkgnum
259 cust_credit.commission_invnum
260 cust_credit.credbatch
263 'Null as void_usernum',
264 'Null as void_reasonnum',
265 'Null as void_reason',
266 'Null as void_reason_text',
267 'Null as void_username',
268 'cust_main.custnum as cust_main_custnum',
269 FS::UI::Web::cust_sql_fields(),
272 "'cust_credit_void' as __classname",
273 qw(cust_credit_void.crednum
274 cust_credit_void.custnum
275 cust_credit_void._date
276 cust_credit_void.amount
277 cust_credit_void.currency
278 cust_credit_void.otaker
279 cust_credit_void.usernum
280 cust_credit_void.reason
281 cust_credit_void.reasonnum
282 cust_credit_void.addlinfo
283 cust_credit_void.closed
284 cust_credit_void.pkgnum
285 cust_credit_void.eventnum
286 cust_credit_void.commission_agentnum
287 cust_credit_void.commission_salesnum
288 cust_credit_void.commission_pkgnum
289 cust_credit_void.commission_invnum
292 qw(cust_credit_void.void_date
293 cust_credit_void.void_usernum
294 cust_credit_void.void_reasonnum
295 cust_credit_void.void_reason
297 'reason.reason as void_reason_text',
298 'vusers.username as void_username',
299 'cust_main.custnum as cust_main_custnum',
300 FS::UI::Web::cust_sql_fields(),
304 push @select, '('.FS::cust_credit->unapplied_sql .') AS unapplied_amount';
305 push @search, FS::cust_credit->unapplied_sql .' > 0';
308 my $where = 'WHERE '. join(' AND ', @search);
310 my $count_query = 'SELECT COUNT(*), SUM(amount) ';
311 $count_query .= ', SUM(' . FS::cust_credit->unapplied_sql . ') ' if $unapplied;
312 $count_query .= 'FROM cust_credit'.
313 $addl_from. FS::UI::Web::join_cust_main('cust_credit').
316 my @count_addl = ( $money_char.'%.2f total credited (gross)' );
317 push @count_addl, $money_char.'%.2f unapplied' if $unapplied;
320 'table' => 'cust_credit',
321 'select' => join(', ',@select),
323 'extra_sql' => $where,
324 'addl_from' => $addl_from. FS::UI::Web::join_cust_main('cust_credit')
327 # Join to get reason text and void username to avoid two extra query per row
328 my $addl_from_void = join(' ',
330 FS::UI::Web::join_cust_main('cust_credit_void'),
331 ' LEFT JOIN reason ON (reason.reasonnum = cust_credit_void.void_reasonnum) ',
332 ' LEFT JOIN access_user as vusers '.
333 'on (vusers.usernum = cust_credit_void.void_usernum) ',
336 my $where_void = $where;
337 $where_void =~ s/cust_credit/cust_credit_void/g;
339 my $sql_query_void = {
340 'table' => 'cust_credit_void',
341 'select' => join(', ',@select_void),
343 'extra_sql' => $where_void,
344 'addl_from' => $addl_from_void,
347 if ($show_voided_credits) {
349 $sql_query = [$sql_query, $sql_query_void];
351 my $count_cust_credit;
352 my $count_cust_credit_void;
355 # Expected fields for count query are count, sum
356 # Get those totals here, and send a fake count query
357 my $count_row = qsearchs({
358 table => 'cust_credit',
359 select => 'count(*), sum(amount)',
361 addl_from => $addl_from . FS::UI::Web::join_cust_main('cust_credit'),
363 $count_cust_credit = $count_row->count || 0;
364 $count_sum = $count_row->sum || 0;
366 $count_row = qsearchs({
367 table => 'cust_credit_void',
368 select => 'count(*)',
369 extra_sql => $where_void,
370 addl_from => $addl_from_void,
372 $count_cust_credit_void = $count_row->count || 0;
374 my $count_combined = $count_cust_credit + $count_cust_credit_void;
376 # Fake count query providing needed values
377 $count_query = "SELECT $count_combined as count, $count_sum as sum";