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