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