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: crednum
47 push @header, emt('Ref No');
48 push @fields, sub { shift->crednum };
49 push @sort_fields, '';
55 # Report Column: Amount
56 push @header, emt('Amount');
57 push @fields, sub { $money_char .sprintf('%.2f', shift->amount) };
58 push @sort_fields, 'amount';
64 # Report Column: Unapplied Amount
66 push @header, emt('Unapplied');
67 push @fields, sub { $money_char .sprintf('%.2f', shift->unapplied_amount) };
68 push @sort_fields, '';
75 # Report Columns: Date, By, Reason, Info
76 push @header, emt('Date'),
81 push @fields, sub { time2str('%b %d %Y', shift->_date ) },
86 push @sort_fields, '_date', 'otaker', 'reasonnum', 'addlinfo';
104 # insert customer email after 'Reason' if this is a commission report
105 if ( $cgi->param('commission_salesnum') or $cgi->param('commission_agentnum') ) {
106 my $sub_customer_email = sub {
107 my $cust_credit = shift;
108 my $cust_event = $cust_credit->cust_event or return '';
109 my $cust_X = $cust_event->cust_X or return '';
110 my $cust_main = $cust_X->cust_main or return '';
111 my ($email) = $cust_main->invoicing_list_emailonly;
112 return encode_entities("<$email>") if length($email);
116 push @fields, $sub_customer_email;
117 push @sort_fields, '';
125 push @header, FS::UI::Web::cust_header();
126 push @fields, \&FS::UI::Web::cust_fields;
127 push @sort_fields, FS::UI::Web::cust_sort_fields;
128 $align .= FS::UI::Web::cust_aligns();
129 push @links, map { $_ ne 'Cust. Status' ? $clink : '' }
130 FS::UI::Web::cust_header();
131 push @color, FS::UI::Web::cust_colors();
132 push @style, FS::UI::Web::cust_styles();
134 if ( $show_voided_credits ) {
136 # Report Column: Void By:
137 push @header, emt('Void By');
140 return $rec->void_username
141 if $rec->isa('FS::cust_credit_void');
144 push @sort_fields, '';
150 # Report Column: Void Date:
151 push @header, emt('Void Date');
154 return time2str('%b %d %Y', $rec->void_date )
155 if $rec->isa('FS::cust_credit_void');
158 push @sort_fields, '';
164 # Report Column: Void Reason:
165 push @header, emt('Void Reason');
168 return $rec->void_reason_text
169 if $rec->isa('FS::cust_credit_void');
172 push @sort_fields, '';
183 if ( $cgi->param('usernum') =~ /^(\d+)$/ ) {
184 push @search, "cust_credit.usernum = $1";
187 if ( $cgi->param('agentnum') && $cgi->param('agentnum') =~ /^(\d+)$/ ) {
188 push @search, "agentnum = $1";
189 my $agent = qsearchs('agent', { 'agentnum' => $1 } );
190 die "unknown agentnum $1" unless $agent;
191 $title = $agent->agent. " $title";
194 if ( $cgi->param('credbatch') =~ /^([\w\-\/\.\:]+)$/ ) {
195 push @search, "cust_credit.credbatch = '$1'";
198 # commission_salesnum
199 if ( $cgi->param('commission_salesnum') =~ /^(\d+)$/ ) {
200 push @search, "commission_salesnum = $1";
203 # commission agentnum
204 if ( $cgi->param('commission_agentnum') =~ /^(\d+)$/ ) {
205 push @search, "commission_agentnum = $1";
208 # commission_classnum
209 if ( grep { $_ eq 'commission_classnum' } $cgi->param ) {
210 $cgi->param('commission_classnum') =~ /^(\d*)$/ or die 'guru meditation #13';
212 push @search, "part_pkg.classnum ". ( $classnum ? " = $classnum"
216 ' LEFT JOIN cust_pkg ON ( commission_pkgnum = cust_pkg.pkgnum ) '.
217 ' LEFT JOIN part_pkg USING ( pkgpart ) ';
220 if ( $cgi->param('refnum') && $cgi->param('refnum') =~ /^(\d+)$/ ) {
221 push @search, "refnum = $1";
222 my $part_referral = qsearchs('part_referral', { 'refnum' => $1 } );
223 die "unknown refnum $1" unless $part_referral;
224 $title = $part_referral->referral. " $title";
228 # 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)
229 if ( grep { $_ eq 'cust_classnum' } $cgi->param ) {
230 my @classnums = grep /^\d*$/, $cgi->param('cust_classnum');
231 push @search, 'COALESCE( cust_main.classnum, 0) IN ( '.
232 join(',', map { $_ || '0' } @classnums ).
238 push @search, FS::cust_credit->unapplied_sql . ' > 0';
241 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
242 push @search, "_date >= $beginning ",
245 push @search, FS::UI::Web::parse_lt_gt($cgi, 'amount' );
247 # Agent virtualization
248 push @search, $FS::CurrentUser::CurrentUser->agentnums_sql(table=>'cust_main');
251 "'cust_credit' as __classname",
252 qw(cust_credit.crednum
260 cust_credit.reasonnum
265 cust_credit.commission_agentnum
266 cust_credit.commission_salesnum
267 cust_credit.commission_pkgnum
268 cust_credit.commission_invnum
269 cust_credit.credbatch
272 'Null as void_usernum',
273 'Null as void_reasonnum',
274 'Null as void_reason',
275 'Null as void_reason_text',
276 'Null as void_username',
277 'cust_main.custnum as cust_main_custnum',
278 FS::UI::Web::cust_sql_fields(),
281 "'cust_credit_void' as __classname",
282 qw(cust_credit_void.crednum
283 cust_credit_void.custnum
284 cust_credit_void._date
285 cust_credit_void.amount
286 cust_credit_void.currency
287 cust_credit_void.otaker
288 cust_credit_void.usernum
289 cust_credit_void.reason
290 cust_credit_void.reasonnum
291 cust_credit_void.addlinfo
292 cust_credit_void.closed
293 cust_credit_void.pkgnum
294 cust_credit_void.eventnum
295 cust_credit_void.commission_agentnum
296 cust_credit_void.commission_salesnum
297 cust_credit_void.commission_pkgnum
298 cust_credit_void.commission_invnum
301 qw(cust_credit_void.void_date
302 cust_credit_void.void_usernum
303 cust_credit_void.void_reasonnum
304 cust_credit_void.void_reason
306 'reason.reason as void_reason_text',
307 'vusers.username as void_username',
308 'cust_main.custnum as cust_main_custnum',
309 FS::UI::Web::cust_sql_fields(),
313 push @select, '('.FS::cust_credit->unapplied_sql .') AS unapplied_amount';
314 push @search, FS::cust_credit->unapplied_sql .' > 0';
317 my $where = 'WHERE '. join(' AND ', @search);
319 my $count_query = 'SELECT COUNT(*), SUM(amount) ';
320 $count_query .= ', SUM(' . FS::cust_credit->unapplied_sql . ') ' if $unapplied;
321 $count_query .= 'FROM cust_credit'.
322 $addl_from. FS::UI::Web::join_cust_main('cust_credit').
325 my @count_addl = ( $money_char.'%.2f total credited (gross)' );
326 push @count_addl, $money_char.'%.2f unapplied' if $unapplied;
329 'table' => 'cust_credit',
330 'select' => join(', ',@select),
332 'extra_sql' => $where,
333 'addl_from' => $addl_from. FS::UI::Web::join_cust_main('cust_credit')
336 # Join to get reason text and void username to avoid two extra query per row
337 my $addl_from_void = join(' ',
339 FS::UI::Web::join_cust_main('cust_credit_void'),
340 ' LEFT JOIN reason ON (reason.reasonnum = cust_credit_void.void_reasonnum) ',
341 ' LEFT JOIN access_user as vusers '.
342 'on (vusers.usernum = cust_credit_void.void_usernum) ',
345 my $where_void = $where;
346 $where_void =~ s/cust_credit/cust_credit_void/g;
348 my $sql_query_void = {
349 'table' => 'cust_credit_void',
350 'select' => join(', ',@select_void),
352 'extra_sql' => $where_void,
353 'addl_from' => $addl_from_void,
356 if ($show_voided_credits) {
358 $sql_query = [$sql_query, $sql_query_void];
360 my $count_cust_credit;
361 my $count_cust_credit_void;
364 # Expected fields for count query are count, sum
365 # Get those totals here, and send a fake count query
366 my $count_row = qsearchs({
367 table => 'cust_credit',
368 select => 'count(*), sum(amount)',
370 addl_from => $addl_from . FS::UI::Web::join_cust_main('cust_credit'),
372 $count_cust_credit = $count_row->count || 0;
373 $count_sum = $count_row->sum || 0;
375 $count_row = qsearchs({
376 table => 'cust_credit_void',
377 select => 'count(*)',
378 extra_sql => $where_void,
379 addl_from => $addl_from_void,
381 $count_cust_credit_void = $count_row->count || 0;
383 my $count_combined = $count_cust_credit + $count_cust_credit_void;
385 # Fake count query providing needed values
386 $count_query = "SELECT $count_combined as count, $count_sum as sum";