3 my $custnum = $cust_main->custnum;
5 my $conf = new FS::Conf;
7 my @payby = grep /\w/, $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 ))
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 if ( $cust_pay_void->closed !~ /^Y/i && $conf->exists('unvoid') ) {
234 $unvoid = qq! (<A HREF="javascript:areyousure('!.
235 qq!${p}misc/unvoid-cust_pay_void.cgi?!. $cust_pay_void->paynum.
236 qq!', 'Are you sure you want to unvoid this payment?')"!.
237 qq! TITLE="Unvoid this payment from the database!.
238 ( $cust_pay_void->payby =~ /^(CARD|CHEK)$/
239 ? ' (do not send anything to the payment gateway)'
246 'date' => $cust_pay_void->_date,
247 'desc' => "<DEL>Payment $info</DEL> <I>voided ".
248 time2str("%D", $cust_pay_void->void_date).
249 " by ". $cust_pay_void->otaker. '</i>'. $unvoid,
250 'void_payment' => $cust_pay_void->paid,
255 #credits (some false laziness w/payments)
256 foreach my $cust_credit ($cust_main->cust_credit) {
258 my @cust_credit_bill = $cust_credit->cust_credit_bill;
259 my @cust_credit_refund = $cust_credit->cust_credit_refund;
261 my( $pre, $post, $desc, $apply, $ext ) = ( '', '', '', '', '' );
262 if ( scalar(@cust_credit_bill) == 0
263 && scalar(@cust_credit_refund) == 0 ) {
264 #completely unapplied
265 $pre = '<B><FONT COLOR="#FF0000">Unapplied ';
266 $post = '</FONT></B>';
267 $apply = qq! (<A HREF="${p}edit/cust_credit_bill.cgi?!.
268 $cust_credit->crednum. '">apply</A>)';
269 } elsif ( scalar(@cust_credit_bill) == 1
270 && scalar(@cust_credit_refund) == 0
271 && $cust_credit->credited == 0 ) {
272 #applied to one invoice, the usual situation
273 $desc = ' applied to Invoice #'. $cust_credit_bill[0]->invnum;
274 } elsif ( scalar(@cust_credit_bill) == 0
275 && scalar(@cust_credit_refund) == 1
276 && $cust_credit->credited == 0 ) {
277 #applied to one refund
278 $desc = ' refunded on '. time2str("%D", $cust_credit_refund[0]->_date);
282 foreach my $app ( sort { $a->_date <=> $b->_date }
283 ( @cust_credit_bill, @cust_credit_refund ) ) {
284 if ( $app->isa('FS::cust_credit_bill') ) {
285 $desc .= ' '.
287 ' applied to Invoice #'. $app->invnum.
289 #' on '. time2str("%D", $app->_date).
290 } elsif ( $app->isa('FS::cust_credit_refund') ) {
291 $desc .= ' '.
293 ' refunded on'. time2str("%D", $app->_date).
296 die "$app is not a FS::cust_credit_bill or a FS::cust_credit_refund";
299 if ( $cust_credit->credited > 0 ) {
300 $desc .= ' <B><FONT COLOR="#FF0000">$'.
301 $cust_credit->credited. ' unapplied</FONT></B>'.
302 qq! (<A HREF="${p}edit/cust_credit_bill.cgi?!.
303 $cust_credit->crednum. '">apply</A>)'.
309 if ( $cust_credit->closed !~ /^Y/i && $conf->exists('deletecredits') ) {
310 $delete = qq! (<A HREF="javascript:areyousure('!.
311 qq!${p}misc/delete-cust_credit.cgi?!. $cust_credit->crednum.
312 qq!', 'Are you sure you want to delete this credit?')">!.
317 if ( $cust_credit->closed !~ /^Y/i
318 && $conf->exists('unapplycredits')
319 && scalar(@cust_credit_bill) ) {
320 $unapply = qq! (<A HREF="javascript:areyousure('!.
321 qq!${p}misc/unapply-cust_credit.cgi?!. $cust_credit->crednum.
322 qq!', 'Are you sure you want to unapply this credit?')">!.
327 'date' => $cust_credit->_date,
328 'desc' => $pre. "Credit$post by ". $cust_credit->otaker.
329 ( $cust_credit->reason
330 ? ' ('. $cust_credit->reason. ')'
333 "$desc$apply$delete$unapply",
334 'credit' => $cust_credit->amount,
340 foreach my $cust_refund ($cust_main->cust_refund) {
342 my $payby = $cust_refund->payby;
343 my $payinfo = $payby eq 'CARD'
344 ? $cust_refund->payinfo_masked
345 : $cust_refund->payinfo;
347 $payby =~ s/^BILL$/Check #/ if $payinfo;
348 $payby =~ s/^CHEK$/Electronic check /;
349 $payby =~ s/^(CARD|COMP)$/$1 /;
352 'date' => $cust_refund->_date,
353 'desc' => "Refund ($payby$payinfo) by ". $cust_refund->otaker,
354 'refund' => $cust_refund->refund,
365 <TH><FONT SIZE=-1>Charge</FONT></TH>
366 <TH><FONT SIZE=-1>Payment</FONT></TH>
367 <TH><FONT SIZE=-1>In-house<BR>Credit</FONT></TH>
368 <TH><FONT SIZE=-1>Refund</FONT></TH>
369 <TH><FONT SIZE=-1>Balance</FONT></TH>
373 #display payment history
377 foreach my $item ( sort { $a->{'date'} <=> $b->{'date'} } @history ) {
379 my $charge = exists($item->{'charge'})
380 ? sprintf('$%.2f', $item->{'charge'})
382 my $payment = exists($item->{'payment'})
383 ? sprintf('- $%.2f', $item->{'payment'})
385 $payment ||= sprintf('<DEL>- $%.2f</DEL>', $item->{'void_payment'})
386 if exists($item->{'void_payment'});
387 my $credit = exists($item->{'credit'})
388 ? sprintf('- $%.2f', $item->{'credit'})
390 my $refund = exists($item->{'refund'})
391 ? sprintf('$%.2f', $item->{'refund'})
394 my $target = exists($item->{'target'}) ? $item->{'target'} : '';
396 $balance += $item->{'charge'} if exists $item->{'charge'};
397 $balance -= $item->{'payment'} if exists $item->{'payment'};
398 $balance -= $item->{'credit'} if exists $item->{'credit'};
399 $balance += $item->{'refund'} if exists $item->{'refund'};
400 $balance = sprintf("%.2f", $balance);
401 $balance =~ s/^\-0\.00$/0.00/; #yay ieee fp
402 ( my $showbalance = '$'. $balance ) =~ s/^\$\-/- \$/;
408 <% unless ( !$target || $target{$target}++ ) { %>
409 <A NAME="<%= $target %>">
411 <%= time2str("%D",$item->{'date'}) %>
412 <% if ( $target && $target{$target} == 1 ) { %>
417 <TD><%= $item->{'desc'} %></TD>
418 <TD ALIGN="right"><%= $charge %></TD>
419 <TD ALIGN="right"><%= $payment %></TD>
420 <TD ALIGN="right"><%= $credit %></TD>
421 <TD ALIGN="right"><%= $refund %></TD>
422 <TD ALIGN="right"><%= $showbalance %></TD>