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