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