echeck options to hide routing number and add account owner name, RT#13885
[freeside.git] / httemplate / view / cust_main / payment_history.html
1 <TABLE>
2   <TR>
3     <TD ALIGN="left">
4
5 %# payment links
6
7 % my $s = 0;
8 % if ( $payby{'BILL'} && $curuser->access_right(['Post payment', 'Post check payment' ]) ) { 
9   <% $s++ ? ' | ' : '' %>
10   <& /elements/popup_link-cust_main.html,
11                'label'       => emt('Enter check payment'),
12                'action'      => "${p}edit/cust_pay.cgi?popup=1;payby=BILL",
13                'cust_main'   => $cust_main,
14                'actionlabel' => emt('Enter check payment'),
15                'width'       => 392,
16   &>
17 % } 
18
19 % if ( $payby{'CASH'} && $curuser->access_right(['Post payment', 'Post cash payment']) ) { 
20   <% $s++ ? ' | ' : '' %>
21   <& /elements/popup_link-cust_main.html,
22                'label'       => emt('Enter cash payment'),
23                'action'      => "${p}edit/cust_pay.cgi?popup=1;payby=CASH",
24                'cust_main'   => $cust_main,
25                'actionlabel' => emt('Enter cash payment'),
26                'width'       => 392,
27   &>
28 % } 
29
30 % if ( $payby{'WEST'} && $curuser->access_right('Post payment') ) { 
31   <% $s++ ? ' | ' : '' %>
32   <A HREF="<% $p %>edit/cust_pay.cgi?payby=WEST;custnum=<% $custnum %>"><% mt('Enter Western Union payment') |h %></A>
33 % } 
34
35 <BR>
36 % $s=0;
37
38 % if ( ( $payby{'CARD'} || $payby{'DCRD'} )
39 %        && $curuser->access_right(['Process payment', 'Process credit card payment'])
40 %        && ! $cust_main->is_encrypted($cust_main->payinfo)
41 %      ) {
42   <% $s++ ? ' | ' : '' %>
43   <A HREF="<% $p %>misc/payment.cgi?payby=CARD;custnum=<% $custnum %>"><% mt('Process credit card payment') |h %></A>
44 % } 
45
46 % if ( ( $payby{'CHEK'} || $payby{'DCHK'} )
47 %        && $curuser->access_right(['Process payment', 'Process Echeck payment'])
48 %        && ! $cust_main->is_encrypted($cust_main->payinfo)
49 %      ) {
50   <% $s++ ? ' | ' : '' %>
51   <A HREF="<% $p %>misc/payment.cgi?payby=CHEK;custnum=<% $custnum %>"><% mt('Process electronic check (ACH) payment') |h %></A>
52 % } 
53
54 % if ( $payby{'MCRD'} && $curuser->access_right('Post payment') ) { 
55   <% $s++ ? ' | ' : '' %>
56   <A HREF="<% $p %>edit/cust_pay.cgi?payby=MCRD;custnum=<% $custnum %>"><% mt('Post manual (offline/POS) credit card payment') |h %></A>
57 % } 
58
59 <BR>
60
61 %# credit link
62
63 % if ( $curuser->access_right('Post credit') ) { 
64   <& /elements/popup_link-cust_main.html,
65                'label'       => emt('Enter credit'),
66                'action'      => "${p}edit/cust_credit.cgi",
67                'cust_main'   => $cust_main,
68                'actionlabel' => emt('Enter credit'),
69                'width'       => 616, #make room for reasons #540 default
70   &>
71   <BR>
72 % } 
73
74 %# refund links
75
76 % $s = 0;
77 % if ( $payby{'BILL'} && $curuser->access_right(['Post refund', 'Post check refund']) ) { 
78   <% $s++ ? ' | ' : '' %>
79   <& /elements/popup_link-cust_main.html,
80                'label'       => emt('Enter check refund'),
81                'action'      => "${p}edit/cust_refund.cgi?popup=1;payby=BILL",
82                'cust_main'   => $cust_main,
83                'actionlabel' => emt('Enter check refund'),
84                'width'       => 392,
85   &>
86 % } 
87
88 % if ( $payby{'CASH'} && $curuser->access_right(['Post refund', 'Post cash refund']) ) { 
89   <% $s++ ? ' | ' : '' %>
90   <& /elements/popup_link-cust_main.html,
91                'label'       => emt('Enter cash refund'),
92                'action'      => "${p}edit/cust_refund.cgi?popup=1;payby=CASH",
93                'cust_main'   => $cust_main,
94                'actionlabel' => emt('Enter cash refund'),
95                'width'       => 392,
96   &>
97 % } 
98
99 %# someday, perhaps.  very few gateways let you do unlinked refunds at all.
100 %# Authorize.net makes you sign a special form
101 %#
102 %#    % if ( ( $payby{'CARD'} || $payby{'DCRD'} )
103 %#    %        && $curuser->access_right('Process refund')
104 %#    %        && ! $cust_main->is_encrypted($cust_main->payinfo)
105 %#    %      ) {
106 %#      <% $s++ ? ' | ' : '' %>
107 %#      <A HREF="<% $p %>misc/refund.cgi?payby=CARD;custnum=<% $custnum %>">Process credit card refund</A>
108 %#    % } 
109 %#    
110 %#    % if ( ( $payby{'CHEK'} || $payby{'DCHK'} )
111 %#    %        && $curuser->access_right('Process refund')
112 %#    %        && ! $cust_main->is_encrypted($cust_main->payinfo)
113 %#    %      ) {
114 %#      <% $s++ ? ' | ' : '' %>
115 %#      <A HREF="<% $p %>misc/refund.cgi?payby=CHEK;custnum=<% $custnum %>">Process electronic check (ACH) refund</A>
116 %#    % } 
117
118 % if ( $payby{'MCRD'} && $curuser->access_right('Post refund') ) { 
119   <% $s++ ? ' | ' : '' %>
120   <A HREF="<% $p %>edit/cust_refund.cgi?payby=MCRD;custnum=<% $custnum %>"><% mt('Post manual (offline/POS) credit card refund') |h %></A>
121 % } 
122
123     </TD>
124     <TD ALIGN="right" VALIGN="top">
125
126 %# invoice reports
127 % if ( $curuser->access_right('List invoices') ) { 
128   <A HREF="<% $p %>search/report_cust_bill.html?custnum=<% $custnum %>"><% mt('Invoice reports') |h %></A>
129 % } 
130 <BR>
131
132 %# XXX payments, credits, refund reports
133
134 %# tax exemption link
135
136 % my $view_exemptions = $curuser->access_right('View customer tax exemptions');
137 % my $add_adjustment = ( $conf->exists('enable_tax_adjustments')
138 %                       && $curuser->access_right('Add customer tax adjustment')
139 %                      );
140 % if ( $view_exemptions || $add_adjustment ) {
141
142 %   if ( $view_exemptions ) {
143       <A HREF="<% $p %>search/cust_tax_exempt_pkg.cgi?custnum=<% $custnum %>"><% mt('View tax exemptions') |h %></A>
144       <% $add_adjustment ? '|' : '' %>
145 %   } 
146
147 %   if ( $add_adjustment ) {
148       <& /elements/popup_link.html, {
149            'action' => $p.'edit/cust_tax_adjustment.html?custnum='. $cust_main->custnum,
150            'label'  => emt('Add tax adjustment'),
151            'actionlabel' => emt('Add tax adjustment'),
152            'height' => 200,
153          }
154       &>
155       |
156       <A HREF="<% $p %>search/cust_tax_adjustment.html?custnum=<% $custnum %>"><% mt('View tax adjustments') |h %></A>
157 %   } 
158
159   <BR>
160 % }
161
162 %# batched payment links
163
164 % if ( ( $conf->exists('batch-enable') || $conf->config('batch-enable_payby') )
165 %      && $curuser->access_right('View customer batched payments')
166 %    )
167 % { 
168     <% mt('View batched payments:') |h %> 
169 %   foreach my $status (qw( Queued In-transit Complete All )) {
170       <A HREF="<% $p %>search/cust_pay_batch.cgi?status=<% $status{$status} %>;custnum=<% $custnum %>"><% mt($status) |h %></A> 
171       <% $status ne 'All' ? '|' : '' %>
172 %   }
173     <BR>
174 % } 
175
176 %# pending payment links
177
178 % if ( $curuser->access_right('View customer pending payments')
179 %      && scalar($cust_main->cust_pay_pending)
180 %    )
181 % {
182     <A HREF="<% $p %>search/cust_pay_pending.html?magic=_date;statusNOT=done;custnum=<% $custnum %>"><% mt('View pending payments') |h %></A><BR>
183 % }
184
185     </TD>
186   </TR>
187   <TR>
188     <TD COLSPAN=2>
189
190 %# and now the table
191
192 <& /elements/table-grid.html &>
193 % my $bgcolor1 = '#eeeeee';
194 %   my $bgcolor2 = '#ffffff';
195 %   my $bgcolor = '';
196
197 <TR>
198   <TH CLASS="grid" BGCOLOR="#cccccc"><% mt('Date') |h %></TH>
199   <TH CLASS="grid" BGCOLOR="#cccccc"><% mt('Description') |h %></TH>
200   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1><% mt('Invoice') |h %></FONT></TH>
201   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1><% mt('Payment') |h %></FONT></TH>
202   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1><% mt('In-house Credit') |h %></FONT></TH>
203   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1><% mt('Refund') |h %></FONT></TH>
204   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1><% mt('Balance') |h %></FONT></TH>
205 </TR>
206
207 %#display payment history
208
209 %my $money_char = $conf->config('money_char') || '$';
210 %
211 %sub balance_forward_row {
212 %  my( $b, $date, $money_char ) = @_;
213 %  ( my $balance_forward = $money_char. $b ) =~ s/^\$\-/-&nbsp;\$/;
214
215    <TR ID="balance_forward_row">
216      <TD CLASS="grid" BGCOLOR="#dddddd">
217        <% time2str($date_format, $date) %>
218      </TD>
219
220      <TD CLASS="grid" BGCOLOR="#dddddd">
221        <I><% mt("Starting balance on [_1]", time2str($date_format, $date) ) |h %></I>
222        (<A HREF="javascript:void(0);" onClick="show_history();"><% mt('show prior history') |h %></A>)
223      </TD>
224
225      <TD CLASS="grid" BGCOLOR="#dddddd"></TD>
226      <TD CLASS="grid" BGCOLOR="#dddddd"></TD>
227      <TD CLASS="grid" BGCOLOR="#dddddd"></TD>
228      <TD CLASS="grid" BGCOLOR="#dddddd"></TD>
229      <TD CLASS="grid" BGCOLOR="#dddddd" ALIGN="right"><I><% $balance_forward %></I></TD>
230
231    </TR>
232 %}
233 %
234 %my $balance = 0;
235 %my %target = ();
236 %
237 %my $years =  $conf->config('payment_history-years') || 2;
238 %my $older_than = time - $years * 31556926; #60*60*24*365.2422
239 %my $hidden = 0;
240 %my $seen = 0;
241 %my $old_history = 0;
242 %my $lastdate = 0;
243 %
244 %foreach my $item ( sort { $a->{'date'} <=> $b->{'date'} } @history ) {
245 %
246 %  $lastdate = $item->{'date'};
247 %
248 %  my $display;
249 %  if ( $item->{'date'} < $older_than ) {
250 %    $display = ' STYLE="display:none" ';
251 %    $hidden = 1;
252 %  } else {
253 %
254 %    $display = '';
255 %
256 %    if ( $hidden && ! $seen++ ) {
257 %      balance_forward_row($balance, $item->{'date'}, $money_char);
258 %    }
259 %
260 %  }
261 %
262 %  if ( $bgcolor eq $bgcolor1 ) {
263 %    $bgcolor = $bgcolor2;
264 %  } else {
265 %    $bgcolor = $bgcolor1;
266 %  }
267 %
268 %  my $charge  = exists($item->{'charge'})
269 %                  ? sprintf("$money_char\%.2f", $item->{'charge'})
270 %                  : '';
271 %
272 %  my $payment = exists($item->{'payment'})
273 %                  ? sprintf("-&nbsp;$money_char\%.2f", $item->{'payment'})
274 %                  : '';
275 %
276 %  $payment ||= sprintf( "<DEL>-&nbsp;$money_char\%.2f</DEL>",
277 %                        $item->{'void_payment'}
278 %                      )
279 %    if exists($item->{'void_payment'});
280 %
281 %  my $credit  = exists($item->{'credit'})
282 %                  ? sprintf("-&nbsp;$money_char\%.2f", $item->{'credit'})
283 %                  : '';
284 %
285 %  my $refund  = exists($item->{'refund'})
286 %                  ? sprintf("$money_char\%.2f", $item->{'refund'})
287 %                  : '';
288 %
289 %  my $target = exists($item->{'target'}) ? $item->{'target'} : '';
290 %
291 %  $balance += $item->{'charge'}  if exists $item->{'charge'};
292 %  $balance -= $item->{'payment'} if exists $item->{'payment'};
293 %  $balance -= $item->{'credit'}  if exists $item->{'credit'};
294 %  $balance += $item->{'refund'}  if exists $item->{'refund'};
295 %  $balance = sprintf("%.2f", $balance);
296 %  $balance =~ s/^\-0\.00$/0.00/; #yay ieee fp
297 %  ( my $showbalance = $money_char. $balance ) =~ s/^\$\-/-&nbsp;\$/;
298 %
299 %
300
301
302   <TR <% $display ? $display.' ID="old_history'.$old_history++.'"'  : ''%>>
303     <TD VALIGN="top" CLASS="grid" BGCOLOR="<% $bgcolor %>">
304 % unless ( !$target || $target{$target}++ ) { 
305
306         <A NAME="<% $target %>">
307 % } 
308
309       <% time2str($date_format, $item->{'date'}) %>
310 % if ( $target && $target{$target} == 1 ) { 
311
312         </A>
313 % } 
314
315       </FONT>
316     </TD>
317     <TD CLASS="grid" BGCOLOR="<% $bgcolor %>">
318       <% $item->{'desc'} %>
319     </TD>
320     <TD VALIGN="top" ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
321       <% $charge  %>
322     </TD>
323     <TD VALIGN="top" ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
324       <% $payment %>
325     </TD>
326     <TD VALIGN="top" ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
327       <% $credit  %>
328     </TD>
329     <TD VALIGN="top" ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
330       <% $refund  %>
331     </TD>
332     <TD VALIGN="top" ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
333       <% $showbalance %>
334     </TD>
335   </TR>
336 % } 
337
338 %if ( scalar(@history) && $hidden && ! $seen++ ) {
339 %  balance_forward_row($balance, $lastdate, $money_char);
340 %}
341
342 </TABLE>
343     </TD>
344   </TR>
345 </TABLE>
346
347 <SCRIPT TYPE="text/javascript">
348
349 function show_history () {
350   //alert('showing history!');
351
352   var balance_forward_row = document.getElementById('balance_forward_row');
353
354   balance_forward_row.style.display = 'none';
355   for ( var i = 0; i < <% $old_history %>; i++ ) {
356     var oldRow = document.getElementById('old_history'+i);
357     oldRow.style.display = '';
358   }
359
360 }
361
362 </SCRIPT>
363
364 <%init>
365
366 my( $cust_main ) = @_;
367 my $custnum = $cust_main->custnum;
368
369 my $conf = new FS::Conf;
370 my $date_format = $conf->config('date_format') || '%m/%d/%Y';
371
372 my $curuser = $FS::CurrentUser::CurrentUser;
373
374 my @payby = grep /\w/, $conf->config('payby');
375 #@payby = (qw( CARD DCRD CHEK DCHK LECB BILL CASH WEST COMP ))
376 @payby = (qw( CARD DCRD CHEK DCHK LECB BILL CASH COMP ))
377   unless @payby;
378 my %payby = map { $_=>1 } @payby;
379
380 my %status = (
381   'Queued'     => 'O', #Open
382   'In-transit' => 'I',
383   'Complete'   => 'R', #Resolved
384   'All'        => '',
385 );
386
387 #get payment history
388 my @history = ();
389
390 my %opt = (
391   ( map { $_ => scalar($conf->config($_)) }
392         qw( card_refund-days date_format )
393   ),
394   ( map { $_ => $conf->exists($_) } 
395         qw( deleteinvoices deletepayments deleterefunds pkg-balances )
396   )
397 );
398
399 #invoices
400 foreach my $cust_bill ($cust_main->cust_bill) {
401   push @history, {
402     'date'   => $cust_bill->_date,
403     'desc'   => include('payment_history/invoice.html', $cust_bill, %opt ),
404     'charge' => $cust_bill->charged,
405   };
406 }
407
408 #statements
409 foreach my $cust_statement ($cust_main->cust_statement) {
410   push @history, {
411     'date'   => $cust_statement->_date,
412     'desc'   => include('payment_history/statement.html', $cust_statement, %opt ),
413     #'charge' => $cust_bill->charged,
414   };
415 }
416
417 #payments (some false laziness w/credits)
418 foreach my $cust_pay ($cust_main->cust_pay) {
419   push @history, {
420     'date'    => $cust_pay->_date,
421     'desc'    => include('payment_history/payment.html', $cust_pay, %opt ),
422     'payment' => $cust_pay->paid,
423     #'target'  => $target, #XXX
424   };
425 }
426
427 #pending payments 
428 foreach my $cust_pay_pending ($cust_main->cust_pay_pending) {
429   push @history, {
430     'date'    => $cust_pay_pending->_date,
431     'desc'    => include('payment_history/pending_payment.html', $cust_pay_pending, %opt ),
432     'void_payment' => $cust_pay_pending->paid, 
433   };
434 }
435
436
437 #voided payments
438 foreach my $cust_pay_void ($cust_main->cust_pay_void) {
439   push @history, {
440     'date'   => $cust_pay_void->_date,
441     'desc'   => include('payment_history/voided_payment.html', $cust_pay_void, %opt ),
442     'void_payment' => $cust_pay_void->paid,
443   };
444
445 }
446
447 #declined payments
448 foreach my $cust_pay_pending ($cust_main->cust_pay_pending_attempt) {
449   push @history, {
450     'date'    => $cust_pay_pending->_date,
451     'desc'    => include('payment_history/attempted_payment.html', $cust_pay_pending, %opt ),
452     'void_payment' => $cust_pay_pending->paid, #??
453     #'target'  => $target, #XXX
454   };
455 }
456
457 #credits (some false laziness w/payments)
458 foreach my $cust_credit ($cust_main->cust_credit) {
459   push @history, {
460     'date'   => $cust_credit->_date,
461     'desc'   => include('payment_history/credit.html', $cust_credit, %opt ),
462     'credit' => $cust_credit->amount,
463   };
464
465 }
466
467 #refunds
468 foreach my $cust_refund ($cust_main->cust_refund) {
469   push @history, {
470     'date'   => $cust_refund->_date,
471     'desc'   => include('payment_history/refund.html', $cust_refund),
472     'refund' => $cust_refund->refund,
473   };
474
475 }
476
477 sub translate_payby {
478     my ($payby,$payinfo) = (shift,shift);
479     my %payby = (
480         BILL    => $payinfo ? emt('Check #') : '',
481         CHEK    => emt('Electronic check '),
482         PREP    => emt('Prepaid card '),
483         CARD    => emt('Credit card #'),
484         COMP    => emt('Complimentary by '),
485         CASH    => emt('Cash'),
486         WEST    => emt('Western Union'),
487         MCRD    => emt('Manual credit card'),
488     );
489     $payby = (exists $payby{$payby}) ? $payby{$payby} : $payby; 
490     $payby;
491 };
492
493 sub translate_payby_refund {
494     my ($payby,$payinfo) = (shift,shift);
495     my %payby = (
496         BILL    => $payinfo ? emt('Check #') : emt('Check'),
497         CHEK    => emt('Electronic check '),
498         CARD    => emt('Credit card #'),
499         COMP    => emt('Complimentary by '),
500     );
501     $payby = (exists $payby{$payby}) ? $payby{$payby} : $payby; 
502     $payby;
503 };
504
505 sub translate_payinfo {
506     my $object = shift;
507     my $payby = $object->payby;
508     my $payinfo = $object->payinfo;
509
510     if ( $payby eq 'CARD' ) {
511         $payinfo = $object->paymask;
512     } elsif ( $payby eq 'CHEK' ) {
513         my( $account, $aba ) = split('@', $object->paymask );
514         if ( $conf->exists('echeck-no_routing') ) {
515           $payinfo = emt("Acct #[_1]", $account);
516         } else {
517           $payinfo = emt("ABA [_1], Acct #[_2]",$aba,$account);
518         }
519     }
520
521     ($payby,$payinfo);
522 }
523
524 sub areyousure_link {
525     my ($url,$msg,$title,$label) = (shift,shift,shift,shift);
526     ' (<A HREF="javascript:areyousure(\''.$url.'\',\''.$msg.'\')" TITLE="'.$title.'">'.$label.'</A>)';
527 }
528
529 </%init>