add net credits to sales/credits/receipts report
[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'  => [ '$%.2f total credited (gross)', ],
7                  #'redirect'    => $link,
8                  'header'      => [ 'Amount',
9                                     'Date',
10                                     FS::UI::Web::cust_header(),
11                                     'By',
12                                     'Reason'
13                                   ],
14                  'fields'      => [
15                    #'crednum',
16                    sub { sprintf('$%.2f', shift->amount ) },
17                    sub { time2str('%b %d %Y', shift->_date ) },
18                    \&FS::UI::Web::cust_fields,
19                    'otaker',
20                    'reason',
21                  ],
22                  #'align' => 'rrrllll',
23                  'align' => 'rr'.FS::UI::Web::cust_aligns().'ll',
24                  'links' => [
25                    '',
26                    '',
27                    ( map { $_ ne 'Cust. Status' ? $clink : '' }
28                          FS::UI::Web::cust_header()
29                    ),
30                    '',
31                    '',
32                  ],
33                  'color' => [ 
34                               '',
35                               '',
36                               FS::UI::Web::cust_colors(),
37                               '',
38                               '',
39                             ],
40                  'style' => [ 
41                               '',
42                               '',
43                               FS::UI::Web::cust_styles(),
44                               '',
45                               '',
46                             ],
47       )
48 %>
49 <%init>
50
51 die "access denied"
52   unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
53
54 my $title = 'Credit Search Results';
55 #my( $count_query, $sql_query );
56
57 my @search = ();
58
59 if ( $cgi->param('otaker') && $cgi->param('otaker') =~ /^([\w\.\-]+)$/ ) {
60   push @search, "cust_credit.otaker = '$1'";
61 }
62
63 if ( $cgi->param('agentnum') && $cgi->param('agentnum') =~ /^(\d+)$/ ) {
64   push @search, "agentnum = $1";
65   my $agent = qsearchs('agent', { 'agentnum' => $1 } );
66   die "unknown agentnum $1" unless $agent;
67   $title = $agent->agent. " $title";
68 }
69
70 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
71 push @search, "_date >= $beginning ",
72               "_date <= $ending";
73
74 push @search, FS::UI::Web::parse_lt_gt($cgi, 'amount' );
75
76 #here is the agent virtualization
77 push @search, $FS::CurrentUser::CurrentUser->agentnums_sql;
78
79 my $where = 'WHERE '. join(' AND ', @search);
80
81 my $count_query = 'SELECT COUNT(*), SUM(amount) '.
82                   'FROM cust_credit LEFT JOIN cust_main USING ( custnum ) '.
83                   $where;
84
85 my $sql_query   = {
86   'table'     => 'cust_credit',
87   'select'    => join(', ',
88                    'cust_credit.*',
89                    'cust_main.custnum as cust_main_custnum',
90                    FS::UI::Web::cust_sql_fields(),
91                  ),
92   'hashref'   => {},
93   'extra_sql' => $where,
94   'addl_from' => 'LEFT JOIN cust_main USING ( custnum )',
95 };
96
97   my $clink = sub {
98     my $cust_bill = shift;
99     $cust_bill->cust_main_custnum
100       ? [ "${p}view/cust_main.cgi?", 'custnum' ]
101       : '';
102   };
103
104 </%init>