filter by customer class on all financial reports, #20573
[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 &>
15 <%init>
16
17 die "access denied"
18   unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
19
20 my $money_char = FS::Conf->new->config('money_char') || '$';
21
22 my $title = emt('Credit Search Results');
23
24 my $unapplied = $cgi->param('unapplied');
25 $title = emt("Unapplied $title") if $unapplied;
26 my $clink = sub {
27   my $cust_bill = shift;
28   $cust_bill->cust_main_custnum
29     ? [ "${p}view/cust_main.cgi?", 'custnum' ]
30     : '';
31 };
32
33 my (@header, @fields, @sort_fields, $align, @links, @color, @style);
34 $align = '';
35
36 #amount
37 push @header, emt('Amount');
38 push @fields, sub { $money_char .sprintf('%.2f', shift->amount) };
39 push @sort_fields, 'amount';
40 $align .= 'r';
41 push @links, '';
42 push @color, '';
43 push @style, '';
44
45 # unapplied amount
46 if ($unapplied) {
47   push @header, emt('Unapplied');
48   push @fields, sub { $money_char .sprintf('%.2f', shift->unapplied_amount) };
49   push @sort_fields, '';
50   $align .= 'r';
51   push @links, '';
52   push @color, '';
53   push @style, '';
54 }
55
56 push @header, emt('Date'), 
57               emt('By'),
58               emt('Reason'),
59               FS::UI::Web::cust_header(),
60               ;
61 push @fields, sub { time2str('%b %d %Y', shift->_date ) },
62               'otaker',
63               'reason',
64               \&FS::UI::Web::cust_fields,
65               ;
66 push @sort_fields, '_date', 'otaker', 'reason';
67 $align .= 'rll'.FS::UI::Web::cust_aligns(),
68 push @links,  '',
69               '',
70               '',
71               ( map { $_ ne 'Cust. Status' ? $clink : '' }
72                          FS::UI::Web::cust_header()
73                    ),
74               ;
75 push @color,  '',
76               '',
77               '',
78               FS::UI::Web::cust_colors(),
79               ;
80 push @style,  '',
81               '',
82               '',
83               FS::UI::Web::cust_styles(),
84               ;
85
86 my @search = ();
87
88 if ( $cgi->param('usernum') =~ /^(\d+)$/ ) {
89   push @search, "cust_credit.usernum = $1";
90 }
91
92 if ( $cgi->param('agentnum') && $cgi->param('agentnum') =~ /^(\d+)$/ ) {
93   push @search, "agentnum = $1";
94   my $agent = qsearchs('agent', { 'agentnum' => $1 } );
95   die "unknown agentnum $1" unless $agent;
96   $title = $agent->agent. " $title";
97 }
98
99 if ( $cgi->param('refnum') && $cgi->param('refnum') =~ /^(\d+)$/ ) {
100   push @search, "refnum = $1";
101   my $part_referral = qsearchs('part_referral', { 'refnum' => $1 } );
102   die "unknown refnum $1" unless $part_referral;
103   $title = $part_referral->referral. " $title";
104 }
105
106 if ( $cgi->param('cust_classnum') ) {
107   my @classnums = grep /^\d+$/, $cgi->param('cust_classnum');
108   push @search, 'cust_main.classnum IN('.join(',',@classnums).')'
109     if @classnums;
110 }
111
112 if ( $unapplied ) {
113   push @search, FS::cust_credit->unapplied_sql . ' > 0';
114 }
115
116 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
117 push @search, "_date >= $beginning ",
118               "_date <= $ending";
119
120 push @search, FS::UI::Web::parse_lt_gt($cgi, 'amount' );
121
122 #here is the agent virtualization
123 push @search, $FS::CurrentUser::CurrentUser->agentnums_sql;
124
125 my @select = (
126     'cust_credit.*',
127     'cust_main.custnum as cust_main_custnum',
128     FS::UI::Web::cust_sql_fields(),
129 );
130
131 if ( $unapplied ) {
132   push @select, '('.FS::cust_credit->unapplied_sql .') AS unapplied_amount';
133   push @search, FS::cust_credit->unapplied_sql .' > 0';
134 }
135
136 my $where = 'WHERE '. join(' AND ', @search);
137
138 my $count_query = 'SELECT COUNT(*), SUM(amount) ';
139 $count_query .= ', SUM(' . FS::cust_credit->unapplied_sql . ') ' if $unapplied;
140 $count_query .= 'FROM cust_credit LEFT JOIN cust_main USING ( custnum ) '.
141                   $where;
142
143 my @count_addl = ( $money_char.'%.2f total credited (gross)' );
144 push @count_addl, $money_char.'%.2f unapplied' if $unapplied;
145
146 my $sql_query   = {
147   'table'     => 'cust_credit',
148   'select'    => join(', ',@select),
149   'hashref'   => {},
150   'extra_sql' => $where,
151   'addl_from' => 'LEFT JOIN cust_main USING ( custnum )',
152 };
153
154 </%init>