b9a06946d77110c3e357267d53cb1c5c90dfbbf8
[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     $apply = ' ('. include( '/elements/popup_link.html',
44                               'label'       => 'apply',
45                               'action'      => "${p}edit/cust_bill_pay.cgi?".
46                                                $cust_pay->paynum,
47                               'actionlabel' => 'Apply payment',
48                               'width'       => 392,
49                               #default# 'height' => 336,
50                           ).
51               ')';
52     #XXX if there are any open refunds
53     $apply.= ' ('. include( '/elements/popup_link.html',
54                               'label'       => 'apply to refund',
55                               'action'      => "${p}edit/cust_pay_refund.cgi?".
56                                                $cust_pay->paynum,
57                               'actionlabel' => 'Apply payment to refund',
58                               'width'       => 392,
59                               #default# 'height' => 336,
60                           ).
61               ')';
62   }
63 } elsif (    scalar(@cust_bill_pay)   == 1
64           && scalar(@cust_pay_refund) == 0
65           && $cust_pay->unapplied == 0     ) {
66   #applied to one invoice, the usual situation
67   $desc = ' '. $cust_bill_pay[0]->applied_to_invoice;
68 } elsif (    scalar(@cust_bill_pay)   == 0
69           && scalar(@cust_pay_refund) == 1
70           && $cust_pay->unapplied == 0     ) {
71   #applied to one refund
72   $desc = ' refunded on '. time2str("%D", $cust_pay_refund[0]->_date);
73 } else {
74   #complicated
75   $desc = '<BR>';
76   foreach my $app ( sort { $a->_date <=> $b->_date }
77                          ( @cust_bill_pay, @cust_pay_refund ) ) {
78     if ( $app->isa('FS::cust_bill_pay') ) {
79       $desc .= '&nbsp;&nbsp;'.
80                '$'. $app->amount.
81                ' '. $app->applied_to_invoice.
82                '<BR>';
83                #' on '. time2str("%D", $cust_bill_pay->_date).
84     } elsif ( $app->isa('FS::cust_pay_refund') ) {
85       $desc .= '&nbsp;&nbsp;'.
86                '$'. $app->amount.
87                ' refunded on '. time2str("%D", $app->_date).
88                '<BR>';
89     } else {
90       die "$app is not a FS::cust_bill_pay or FS::cust_pay_refund";
91     }
92   }
93   if ( $cust_pay->unapplied > 0 ) {
94     $desc .= '&nbsp;&nbsp;'.
95              '<B><FONT COLOR="#FF0000">$'.
96              $cust_pay->unapplied. ' unapplied</FONT></B>';
97     if ( $curuser->access_right('Apply payment') ) {
98       $apply = ' ('. include( '/elements/popup_link.html',
99                                 'label'      => 'apply',
100                                 'action'     => "${p}edit/cust_bill_pay.cgi?".
101                                                 $cust_pay->paynum,
102                                 'actionlabel' => 'Apply payment',
103                                 'width'      => 392,
104                                 #default# 'height' => 336,
105                            ).
106                ')';
107       #XXX if there are any open refunds
108       $apply.= ' ('. include( '/elements/popup_link.html',
109                                 'label'      => 'apply to refund',
110                                 'action'     => "${p}edit/cust_pay_refund.cgi?".
111                                                 $cust_pay->paynum,
112                                 'actionlabel' => 'Apply payment to refund',
113                                 'width'      => 392,
114                                 #default# 'height' => 336,
115                            ).
116                ')';
117     }
118     $desc .= '<BR>';
119   }
120 }
121
122 my $view =
123   ' ('. include('/elements/popup_link.html',
124                   'label'     => 'view receipt',
125                   'action'    => "${p}view/cust_pay.html?link=popup;paynum=".
126                                   $cust_pay->paynum,
127                   'actionlabel' => 'Payment Receipt',
128                ).
129    ')';
130
131 my $refund = '';
132 my $refund_days = $conf->config('card_refund-days') || 120;
133 if (    $cust_pay->closed !~ /^Y/i
134      && $cust_pay->payby =~ /^(CARD|CHEK)$/
135      && time-$cust_pay->_date < $refund_days*86400
136      && $cust_pay->unrefunded > 0
137      && $curuser->access_right('Refund payment')
138 ) {
139   $refund = qq! (<A HREF="${p}edit/cust_refund.cgi?payby=$1;!.
140             qq!paynum=!. $cust_pay->paynum. '"'.
141             qq! TITLE="Send a refund for this payment to the payment gateway"!.
142             qq!>refund</A>)!;
143 }
144
145 my $void = '';
146 if (    $cust_pay->closed !~ /^Y/i
147      && (    ( $cust_pay->payby eq 'CARD'
148                && $curuser->access_right('Credit card void')
149              )
150           || ( $cust_pay->payby eq 'CHEK'
151                && $curuser->access_right('Echeck void')
152              )
153           || ( $cust_pay->payby !~ /^(CARD|CHEK)$/
154                && $curuser->access_right('Regular void')
155              )
156         )
157    )
158 {
159   $void = qq! (<A HREF="javascript:areyousure('!.
160           qq!${p}misc/void-cust_pay.cgi?!. $cust_pay->paynum.
161           qq!', 'Are you sure you want to void this payment?')"!.
162           qq! TITLE="Void this payment from the database!.
163             ( $cust_pay->payby =~ /^(CARD|CHEK)$/
164               ? ' (do not send anything to the payment gateway)'
165               : '' 
166             ). '"'.
167           qq!>void</A>)!;
168 }
169
170 my $delete = '';
171 if ( $cust_pay->closed !~ /^Y/i
172      && $conf->exists('deletepayments')
173      && $curuser->access_right('Delete payment')
174    )
175 {
176   $delete = qq! (<A HREF="javascript:areyousure('!.
177             qq!${p}misc/delete-cust_pay.cgi?!. $cust_pay->paynum.
178             qq!', 'Are you sure you want to delete this payment?')"!.
179             qq! TITLE="Delete this payment from the database completely - not recommended"!.
180             qq!>delete</A>)!;
181 }
182
183 my $unapply = '';
184 if (    $cust_pay->closed !~ /^Y/i
185      && scalar(@cust_bill_pay)           
186      && $curuser->access_right('Unapply payment')
187    )
188 {
189   $unapply = qq! (<A HREF="javascript:areyousure('!.
190              qq!${p}misc/unapply-cust_pay.cgi?!. $cust_pay->paynum.
191              qq!', 'Are you sure you want to unapply this payment?')"!.
192              qq! TITLE="Keep this payment, but dissociate it from the invoices it is currently applied against"!.
193              qq!>unapply</A>)!;
194 }
195
196 my $otaker = $cust_pay->otaker;
197 $otaker = '<i>auto billing</i>'          if $otaker eq 'fs_daily';
198 $otaker = '<i>customer self-service</i>' if $otaker eq 'fs_selfservice';
199
200 </%init>