optimize customer view: avoid looking up config values inside loops, RT#4728
[freeside.git] / httemplate / view / cust_main / payment_history / payment.html
1 <% $pre %>Payment<% $post %> by <% $otaker %>
2 <% "$info$desc$view$apply$refund$void$delete$unapply" %>
3 <%init>
4
5 my( $cust_pay, %opt ) = @_;
6
7 my $curuser = $FS::CurrentUser::CurrentUser;
8
9 my $payby = $cust_pay->payby;
10
11 my $payinfo;
12 if ( $payby eq 'CARD' ) {
13   $payinfo = $cust_pay->paymask;
14 } elsif ( $payby eq 'CHEK' ) {
15   my( $account, $aba ) = split('@', $cust_pay->paymask );
16   $payinfo = "ABA $aba, Acct #$account";
17 } else {
18   $payinfo = $cust_pay->payinfo;
19 }
20 my @cust_bill_pay = $cust_pay->cust_bill_pay;
21 my @cust_pay_refund = $cust_pay->cust_pay_refund;
22
23 my $target = "$payby$payinfo";
24 $payby =~ s/^BILL$/Check #/ if $payinfo;
25 $payby =~ s/^CHEK$/Electronic check /;
26 $payby =~ s/^PREP$/Prepaid card /;
27 $payby =~ s/^CARD$/Credit card #/; 
28 $payby =~ s/^COMP$/Complimentary by /; 
29 $payby =~ s/^CASH$/Cash/;
30 $payby =~ s/^WEST$/Western Union/;
31 $payby =~ s/^MCRD$/Manual credit card/;
32 $payby =~ s/^BILL$//;
33 my $info = $payby ? "($payby$payinfo)" : '';
34
35 my( $pre, $post, $desc, $apply, $ext ) = ( '', '', '', '', '' );
36 if (    scalar(@cust_bill_pay)   == 0
37      && scalar(@cust_pay_refund) == 0 ) {
38   #completely unapplied
39   $pre = '<B><FONT COLOR="#FF0000">Unapplied ';
40   $post = '</FONT></B>';
41   if ( $curuser->access_right('Apply payment') ) {
42     if ( $cust_pay->cust_main->total_owed > 0 ) {
43       $apply = ' ('.
44                include( '/elements/popup_link.html',
45                           'label'       => 'apply',
46                           'action'      => "${p}edit/cust_bill_pay.cgi?".
47                                            $cust_pay->paynum,
48                           'actionlabel' => 'Apply payment',
49                           'width'       => 392,
50                           #default# 'height' => 336,
51                       ).
52                 ')';
53     }
54     if ( $cust_pay->cust_main->total_unapplied_refunds > 0 ) {
55       $apply.= ' ('.
56                include( '/elements/popup_link.html',
57                           'label'       => 'apply to refund',
58                           'action'      => "${p}edit/cust_pay_refund.cgi?".
59                                            $cust_pay->paynum,
60                           'actionlabel' => 'Apply payment to refund',
61                           'width'       => 392,
62                           #default# 'height' => 336,
63                       ).
64                ')';
65     }
66   }
67 } elsif (    scalar(@cust_bill_pay)   == 1
68           && scalar(@cust_pay_refund) == 0
69           && $cust_pay->unapplied == 0     ) {
70   #applied to one invoice, the usual situation
71   $desc = ' '. $cust_bill_pay[0]->applied_to_invoice;
72 } elsif (    scalar(@cust_bill_pay)   == 0
73           && scalar(@cust_pay_refund) == 1
74           && $cust_pay->unapplied == 0     ) {
75   #applied to one refund
76   $desc = ' refunded on '. time2str("%D", $cust_pay_refund[0]->_date);
77 } else {
78   #complicated
79   $desc = '<BR>';
80   foreach my $app ( sort { $a->_date <=> $b->_date }
81                          ( @cust_bill_pay, @cust_pay_refund ) ) {
82     if ( $app->isa('FS::cust_bill_pay') ) {
83       $desc .= '&nbsp;&nbsp;'.
84                '$'. $app->amount.
85                ' '. $app->applied_to_invoice.
86                '<BR>';
87                #' on '. time2str("%D", $cust_bill_pay->_date).
88     } elsif ( $app->isa('FS::cust_pay_refund') ) {
89       $desc .= '&nbsp;&nbsp;'.
90                '$'. $app->amount.
91                ' refunded on '. time2str("%D", $app->_date).
92                '<BR>';
93     } else {
94       die "$app is not a FS::cust_bill_pay or FS::cust_pay_refund";
95     }
96   }
97   if ( $cust_pay->unapplied > 0 ) {
98     $desc .= '&nbsp;&nbsp;'.
99              '<B><FONT COLOR="#FF0000">$'.
100              $cust_pay->unapplied. ' unapplied</FONT></B>';
101     if ( $curuser->access_right('Apply payment') ) {
102       if ( $cust_pay->cust_main->total_owed > 0 ) {
103         $apply = ' ('.
104                  include( '/elements/popup_link.html',
105                             'label'      => 'apply',
106                             'action'     => "${p}edit/cust_bill_pay.cgi?".
107                                             $cust_pay->paynum,
108                             'actionlabel' => 'Apply payment',
109                             'width'      => 392,
110                             #default# 'height' => 336,
111                         ).
112                  ')';
113       }
114       if ( $cust_pay->cust_main->total_unapplied_refunds > 0 ) {
115         $apply.= ' ('.
116                  include( '/elements/popup_link.html',
117                             'label'      => 'apply to refund',
118                             'action'     => "${p}edit/cust_pay_refund.cgi?".
119                                             $cust_pay->paynum,
120                             'actionlabel' => 'Apply payment to refund',
121                             'width'      => 392,
122                             #default# 'height' => 336,
123                         ).
124                  ')';
125       }
126     }
127     $desc .= '<BR>';
128   }
129 }
130
131 my $view =
132   ' ('. include('/elements/popup_link.html',
133                   'label'     => 'view receipt',
134                   'action'    => "${p}view/cust_pay.html?link=popup;paynum=".
135                                   $cust_pay->paynum,
136                   'actionlabel' => 'Payment Receipt',
137                ).
138    ')';
139
140 my $refund = '';
141 my $refund_days = $opt{'card_refund-days'} || 120;
142 if (    $cust_pay->closed !~ /^Y/i
143      && $cust_pay->payby =~ /^(CARD|CHEK)$/
144      && time-$cust_pay->_date < $refund_days*86400
145      && $cust_pay->unrefunded > 0
146      && $curuser->access_right('Refund payment')
147 ) {
148   $refund = qq! (<A HREF="${p}edit/cust_refund.cgi?payby=$1;!.
149             qq!paynum=!. $cust_pay->paynum. '"'.
150             qq! TITLE="Send a refund for this payment to the payment gateway"!.
151             qq!>refund</A>)!;
152 }
153
154 my $void = '';
155 if (    $cust_pay->closed !~ /^Y/i
156      && (    ( $cust_pay->payby eq 'CARD'
157                && $curuser->access_right('Credit card void')
158              )
159           || ( $cust_pay->payby eq 'CHEK'
160                && $curuser->access_right('Echeck void')
161              )
162           || ( $cust_pay->payby !~ /^(CARD|CHEK)$/
163                && $curuser->access_right('Regular void')
164              )
165         )
166    )
167 {
168   $void = qq! (<A HREF="javascript:areyousure('!.
169           qq!${p}misc/void-cust_pay.cgi?!. $cust_pay->paynum.
170           qq!', 'Are you sure you want to void this payment?')"!.
171           qq! TITLE="Void this payment from the database!.
172             ( $cust_pay->payby =~ /^(CARD|CHEK)$/
173               ? ' (do not send anything to the payment gateway)'
174               : '' 
175             ). '"'.
176           qq!>void</A>)!;
177 }
178
179 my $delete = '';
180 if ( $cust_pay->closed !~ /^Y/i
181      && $opt{'deletepayments'}
182      && $curuser->access_right('Delete payment')
183    )
184 {
185   $delete = qq! (<A HREF="javascript:areyousure('!.
186             qq!${p}misc/delete-cust_pay.cgi?!. $cust_pay->paynum.
187             qq!', 'Are you sure you want to delete this payment?')"!.
188             qq! TITLE="Delete this payment from the database completely - not recommended"!.
189             qq!>delete</A>)!;
190 }
191
192 my $unapply = '';
193 if (    $cust_pay->closed !~ /^Y/i
194      && scalar(@cust_bill_pay)           
195      && $curuser->access_right('Unapply payment')
196    )
197 {
198   $unapply = qq! (<A HREF="javascript:areyousure('!.
199              qq!${p}misc/unapply-cust_pay.cgi?!. $cust_pay->paynum.
200              qq!', 'Are you sure you want to unapply this payment?')"!.
201              qq! TITLE="Keep this payment, but dissociate it from the invoices it is currently applied against"!.
202              qq!>unapply</A>)!;
203 }
204
205 my $otaker = $cust_pay->otaker;
206 $otaker = '<i>auto billing</i>'          if $otaker eq 'fs_daily';
207 $otaker = '<i>customer self-service</i>' if $otaker eq 'fs_selfservice';
208
209 </%init>