3 my $custnum = $cust_main->custnum;
5 my $conf = new FS::Conf;
7 my @payby = $conf->config('payby');
8 #@payby = (qw( CARD DCRD CHEK DCHK LECB BILL CASH WEST COMP ))
9 @payby = (qw( CARD DCRD CHEK DCHK LECB BILL CASH COMP ))
10 unless grep /\w/, @payby;
11 my %payby = map { $_=>1 } @payby;
17 <BR><BR><A NAME="history"><FONT SIZE="+2">Payment History</FONT></A><BR>
19 <% if ( $payby{'BILL'} ) { %>
21 <%= $s++ ? ' | ' : '' %>
22 <A HREF="<%= $p %>edit/cust_pay.cgi?payby=BILL;custnum=<%= $custnum %>">Post check payment</A>
26 <% if ( $payby{'CASH'} ) { %>
28 <%= $s++ ? ' | ' : '' %>
29 <A HREF="<%= $p %>edit/cust_pay.cgi?payby=CASH;custnum=<%= $custnum %>">Post cash payment</A>
33 <% if ( $payby{'WEST'} ) { %>
35 <%= $s++ ? ' | ' : '' %>
36 <A HREF="<%= $p %>edit/cust_pay.cgi?payby=WEST;custnum=<%= $custnum %>">Post Western Union payment</A>
40 <% if ( $payby{'CARD'} || $payby{'DCRD'} ) { %>
42 <%= $s++ ? ' | ' : '' %>
43 <A HREF="<%= $p %>misc/payment.cgi?payby=CARD;custnum=<%= $custnum %>">Process credit card payment</A>
47 <% if ( $payby{'CHEK'} || $payby{'DCHK'} ) { %>
49 <%= $s++ ? ' | ' : '' %>
50 <A HREF="<%= $p %>misc/payment.cgi?payby=CHEK;custnum=<%= $custnum %>">Process electronic check (ACH) payment</A>
54 <% if ( $payby{'MCRD'} ) { %>
56 <%= $s++ ? ' | ' : '' %>
57 <A HREF="<%= $p %>edit/cust_pay.cgi?payby=MCRD;custnum=<%= $custnum %>">Post manual credit card payment</A>
61 <BR><A HREF="<%= $p %>edit/cust_credit.cgi?<%= $custnum %>">Post credit</A>
69 foreach my $cust_bill ($cust_main->cust_bill) {
70 my $pre = ( $cust_bill->owed > 0 )
71 ? '<B><FONT SIZE="+1" COLOR="#FF0000">Open '
73 my $post = ( $cust_bill->owed > 0 ) ? '</FONT></B>' : '';
74 my $invnum = $cust_bill->invnum;
76 'date' => $cust_bill->_date,
77 'desc' => qq!<A HREF="${p}view/cust_bill.cgi?$invnum">!. $pre.
78 "Invoice #$invnum (Balance \$". $cust_bill->owed. ')'.
80 'charge' => $cust_bill->charged,
84 #payments (some false laziness w/credits)
85 foreach my $cust_pay ($cust_main->cust_pay) {
87 my $payby = $cust_pay->payby;
90 if ( $payby eq 'CARD' ) {
91 $payinfo = $cust_pay->payinfo_masked;
92 } elsif ( $payby eq 'CHEK' && $cust_pay->payinfo =~ /^(\d+)\@(\d+)$/ ) {
93 $payinfo = "ABA $2, Acct# $1";
95 $payinfo = $cust_pay->payinfo;
97 my @cust_bill_pay = $cust_pay->cust_bill_pay;
98 my @cust_pay_refund = $cust_pay->cust_pay_refund;
100 my $target = "$payby$payinfo";
101 $payby =~ s/^BILL$/Check #/ if $payinfo;
102 $payby =~ s/^CHEK$/Electronic check /;
103 $payby =~ s/^PREP$/Prepaid card /;
104 $payby =~ s/^CARD$/Credit card #/;
105 $payby =~ s/^COMP$/Complimentary by /;
106 $payby =~ s/^CASH$/Cash/;
107 $payby =~ s/^WEST$/Western Union/;
108 $payby =~ s/^MCRD$/Manual credit card/;
109 $payby =~ s/^BILL$//;
110 my $info = $payby ? " ($payby$payinfo)" : '';
112 my( $pre, $post, $desc, $apply, $ext ) = ( '', '', '', '', '' );
113 if ( scalar(@cust_bill_pay) == 0
114 && scalar(@cust_pay_refund) == 0 ) {
115 #completely unapplied
116 $pre = '<B><FONT COLOR="#FF0000">Unapplied ';
117 $post = '</FONT></B>';
118 $apply = qq! (<A HREF="${p}edit/cust_bill_pay.cgi?!.
119 $cust_pay->paynum. '">apply</A>)';
120 } elsif ( scalar(@cust_bill_pay) == 1
121 && scalar(@cust_pay_refund) == 0
122 && $cust_pay->unapplied == 0 ) {
123 #applied to one invoice, the usual situation
124 $desc = ' applied to Invoice #'. $cust_bill_pay[0]->invnum;
125 } elsif ( scalar(@cust_bill_pay) == 0
126 && scalar(@cust_pay_refund) == 1
127 && $cust_pay->unapplied == 0 ) {
128 #applied to one refund
129 $desc = ' refunded on '. time2str("%D", $cust_pay_refund[0]->_date);
133 foreach my $app ( sort { $a->_date <=> $b->_date }
134 ( @cust_bill_pay, @cust_pay_refund ) ) {
135 if ( $app->isa('FS::cust_bill_pay') ) {
136 $desc .= ' '.
138 ' applied to Invoice #'. $app->invnum.
140 #' on '. time2str("%D", $cust_bill_pay->_date).
141 } elsif ( $app->isa('FS::cust_pay_refund') ) {
142 $desc .= ' '.
144 ' refunded on'. time2str("%D", $app->_date).
147 die "$app is not a FS::cust_bill_pay or FS::cust_pay_refund";
150 if ( $cust_pay->unapplied > 0 ) {
151 $desc .= ' '.
152 '<B><FONT COLOR="#FF0000">$'.
153 $cust_pay->unapplied. ' unapplied</FONT></B>'.
154 qq! (<A HREF="${p}edit/cust_bill_pay.cgi?!.
155 $cust_pay->paynum. '">apply</A>)'.
161 my $refund_days = $conf->config('card_refund-days') || 120;
162 if ( $cust_pay->closed !~ /^Y/i
163 && $cust_pay->payby =~ /^(CARD|CHEK)$/
164 && time-$cust_pay->_date < $refund_days*86400
165 && $cust_pay->unrefunded > 0
167 $refund = qq! (<A HREF="${p}edit/cust_refund.cgi?payby=$1;!.
168 qq!paynum=!. $cust_pay->paynum. '"'.
169 qq! TITLE="Send a refund for this payment to the payment gateway"!.
174 if ( $cust_pay->closed !~ /^Y/i
175 && ( $cust_pay->payby ne 'CARD' || $conf->exists('cc-void') )
176 && ( $cust_pay->payby ne 'CHEK' || $conf->exists('echeck-void') )
178 $void = qq! (<A HREF="javascript:areyousure('!.
179 qq!${p}misc/void-cust_pay.cgi?!. $cust_pay->paynum.
180 qq!', 'Are you sure you want to void this payment?')"!.
181 qq! TITLE="Void this payment from the database!.
182 ( $cust_pay->payby =~ /^(CARD|CHEK)$/
183 ? ' (do not send anything to the payment gateway)'
190 if ( $cust_pay->closed !~ /^Y/i && $conf->exists('deletepayments') ) {
191 $delete = qq! (<A HREF="javascript:areyousure('!.
192 qq!${p}misc/delete-cust_pay.cgi?!. $cust_pay->paynum.
193 qq!', 'Are you sure you want to delete this payment?')"!.
194 qq! TITLE="Delete this payment from the database completely - not recommended"!.
199 if ( $cust_pay->closed !~ /^Y/i
200 && $conf->exists('unapplypayments')
201 && scalar(@cust_bill_pay) ) {
202 $unapply = qq! (<A HREF="javascript:areyousure('!.
203 qq!${p}misc/unapply-cust_pay.cgi?!. $cust_pay->paynum.
204 qq!', 'Are you sure you want to unapply this payment?')"!.
205 qq! TITLE="Keep this payment, but dissociate it from the invoices it is currently applied against"!.
210 'date' => $cust_pay->_date,
211 'desc' => $pre. "Payment$post$info$desc".
212 "$apply$refund$void$delete$unapply",
213 'payment' => $cust_pay->paid,
219 foreach my $cust_pay_void ($cust_main->cust_pay_void) {
221 my $payby = $cust_pay_void->payby;
222 my $payinfo = $payby eq 'CARD'
223 ? $cust_pay_void->payinfo_masked
224 : $cust_pay_void->payinfo;
226 $payby =~ s/^BILL$/Check #/ if $payinfo;
227 $payby =~ s/^CHEK$/Electronic check /;
228 $payby =~ s/^BILL$//;
229 $payby =~ s/^(CARD|COMP)$/$1 /;
230 my $info = $payby ? " ($payby$payinfo)" : '';
233 'date' => $cust_pay_void->_date,
234 'desc' => "<DEL>Payment $info</DEL> <I>voided ".
235 time2str("%D", $cust_pay_void->void_date).
236 " by ". $cust_pay_void->otaker. '</i>',
237 'void_payment' => $cust_pay_void->paid,
242 #credits (some false laziness w/payments)
243 foreach my $cust_credit ($cust_main->cust_credit) {
245 my @cust_credit_bill = $cust_credit->cust_credit_bill;
246 my @cust_credit_refund = $cust_credit->cust_credit_refund;
248 my( $pre, $post, $desc, $apply, $ext ) = ( '', '', '', '', '' );
249 if ( scalar(@cust_credit_bill) == 0
250 && scalar(@cust_credit_refund) == 0 ) {
251 #completely unapplied
252 $pre = '<B><FONT COLOR="#FF0000">Unapplied ';
253 $post = '</FONT></B>';
254 $apply = qq! (<A HREF="${p}edit/cust_credit_bill.cgi?!.
255 $cust_credit->crednum. '">apply</A>)';
256 } elsif ( scalar(@cust_credit_bill) == 1
257 && scalar(@cust_credit_refund) == 0
258 && $cust_credit->credited == 0 ) {
259 #applied to one invoice, the usual situation
260 $desc = ' applied to Invoice #'. $cust_credit_bill[0]->invnum;
261 } elsif ( scalar(@cust_credit_bill) == 0
262 && scalar(@cust_credit_refund) == 1
263 && $cust_credit->credited == 0 ) {
264 #applied to one refund
265 $desc = ' refunded on '. time2str("%D", $cust_credit_refund[0]->_date);
269 foreach my $app ( sort { $a->_date <=> $b->_date }
270 ( @cust_credit_bill, @cust_credit_refund ) ) {
271 if ( $app->isa('FS::cust_credit_bill') ) {
272 $desc .= ' '.
274 ' applied to Invoice #'. $app->invnum.
276 #' on '. time2str("%D", $app->_date).
277 } elsif ( $app->isa('FS::cust_credit_refund') ) {
278 $desc .= ' '.
280 ' refunded on'. time2str("%D", $app->_date).
283 die "$app is not a FS::cust_credit_bill or a FS::cust_credit_refund";
286 if ( $cust_credit->credited > 0 ) {
287 $desc .= ' <B><FONT COLOR="#FF0000">$'.
288 $cust_credit->credited. ' unapplied</FONT></B>'.
289 qq! (<A HREF="${p}edit/cust_credit_bill.cgi?!.
290 $cust_credit->crednum. '">apply</A>)'.
296 if ( $cust_credit->closed !~ /^Y/i && $conf->exists('deletecredits') ) {
297 $delete = qq! (<A HREF="javascript:areyousure('!.
298 qq!${p}misc/delete-cust_credit.cgi?!. $cust_credit->crednum.
299 qq!', 'Are you sure you want to delete this credit?')">!.
304 if ( $cust_credit->closed !~ /^Y/i
305 && $conf->exists('unapplycredits')
306 && scalar(@cust_credit_bill) ) {
307 $unapply = qq! (<A HREF="javascript:areyousure('!.
308 qq!${p}misc/unapply-cust_credit.cgi?!. $cust_credit->crednum.
309 qq!', 'Are you sure you want to unapply this credit?')">!.
314 'date' => $cust_credit->_date,
315 'desc' => $pre. "Credit$post by ". $cust_credit->otaker.
316 ' ('. $cust_credit->reason. ')'.
317 "$desc$apply$delete$unapply",
318 'credit' => $cust_credit->amount,
324 foreach my $cust_refund ($cust_main->cust_refund) {
326 my $payby = $cust_refund->payby;
327 my $payinfo = $payby eq 'CARD'
328 ? $cust_refund->payinfo_masked
329 : $cust_refund->payinfo;
331 $payby =~ s/^BILL$/Check #/ if $payinfo;
332 $payby =~ s/^CHEK$/Electronic check /;
333 $payby =~ s/^(CARD|COMP)$/$1 /;
336 'date' => $cust_refund->_date,
337 'desc' => "Refund ($payby$payinfo) by ". $cust_refund->otaker,
338 'refund' => $cust_refund->refund,
349 <TH><FONT SIZE=-1>Charge</FONT></TH>
350 <TH><FONT SIZE=-1>Payment</FONT></TH>
351 <TH><FONT SIZE=-1>In-house<BR>Credit</FONT></TH>
352 <TH><FONT SIZE=-1>Refund</FONT></TH>
353 <TH><FONT SIZE=-1>Balance</FONT></TH>
357 #display payment history
361 foreach my $item ( sort { $a->{'date'} <=> $b->{'date'} } @history ) {
363 my $charge = exists($item->{'charge'})
364 ? sprintf('$%.2f', $item->{'charge'})
366 my $payment = exists($item->{'payment'})
367 ? sprintf('- $%.2f', $item->{'payment'})
369 $payment ||= sprintf('<DEL>- $%.2f</DEL>', $item->{'void_payment'})
370 if exists($item->{'void_payment'});
371 my $credit = exists($item->{'credit'})
372 ? sprintf('- $%.2f', $item->{'credit'})
374 my $refund = exists($item->{'refund'})
375 ? sprintf('$%.2f', $item->{'refund'})
378 my $target = exists($item->{'target'}) ? $item->{'target'} : '';
380 $balance += $item->{'charge'} if exists $item->{'charge'};
381 $balance -= $item->{'payment'} if exists $item->{'payment'};
382 $balance -= $item->{'credit'} if exists $item->{'credit'};
383 $balance += $item->{'refund'} if exists $item->{'refund'};
384 $balance = sprintf("%.2f", $balance);
385 $balance =~ s/^\-0\.00$/0.00/; #yay ieee fp
386 ( my $showbalance = '$'. $balance ) =~ s/^\$\-/- \$/;
392 <% unless ( !$target || $target{$target}++ ) { %>
393 <A NAME="<%= $target %>">
395 <%= time2str("%D",$item->{'date'}) %>
396 <% if ( $target && $target{$target} == 1 ) { %>
401 <TD><%= $item->{'desc'} %></TD>
402 <TD ALIGN="right"><%= $charge %></TD>
403 <TD ALIGN="right"><%= $payment %></TD>
404 <TD ALIGN="right"><%= $credit %></TD>
405 <TD ALIGN="right"><%= $refund %></TD>
406 <TD ALIGN="right"><%= $showbalance %></TD>