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