1 <& elements/search.html,
3 'name' => emt('credits'),
5 'count_query' => $count_query,
6 'count_addl' => \@count_addl,
9 'sort_fields' => \@sort_fields,
18 unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
20 my $money_char = FS::Conf->new->config('money_char') || '$';
22 my $title = emt('Credit Search Results');
24 my $unapplied = $cgi->param('unapplied');
25 $title = emt("Unapplied $title") if $unapplied;
27 my $cust_bill = shift;
28 $cust_bill->cust_main_custnum
29 ? [ "${p}view/cust_main.cgi?", 'custnum' ]
33 my (@header, @fields, @sort_fields, $align, @links, @color, @style);
37 push @header, emt('Amount');
38 push @fields, sub { $money_char .sprintf('%.2f', shift->amount) };
39 push @sort_fields, 'amount';
47 push @header, emt('Unapplied');
48 push @fields, sub { $money_char .sprintf('%.2f', shift->unapplied_amount) };
49 push @sort_fields, '';
56 push @header, emt('Date'),
60 push @fields, sub { time2str('%b %d %Y', shift->_date ) },
64 push @sort_fields, '_date', 'otaker', 'reason';
79 # insert customer email after 'Reason' if this is a commission report
80 if ( $cgi->param('commission_salesnum') or $cgi->param('commission_agentnum') ) {
81 my $sub_customer_email = sub {
82 my $cust_credit = shift;
83 return '' unless $cust_credit->eventnum;
84 my $cust_event = FS::cust_event->by_key($cust_credit->eventnum)
86 my $cust_X = $cust_event->cust_X or return '';
87 my $cust_main = $cust_X->cust_main or return '';
88 my ($email) = $cust_main->invoicing_list_emailonly;
89 return encode_entities("<$email>") if length($email);
93 push @fields, $sub_customer_email;
94 push @sort_fields, '';
102 push @header, FS::UI::Web::cust_header();
103 push @fields, \&FS::UI::Web::cust_fields;
104 push @sort_fields, FS::UI::Web::cust_sort_fields;
105 $align .= FS::UI::Web::cust_aligns();
106 push @links, map { $_ ne 'Cust. Status' ? $clink : '' }
107 FS::UI::Web::cust_header();
108 push @color, FS::UI::Web::cust_colors();
109 push @style, FS::UI::Web::cust_styles();
115 if ( $cgi->param('usernum') =~ /^(\d+)$/ ) {
116 push @search, "cust_credit.usernum = $1";
119 if ( $cgi->param('agentnum') && $cgi->param('agentnum') =~ /^(\d+)$/ ) {
120 push @search, "agentnum = $1";
121 my $agent = qsearchs('agent', { 'agentnum' => $1 } );
122 die "unknown agentnum $1" unless $agent;
123 $title = $agent->agent. " $title";
126 if ( $cgi->param('credbatch') =~ /^([\w\-\/\.\:]+)$/ ) {
127 push @search, "cust_credit.credbatch = '$1'";
130 # commission_salesnum
131 if ( $cgi->param('commission_salesnum') =~ /^(\d+)$/ ) {
132 push @search, "commission_salesnum = $1";
135 # commission agentnum
136 if ( $cgi->param('commission_agentnum') =~ /^(\d+)$/ ) {
137 push @search, "commission_agentnum = $1";
140 # commission_classnum
141 if ( grep { $_ eq 'commission_classnum' } $cgi->param ) {
142 $cgi->param('commission_classnum') =~ /^(\d*)$/ or die 'guru meditation #13';
144 push @search, "part_pkg.classnum ". ( $classnum ? " = $classnum"
148 ' LEFT JOIN cust_pkg ON ( commission_pkgnum = cust_pkg.pkgnum ) '.
149 ' LEFT JOIN part_pkg USING ( pkgpart ) ';
152 if ( $cgi->param('refnum') && $cgi->param('refnum') =~ /^(\d+)$/ ) {
153 push @search, "refnum = $1";
154 my $part_referral = qsearchs('part_referral', { 'refnum' => $1 } );
155 die "unknown refnum $1" unless $part_referral;
156 $title = $part_referral->referral. " $title";
160 # 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)
161 if ( grep { $_ eq 'cust_classnum' } $cgi->param ) {
162 my @classnums = grep /^\d*$/, $cgi->param('cust_classnum');
163 push @search, 'COALESCE( cust_main.classnum, 0) IN ( '.
164 join(',', map { $_ || '0' } @classnums ).
170 push @search, FS::cust_credit->unapplied_sql . ' > 0';
173 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
174 push @search, "_date >= $beginning ",
177 push @search, FS::UI::Web::parse_lt_gt($cgi, 'amount' );
179 #here is the agent virtualization
180 push @search, $FS::CurrentUser::CurrentUser->agentnums_sql(table=>'cust_main');
184 'cust_main.custnum as cust_main_custnum',
185 FS::UI::Web::cust_sql_fields(),
189 push @select, '('.FS::cust_credit->unapplied_sql .') AS unapplied_amount';
190 push @search, FS::cust_credit->unapplied_sql .' > 0';
193 my $where = 'WHERE '. join(' AND ', @search);
195 my $count_query = 'SELECT COUNT(*), SUM(amount) ';
196 $count_query .= ', SUM(' . FS::cust_credit->unapplied_sql . ') ' if $unapplied;
197 $count_query .= 'FROM cust_credit'.
198 $addl_from. FS::UI::Web::join_cust_main('cust_credit').
201 my @count_addl = ( $money_char.'%.2f total credited (gross)' );
202 push @count_addl, $money_char.'%.2f unapplied' if $unapplied;
205 'table' => 'cust_credit',
206 'select' => join(', ',@select),
208 'extra_sql' => $where,
209 'addl_from' => $addl_from. FS::UI::Web::join_cust_main('cust_credit')