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