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