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