88bbe9bd904b1470d2cf84824cc4b2a6d1303bcb
[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 $conf = new FS::Conf;
8 my $curuser = $FS::CurrentUser::CurrentUser;
9
10 my @cust_credit_bill = $cust_credit->cust_credit_bill;
11 my @cust_credit_refund = $cust_credit->cust_credit_refund;
12
13 my $desc = '';
14 if ( $opt{'pkg-balances'} && $cust_credit->pkgnum ) {
15   my $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $cust_credit->pkgnum } );
16   $desc .= ' for '. $cust_pkg->pkg_label_long;
17 }
18
19 my %cust_credit_bill_width = ('width' => 392);
20 my %cust_credit_bill_height = ();
21 if ($conf->exists('cust_credit_bill_pkg-manual')) {
22   %cust_credit_bill_width = ('width' => 592);
23   %cust_credit_bill_height = ('height' => 436);
24 }
25
26 my( $pre, $post, $apply, $ext ) = ( '', '', '', '' );
27 if (    scalar(@cust_credit_bill)   == 0
28      && scalar(@cust_credit_refund) == 0 ) {
29   #completely unapplied
30   $pre = '<B><FONT COLOR="#FF0000">Unapplied ';
31   $post = '</FONT></B>';
32   if ( $curuser->access_right('Apply credit') ) {
33     if ( $cust_credit->cust_main->total_owed > 0 ) {
34       $apply = ' ('.
35                include( '/elements/popup_link.html',
36                           'label'    => 'apply',
37                           'action'   => "${p}edit/cust_credit_bill.cgi?".
38                                         $cust_credit->crednum,
39                           'actionlabel' => 'Apply credit',
40                           %cust_credit_bill_width,
41                           %cust_credit_bill_height,
42                       ).
43                 ')';
44     }
45     if ( $cust_credit->cust_main->total_unapplied_refunds > 0 ) {
46       $apply.= ' ('.
47                include( '/elements/popup_link.html',
48                           'label'    => 'apply to refund',
49                           'action'   => "${p}edit/cust_credit_refund.cgi?".
50                                         $cust_credit->crednum,
51                           'actionlabel' => 'Apply credit to refund',
52                           'width'    => 392,
53                           #default# 'height' => 336,
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 .= ' refunded on '.  time2str("%D", $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;'.
75                '$'. $app->amount.
76                ' '. $app->applied_to_invoice.
77                '<BR>';
78                #' on '. time2str("%D", $app->_date).
79     } elsif ( $app->isa('FS::cust_credit_refund') ) {
80       $desc .= '&nbsp;&nbsp;'.
81                '$'. $app->amount.
82                ' refunded on '. time2str("%D", $app->_date).
83                '<BR>';
84     } else {
85       die "$app is not a FS::cust_credit_bill or a FS::cust_credit_refund";
86     }
87   }
88   if ( $cust_credit->credited > 0 ) {
89     $desc .= '&nbsp;&nbsp;<B><FONT COLOR="#FF0000">$'.
90              $cust_credit->credited. ' unapplied</FONT></B>';
91     if ( $curuser->access_right('Apply credit') ) {
92       if ( $cust_credit->cust_main->total_owed > 0 ) {
93         $apply = ' ('.
94                  include( '/elements/popup_link.html',
95                             'label'       => 'apply',
96                             'action'      => "${p}edit/cust_credit_bill.cgi?".
97                                              $cust_credit->crednum,
98                             'actionlabel' => 'Apply credit',
99                             %cust_credit_bill_width,
100                             %cust_credit_bill_height,
101                         ).
102                  ')';
103       }
104       if ( $cust_credit->cust_main->total_unapplied_refunds > 0 ) {
105         $apply.= ' ('.
106                  include( '/elements/popup_link.html',
107                             'label'       => 'apply to refund',
108                             'action'      => "${p}edit/cust_credit_refund.cgi?".
109                                              $cust_credit->crednum,
110                             'actionlabel' => 'Apply credit to refund',
111                             'width'       => 392,
112                             #default# 'height' => 336,
113                         ).
114                  ')';
115       }
116     }
117     $desc .= '<BR>';
118   }
119 }
120 #
121 my $delete = '';
122 if ( $cust_credit->closed !~ /^Y/i
123
124      #s'pose deleting a credit isn't bad like deleting a payment
125      # and this needs to be generally available until we have credit voiding..
126      #&& $conf->exists('deletecredits')
127
128      && $curuser->access_right('Delete credit')
129    )
130 {
131   $delete = qq! (<A HREF="javascript:areyousure('!.
132             qq!${p}misc/delete-cust_credit.cgi?!. $cust_credit->crednum.
133             qq!', 'Are you sure you want to delete this credit?')">!.
134             qq!delete</A>)!;
135 }
136
137 my $unapply = '';
138 if (    $cust_credit->closed !~ /^Y/i
139      && scalar(@cust_credit_bill)
140      && $curuser->access_right('Unapply credit')
141    )
142 {
143   $unapply = qq! (<A HREF="javascript:areyousure('!.
144              qq!${p}misc/unapply-cust_credit.cgi?!. $cust_credit->crednum.
145              qq!', 'Are you sure you want to unapply this credit?')">!.
146              qq!unapply</A>)!;
147 }
148
149 my $reason = $cust_credit->reason
150                ? ' ('. $cust_credit->reason. ')'
151                : '';
152
153 </%init>
154