experimental package balances, RT#4339
[freeside.git] / httemplate / view / cust_main / payment_history / credit.html
1 <% $pre %>Credit<% $post %>
2 by <% $cust_credit->otaker %><% "$reason$desc$apply$delete$unapply" %>
3 <%init>
4
5 my( $cust_credit, %opt ) = @_;
6
7 my $curuser = $FS::CurrentUser::CurrentUser;
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( $pre, $post, $apply, $ext ) = ( '', '', '', '' );
19 if (    scalar(@cust_credit_bill)   == 0
20      && scalar(@cust_credit_refund) == 0 ) {
21   #completely unapplied
22   $pre = '<B><FONT COLOR="#FF0000">Unapplied ';
23   $post = '</FONT></B>';
24   if ( $curuser->access_right('Apply credit') ) {
25     if ( $cust_credit->cust_main->total_owed > 0 ) {
26       $apply = ' ('.
27                include( '/elements/popup_link.html',
28                           'label'    => 'apply',
29                           'action'   => "${p}edit/cust_credit_bill.cgi?".
30                                         $cust_credit->crednum,
31                           'actionlabel' => 'Apply credit',
32                           'width'    => 392,
33                           #default# 'height' => 336,
34                       ).
35                 ')';
36     }
37     if ( $cust_credit->cust_main->total_unapplied_refunds > 0 ) {
38       $apply.= ' ('.
39                include( '/elements/popup_link.html',
40                           'label'    => 'apply to refund',
41                           'action'   => "${p}edit/cust_credit_refund.cgi?".
42                                         $cust_credit->crednum,
43                           'actionlabel' => 'Apply credit to refund',
44                           'width'    => 392,
45                           #default# 'height' => 336,
46                       ).
47                ')';
48     }
49   }
50 } elsif (    scalar(@cust_credit_bill)   == 1
51           && scalar(@cust_credit_refund) == 0
52           && $cust_credit->credited == 0      ) {
53   #applied to one invoice, the usual situation
54   $desc .= ' '. $cust_credit_bill[0]->applied_to_invoice;
55 } elsif (    scalar(@cust_credit_bill)   == 0
56           && scalar(@cust_credit_refund) == 1
57           && $cust_credit->credited == 0      ) {
58   #applied to one refund
59   $desc .= ' refunded on '.  time2str("%D", $cust_credit_refund[0]->_date);
60 } else {
61   #complicated
62   $desc .= '<BR>';
63   foreach my $app ( sort { $a->_date <=> $b->_date }
64                          ( @cust_credit_bill, @cust_credit_refund ) ) {
65     if ( $app->isa('FS::cust_credit_bill') ) {
66       $desc .= '&nbsp;&nbsp;'.
67                '$'. $app->amount.
68                ' '. $app->applied_to_invoice.
69                '<BR>';
70                #' on '. time2str("%D", $app->_date).
71     } elsif ( $app->isa('FS::cust_credit_refund') ) {
72       $desc .= '&nbsp;&nbsp;'.
73                '$'. $app->amount.
74                ' refunded on '. time2str("%D", $app->_date).
75                '<BR>';
76     } else {
77       die "$app is not a FS::cust_credit_bill or a FS::cust_credit_refund";
78     }
79   }
80   if ( $cust_credit->credited > 0 ) {
81     $desc .= '&nbsp;&nbsp;<B><FONT COLOR="#FF0000">$'.
82              $cust_credit->credited. ' unapplied</FONT></B>';
83     if ( $curuser->access_right('Apply credit') ) {
84       if ( $cust_credit->cust_main->total_owed > 0 ) {
85         $apply = ' ('.
86                  include( '/elements/popup_link.html',
87                             'label'       => 'apply',
88                             'action'      => "${p}edit/cust_credit_bill.cgi?".
89                                              $cust_credit->crednum,
90                             'actionlabel' => 'Apply credit',
91                             'width'       => 392,
92                             #default# 'height' => 336,
93                         ).
94                  ')';
95       }
96       if ( $cust_credit->cust_main->total_unapplied_refunds > 0 ) {
97         $apply.= ' ('.
98                  include( '/elements/popup_link.html',
99                             'label'       => 'apply to refund',
100                             'action'      => "${p}edit/cust_credit_refund.cgi?".
101                                              $cust_credit->crednum,
102                             'actionlabel' => 'Apply credit to refund',
103                             'width'       => 392,
104                             #default# 'height' => 336,
105                         ).
106                  ')';
107       }
108     }
109     $desc .= '<BR>';
110   }
111 }
112 #
113 my $delete = '';
114 if ( $cust_credit->closed !~ /^Y/i
115
116      #s'pose deleting a credit isn't bad like deleting a payment
117      # and this needs to be generally available until we have credit voiding..
118      #&& $conf->exists('deletecredits')
119
120      && $curuser->access_right('Delete credit')
121    )
122 {
123   $delete = qq! (<A HREF="javascript:areyousure('!.
124             qq!${p}misc/delete-cust_credit.cgi?!. $cust_credit->crednum.
125             qq!', 'Are you sure you want to delete this credit?')">!.
126             qq!delete</A>)!;
127 }
128
129 my $unapply = '';
130 if (    $cust_credit->closed !~ /^Y/i
131      && scalar(@cust_credit_bill)
132      && $curuser->access_right('Unapply credit')
133    )
134 {
135   $unapply = qq! (<A HREF="javascript:areyousure('!.
136              qq!${p}misc/unapply-cust_credit.cgi?!. $cust_credit->crednum.
137              qq!', 'Are you sure you want to unapply this credit?')">!.
138              qq!unapply</A>)!;
139 }
140
141 my $reason = $cust_credit->reason
142                ? ' ('. $cust_credit->reason. ')'
143                : '';
144
145 </%init>
146