invoice voiding, RT#18677
[freeside.git] / httemplate / view / cust_main / payment_history / payment.html
1 <% $payment %>
2 <% "$info$desc$view$apply$refund$void$delete$unapply" %>
3 <%init>
4
5 my( $cust_pay, %opt ) = @_;
6
7 my $date_format = $opt{'date_format'} || '%m/%d/%Y';
8
9 my $conf = new FS::Conf;
10 my $money_char = $conf->config('money_char') || '$';
11 my $curuser = $FS::CurrentUser::CurrentUser;
12
13 my @cust_bill_pay = $cust_pay->cust_bill_pay;
14 my @cust_pay_refund = $cust_pay->cust_pay_refund;
15
16 my ($payby,$payinfo) = translate_payinfo($cust_pay);
17 my $target = "$payby$payinfo";
18 $payby = translate_payby($payby,$payinfo);
19 my $info = $payby ? "($payby$payinfo)" : '';
20
21 my $desc = '';
22 if ( $opt{'pkg-balances'} && $cust_pay->pkgnum ) {
23   my $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $cust_pay->pkgnum } );
24   $desc .= ' for '. $cust_pkg->pkg_label_long;
25 }
26
27 my %cust_bill_pay_width = ('width' => 392);
28 my %cust_bill_pay_height = ();
29 if ($conf->exists('cust_bill_pay_pkg-manual')) {
30   %cust_bill_pay_width = ('width' => 592);
31   %cust_bill_pay_height = ('height' => 436);
32 }
33
34 my $apply = '';
35
36 my $italicize_otaker = 0;
37 my $otaker = $cust_pay->otaker;
38 if ( $otaker eq 'fs_daily' ) {
39     $otaker = 'auto billing';
40     $italicize_otaker = 1;
41 }
42 if ( $otaker eq 'fs_selfservice' ) {
43     $otaker = 'customer self-service';
44     $italicize_otaker = 1;
45 }
46
47 my $payment = emt("Payment by [_1]",$otaker);
48 $payment =~ s/$otaker/<i>$otaker<\/i>/ if $italicize_otaker;
49
50 if (    scalar(@cust_bill_pay)   == 0
51      && scalar(@cust_pay_refund) == 0 ) {
52   #completely unapplied
53   $payment = emt("Unapplied Payment by [_1]",$otaker);
54   $payment =~ s/$otaker/<i>$otaker<\/i>/ if $italicize_otaker;
55   $payment = '<B><FONT COLOR="#FF0000">'.$payment.'</FONT></B>';
56   if ( $curuser->access_right('Apply payment') ) {
57     if ( $cust_pay->cust_main->total_owed > 0 ) {
58       $apply = ' ('.
59                include( '/elements/popup_link.html',
60                           'label'       => emt('apply'),
61                           'action'      => "${p}edit/cust_bill_pay.cgi?".
62                                            $cust_pay->paynum,
63                           'actionlabel' => emt('Apply payment'),
64                           %cust_bill_pay_width,
65                           %cust_bill_pay_height,
66                       ).
67                 ')';
68     }
69     if ( $cust_pay->cust_main->total_unapplied_refunds > 0 ) {
70       $apply.= ' ('.
71                include( '/elements/popup_link.html',
72                           'label'       => emt('apply to refund'),
73                           'action'      => "${p}edit/cust_pay_refund.cgi?".
74                                            $cust_pay->paynum,
75                           'actionlabel' => emt('Apply payment to refund'),
76                           'width'       => 392,
77                       ).
78                ')';
79     }
80   }
81 } elsif (    scalar(@cust_bill_pay)   == 1
82           && scalar(@cust_pay_refund) == 0
83           && $cust_pay->unapplied == 0     ) {
84   #applied to one invoice, the usual situation
85   $desc .= ' '. $cust_bill_pay[0]->applied_to_invoice;
86 } elsif (    scalar(@cust_bill_pay)   == 0
87           && scalar(@cust_pay_refund) == 1
88           && $cust_pay->unapplied == 0     ) {
89   #applied to one refund
90   $desc .= emt(" refunded on [_1]", time2str($date_format, $cust_pay_refund[0]->_date) );
91 } else {
92   #complicated
93   $desc .= '<BR>';
94   foreach my $app ( sort { $a->_date <=> $b->_date }
95                          ( @cust_bill_pay, @cust_pay_refund ) ) {
96     if ( $app->isa('FS::cust_bill_pay') ) {
97       $desc .= '&nbsp;&nbsp;' . $money_char . $app->amount . ' ' .
98                 $app->applied_to_invoice . '<BR>';
99     } elsif ( $app->isa('FS::cust_pay_refund') ) {
100       $desc .= '&nbsp;&nbsp;' .
101                emt("[_1][_2] refunded on [_3]", $money_char, $app->amount,
102                         time2str($date_format, $app->_date) ) . '<BR>';
103     } else {
104       die "$app is not a FS::cust_bill_pay or FS::cust_pay_refund";
105     }
106   }
107   if ( $cust_pay->unapplied > 0 ) {
108     $desc .= '&nbsp;&nbsp;'.
109              '<B><FONT COLOR="#FF0000">' .
110              emt("[_1][_2] unapplied", $money_char, $cust_pay->unapplied) .
111              '</FONT></B>';
112     if ( $curuser->access_right('Apply payment') ) {
113       if ( $cust_pay->cust_main->total_owed > 0 ) {
114         $apply = ' ('.
115                  include( '/elements/popup_link.html',
116                             'label'      => emt('apply'),
117                             'action'     => "${p}edit/cust_bill_pay.cgi?".
118                                             $cust_pay->paynum,
119                             'actionlabel' => emt('Apply payment'),
120                             %cust_bill_pay_width,
121                             %cust_bill_pay_height,
122                         ).
123                  ')';
124       }
125       if ( $cust_pay->cust_main->total_unapplied_refunds > 0 ) {
126         $apply.= ' ('.
127                  include( '/elements/popup_link.html',
128                             'label'      => emt('apply to refund'),
129                             'action'     => "${p}edit/cust_pay_refund.cgi?".
130                                             $cust_pay->paynum,
131                             'actionlabel' => emt('Apply payment to refund'),
132                             'width'      => 392,
133                         ).
134                  ')';
135       }
136     }
137     $desc .= '<BR>';
138   }
139 }
140
141 my $view =
142   ' ('. include('/elements/popup_link.html',
143                   'label'     => emt('view receipt'),
144                   'action'    => "${p}view/cust_pay.html?link=popup;paynum=".
145                                   $cust_pay->paynum,
146                   'actionlabel' => emt('Payment Receipt'),
147                ).
148    ')';
149
150 my $refund = '';
151 my $refund_days = $opt{'card_refund-days'} || 120;
152 my @rights = ('Refund payment');
153 push @rights, 'Refund credit card payment' if $payby eq 'CARD';
154 push @rights, 'Refund Echeck payment' if $payby eq 'CHEK';
155 if (    $cust_pay->closed !~ /^Y/i
156      && $cust_pay->payby =~ /^(CARD|CHEK)$/
157      && time-$cust_pay->_date < $refund_days*86400
158      && $cust_pay->unrefunded > 0
159      && $curuser->access_right(\@rights)
160 ) {
161   $refund = qq! (<A HREF="${p}edit/cust_refund.cgi?payby=$1;!.
162             qq!paynum=!. $cust_pay->paynum. '"'.
163             qq! TITLE="! .emt('Send a refund for this payment to the payment gateway') 
164             . '">' . emt('refund') . '</A>)';
165 }
166
167 my $void = '';
168 my $voidmsg = $cust_pay->payby =~ /^(CARD|CHEK)$/
169               ? ' (' . emt('do not send anything to the payment gateway').')'
170               : '';
171 $void = areyousure_link("${p}misc/void-cust_pay.cgi?".$cust_pay->paynum,
172                         emt('Are you sure you want to void this payment?'),
173                         emt('Void this payment from the database') . $voidmsg,
174                         emt('void')
175                        )
176     if (    $cust_pay->closed !~ /^Y/i
177      && (    ( $cust_pay->payby eq 'CARD'
178                && $curuser->access_right('Credit card void')
179              )
180           || ( $cust_pay->payby eq 'CHEK'
181                && $curuser->access_right('Echeck void')
182              )
183           || ( $cust_pay->payby !~ /^(CARD|CHEK)$/
184                && $curuser->access_right('Void payments')
185              )
186         )
187    );
188
189 my $delete = '';
190 $delete = areyousure_link("${p}misc/delete-cust_pay.cgi?".$cust_pay->paynum,
191                             emt('Are you sure you want to delete this payment?'),
192                             emt('Delete this payment from the database completely - not recommended'),
193                             emt('delete')
194                          )
195     if ( $cust_pay->closed !~ /^Y/i && $opt{'deletepayments'}
196                                  && $curuser->access_right('Delete payment') );
197
198 my $unapply = '';
199 $unapply = areyousure_link("${p}misc/unapply-cust_pay.cgi?".$cust_pay->paynum,
200                             emt('Are you sure you want to unapply this payment?'),
201                             emt('Keep this payment, but dissociate it from the invoices it is currently applied against'),
202                             emt('unapply')
203                           )
204     if ( $cust_pay->closed !~ /^Y/i && scalar(@cust_bill_pay)           
205                                 && $curuser->access_right('Unapply payment') );
206
207 </%init>