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