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 <BR><A HREF="<%= $p %>edit/cust_credit.cgi?<%= $custnum %>">Post credit</A>
62 foreach my $cust_bill ($cust_main->cust_bill) {
63 my $pre = ( $cust_bill->owed > 0 )
64 ? '<B><FONT SIZE="+1" COLOR="#FF0000">Open '
66 my $post = ( $cust_bill->owed > 0 ) ? '</FONT></B>' : '';
67 my $invnum = $cust_bill->invnum;
69 'date' => $cust_bill->_date,
70 'desc' => qq!<A HREF="${p}view/cust_bill.cgi?$invnum">!. $pre.
71 "Invoice #$invnum (Balance \$". $cust_bill->owed. ')'.
73 'charge' => $cust_bill->charged,
77 #payments (some false laziness w/credits)
78 foreach my $cust_pay ($cust_main->cust_pay) {
80 my $payby = $cust_pay->payby;
83 if ( $payby eq 'CARD' ) {
84 $payinfo = $cust_pay->payinfo_masked;
85 } elsif ( $payby eq 'CHEK' && $cust_pay->payinfo =~ /^(\d+)\@(\d+)$/ ) {
86 $payinfo = "ABA $2, Acct# $1";
88 $payinfo = $cust_pay->payinfo;
90 my @cust_bill_pay = $cust_pay->cust_bill_pay;
91 my @cust_pay_refund = $cust_pay->cust_pay_refund;
93 my $target = "$payby$payinfo";
94 $payby =~ s/^BILL$/Check #/ if $payinfo;
95 $payby =~ s/^CHEK$/Electronic check /;
96 $payby =~ s/^PREP$/Prepaid card /;
97 $payby =~ s/^CARD$/Credit card #/;
98 $payby =~ s/^COMP$/Complimentary by /;
99 $payby =~ s/^CASH$/Cash/;
100 $payby =~ s/^WEST$/Western Union/;
101 $payby =~ s/^BILL$//;
102 my $info = $payby ? " ($payby$payinfo)" : '';
104 my( $pre, $post, $desc, $apply, $ext ) = ( '', '', '', '', '' );
105 if ( scalar(@cust_bill_pay) == 0
106 && scalar(@cust_pay_refund) == 0 ) {
107 #completely unapplied
108 $pre = '<B><FONT COLOR="#FF0000">Unapplied ';
109 $post = '</FONT></B>';
110 $apply = qq! (<A HREF="${p}edit/cust_bill_pay.cgi?!.
111 $cust_pay->paynum. '">apply</A>)';
112 } elsif ( scalar(@cust_bill_pay) == 1
113 && scalar(@cust_pay_refund) == 0
114 && $cust_pay->unapplied == 0 ) {
115 #applied to one invoice, the usual situation
116 $desc = ' applied to Invoice #'. $cust_bill_pay[0]->invnum;
117 } elsif ( scalar(@cust_bill_pay) == 0
118 && scalar(@cust_pay_refund) == 1
119 && $cust_pay->unapplied == 0 ) {
120 #applied to one refund
121 $desc = ' refunded on '. time2str("%D", $cust_pay_refund[0]->_date);
125 foreach my $app ( sort { $a->_date <=> $b->_date }
126 ( @cust_bill_pay, @cust_pay_refund ) ) {
127 if ( $app->isa('FS::cust_bill_pay') ) {
128 $desc .= ' '.
130 ' applied to Invoice #'. $app->invnum.
132 #' on '. time2str("%D", $cust_bill_pay->_date).
133 } elsif ( $app->isa('FS::cust_pay_refund') ) {
134 $desc .= ' '.
136 ' refunded on'. time2str("%D", $app->_date).
139 die "$app is not a FS::cust_bill_pay or FS::cust_pay_refund";
142 if ( $cust_pay->unapplied > 0 ) {
143 $desc .= ' '.
144 '<B><FONT COLOR="#FF0000">$'.
145 $cust_pay->unapplied. ' unapplied</FONT></B>'.
146 qq! (<A HREF="${p}edit/cust_bill_pay.cgi?!.
147 $cust_pay->paynum. '">apply</A>)'.
153 my $refund_days = $conf->config('card_refund-days') || 120;
154 if ( $cust_pay->closed !~ /^Y/i
155 && $cust_pay->payby =~ /^(CARD|CHEK)$/
156 && time-$cust_pay->_date < $refund_days*86400
157 && $cust_pay->unrefunded > 0
159 $refund = qq! (<A HREF="${p}edit/cust_refund.cgi?payby=$1;!.
160 qq!paynum=!. $cust_pay->paynum. '"'.
161 qq! TITLE="Send a refund for this payment to the payment gateway"!.
166 if ( $cust_pay->closed !~ /^Y/i
167 && ( $cust_pay->payby ne 'CARD' || $conf->exists('cc-void') )
168 && ( $cust_pay->payby ne 'CHEK' || $conf->exists('echeck-void') )
170 $void = qq! (<A HREF="javascript:areyousure('!.
171 qq!${p}misc/void-cust_pay.cgi?!. $cust_pay->paynum.
172 qq!', 'Are you sure you want to void this payment?')"!.
173 qq! TITLE="Void this payment from the database!.
174 ( $cust_pay->payby =~ /^(CARD|CHEK)$/
175 ? ' (do not send anything to the payment gateway)'
182 if ( $cust_pay->closed !~ /^Y/i && $conf->exists('deletepayments') ) {
183 $delete = qq! (<A HREF="javascript:areyousure('!.
184 qq!${p}misc/delete-cust_pay.cgi?!. $cust_pay->paynum.
185 qq!', 'Are you sure you want to delete this payment?')"!.
186 qq! TITLE="Delete this payment from the database completely - not recommended"!.
191 if ( $cust_pay->closed !~ /^Y/i
192 && $conf->exists('unapplypayments')
193 && scalar(@cust_bill_pay) ) {
194 $unapply = qq! (<A HREF="javascript:areyousure('!.
195 qq!${p}misc/unapply-cust_pay.cgi?!. $cust_pay->paynum.
196 qq!', 'Are you sure you want to unapply this payment?')"!.
197 qq! TITLE="Keep this payment, but dissociate it from the invoices it is currently applied against"!.
202 'date' => $cust_pay->_date,
203 'desc' => $pre. "Payment$post$info$desc".
204 "$apply$refund$void$delete$unapply",
205 'payment' => $cust_pay->paid,
211 foreach my $cust_pay_void ($cust_main->cust_pay_void) {
213 my $payby = $cust_pay_void->payby;
214 my $payinfo = $payby eq 'CARD'
215 ? $cust_pay_void->payinfo_masked
216 : $cust_pay_void->payinfo;
218 $payby =~ s/^BILL$/Check #/ if $payinfo;
219 $payby =~ s/^CHEK$/Electronic check /;
220 $payby =~ s/^BILL$//;
221 $payby =~ s/^(CARD|COMP)$/$1 /;
222 my $info = $payby ? " ($payby$payinfo)" : '';
225 'date' => $cust_pay_void->_date,
226 'desc' => "<DEL>Payment $info</DEL> <I>voided ".
227 time2str("%D", $cust_pay_void->void_date).
228 " by ". $cust_pay_void->otaker. '</i>',
229 'void_payment' => $cust_pay_void->paid,
234 #credits (some false laziness w/payments)
235 foreach my $cust_credit ($cust_main->cust_credit) {
237 my @cust_credit_bill = $cust_credit->cust_credit_bill;
238 my @cust_credit_refund = $cust_credit->cust_credit_refund;
240 my( $pre, $post, $desc, $apply, $ext ) = ( '', '', '', '', '' );
241 if ( scalar(@cust_credit_bill) == 0
242 && scalar(@cust_credit_refund) == 0 ) {
243 #completely unapplied
244 $pre = '<B><FONT COLOR="#FF0000">Unapplied ';
245 $post = '</FONT></B>';
246 $apply = qq! (<A HREF="${p}edit/cust_credit_bill.cgi?!.
247 $cust_credit->crednum. '">apply</A>)';
248 } elsif ( scalar(@cust_credit_bill) == 1
249 && scalar(@cust_credit_refund) == 0
250 && $cust_credit->credited == 0 ) {
251 #applied to one invoice, the usual situation
252 $desc = ' applied to Invoice #'. $cust_credit_bill[0]->invnum;
253 } elsif ( scalar(@cust_credit_bill) == 0
254 && scalar(@cust_credit_refund) == 1
255 && $cust_credit->credited == 0 ) {
256 #applied to one refund
257 $desc = ' refunded on '. time2str("%D", $cust_credit_refund[0]->_date);
261 foreach my $app ( sort { $a->_date <=> $b->_date }
262 ( @cust_credit_bill, @cust_credit_refund ) ) {
263 if ( $app->isa('FS::cust_credit_bill') ) {
264 $desc .= ' '.
266 ' applied to Invoice #'. $app->invnum.
268 #' on '. time2str("%D", $app->_date).
269 } elsif ( $app->isa('FS::cust_credit_refund') ) {
270 $desc .= ' '.
272 ' refunded on'. time2str("%D", $app->_date).
275 die "$app is not a FS::cust_credit_bill or a FS::cust_credit_refund";
278 if ( $cust_credit->credited > 0 ) {
279 $desc .= ' <B><FONT COLOR="#FF0000">$'.
280 $cust_credit->credited. ' unapplied</FONT></B>'.
281 qq! (<A HREF="${p}edit/cust_credit_bill.cgi?!.
282 $cust_credit->crednum. '">apply</A>)'.
288 if ( $cust_credit->closed !~ /^Y/i && $conf->exists('deletecredits') ) {
289 $delete = qq! (<A HREF="javascript:areyousure('!.
290 qq!${p}misc/delete-cust_credit.cgi?!. $cust_credit->crednum.
291 qq!', 'Are you sure you want to delete this credit?')">!.
296 if ( $cust_credit->closed !~ /^Y/i
297 && $conf->exists('unapplycredits')
298 && scalar(@cust_credit_bill) ) {
299 $unapply = qq! (<A HREF="javascript:areyousure('!.
300 qq!${p}misc/unapply-cust_credit.cgi?!. $cust_credit->crednum.
301 qq!', 'Are you sure you want to unapply this credit?')">!.
306 'date' => $cust_credit->_date,
307 'desc' => $pre. "Credit$post by ". $cust_credit->otaker.
308 ' ('. $cust_credit->reason. ')'.
309 "$desc$apply$delete$unapply",
310 'credit' => $cust_credit->amount,
316 foreach my $cust_refund ($cust_main->cust_refund) {
318 my $payby = $cust_refund->payby;
319 my $payinfo = $payby eq 'CARD'
320 ? $cust_refund->payinfo_masked
321 : $cust_refund->payinfo;
323 $payby =~ s/^BILL$/Check #/ if $payinfo;
324 $payby =~ s/^CHEK$/Electronic check /;
325 $payby =~ s/^(CARD|COMP)$/$1 /;
328 'date' => $cust_refund->_date,
329 'desc' => "Refund ($payby$payinfo) by ". $cust_refund->otaker,
330 'refund' => $cust_refund->refund,
341 <TH><FONT SIZE=-1>Charge</FONT></TH>
342 <TH><FONT SIZE=-1>Payment</FONT></TH>
343 <TH><FONT SIZE=-1>In-house<BR>Credit</FONT></TH>
344 <TH><FONT SIZE=-1>Refund</FONT></TH>
345 <TH><FONT SIZE=-1>Balance</FONT></TH>
349 #display payment history
353 foreach my $item ( sort { $a->{'date'} <=> $b->{'date'} } @history ) {
355 my $charge = exists($item->{'charge'})
356 ? sprintf('$%.2f', $item->{'charge'})
358 my $payment = exists($item->{'payment'})
359 ? sprintf('- $%.2f', $item->{'payment'})
361 $payment ||= sprintf('<DEL>- $%.2f</DEL>', $item->{'void_payment'})
362 if exists($item->{'void_payment'});
363 my $credit = exists($item->{'credit'})
364 ? sprintf('- $%.2f', $item->{'credit'})
366 my $refund = exists($item->{'refund'})
367 ? sprintf('$%.2f', $item->{'refund'})
370 my $target = exists($item->{'target'}) ? $item->{'target'} : '';
372 $balance += $item->{'charge'} if exists $item->{'charge'};
373 $balance -= $item->{'payment'} if exists $item->{'payment'};
374 $balance -= $item->{'credit'} if exists $item->{'credit'};
375 $balance += $item->{'refund'} if exists $item->{'refund'};
376 $balance = sprintf("%.2f", $balance);
377 $balance =~ s/^\-0\.00$/0.00/; #yay ieee fp
378 ( my $showbalance = '$'. $balance ) =~ s/^\$\-/- \$/;
384 <% unless ( !$target || $target{$target}++ ) { %>
385 <A NAME="<%= $target %>">
387 <%= time2str("%D",$item->{'date'}) %>
388 <% if ( $target && $target{$target} == 1 ) { %>
393 <TD><%= $item->{'desc'} %></TD>
394 <TD ALIGN="right"><%= $charge %></TD>
395 <TD ALIGN="right"><%= $payment %></TD>
396 <TD ALIGN="right"><%= $credit %></TD>
397 <TD ALIGN="right"><%= $refund %></TD>
398 <TD ALIGN="right"><%= $showbalance %></TD>