RT#39586 Manual check refunds cannot be unapplied [better whitespace handling]
[freeside.git] / httemplate / view / cust_main / payment_history / credit.html
1 <% $credit. ' '. $reason. $desc. $change_pkg. $apply. $delete. $unapply. $void %>
2 <%init>
3
4 my( $cust_credit, %opt ) = @_;
5
6 my $date_format = $opt{'date_format'} || '%m/%d/%Y';
7
8 my @cust_credit_bill = $cust_credit->cust_credit_bill;
9 my @cust_credit_refund = $cust_credit->cust_credit_refund;
10
11 my $desc = '';
12 if ( $opt{'pkg-balances'} && $cust_credit->pkgnum ) {
13   my $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $cust_credit->pkgnum } );
14   $desc .= ' for '. $cust_pkg->pkg_label_long;
15 }
16
17 my %cust_credit_bill_width = ('width' => 392);
18 my %cust_credit_bill_height = ();
19 if ( $opt{'cust_credit_bill_pkg-manual'} ) {
20   %cust_credit_bill_width = ('width' => 592);
21   %cust_credit_bill_height = ('height' => 436);
22 }
23
24 my( $apply, $ext ) = ( '', '', '', '' );
25 my $credit = emt("Credit by [_1]", $cust_credit->otaker );
26 if (    scalar(@cust_credit_bill)   == 0
27      && scalar(@cust_credit_refund) == 0 ) {
28   #completely unapplied
29   $credit = '<B><FONT COLOR="#FF0000">'
30         . emt("Unapplied Credit by [_1]", $cust_credit->otaker )
31         . '</FONT></B>';
32   if ( $opt{'Apply credit'} ) {
33     if ( $opt{total_owed} > 0 ) {
34       $apply = ' ('.
35                include( '/elements/popup_link.html',
36                           'label'    => emt('apply'),
37                           'action'   => "${p}edit/cust_credit_bill.cgi?".
38                                         $cust_credit->crednum,
39                           'actionlabel' => emt('Apply credit'),
40                           %cust_credit_bill_width,
41                           %cust_credit_bill_height,
42                       ).
43                 ')';
44     }
45     if ( $opt{total_unapplied_refunds} > 0 ) {
46       $apply.= ' ('.
47                include( '/elements/popup_link.html',
48                           'label'    => emt('apply refund'),
49                           'style'    => 'white-space: nowrap',
50                           'action'   => "${p}edit/cust_credit_refund.cgi?".
51                                         $cust_credit->crednum,
52                           'actionlabel' => emt('Apply credit to refund'),
53                           'width'    => 392,
54                       ).
55                ')';
56     }
57   }
58 } elsif (    scalar(@cust_credit_bill)   == 1
59           && scalar(@cust_credit_refund) == 0
60           && $cust_credit->credited == 0      ) {
61   #applied to one invoice, the usual situation
62   $desc .= ' '. $cust_credit_bill[0]->applied_to_invoice;
63 } elsif (    scalar(@cust_credit_bill)   == 0
64           && scalar(@cust_credit_refund) == 1
65           && $cust_credit->credited == 0      ) {
66   #applied to one refund
67   $desc .= emt(" refunded on [_1]", time2str($date_format, $cust_credit_refund[0]->_date) );
68 } else {
69   #complicated
70   $desc .= '<BR>';
71   foreach my $app ( sort { $a->_date <=> $b->_date }
72                          ( @cust_credit_bill, @cust_credit_refund ) ) {
73     if ( $app->isa('FS::cust_credit_bill') ) {
74       $desc .= '&nbsp;&nbsp;' . $opt{money_char} . $app->amount . ' '
75                     . $app->applied_to_invoice . '<BR>';
76     } elsif ( $app->isa('FS::cust_credit_refund') ) {
77       $desc .= '&nbsp;&nbsp;' .
78                emt("[_1][_2] refunded on [_3]", $opt{money_char}, $app->amount,
79                     time2str($date_format, $app->_date) ) . '<BR>';
80     } else {
81       die "$app is not a FS::cust_credit_bill or a FS::cust_credit_refund";
82     }
83   }
84   if ( $cust_credit->credited > 0 ) {
85     $desc .= '&nbsp;&nbsp;<B><FONT COLOR="#FF0000">'.
86               emt("[_1][_2] unapplied",$opt{money_char},$cust_credit->credited).
87               '</FONT></B>';
88     if ( $opt{'Apply credit'} ) {
89       if ( $opt{total_owed} > 0 ) {
90         $apply = ' ('.
91                  include( '/elements/popup_link.html',
92                             'label'       => emt('apply'),
93                             'action'      => "${p}edit/cust_credit_bill.cgi?".
94                                              $cust_credit->crednum,
95                             'actionlabel' => emt('Apply credit'),
96                             %cust_credit_bill_width,
97                             %cust_credit_bill_height,
98                         ).
99                  ')';
100       }
101       if ( $opt{total_unapplied_refunds} > 0 ) {
102         $apply.= ' ('.
103                  include( '/elements/popup_link.html',
104                             'label'       => emt('apply refund'),
105                             'style'       => 'white-space: nowrap',
106                             'action'      => "${p}edit/cust_credit_refund.cgi?".
107                                              $cust_credit->crednum,
108                             'actionlabel' => emt('Apply credit to refund'),
109                             'width'       => 392,
110                         ).
111                  ')';
112       }
113     }
114     $desc .= '<BR>';
115   }
116 }
117
118 my $change_pkg = '';
119 if ( $apply && $opt{'pkg-balances'} && $cust_credit->pkgnum ) {
120   $change_pkg =
121   ' ('. include('/elements/popup_link.html',
122                   'label'       => emt('change package'),
123                   'action'      => "${p}edit/cust_credit-pkgnum.html?crednum=".
124                                     $cust_credit->crednum,
125                   'actionlabel' => emt('Change credit package'),
126                   'width'       => 763,
127                ).
128    ')';
129 }
130
131 my $void = '';
132 $void = ' ('.
133                include( '/elements/popup_link.html',
134                     'label'    => emt('void'),
135                     'action'   => "${p}misc/void-cust_credit.html?".
136                                   $cust_credit->crednum,
137                     'actionlabel' => emt('Void credit'),
138                 ).
139           ')'
140   if $cust_credit->closed !~ /^Y/i
141   && $opt{'Void credit'};
142
143 my $delete = '';
144 $delete = areyousure_link("${p}misc/delete-cust_credit.cgi?".$cust_credit->crednum,
145                             emt('Are you sure you want to delete this credit?'),
146                             '',
147                             emt('delete')
148                           )
149   if $cust_credit->closed !~ /^Y/i
150   && $opt{'Delete credit'};
151
152 my $unapply = '';
153
154 if ($opt{'Unapply credit'} && !$cust_credit->closed) {
155   $unapply = areyousure_link("${p}misc/unapply-cust_credit.cgi?".$cust_credit->crednum,
156                               emt('Are you sure you want to unapply this credit from invoices?'),
157                               emt('Keep this credit, but dissociate it from the invoices it is currently applied against'),
158                               emt('unapply')
159                             )
160     if @cust_credit_bill;
161   $unapply .= areyousure_link("${p}misc/unapply-cust_credit_refund.cgi?".$cust_credit->crednum,
162                                emt('Are you sure you want to unapply this credit from refunds?'),
163                                emt('Keep this credit, but dissociate it from the refunds it is currently applied to'),
164                                emt('unapply refunds')
165                              )
166     if $cust_credit->refund_to_unapply;
167 }
168
169 my $reason = $cust_credit->reason;
170 $reason = $reason ? " ($reason)" : '';
171
172 </%init>
173