RT#41501: OBH: Separate credit additional info into separate field on reports [v3...
[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               emt('Info'),
60               ;
61 push @fields, sub { time2str('%b %d %Y', shift->_date ) },
62               'otaker',
63               'reason_only',
64               'addlinfo',
65               ;
66 push @sort_fields, '_date', 'otaker', 'reasonnum', 'addlinfo';
67 $align .= 'rlll';
68 push @links,  '',
69               '',
70               '',
71               '',
72               ;
73 push @color,  '',
74               '',
75               '',
76               '',
77               ;
78 push @style,  '',
79               '',
80               '',
81               '',
82               ;
83
84 # insert customer email after 'Reason' if this is a commission report
85 if ( $cgi->param('commission_salesnum') or $cgi->param('commission_agentnum') ) {
86   my $sub_customer_email = sub {
87     my $cust_credit = shift;
88     return '' unless $cust_credit->eventnum;
89     my $cust_event = FS::cust_event->by_key($cust_credit->eventnum)
90       or return '';
91     my $cust_X = $cust_event->cust_X or return '';
92     my $cust_main = $cust_X->cust_main or return '';
93     my ($email) = $cust_main->invoicing_list_emailonly;
94     return encode_entities("<$email>") if length($email);
95     '';
96   };
97   push @header, '';
98   push @fields, $sub_customer_email;
99   push @sort_fields, '';
100   $align .= 'l';
101   push @links, '';
102   push @color, '';
103   push @style, '';
104 }
105
106 # cust fields
107 push @header, FS::UI::Web::cust_header();
108 push @fields, \&FS::UI::Web::cust_fields;
109 push @sort_fields, FS::UI::Web::cust_sort_fields;
110 $align .= FS::UI::Web::cust_aligns();
111 push @links, map { $_ ne 'Cust. Status' ? $clink : '' }
112                          FS::UI::Web::cust_header();
113 push @color, FS::UI::Web::cust_colors();
114 push @style, FS::UI::Web::cust_styles();
115
116
117 my @search = ();
118 my $addl_from = '';
119
120 if ( $cgi->param('usernum') =~ /^(\d+)$/ ) {
121   push @search, "cust_credit.usernum = $1";
122 }
123
124 if ( $cgi->param('agentnum') && $cgi->param('agentnum') =~ /^(\d+)$/ ) {
125   push @search, "agentnum = $1";
126   my $agent = qsearchs('agent', { 'agentnum' => $1 } );
127   die "unknown agentnum $1" unless $agent;
128   $title = $agent->agent. " $title";
129 }
130
131 if ( $cgi->param('credbatch') =~ /^([\w\-\/\.\:]+)$/ ) {
132   push @search, "cust_credit.credbatch = '$1'";
133 }
134
135 # commission_salesnum
136 if ( $cgi->param('commission_salesnum') =~ /^(\d+)$/ ) {
137   push @search, "commission_salesnum = $1";
138 }
139
140 # commission agentnum
141 if ( $cgi->param('commission_agentnum') =~ /^(\d+)$/ ) {
142   push @search, "commission_agentnum = $1";
143 }
144
145 # commission_classnum
146 if ( grep { $_ eq 'commission_classnum' } $cgi->param ) {
147   $cgi->param('commission_classnum') =~ /^(\d*)$/ or die 'guru meditation #13';
148   my $classnum = $1;
149   push @search, "part_pkg.classnum ". ( $classnum ? " = $classnum"
150                                                  : " IS NULL "    );
151
152   $addl_from .=
153     ' LEFT JOIN cust_pkg ON ( commission_pkgnum = cust_pkg.pkgnum ) '.
154     ' LEFT JOIN part_pkg USING ( pkgpart ) ';
155 }
156
157 if ( $cgi->param('refnum') && $cgi->param('refnum') =~ /^(\d+)$/ ) {
158   push @search, "refnum = $1";
159   my $part_referral = qsearchs('part_referral', { 'refnum' => $1 } );
160   die "unknown refnum $1" unless $part_referral;
161   $title = $part_referral->referral. " $title";
162 }
163
164
165 # 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)
166 if ( grep { $_ eq 'cust_classnum' } $cgi->param ) {
167   my @classnums = grep /^\d*$/, $cgi->param('cust_classnum');
168   push @search, 'COALESCE( cust_main.classnum, 0) IN ( '.
169                     join(',', map { $_ || '0' } @classnums ).
170                 ' )'
171     if @classnums;
172 }
173
174 if ( $unapplied ) {
175   push @search, FS::cust_credit->unapplied_sql . ' > 0';
176 }
177
178 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
179 push @search, "_date >= $beginning ",
180               "_date <= $ending";
181
182 push @search, FS::UI::Web::parse_lt_gt($cgi, 'amount' );
183
184 #here is the agent virtualization
185 push @search, $FS::CurrentUser::CurrentUser->agentnums_sql(table=>'cust_main');
186
187 my @select = (
188     'cust_credit.*',
189     'cust_main.custnum as cust_main_custnum',
190     FS::UI::Web::cust_sql_fields(),
191 );
192
193 if ( $unapplied ) {
194   push @select, '('.FS::cust_credit->unapplied_sql .') AS unapplied_amount';
195   push @search, FS::cust_credit->unapplied_sql .' > 0';
196 }
197
198 my $where = 'WHERE '. join(' AND ', @search);
199
200 my $count_query = 'SELECT COUNT(*), SUM(amount) ';
201 $count_query .= ', SUM(' . FS::cust_credit->unapplied_sql . ') ' if $unapplied;
202 $count_query .= 'FROM cust_credit'. 
203                   $addl_from. FS::UI::Web::join_cust_main('cust_credit').
204                   $where;
205
206 my @count_addl = ( $money_char.'%.2f total credited (gross)' );
207 push @count_addl, $money_char.'%.2f unapplied' if $unapplied;
208
209 my $sql_query   = {
210   'table'     => 'cust_credit',
211   'select'    => join(', ',@select),
212   'hashref'   => {},
213   'extra_sql' => $where,
214   'addl_from' => $addl_from. FS::UI::Web::join_cust_main('cust_credit')
215 };
216
217 </%init>