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