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 selectbox for show_voided_credits:
37 # - value='': use default from $conf
38 # - value="0" : override default, do not show voided credits
39 # - value="1" : override default, show voided credits
40 my $show_voided_credits;
41 $show_voided_credits = $conf->config('show_voided_credits');
42 $show_voided_credits = $cgi->param('show_voided_credits')
43 if $cgi->param('show_voided_credits') =~ /^(\d)$/;
45 my (@header, @fields, @sort_fields, $align, @links, @color, @style);
48 # Report Column: Amount
49 push @header, emt('Amount');
50 push @fields, sub { $money_char .sprintf('%.2f', shift->amount) };
51 push @sort_fields, 'amount';
57 # Report Column: Unapplied Amount
59 push @header, emt('Unapplied');
60 push @fields, sub { $money_char .sprintf('%.2f', shift->unapplied_amount) };
61 push @sort_fields, '';
68 # Report Columns: Date, By, Reason, Info
69 push @header, emt('Date'),
74 push @fields, sub { time2str('%b %d %Y', shift->_date ) },
79 push @sort_fields, '_date', 'otaker', 'reasonnum', 'addlinfo';
97 # insert customer email after 'Reason' if this is a commission report
98 if ( $cgi->param('commission_salesnum') or $cgi->param('commission_agentnum') ) {
99 my $sub_customer_email = sub {
100 my $cust_credit = shift;
101 my $cust_event = $cust_credit->cust_event or return '';
102 my $cust_X = $cust_event->cust_X or return '';
103 my $cust_main = $cust_X->cust_main or return '';
104 my ($email) = $cust_main->invoicing_list_emailonly;
105 return encode_entities("<$email>") if length($email);
109 push @fields, $sub_customer_email;
110 push @sort_fields, '';
118 push @header, FS::UI::Web::cust_header();
119 push @fields, \&FS::UI::Web::cust_fields;
120 push @sort_fields, FS::UI::Web::cust_sort_fields;
121 $align .= FS::UI::Web::cust_aligns();
122 push @links, map { $_ ne 'Cust. Status' ? $clink : '' }
123 FS::UI::Web::cust_header();
124 push @color, FS::UI::Web::cust_colors();
125 push @style, FS::UI::Web::cust_styles();
127 if ( $show_voided_credits ) {
129 # Report Column: Void By:
130 push @header, emt('Void By');
133 return $rec->void_username
134 if $rec->isa('FS::cust_credit_void');
137 push @sort_fields, '';
143 # Report Column: Void Date:
144 push @header, emt('Void Date');
147 return time2str('%b %d %Y', $rec->void_date )
148 if $rec->isa('FS::cust_credit_void');
151 push @sort_fields, '';
157 # Report Column: Void Reason:
158 push @header, emt('Void Reason');
161 return $rec->void_reason_text
162 if $rec->isa('FS::cust_credit_void');
165 push @sort_fields, '';
176 if ( $cgi->param('usernum') =~ /^(\d+)$/ ) {
177 push @search, "cust_credit.usernum = $1";
180 if ( $cgi->param('agentnum') && $cgi->param('agentnum') =~ /^(\d+)$/ ) {
181 push @search, "agentnum = $1";
182 my $agent = qsearchs('agent', { 'agentnum' => $1 } );
183 die "unknown agentnum $1" unless $agent;
184 $title = $agent->agent. " $title";
187 if ( $cgi->param('credbatch') =~ /^([\w\-\/\.\:]+)$/ ) {
188 push @search, "cust_credit.credbatch = '$1'";
191 # commission_salesnum
192 if ( $cgi->param('commission_salesnum') =~ /^(\d+)$/ ) {
193 push @search, "commission_salesnum = $1";
196 # commission agentnum
197 if ( $cgi->param('commission_agentnum') =~ /^(\d+)$/ ) {
198 push @search, "commission_agentnum = $1";
201 # commission_classnum
202 if ( grep { $_ eq 'commission_classnum' } $cgi->param ) {
203 $cgi->param('commission_classnum') =~ /^(\d*)$/ or die 'guru meditation #13';
205 push @search, "part_pkg.classnum ". ( $classnum ? " = $classnum"
209 ' LEFT JOIN cust_pkg ON ( commission_pkgnum = cust_pkg.pkgnum ) '.
210 ' LEFT JOIN part_pkg USING ( pkgpart ) ';
213 if ( $cgi->param('refnum') && $cgi->param('refnum') =~ /^(\d+)$/ ) {
214 push @search, "refnum = $1";
215 my $part_referral = qsearchs('part_referral', { 'refnum' => $1 } );
216 die "unknown refnum $1" unless $part_referral;
217 $title = $part_referral->referral. " $title";
221 # 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)
222 if ( grep { $_ eq 'cust_classnum' } $cgi->param ) {
223 my @classnums = grep /^\d*$/, $cgi->param('cust_classnum');
224 push @search, 'COALESCE( cust_main.classnum, 0) IN ( '.
225 join(',', map { $_ || '0' } @classnums ).
231 push @search, FS::cust_credit->unapplied_sql . ' > 0';
234 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
235 push @search, "_date >= $beginning ",
238 push @search, FS::UI::Web::parse_lt_gt($cgi, 'amount' );
240 # Agent virtualization
241 push @search, $FS::CurrentUser::CurrentUser->agentnums_sql(table=>'cust_main');
244 "'cust_credit' as __classname",
245 qw(cust_credit.crednum
253 cust_credit.reasonnum
258 cust_credit.commission_agentnum
259 cust_credit.commission_salesnum
260 cust_credit.commission_pkgnum
261 cust_credit.commission_invnum
262 cust_credit.credbatch
265 'Null as void_usernum',
266 'Null as void_reasonnum',
267 'Null as void_reason',
268 'Null as void_reason_text',
269 'Null as void_username',
270 'cust_main.custnum as cust_main_custnum',
271 FS::UI::Web::cust_sql_fields(),
274 "'cust_credit_void' as __classname",
275 qw(cust_credit_void.crednum
276 cust_credit_void.custnum
277 cust_credit_void._date
278 cust_credit_void.amount
279 cust_credit_void.currency
280 cust_credit_void.otaker
281 cust_credit_void.usernum
282 cust_credit_void.reason
283 cust_credit_void.reasonnum
284 cust_credit_void.addlinfo
285 cust_credit_void.closed
286 cust_credit_void.pkgnum
287 cust_credit_void.eventnum
288 cust_credit_void.commission_agentnum
289 cust_credit_void.commission_salesnum
290 cust_credit_void.commission_pkgnum
291 cust_credit_void.commission_invnum
294 qw(cust_credit_void.void_date
295 cust_credit_void.void_usernum
296 cust_credit_void.void_reasonnum
297 cust_credit_void.void_reason
299 'reason.reason as void_reason_text',
300 'vusers.username as void_username',
301 'cust_main.custnum as cust_main_custnum',
302 FS::UI::Web::cust_sql_fields(),
306 push @select, '('.FS::cust_credit->unapplied_sql .') AS unapplied_amount';
307 push @search, FS::cust_credit->unapplied_sql .' > 0';
310 my $where = 'WHERE '. join(' AND ', @search);
312 my $count_query = 'SELECT COUNT(*), SUM(amount) ';
313 $count_query .= ', SUM(' . FS::cust_credit->unapplied_sql . ') ' if $unapplied;
314 $count_query .= 'FROM cust_credit'.
315 $addl_from. FS::UI::Web::join_cust_main('cust_credit').
318 my @count_addl = ( $money_char.'%.2f total credited (gross)' );
319 push @count_addl, $money_char.'%.2f unapplied' if $unapplied;
322 'table' => 'cust_credit',
323 'select' => join(', ',@select),
325 'extra_sql' => $where,
326 'addl_from' => $addl_from. FS::UI::Web::join_cust_main('cust_credit')
329 # Join to get reason text and void username to avoid two extra query per row
330 my $addl_from_void = join(' ',
332 FS::UI::Web::join_cust_main('cust_credit_void'),
333 ' LEFT JOIN reason ON (reason.reasonnum = cust_credit_void.void_reasonnum) ',
334 ' LEFT JOIN access_user as vusers '.
335 'on (vusers.usernum = cust_credit_void.void_usernum) ',
338 my $where_void = $where;
339 $where_void =~ s/cust_credit/cust_credit_void/g;
341 my $sql_query_void = {
342 'table' => 'cust_credit_void',
343 'select' => join(', ',@select_void),
345 'extra_sql' => $where_void,
346 'addl_from' => $addl_from_void,
349 if ($show_voided_credits) {
351 $sql_query = [$sql_query, $sql_query_void];
353 my $count_cust_credit;
354 my $count_cust_credit_void;
357 # Expected fields for count query are count, sum
358 # Get those totals here, and send a fake count query
359 my $count_row = qsearchs({
360 table => 'cust_credit',
361 select => 'count(*), sum(amount)',
363 addl_from => $addl_from . FS::UI::Web::join_cust_main('cust_credit'),
365 $count_cust_credit = $count_row->count || 0;
366 $count_sum = $count_row->sum || 0;
368 $count_row = qsearchs({
369 table => 'cust_credit_void',
370 select => 'count(*)',
371 extra_sql => $where_void,
372 addl_from => $addl_from_void,
374 $count_cust_credit_void = $count_row->count || 0;
376 my $count_combined = $count_cust_credit + $count_cust_credit_void;
378 # Fake count query providing needed values
379 $count_query = "SELECT $count_combined as count, $count_sum as sum";