unapplied payment/refund/credit reports, RT#7503
[freeside.git] / httemplate / search / cust_credit.html
1 <% include( 'elements/search.html',
2                  'title'       => $title,
3                  'name'        => 'credits',
4                  'query'       => $sql_query,
5                  'count_query' => $count_query,
6                  'count_addl'  => \@count_addl,
7                  #'redirect'    => $link,
8                  'header'      => \@header,
9                  'fields'      => \@fields,
10                  'align' => $align,
11                  'links' => \@links,
12                  'color' => \@color,
13                  'style' => \@style,
14       )
15 %>
16 <%init>
17
18 die "access denied"
19   unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
20
21 my $money_char = FS::Conf->new->config('money_char') || '$';
22
23 my $title = 'Credit Search Results';
24 #my( $count_query, $sql_query );
25
26 my $unapplied = $cgi->param('unapplied');
27 $title = "Unapplied $title" if $unapplied;
28 my $clink = sub {
29   my $cust_bill = shift;
30   $cust_bill->cust_main_custnum
31     ? [ "${p}view/cust_main.cgi?", 'custnum' ]
32     : '';
33 };
34
35 my (@header, @fields, $align, @links, @color, @style);
36 $align = '';
37
38 #amount
39 push @header, 'Amount';
40 push @fields, sub { $money_char .sprintf('%.2f', shift->amount) };
41 $align .= 'r';
42 push @links, '';
43 push @color, '';
44 push @style, '';
45
46 # unapplied amount
47 if ($unapplied) {
48   push @header, 'Unapplied';
49   push @fields, sub { $money_char .sprintf('%.2f', shift->unapplied_amount) };
50   $align .= 'r';
51   push @links, '';
52   push @color, '';
53   push @style, '';
54 }
55
56 push @header, 'Date', 
57               'By',
58               '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 $align .= 'rll'.FS::UI::Web::cust_aligns(),
67 push @links,  '',
68               '',
69               '',
70               ( map { $_ ne 'Cust. Status' ? $clink : '' }
71                          FS::UI::Web::cust_header()
72                    ),
73               ;
74 push @color,  '',
75               '',
76               '',
77               FS::UI::Web::cust_colors(),
78               ;
79 push @style,  '',
80               '',
81               '',
82               FS::UI::Web::cust_styles(),
83               ;
84
85 my @search = ();
86
87 if ( $cgi->param('usernum') =~ /^(\d+)$/ ) {
88   push @search, "cust_credit.usernum = $1";
89 }
90
91 if ( $cgi->param('agentnum') && $cgi->param('agentnum') =~ /^(\d+)$/ ) {
92   push @search, "agentnum = $1";
93   my $agent = qsearchs('agent', { 'agentnum' => $1 } );
94   die "unknown agentnum $1" unless $agent;
95   $title = $agent->agent. " $title";
96 }
97
98 if ( $unapplied ) {
99   push @search, FS::cust_credit->unapplied_sql . ' > 0';
100 }
101
102 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
103 push @search, "_date >= $beginning ",
104               "_date <= $ending";
105
106 push @search, FS::UI::Web::parse_lt_gt($cgi, 'amount' );
107
108 #here is the agent virtualization
109 push @search, $FS::CurrentUser::CurrentUser->agentnums_sql;
110
111 my @select = (
112     'cust_credit.*',
113     'cust_main.custnum as cust_main_custnum',
114     FS::UI::Web::cust_sql_fields(),
115 );
116
117 if ( $unapplied ) {
118   push @select, '('.FS::cust_credit->unapplied_sql .') AS unapplied_amount';
119   push @search, FS::cust_credit->unapplied_sql .' > 0';
120 }
121
122 my $where = 'WHERE '. join(' AND ', @search);
123
124 my $count_query = 'SELECT COUNT(*), SUM(amount) ';
125 $count_query .= ', SUM(' . FS::cust_credit->unapplied_sql . ') ' if $unapplied;
126 $count_query .= 'FROM cust_credit LEFT JOIN cust_main USING ( custnum ) '.
127                   $where;
128
129 my @count_addl = ( $money_char.'%.2f total credited (gross)' );
130 push @count_addl, $money_char.'%.2f unapplied' if $unapplied;
131
132 my $sql_query   = {
133   'table'     => 'cust_credit',
134   'select'    => join(', ',@select),
135   'hashref'   => {},
136   'extra_sql' => $where,
137   'addl_from' => 'LEFT JOIN cust_main USING ( custnum )',
138 };
139
140 </%init>