summaryrefslogtreecommitdiff
path: root/httemplate/view/cust_main/payment_history/credit.html
blob: bf769455b3b62b437bc518cb61716ebc1c6ff36d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<% $pre %>Credit<% $post %>
by <% $cust_credit->otaker %><% "$reason$desc$apply$delete$unapply" %>
<%init>

my( $cust_credit, %opt ) = @_;

my $curuser = $FS::CurrentUser::CurrentUser;

my @cust_credit_bill = $cust_credit->cust_credit_bill;
my @cust_credit_refund = $cust_credit->cust_credit_refund;

my( $pre, $post, $desc, $apply, $ext ) = ( '', '', '', '', '' );
if (    scalar(@cust_credit_bill)   == 0
     && scalar(@cust_credit_refund) == 0 ) {
  #completely unapplied
  $pre = '<B><FONT COLOR="#FF0000">Unapplied ';
  $post = '</FONT></B>';
  if ( $curuser->access_right('Apply credit') ) {
    $apply = ' ('. include( '/elements/popup_link.html',
                              'label'    => 'apply',
                              'action'   => "${p}edit/cust_credit_bill.cgi?".
                                            $cust_credit->crednum,
                              'actionlabel' => 'Apply credit',
                              'width'    => 392,
                              #default# 'height' => 336,
                          ).
              ')';
  }
} elsif (    scalar(@cust_credit_bill)   == 1
          && scalar(@cust_credit_refund) == 0
          && $cust_credit->credited == 0      ) {
  #applied to one invoice, the usual situation
  $desc = ' '. $cust_credit_bill[0]->applied_to_invoice;
} elsif (    scalar(@cust_credit_bill)   == 0
          && scalar(@cust_credit_refund) == 1
          && $cust_credit->credited == 0      ) {
  #applied to one refund
  $desc = ' refunded on '.  time2str("%D", $cust_credit_refund[0]->_date);
} else {
  #complicated
  $desc = '<BR>';
  foreach my $app ( sort { $a->_date <=> $b->_date }
                         ( @cust_credit_bill, @cust_credit_refund ) ) {
    if ( $app->isa('FS::cust_credit_bill') ) {
      $desc .= '&nbsp;&nbsp;'.
               '$'. $app->amount.
               ' '. $app->applied_to_invoice.
               '<BR>';
               #' on '. time2str("%D", $app->_date).
    } elsif ( $app->isa('FS::cust_credit_refund') ) {
      $desc .= '&nbsp;&nbsp;'.
               '$'. $app->amount.
               ' refunded on '. time2str("%D", $app->_date).
               '<BR>';
    } else {
      die "$app is not a FS::cust_credit_bill or a FS::cust_credit_refund";
    }
  }
  if ( $cust_credit->credited > 0 ) {
    $desc .= '&nbsp;&nbsp;<B><FONT COLOR="#FF0000">$'.
             $cust_credit->credited. ' unapplied</FONT></B>';
    if ( $curuser->access_right('Apply credit') ) {
      $desc = ' ('. include( '/elements/popup_link.html',
                              'label'       => 'apply',
                              'action'      => "${p}edit/cust_credit_bill.cgi?".
                                               $cust_credit->crednum,
                              'actionlabel' => 'Apply credit',
                              'width'       => 392,
                              #default# 'height' => 336,
                          ).
              ')';
    }
    $desc .= '<BR>';
  }
}
#
my $delete = '';
if ( $cust_credit->closed !~ /^Y/i

     #s'pose deleting a credit isn't bad like deleting a payment
     # and this needs to be generally available until we have credit voiding..
     #&& $conf->exists('deletecredits')

     && $curuser->access_right('Delete credit')
   )
{
  $delete = qq! (<A HREF="javascript:areyousure('!.
            qq!${p}misc/delete-cust_credit.cgi?!. $cust_credit->crednum.
            qq!', 'Are you sure you want to delete this credit?')">!.
            qq!delete</A>)!;
}

my $unapply = '';
if (    $cust_credit->closed !~ /^Y/i
     && scalar(@cust_credit_bill)
     && $curuser->access_right('Unapply credit')
   )
{
  $unapply = qq! (<A HREF="javascript:areyousure('!.
             qq!${p}misc/unapply-cust_credit.cgi?!. $cust_credit->crednum.
             qq!', 'Are you sure you want to unapply this credit?')">!.
             qq!unapply</A>)!;
}

my $reason = $cust_credit->reason
               ? ' ('. $cust_credit->reason. ')'
               : '';

</%init>