add reporting on (and resolution of) stuck pending transactions, RT#4837 (RT#3572)
[freeside.git] / httemplate / view / cust_main / payment_history.html
1 <BR><BR><A NAME="history"><FONT SIZE="+2">Payment History</FONT></A><BR>
2
3 %# payment links
4
5 % my $s = 0;
6 % if ( $payby{'BILL'} && $curuser->access_right('Post payment') ) { 
7   <% $s++ ? ' | ' : '' %>
8   <% include('/elements/popup_link-cust_main.html',
9                'label'       => 'Enter check payment',
10                'action'      => "${p}edit/cust_pay.cgi?popup=1;payby=BILL",
11                'cust_main'   => $cust_main,
12                'actionlabel' => 'Enter check payment',
13                'width'       => 392,
14                #default# 'height' => 336,
15             )
16   %>
17 % } 
18
19 % if ( $payby{'CASH'} && $curuser->access_right('Post payment') ) { 
20   <% $s++ ? ' | ' : '' %>
21   <% include('/elements/popup_link-cust_main.html',
22                'label'       => 'Enter cash payment',
23                'action'      => "${p}edit/cust_pay.cgi?popup=1;payby=CASH",
24                'cust_main'   => $cust_main,
25                'actionlabel' => 'Enter cash payment',
26                'width'       => 392,
27                #default# 'height' => 336,
28             )
29   %>
30 % } 
31
32 % if ( $payby{'WEST'} && $curuser->access_right('Post payment') ) { 
33   <% $s++ ? ' | ' : '' %>
34   <A HREF="<% $p %>edit/cust_pay.cgi?payby=WEST;custnum=<% $custnum %>">Enter Western Union payment</A>
35 % } 
36
37 % if ( ( $payby{'CARD'} || $payby{'DCRD'} )
38 %        && $curuser->access_right('Process payment')
39 %        && ! $cust_main->is_encrypted($cust_main->payinfo)
40 %      ) {
41   <% $s++ ? ' | ' : '' %>
42   <A HREF="<% $p %>misc/payment.cgi?payby=CARD;custnum=<% $custnum %>">Process credit card payment</A>
43 % } 
44
45 % if ( ( $payby{'CHEK'} || $payby{'DCHK'} )
46 %        && $curuser->access_right('Process payment')
47 %        && ! $cust_main->is_encrypted($cust_main->payinfo)
48 %      ) {
49   <% $s++ ? ' | ' : '' %>
50   <A HREF="<% $p %>misc/payment.cgi?payby=CHEK;custnum=<% $custnum %>">Process electronic check (ACH) payment</A>
51 % } 
52
53 % if ( $payby{'MCRD'} && $curuser->access_right('Post payment') ) { 
54   <% $s++ ? ' | ' : '' %>
55   <A HREF="<% $p %>edit/cust_pay.cgi?payby=MCRD;custnum=<% $custnum %>">Post manual (offline/POS) credit card payment</A>
56 % } 
57
58 <BR>
59
60 %# credit link
61
62 % if ( $curuser->access_right('Post credit') ) { 
63   <% include('/elements/popup_link-cust_main.html',
64                'label'       => 'Enter credit',
65                'action'      => "${p}edit/cust_credit.cgi",
66                'cust_main'   => $cust_main,
67                'actionlabel' => 'Enter credit',
68                'width'       => 392,
69                #default# 'height' => 336,
70             )
71   %>
72   <BR>
73 % } 
74
75 %# refund links
76
77 % $s = 0;
78 % if ( $payby{'BILL'} && $curuser->access_right('Post refund') ) { 
79   <% $s++ ? ' | ' : '' %>
80   <% include('/elements/popup_link-cust_main.html',
81                'label'       => 'Enter check refund',
82                'action'      => "${p}edit/cust_refund.cgi?popup=1;payby=BILL",
83                'cust_main'   => $cust_main,
84                'actionlabel' => 'Enter check refund',
85                'width'       => 392,
86                #default# 'height' => 336,
87             )
88   %>
89 % } 
90
91 % if ( $payby{'CASH'} && $curuser->access_right('Post refund') ) { 
92   <% $s++ ? ' | ' : '' %>
93   <% include('/elements/popup_link-cust_main.html',
94                'label'       => 'Enter cash refund',
95                'action'      => "${p}edit/cust_refund.cgi?popup=1;payby=CASH",
96                'cust_main'   => $cust_main,
97                'actionlabel' => 'Enter cash refund',
98                'width'       => 392,
99                #default# 'height' => 336,
100             )
101   %>
102 % } 
103
104 %# someday, perhaps.  very few gateways let you do unlinked refunds at all.
105 %# Authorize.net makes you sign a special form
106 %#
107 %#    % if ( ( $payby{'CARD'} || $payby{'DCRD'} )
108 %#    %        && $curuser->access_right('Process refund')
109 %#    %        && ! $cust_main->is_encrypted($cust_main->payinfo)
110 %#    %      ) {
111 %#      <% $s++ ? ' | ' : '' %>
112 %#      <A HREF="<% $p %>misc/refund.cgi?payby=CARD;custnum=<% $custnum %>">Process credit card refund</A>
113 %#    % } 
114 %#    
115 %#    % if ( ( $payby{'CHEK'} || $payby{'DCHK'} )
116 %#    %        && $curuser->access_right('Process refund')
117 %#    %        && ! $cust_main->is_encrypted($cust_main->payinfo)
118 %#    %      ) {
119 %#      <% $s++ ? ' | ' : '' %>
120 %#      <A HREF="<% $p %>misc/refund.cgi?payby=CHEK;custnum=<% $custnum %>">Process electronic check (ACH) refund</A>
121 %#    % } 
122
123 % if ( $payby{'MCRD'} && $curuser->access_right('Post refund') ) { 
124   <% $s++ ? ' | ' : '' %>
125   <A HREF="<% $p %>edit/cust_refund.cgi?payby=MCRD;custnum=<% $custnum %>">Post manual (offline/POS) credit card refund</A>
126 % } 
127
128 <BR>
129
130 %# tax exemption link
131
132 % if ( $curuser->access_right('View customer tax exemptions') ) { 
133   <A HREF="<% $p %>search/cust_tax_exempt_pkg.cgi?custnum=<% $custnum %>">View tax exemptions</A>
134   <BR>
135 % } 
136
137 %# batched payment links
138
139 % if ( ( $conf->exists('batch-enable') || $conf->config('batch-enable_payby') )
140 %      && $curuser->access_right('View customer batched payments')
141 %    )
142 % { 
143     View batched payments:
144 %   foreach my $status (qw( Queued In-transit Complete All )) {
145       <A HREF="<% $p %>search/cust_pay_batch.cgi?status=<% $status{$status} %>;custnum=<% $custnum %>"><% $status %></A> 
146       <% $status ne 'All' ? '|' : '' %>
147 %   }
148     <BR>
149 % } 
150
151 %# pending payment links
152
153 % if ( $curuser->access_right('View customer pending payments')
154 %      && scalar($cust_main->cust_pay_pending)
155 %    )
156 % {
157     <A HREF="<% $p %>search/cust_pay_pending.html?magic=_date;statusNOT=done;custnum=<% $custnum %>">View pending payments</A><BR>
158 % }
159
160 %# and now the table
161
162 <% include("/elements/table-grid.html") %>
163 % my $bgcolor1 = '#eeeeee';
164 %   my $bgcolor2 = '#ffffff';
165 %   my $bgcolor = '';
166
167 <TR>
168   <TH CLASS="grid" BGCOLOR="#cccccc">Date</TH>
169   <TH CLASS="grid" BGCOLOR="#cccccc">Description</TH>
170   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1>Invoice</FONT></TH>
171   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1>Payment</FONT></TH>
172   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1>In-house<BR>Credit</FONT></TH>
173   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1>Refund</FONT></TH>
174   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1>Balance</FONT></TH>
175 </TR>
176
177 %#display payment history
178
179 %my $money_char = $conf->config('money_char') || '$';
180 %
181 %sub balance_forward_row {
182 %  my( $b, $date, $money_char ) = @_;
183 %  ( my $balance_forward = $money_char. $b ) =~ s/^\$\-/-&nbsp;\$/;
184
185    <TR ID="balance_forward_row">
186      <TD CLASS="grid" BGCOLOR="#dddddd">
187        <% time2str("%D",$date) %>
188      </TD>
189
190      <TD CLASS="grid" BGCOLOR="#dddddd">
191        <I>Starting balance on <% time2str("%D",$date) %></I>
192        (<A HREF="javascript:void(0);" onClick="show_history();">show prior history</A>)
193      </TD>
194
195      <TD CLASS="grid" BGCOLOR="#dddddd"></TD>
196      <TD CLASS="grid" BGCOLOR="#dddddd"></TD>
197      <TD CLASS="grid" BGCOLOR="#dddddd"></TD>
198      <TD CLASS="grid" BGCOLOR="#dddddd"></TD>
199      <TD CLASS="grid" BGCOLOR="#dddddd" ALIGN="right"><I><% $balance_forward %></I></TD>
200
201    </TR>
202 %}
203 %
204 %my $balance = 0;
205 %my %target = ();
206 %
207 %my $years =  $conf->config('payment_history-years') || 2;
208 %my $older_than = time - $years * 31556736; #60*60*24*365.24
209 %my $hidden = 0;
210 %my $seen = 0;
211 %my $old_history = 0;
212 %my $lastdate = 0;
213 %
214 %foreach my $item ( sort { $a->{'date'} <=> $b->{'date'} } @history ) {
215 %
216 %  $lastdate = $item->{'date'};
217 %
218 %  my $display;
219 %  if ( $item->{'date'} < $older_than ) {
220 %    $display = ' STYLE="display:none" ';
221 %    $hidden = 1;
222 %  } else {
223 %
224 %    $display = '';
225 %
226 %    if ( $hidden && ! $seen++ ) {
227 %      balance_forward_row($balance, $item->{'date'}, $money_char);
228 %    }
229 %
230 %  }
231 %
232 %  if ( $bgcolor eq $bgcolor1 ) {
233 %    $bgcolor = $bgcolor2;
234 %  } else {
235 %    $bgcolor = $bgcolor1;
236 %  }
237 %
238 %  my $charge  = exists($item->{'charge'})
239 %                  ? sprintf("$money_char\%.2f", $item->{'charge'})
240 %                  : '';
241 %
242 %  my $payment = exists($item->{'payment'})
243 %                  ? sprintf("-&nbsp;$money_char\%.2f", $item->{'payment'})
244 %                  : '';
245 %
246 %  $payment ||= sprintf( "<DEL>-&nbsp;$money_char\%.2f</DEL>",
247 %                        $item->{'void_payment'}
248 %                      )
249 %    if exists($item->{'void_payment'});
250 %
251 %  my $credit  = exists($item->{'credit'})
252 %                  ? sprintf("-&nbsp;$money_char\%.2f", $item->{'credit'})
253 %                  : '';
254 %
255 %  my $refund  = exists($item->{'refund'})
256 %                  ? sprintf("$money_char\%.2f", $item->{'refund'})
257 %                  : '';
258 %
259 %  my $target = exists($item->{'target'}) ? $item->{'target'} : '';
260 %
261 %  $balance += $item->{'charge'}  if exists $item->{'charge'};
262 %  $balance -= $item->{'payment'} if exists $item->{'payment'};
263 %  $balance -= $item->{'credit'}  if exists $item->{'credit'};
264 %  $balance += $item->{'refund'}  if exists $item->{'refund'};
265 %  $balance = sprintf("%.2f", $balance);
266 %  $balance =~ s/^\-0\.00$/0.00/; #yay ieee fp
267 %  ( my $showbalance = $money_char. $balance ) =~ s/^\$\-/-&nbsp;\$/;
268 %
269 %
270
271
272   <TR <% $display ? $display.' ID="old_history'.$old_history++.'"'  : ''%>>
273     <TD CLASS="grid" BGCOLOR="<% $bgcolor %>">
274 % unless ( !$target || $target{$target}++ ) { 
275
276         <A NAME="<% $target %>">
277 % } 
278
279       <% time2str("%D",$item->{'date'}) %>
280 % if ( $target && $target{$target} == 1 ) { 
281
282         </A>
283 % } 
284
285       </FONT>
286     </TD>
287     <TD CLASS="grid" BGCOLOR="<% $bgcolor %>">
288       <% $item->{'desc'} %>
289     </TD>
290     <TD ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
291       <% $charge  %>
292     </TD>
293     <TD ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
294       <% $payment %>
295     </TD>
296     <TD ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
297       <% $credit  %>
298     </TD>
299     <TD ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
300       <% $refund  %>
301     </TD>
302     <TD ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
303       <% $showbalance %>
304     </TD>
305   </TR>
306 % } 
307
308 %if ( scalar(@history) && $hidden && ! $seen++ ) {
309 %  balance_forward_row($balance, $lastdate, $money_char);
310 %}
311
312 </TABLE>
313
314 <SCRIPT TYPE="text/javascript">
315
316 function show_history () {
317   //alert('showing history!');
318
319   var balance_forward_row = document.getElementById('balance_forward_row');
320
321   balance_forward_row.style.display = 'none';
322   for ( var i = 0; i < <% $old_history %>; i++ ) {
323     var oldRow = document.getElementById('old_history'+i);
324     oldRow.style.display = '';
325   }
326
327 }
328
329 </SCRIPT>
330
331 <%init>
332
333 my( $cust_main ) = @_;
334 my $custnum = $cust_main->custnum;
335
336 my $conf = new FS::Conf;
337
338 my $curuser = $FS::CurrentUser::CurrentUser;
339
340 my @payby = grep /\w/, $conf->config('payby');
341 #@payby = (qw( CARD DCRD CHEK DCHK LECB BILL CASH WEST COMP ))
342 @payby = (qw( CARD DCRD CHEK DCHK LECB BILL CASH COMP ))
343   unless @payby;
344 my %payby = map { $_=>1 } @payby;
345
346 my %status = (
347   'Queued'     => 'O', #Open
348   'In-transit' => 'I',
349   'Complete'   => 'R', #Resolved
350   'All'        => '',
351 );
352
353 #get payment history
354 my @history = ();
355
356 my %opt =
357   ( map { $_ => scalar($conf->config($_)) }
358         qw( card_refund-days )
359   ),
360   ( map { $_ => $conf->exists($_) } 
361         qw( deletepayments deleterefunds )
362   );
363
364 #invoices
365 foreach my $cust_bill ($cust_main->cust_bill) {
366   push @history, {
367     'date'   => $cust_bill->_date,
368     'desc'   => include('payment_history/invoice.html', $cust_bill, %opt ),
369     'charge' => $cust_bill->charged,
370   };
371 }
372
373 #payments (some false laziness w/credits)
374 foreach my $cust_pay ($cust_main->cust_pay) {
375   push @history, {
376     'date'    => $cust_pay->_date,
377     'desc'    => include('payment_history/payment.html', $cust_pay, %opt ),
378     'payment' => $cust_pay->paid,
379     #'target'  => $target, #XXX
380   };
381 }
382
383 #voided payments
384 foreach my $cust_pay_void ($cust_main->cust_pay_void) {
385   push @history, {
386     'date'   => $cust_pay_void->_date,
387     'desc'   => include('payment_history/voided_payment.html', $cust_pay_void),
388     'void_payment' => $cust_pay_void->paid,
389   };
390
391 }
392
393 #credits (some false laziness w/payments)
394 foreach my $cust_credit ($cust_main->cust_credit) {
395   push @history, {
396     'date'   => $cust_credit->_date,
397     'desc'   => include('payment_history/credit.html', $cust_credit),
398     'credit' => $cust_credit->amount,
399   };
400
401 }
402
403 #refunds
404 foreach my $cust_refund ($cust_main->cust_refund) {
405   push @history, {
406     'date'   => $cust_refund->_date,
407     'desc'   => include('payment_history/refund.html', $cust_refund),
408     'refund' => $cust_refund->refund,
409   };
410
411 }
412
413 </%init>