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