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