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