optimize customer view (esp. with lots of commission credits), RT#26416
[freeside.git] / httemplate / view / cust_main / payment_history / credit.html
1 <% $credit %>
2 <% "$reason$desc$apply$delete$unapply" %>
3 <%init>
4
5 my( $cust_credit, %opt ) = @_;
6
7 my $date_format = $opt{'date_format'} || '%m/%d/%Y';
8
9 my @cust_credit_bill = $cust_credit->cust_credit_bill;
10 my @cust_credit_refund = $cust_credit->cust_credit_refund;
11
12 my $desc = '';
13 if ( $opt{'pkg-balances'} && $cust_credit->pkgnum ) {
14   my $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $cust_credit->pkgnum } );
15   $desc .= ' for '. $cust_pkg->pkg_label_long;
16 }
17
18 my %cust_credit_bill_width = ('width' => 392);
19 my %cust_credit_bill_height = ();
20 if ( $opt{'cust_credit_bill_pkg-manual'} ) {
21   %cust_credit_bill_width = ('width' => 592);
22   %cust_credit_bill_height = ('height' => 436);
23 }
24
25 my( $apply, $ext ) = ( '', '', '', '' );
26 my $credit = emt("Credit by [_1]", $cust_credit->otaker );
27 if (    scalar(@cust_credit_bill)   == 0
28      && scalar(@cust_credit_refund) == 0 ) {
29   #completely unapplied
30   $credit = '<B><FONT COLOR="#FF0000">'
31         . emt("Unapplied Credit by [_1]", $cust_credit->otaker )
32         . '</FONT></B>';
33   if ( $opt{'Apply credit'} ) {
34     if ( $opt{total_owed} > 0 ) {
35       $apply = ' ('.
36                include( '/elements/popup_link.html',
37                           'label'    => emt('apply'),
38                           'action'   => "${p}edit/cust_credit_bill.cgi?".
39                                         $cust_credit->crednum,
40                           'actionlabel' => emt('Apply credit'),
41                           %cust_credit_bill_width,
42                           %cust_credit_bill_height,
43                       ).
44                 ')';
45     }
46     if ( $opt{total_unapplied_refunds} > 0 ) {
47       $apply.= ' ('.
48                include( '/elements/popup_link.html',
49                           'label'    => emt('apply to refund'),
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 to refund'),
105                             'action'      => "${p}edit/cust_credit_refund.cgi?".
106                                              $cust_credit->crednum,
107                             'actionlabel' => emt('Apply credit to refund'),
108                             'width'       => 392,
109                         ).
110                  ')';
111       }
112     }
113     $desc .= '<BR>';
114   }
115 }
116 #
117 my $delete = '';
118 $delete = areyousure_link("${p}misc/delete-cust_credit.cgi?".$cust_credit->crednum,
119                             emt('Are you sure you want to delete this credit?'),
120                             '',
121                             emt('delete')
122                           )
123   if $cust_credit->closed !~ /^Y/i
124   && $opt{'Delete credit'};
125
126 my $unapply = '';
127 $unapply = areyousure_link("${p}misc/unapply-cust_credit.cgi?".$cust_credit->crednum,
128                             emt('Are you sure you want to unapply this credit?'),
129                             '',
130                             emt('unapply')
131                           )
132   if $cust_credit->closed !~ /^Y/i
133   && scalar(@cust_credit_bill)
134   && $opt{'Unapply credit'};
135
136 my $reason = $cust_credit->reason;
137 $reason = $reason ? " ($reason)" : '';
138
139 </%init>
140