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