RT#39586 Manual check refunds cannot be unapplied [source_paynum field, reason bug...
[freeside.git] / httemplate / view / cust_main / payment_history / credit.html
1 <% $credit. ' '. $reason. $desc. $change_pkg. $apply . $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                           'action'   => "${p}edit/cust_credit_refund.cgi?".
50                                         $cust_credit->crednum,
51                           'actionlabel' => emt('Apply credit to refund'),
52                           'width'    => 392,
53                       ).
54                ')';
55     }
56   }
57 } elsif (    scalar(@cust_credit_bill)   == 1
58           && scalar(@cust_credit_refund) == 0
59           && $cust_credit->credited == 0      ) {
60   #applied to one invoice, the usual situation
61   $desc .= ' '. $cust_credit_bill[0]->applied_to_invoice;
62 } elsif (    scalar(@cust_credit_bill)   == 0
63           && scalar(@cust_credit_refund) == 1
64           && $cust_credit->credited == 0      ) {
65   #applied to one refund
66   $desc .= emt(" refunded on [_1]", time2str($date_format, $cust_credit_refund[0]->_date) );
67 } else {
68   #complicated
69   $desc .= '<BR>';
70   foreach my $app ( sort { $a->_date <=> $b->_date }
71                          ( @cust_credit_bill, @cust_credit_refund ) ) {
72     if ( $app->isa('FS::cust_credit_bill') ) {
73       $desc .= '&nbsp;&nbsp;' . $opt{money_char} . $app->amount . ' '
74                     . $app->applied_to_invoice . '<BR>';
75     } elsif ( $app->isa('FS::cust_credit_refund') ) {
76       $desc .= '&nbsp;&nbsp;' .
77                emt("[_1][_2] refunded on [_3]", $opt{money_char}, $app->amount,
78                     time2str($date_format, $app->_date) ) . '<BR>';
79     } else {
80       die "$app is not a FS::cust_credit_bill or a FS::cust_credit_refund";
81     }
82   }
83   if ( $cust_credit->credited > 0 ) {
84     $desc .= '&nbsp;&nbsp;<B><FONT COLOR="#FF0000">'.
85               emt("[_1][_2] unapplied",$opt{money_char},$cust_credit->credited).
86               '</FONT></B>';
87     if ( $opt{'Apply credit'} ) {
88       if ( $opt{total_owed} > 0 ) {
89         $apply = ' ('.
90                  include( '/elements/popup_link.html',
91                             'label'       => emt('apply'),
92                             'action'      => "${p}edit/cust_credit_bill.cgi?".
93                                              $cust_credit->crednum,
94                             'actionlabel' => emt('Apply credit'),
95                             %cust_credit_bill_width,
96                             %cust_credit_bill_height,
97                         ).
98                  ')';
99       }
100       if ( $opt{total_unapplied_refunds} > 0 ) {
101         $apply.= ' ('.
102                  include( '/elements/popup_link.html',
103                             'label'       => emt('apply refund'),
104                             'action'      => "${p}edit/cust_credit_refund.cgi?".
105                                              $cust_credit->crednum,
106                             'actionlabel' => emt('Apply credit to refund'),
107                             'width'       => 392,
108                         ).
109                  ')';
110       }
111     }
112     $desc .= '<BR>';
113   }
114 }
115
116 my $change_pkg = '';
117 if ( $apply && $opt{'pkg-balances'} && $cust_credit->pkgnum ) {
118   $change_pkg =
119   ' ('. include('/elements/popup_link.html',
120                   'label'       => emt('change package'),
121                   'action'      => "${p}edit/cust_credit-pkgnum.html?crednum=".
122                                     $cust_credit->crednum,
123                   'actionlabel' => emt('Change credit package'),
124                   'width'       => 763,
125                ).
126    ')';
127 }
128
129 my $void = '';
130 $void = ' ('.
131                include( '/elements/popup_link.html',
132                     'label'    => emt('void'),
133                     'action'   => "${p}misc/void-cust_credit.html?".
134                                   $cust_credit->crednum,
135                     'actionlabel' => emt('Void credit'),
136                 ).
137           ')'
138   if $cust_credit->closed !~ /^Y/i
139   && $opt{'Void credit'};
140
141 my $unapply = '';
142
143 if ($opt{'Unapply credit'} && !$cust_credit->closed) {
144   $unapply = areyousure_link("${p}misc/unapply-cust_credit.cgi?".$cust_credit->crednum,
145                               emt('Are you sure you want to unapply this credit from invoices?'),
146                               emt('Keep this credit, but dissociate it from the invoices it is currently applied against'),
147                               emt('unapply')
148                             )
149     if @cust_credit_bill;
150   $unapply .= areyousure_link("${p}misc/unapply-cust_credit_refund.cgi?".$cust_credit->crednum,
151                                emt('Are you sure you want to unapply this credit from refunds?'),
152                                emt('Keep this credit, but dissociate it from the refunds it is currently applied to'),
153                                emt('unapply refunds')
154                              )
155     if $cust_credit->refund_to_unapply;
156 }
157
158 my $reason = $cust_credit->reason;
159 $reason = $reason ? " ($reason)" : '';
160
161 </%init>
162