Change selectbox to checkbox on Credit Report form RT#73200
[freeside.git] / httemplate / search / cust_credit.html
1 <& elements/search.html,
2                  'title'       => $title,
3                  'name'        => emt('credits'),
4                  'query'       => $sql_query,
5                  'count_query' => $count_query,
6                  'count_addl'  => \@count_addl,
7                  'header'      => \@header,
8                  'fields'      => \@fields,
9                  'sort_fields' => \@sort_fields,
10                  'align' => $align,
11                  'links' => \@links,
12                  'color' => \@color,
13                  'style' => \@style,
14                  'classname_from_column' => 1,
15 &>
16 <%init>
17
18 die "access denied"
19   unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
20
21 my $conf = new FS::Conf;
22
23 my $money_char = $conf->config('money_char') || '$';
24
25 my $title = emt('Credit Search Results');
26
27 my $unapplied = $cgi->param('unapplied');
28 $title = emt("Unapplied $title") if $unapplied;
29 my $clink = sub {
30   my $cust_bill = shift;
31   $cust_bill->cust_main_custnum
32     ? [ "${p}view/cust_main.cgi?", 'custnum' ]
33     : '';
34 };
35
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');
42
43 my (@header, @fields, @sort_fields, $align, @links, @color, @style);
44 $align = '';
45
46 # Report Column: Amount
47 push @header, emt('Amount');
48 push @fields, sub { $money_char .sprintf('%.2f', shift->amount) };
49 push @sort_fields, 'amount';
50 $align .= 'r';
51 push @links, '';
52 push @color, '';
53 push @style, '';
54
55 # Report Column: Unapplied Amount
56 if ($unapplied) {
57   push @header, emt('Unapplied');
58   push @fields, sub { $money_char .sprintf('%.2f', shift->unapplied_amount) };
59   push @sort_fields, '';
60   $align .= 'r';
61   push @links, '';
62   push @color, '';
63   push @style, '';
64 }
65
66 # Report Columns: Date, By, Reason, Info
67 push @header, emt('Date'),
68               emt('By'),
69               emt('Reason'),
70               emt('Info'),
71               ;
72 push @fields, sub { time2str('%b %d %Y', shift->_date ) },
73               'otaker',
74               'reason_only',
75               'addlinfo',
76               ;
77 push @sort_fields, '_date', 'otaker', 'reasonnum', 'addlinfo';
78 $align .= 'rlll';
79 push @links,  '',
80               '',
81               '',
82               '',
83               ;
84 push @color,  '',
85               '',
86               '',
87               '',
88               ;
89 push @style,  '',
90               '',
91               '',
92               '',
93               ;
94
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);
104     '';
105   };
106   push @header, '';
107   push @fields, $sub_customer_email;
108   push @sort_fields, '';
109   $align .= 'l';
110   push @links, '';
111   push @color, '';
112   push @style, '';
113 }
114
115 # cust 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();
124
125 if ( $show_voided_credits ) {
126
127   # Report Column: Void By:
128   push @header, emt('Void By');
129   push @fields, sub {
130     my $rec = shift;
131     return $rec->void_username
132       if $rec->isa('FS::cust_credit_void');
133     return '';
134   };
135   push @sort_fields, '';
136   $align .= 'l';
137   push @links, '';
138   push @color, '';
139   push @style, '';
140
141   # Report Column: Void Date:
142   push @header, emt('Void Date');
143   push @fields, sub {
144     my $rec = shift;
145     return time2str('%b %d %Y', $rec->void_date )
146       if $rec->isa('FS::cust_credit_void');
147     return '';
148   };
149   push @sort_fields, '';
150   $align .= 'l';
151   push @links, '';
152   push @color, '';
153   push @style, '';
154
155   # Report Column: Void Reason:
156   push @header, emt('Void Reason');
157   push @fields, sub {
158     my $rec = shift;
159     return $rec->void_reason_text
160       if $rec->isa('FS::cust_credit_void');
161     return '';
162   };
163   push @sort_fields, '';
164   $align .= 'l';
165   push @links, '';
166   push @color, '';
167   push @style, '';
168 }
169
170
171 my @search = ();
172 my $addl_from = '';
173
174 if ( $cgi->param('usernum') =~ /^(\d+)$/ ) {
175   push @search, "cust_credit.usernum = $1";
176 }
177
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";
183 }
184
185 if ( $cgi->param('credbatch') =~ /^([\w\-\/\.\:]+)$/ ) {
186   push @search, "cust_credit.credbatch = '$1'";
187 }
188
189 # commission_salesnum
190 if ( $cgi->param('commission_salesnum') =~ /^(\d+)$/ ) {
191   push @search, "commission_salesnum = $1";
192 }
193
194 # commission agentnum
195 if ( $cgi->param('commission_agentnum') =~ /^(\d+)$/ ) {
196   push @search, "commission_agentnum = $1";
197 }
198
199 # commission_classnum
200 if ( grep { $_ eq 'commission_classnum' } $cgi->param ) {
201   $cgi->param('commission_classnum') =~ /^(\d*)$/ or die 'guru meditation #13';
202   my $classnum = $1;
203   push @search, "part_pkg.classnum ". ( $classnum ? " = $classnum"
204                                                  : " IS NULL "    );
205
206   $addl_from .=
207     ' LEFT JOIN cust_pkg ON ( commission_pkgnum = cust_pkg.pkgnum ) '.
208     ' LEFT JOIN part_pkg USING ( pkgpart ) ';
209 }
210
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";
216 }
217
218
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 ).
224                 ' )'
225     if @classnums;
226 }
227
228 if ( $unapplied ) {
229   push @search, FS::cust_credit->unapplied_sql . ' > 0';
230 }
231
232 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
233 push @search, "_date >= $beginning ",
234               "_date <= $ending";
235
236 push @search, FS::UI::Web::parse_lt_gt($cgi, 'amount' );
237
238 # Agent virtualization
239 push @search, $FS::CurrentUser::CurrentUser->agentnums_sql(table=>'cust_main');
240
241 my @select = (
242   "'cust_credit' as __classname",
243   qw(cust_credit.crednum
244      cust_credit.custnum
245      cust_credit._date
246      cust_credit.amount
247      cust_credit.currency
248      cust_credit.otaker
249      cust_credit.usernum
250      cust_credit.reason
251      cust_credit.reasonnum
252      cust_credit.addlinfo
253      cust_credit.closed
254      cust_credit.pkgnum
255      cust_credit.eventnum
256      cust_credit.commission_agentnum
257      cust_credit.commission_salesnum
258      cust_credit.commission_pkgnum
259      cust_credit.commission_invnum
260      cust_credit.credbatch
261      ),
262   'Null as void_date',
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(),
270 );
271 my @select_void = (
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
290      ),
291   'Null as credbatch',
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
296   ),
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(),
301 );
302
303 if ( $unapplied ) {
304   push @select, '('.FS::cust_credit->unapplied_sql .') AS unapplied_amount';
305   push @search, FS::cust_credit->unapplied_sql .' > 0';
306 }
307
308 my $where = 'WHERE '. join(' AND ', @search);
309
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').
314                   $where;
315
316 my @count_addl = ( $money_char.'%.2f total credited (gross)' );
317 push @count_addl, $money_char.'%.2f unapplied' if $unapplied;
318
319 my $sql_query   = {
320   'table'     => 'cust_credit',
321   'select'    => join(', ',@select),
322   'hashref'   => {},
323   'extra_sql' => $where,
324   'addl_from' => $addl_from. FS::UI::Web::join_cust_main('cust_credit')
325 };
326
327 # Join to get reason text and void username to avoid two extra query per row
328 my $addl_from_void = join(' ',
329   $addl_from,
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) ',
334 );
335
336 my $where_void = $where;
337 $where_void =~ s/cust_credit/cust_credit_void/g;
338
339 my $sql_query_void = {
340   'table'     => 'cust_credit_void',
341   'select'    => join(', ',@select_void),
342   'hashref'   => {},
343   'extra_sql' => $where_void,
344   'addl_from' => $addl_from_void,
345 };
346
347 if ($show_voided_credits) {
348
349   $sql_query = [$sql_query, $sql_query_void];
350
351   my $count_cust_credit;
352   my $count_cust_credit_void;
353   my $count_sum;
354
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)',
360     extra_sql => $where,
361     addl_from => $addl_from . FS::UI::Web::join_cust_main('cust_credit'),
362   });
363   $count_cust_credit = $count_row->count || 0;
364   $count_sum = $count_row->sum || 0;
365
366   $count_row = qsearchs({
367     table => 'cust_credit_void',
368     select => 'count(*)',
369     extra_sql => $where_void,
370     addl_from => $addl_from_void,
371   });
372   $count_cust_credit_void = $count_row->count || 0;
373
374   my $count_combined = $count_cust_credit + $count_cust_credit_void;
375
376   # Fake count query providing needed values
377   $count_query = "SELECT $count_combined as count, $count_sum as sum";
378 }
379
380 </%init>