X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=httemplate%2Fview%2Fcust_main.cgi;h=cf899d0410684c5e58b109900867d5b6e918dd59;hb=aa2daf29c5bb13a3728b434159afc4065367af91;hp=7528968478455f1ad8fe974680e365b9ae7fbfb4;hpb=2066bf9d3ebb4e53c49ab8b4b447c4eb88e425a4;p=freeside.git
diff --git a/httemplate/view/cust_main.cgi b/httemplate/view/cust_main.cgi
index 752896847..cf899d041 100755
--- a/httemplate/view/cust_main.cgi
+++ b/httemplate/view/cust_main.cgi
@@ -1,50 +1,75 @@
+
<%
-#
-
-use strict;
-use vars qw ( $cgi $query $custnum $cust_main $hashref $agent $referral
- @packages $package @history @bills $bill @credits $credit
- $balance $item @agents @referrals @invoicing_list $n1 $conf );
-use CGI;
-use CGI::Carp qw(fatalsToBrowser);
-use Date::Format;
-use FS::UID qw(cgisuidsetup);
-use FS::Record qw(qsearchs qsearch);
-use FS::CGI qw(header menubar popurl table itable ntable);
-use FS::cust_credit;
-use FS::cust_pay;
-use FS::cust_bill;
-use FS::part_pkg;
-use FS::cust_pkg;
-use FS::part_referral;
-use FS::agent;
-use FS::cust_main;
-use FS::cust_refund;
-
-$cgi = new CGI;
-&cgisuidsetup($cgi);
-
-$conf = new FS::Conf;
-
-print $cgi->header( '-expires' => 'now' ), header("Customer View", menubar(
+
+my $conf = new FS::Conf;
+
+my %uiview = ();
+my %uiadd = ();
+foreach my $part_svc ( qsearch('part_svc',{}) ) {
+ $uiview{$part_svc->svcpart} = popurl(2). "view/". $part_svc->svcdb . ".cgi";
+ $uiadd{$part_svc->svcpart}= popurl(2). "edit/". $part_svc->svcdb . ".cgi";
+}
+
+print header("Customer View", menubar(
'Main Menu' => popurl(2)
));
+%>
+
+
+
+<%
+
die "No customer specified (bad URL)!" unless $cgi->keywords;
-($query) = $cgi->keywords; # needs parens with my, ->keywords returns array
+my($query) = $cgi->keywords; # needs parens with my, ->keywords returns array
$query =~ /^(\d+)$/;
-$custnum = $1;
-$cust_main = qsearchs('cust_main',{'custnum'=>$custnum});
+my $custnum = $1;
+my $cust_main = qsearchs('cust_main',{'custnum'=>$custnum});
die "Customer not found!" unless $cust_main;
-$hashref = $cust_main->hashref;
-print qq!Edit this customer!;
-print qq! | Delete this customer!
+print qq!Edit this customer!;
+
+%>
+
+
+
+<%
+
+print qq! | !.
+ 'Cancel this customer'
+ if $cust_main->ncancelled_pkgs;
+
+print qq! | !.
+ 'Delete this customer'
if $conf->exists('deletecustomers');
+
+unless ( $conf->exists('disable_customer_referrals') ) {
+ print qq! | !,
+ qq!Refer a new customer!;
+
+ print qq! | !,
+ qq!View this customer's referrals!;
+}
+
print '
';
+my $signupurl = $conf->config('signupurl');
+if ( $signupurl ) {
+print "This customer's signup URL: ".
+ "$signupurl?ref=$custnum
";
+}
+
print '';
print &itable(), '
';
@@ -53,11 +78,15 @@ print '';
print "Billing address", &ntable("#cccccc"), " |
",
&ntable("#cccccc",2),
- ' |
Contact name | ',
+ '
Contact name | ',
'',
$cust_main->last, ', ', $cust_main->first,
- ' | SS# | ',
- $cust_main->ss || ' ', ' |
',
+ '';
+print 'SS# | ',
+ $cust_main->ss || ' ', ' | '
+ if $conf->exists('show_ss');
+
+print '',
'Company | ',
$cust_main->company,
' |
',
@@ -78,9 +107,13 @@ print '';
$cust_main->country,
' | ',
;
- print 'Day Phone | ',
+ my $daytime_label = FS::Msgcat::_gettext('daytime') || 'Day Phone';
+ my $night_label = FS::Msgcat::_gettext('night') || 'Night Phone';
+ print ' |
'. $daytime_label.
+ ' | ',
$cust_main->daytime || ' ', ' |
',
- 'Night Phone | ',
+ ' |
'. $night_label.
+ ' | ',
$cust_main->night || ' ', ' |
',
'Fax | ',
$cust_main->fax || ' ', ' |
',
@@ -117,10 +150,10 @@ print '';
$cust_main->get("${pre}country"),
' | ',
;
- print 'Day Phone | ',
+ print '
'. $daytime_label. ' | ',
'',
$cust_main->get("${pre}daytime") || ' ', ' |
',
- 'Night Phone | '.
+ '
'. $night_label. ' | '.
'',
$cust_main->get("${pre}night") || ' ', ' |
',
'Fax | ',
@@ -135,58 +168,103 @@ print ' | ';
print '';
print &ntable("#cccccc"), " |
", &ntable("#cccccc",2),
- ' |
Customer number | ',
+ ' |
Customer number | ',
$custnum, ' |
',
;
- @agents = qsearch( 'agent', {} );
+ my @agents = qsearch( 'agent', {} );
+ my $agent;
unless ( scalar(@agents) == 1 ) {
- $agent = qsearchs('agent',{
- 'agentnum' => $cust_main->agentnum
- } );
+ $agent = qsearchs('agent',{ 'agentnum' => $cust_main->agentnum } );
print 'Agent | ',
$agent->agentnum, ": ", $agent->agent, ' |
';
+ } else {
+ $agent = $agents[0];
}
- @referrals = qsearch( 'part_referral', {} );
+ my @referrals = qsearch( 'part_referral', {} );
unless ( scalar(@referrals) == 1 ) {
my $referral = qsearchs('part_referral', {
'refnum' => $cust_main->refnum
} );
- print 'Referral | ',
+ print ' |
Advertising source | ',
$referral->refnum, ": ", $referral->referral, ' |
';
}
print 'Order taker | ',
$cust_main->otaker, ' |
';
+ print 'Referring Customer | ';
+ my $referring_cust_main = '';
+ if ( $cust_main->referral_custnum
+ && ( $referring_cust_main =
+ qsearchs('cust_main', { custnum => $cust_main->referral_custnum } )
+ )
+ ) {
+ print ''.
+ $cust_main->referral_custnum. ': '.
+ ( $referring_cust_main->company
+ ? $referring_cust_main->company. ' ('.
+ $referring_cust_main->last. ', '. $referring_cust_main->first.
+ ')'
+ : $referring_cust_main->last. ', '. $referring_cust_main->first
+ ).
+ '';
+ }
+ print ' |
';
+
print '';
print '
';
- @invoicing_list = $cust_main->invoicing_list;
+if ( $conf->config('payby-default') ne 'HIDE' ) {
+
+ my @invoicing_list = $cust_main->invoicing_list;
print "Billing information (",
- qq!!, "Bill now)",
+ qq!!, "Bill now)",
&ntable("#cccccc"), "", &ntable("#cccccc",2),
- ' |
Tax exempt | ',
+ ' |
Tax exempt | ',
$cust_main->tax ? 'yes' : 'no',
' |
',
- 'Postal invoices | ',
+ ' |
Postal invoices | ',
( grep { $_ eq 'POST' } @invoicing_list ) ? 'yes' : 'no',
' |
',
- 'Email invoices | ',
+ ' |
Email invoices | ',
join(', ', grep { $_ ne 'POST' } @invoicing_list ) || 'no',
' |
',
- 'Billing type | ',
+ ' |
Billing type | ',
;
- if ( $cust_main->payby eq 'CARD' ) {
- print 'Credit card |
',
+ if ( $cust_main->payby eq 'CARD' || $cust_main->payby eq 'DCRD' ) {
+ my $payinfo = $cust_main->payinfo_masked;
+ print 'Credit card ',
+ ( $cust_main->payby eq 'CARD' ? '(automatic)' : '(on-demand)' ),
+ '',
'Card number | ',
- $cust_main->payinfo, ' |
',
+ $payinfo, '',
'Expiration | ',
$cust_main->paydate, ' |
',
'Name on card | ',
$cust_main->payname, ' |
'
;
+ } elsif ( $cust_main->payby eq 'CHEK' || $cust_main->payby eq 'DCHK') {
+ my( $account, $aba ) = split('@', $cust_main->payinfo );
+ print 'Electronic check ',
+ ( $cust_main->payby eq 'CHEK' ? '(automatic)' : '(on-demand)' ),
+ '',
+ 'Account number | ',
+ $account, ' |
',
+ 'ABA/Routing code | ',
+ $aba, ' |
',
+ 'Bank name | ',
+ $cust_main->payname, ' |
'
+ ;
+ } elsif ( $cust_main->payby eq 'LECB' ) {
+ $cust_main->payinfo =~ /^(\d{3})(\d{3})(\d{4})$/;
+ my $payinfo = "$1-$2-$3";
+ print 'Phone bill billing',
+ 'Phone number | ',
+ $payinfo, ' |
',
+ ;
} elsif ( $cust_main->payby eq 'BILL' ) {
print 'Billing';
print 'P.O. | ',
@@ -199,7 +277,7 @@ print ' ';
;
} elsif ( $cust_main->payby eq 'COMP' ) {
print 'Complimentary |
',
- 'Authorized by | ',
+ ' |
Authorized by | ',
$cust_main->payinfo, ' |
',
'Expiration | ',
$cust_main->paydate, ' |
',
@@ -208,192 +286,735 @@ print '
';
print "";
+}
+
print '';
-print qq!
Packages !,
-# qq!
Click on package number to view/edit package.!,
+if ( defined $cust_main->dbdef_table->column('comments')
+ && $cust_main->comments =~ /[^\s\n\r]/ )
+{
+ print "
Comments". &ntable("#cccccc"). "".
+ &ntable("#cccccc",2).
+ ' |
'.
+ encode_entities($cust_main->comments).
+ ' |
';
+}
+
+print '';
+
+print '
'.
+ '
';
+
+if ( $conf->config('payby-default') ne 'HIDE' ) {
+
+ print
+ qq!
!;
+
+}
+
+%>
+
+
+
+<%
+
+print qq!Packages !,
qq!( Order and cancel packages (preserves services) )!,
;
-#display packages
-
-#formatting
-print qq!!, &table(), "\n",
- qq!Package | !,
- qq!Dates | Services |
\n!,
- qq!Setup | !,
- qq!Next bill!,
- qq! | Susp. | Expire!,
- qq! | !,
- qq!Cancel | !,
- qq!
\n!;
+#begin display packages
#get package info
-if ( $conf->exists('hidecancelledpackages') ) {
- @packages = $cust_main->ncancelled_pkgs;
-} else {
- @packages = $cust_main->all_pkgs;
-}
-$n1 = '';
-foreach $package (@packages) {
- my $pkgnum = $package->pkgnum;
- my $pkg = $package->part_pkg->pkg;
- my $comment = $package->part_pkg->comment;
- my $pkgview = popurl(2). "view/cust_pkg.cgi?$pkgnum";
- my @cust_svc = qsearch( 'cust_svc', { 'pkgnum' => $pkgnum } );
- my $rowspan = scalar(@cust_svc) || 1;
-
- my $button_cgi = new CGI;
- $button_cgi->param('clone', $package->part_pkg->pkgpart);
- $button_cgi->param('pkgnum', $package->pkgnum);
- my $button_url = popurl(2). "edit/part_pkg.cgi?". $button_cgi->query_string;
-
- #print $n1, qq!$pkgnum | !,
- print $n1, qq!$pkgnum | !,
- qq!!,
- #qq!$pkg - $comment!,
- qq!$pkg - $comment!,
- qq! ( Edit | Customize pricing ) | !,
- ;
- for ( qw( setup bill susp expire cancel ) ) {
- print "", ( $package->getfield($_)
- ? time2str("%D", $package->getfield($_) )
- : ' '
- ), ' | ',
- ;
+my $packages = get_packages($cust_main, $conf);
+
+if ( @$packages ) {
+%>
+
+
+ Package |
+ Status |
+ Services |
+
+<%
+foreach my $pkg (sort pkgsort_pkgnum_cancel @$packages) {
+ my $rowspan = 0;
+
+ if ($pkg->{cancel}) {
+ $rowspan = 0;
+ } else {
+ foreach my $svcpart (@{$pkg->{svcparts}}) {
+ $rowspan += $svcpart->{count};
+ $rowspan++ if ($svcpart->{count} < $svcpart->{quantity});
+ }
+ }
+
+%>
+
+
+ >
+ <%=$pkg->{pkgnum}%>:
+ <%=$pkg->{pkg}%> - <%=$pkg->{comment}%>
+<% unless ($pkg->{cancel}) { %>
+ ( <%=pkg_change_link($pkg)%> )
+ ( <%=pkg_dates_link($pkg)%> | <%=pkg_customize_link($pkg,$custnum)%> )
+<% } %>
+ |
+<%
+ #foreach (qw(setup last_bill next_bill susp expire cancel)) {
+ # print qq! ! . pkg_datestr($pkg,$_,$conf) . qq! | \n!;
+ #}
+ print "". &itable('');
+
+ sub freq {
+
+ #false laziness w/edit/part_pkg.cgi
+ my %freq = ( #move this
+ '1d' => 'daily',
+ '1w' => 'weekly',
+ '2w' => 'biweekly (every 2 weeks)',
+ '1' => 'monthly',
+ '2' => 'bimonthly (every 2 months)',
+ '3' => 'quarterly (every 3 months)',
+ '6' => 'semiannually (every 6 months)',
+ '12' => 'annually',
+ '24' => 'biannually (every 2 years)',
+ );
+
+ my $freq = shift;
+ exists $freq{$freq} ? $freq{$freq} : "every $freq months";
}
- my $n2 = '';
- foreach my $cust_svc ( @cust_svc ) {
- my($label, $value, $svcdb) = $cust_svc->label;
- my($svcnum) = $cust_svc->svcnum;
- my($sview) = popurl(2). "view";
- print $n2,qq! | $label | !,
- qq!$value | !;
- $n2="
";
+ #eomove
+
+ if ( $pkg->{cancel} ) { #status: cancelled
+
+ print '
Cancelled | '.
+ ''. pkg_datestr($pkg,'cancel',$conf). ' |
';
+ unless ( $pkg->{setup} ) {
+ print 'Never billed |
';
+ } else {
+ print "Setup | ".
+ pkg_datestr($pkg, 'setup',$conf). ' |
';
+ print "Last bill | ".
+ pkg_datestr($pkg, 'last_bill',$conf). ' |
'
+ if $pkg->{'last_bill'};
+ print "Suspended | ".
+ pkg_datestr($pkg, 'susp',$conf). ' |
'
+ if $pkg->{'susp'};
+ }
+
+ } else {
+
+ if ( $pkg->{susp} ) { #status: suspended
+ print 'Suspended | '.
+ ''. pkg_datestr($pkg,'susp',$conf). ' |
';
+ unless ( $pkg->{setup} ) {
+ print 'Never billed |
';
+ } else {
+ print "Setup | ".
+ pkg_datestr($pkg, 'setup',$conf). ' |
';
+ }
+ print "Last bill | ".
+ pkg_datestr($pkg, 'last_bill',$conf). ' |
'
+ if $pkg->{'last_bill'};
+ # next bill ??
+ print "Expires | ".
+ pkg_datestr($pkg, 'expire',$conf). ' |
'
+ if $pkg->{'expire'};
+ print '( '. pkg_unsuspend_link($pkg).
+ ' | '. pkg_cancel_link($pkg). ' ) |
';
+
+ } else { #status: active
+
+ unless ( $pkg->{setup} ) { #not setup
+
+ print 'Not yet billed (';
+ unless ( $pkg->{freq} ) {
+ print 'one-time charge) |
';
+ print '( '. pkg_cancel_link($pkg).
+ ' ) | ';
+ } else {
+ print 'billed '. freq($pkg->{freq}). ')
';
+ }
+
+ } else { #setup
+
+ unless ( $pkg->{freq} ) {
+ print "One-time charge |
".
+ 'Billed | '.
+ pkg_datestr($pkg,'setup',$conf). ' |
';
+ } else {
+ print 'Active'.
+ ', billed '. freq($pkg->{freq}). ' |
'.
+ 'Setup | '.
+ pkg_datestr($pkg, 'setup',$conf). ' |
';
+ }
+
+ }
+
+ print "Last bill | ".
+ pkg_datestr($pkg, 'last_bill',$conf). ' |
'
+ if $pkg->{'last_bill'};
+ print "Next bill | ".
+ pkg_datestr($pkg, 'next_bill',$conf). ' |
'
+ if $pkg->{'next_bill'};
+ print "Expires | ".
+ pkg_datestr($pkg, 'expire',$conf). ' |
'
+ if $pkg->{'expire'};
+ if ( $pkg->{freq} ) {
+ print '( '. pkg_suspend_link($pkg).
+ ' | '. pkg_cancel_link($pkg). ' ) |
';
+ }
+
+ }
+
}
- $n1="";
-}
-print "
";
-#formatting
-print "
";
+ print "\n";
-#formatting
-print qq!
Payment History!,
- qq!!,
- qq! ( Click on invoice to view invoice/enter payment. | !,
- qq!!,
- qq!Post credit / refund )!;
+ if ($rowspan == 0) { print qq!
\n!; next; }
+
+ my $cnt = 0;
+ foreach my $svcpart (sort {$a->{svcpart} <=> $b->{svcpart}} @{$pkg->{svcparts}}) {
+ foreach my $service (@{$svcpart->{services}}) {
+ print '' if ($cnt > 0);
+%>
+ <%=svc_link($svcpart,$service)%> |
+ <%=svc_label_link($svcpart,$service)%> ( <%=svc_unprovision_link($service)%> ) |
+
+<%
+ $cnt++;
+ }
+ if ($svcpart->{count} < $svcpart->{quantity}) {
+ print qq!\n! if ($cnt > 0);
+ print qq! !.svc_provision_link($pkg, $svcpart, $conf).qq! | \n
\n!;
+ }
+ }
+}
+print '';
+}
-#get payment history
+#end display packages
+%>
+
+
+
+<% if ( $conf->config('payby-default') ne 'HIDE' ) { %>
+
+
Payment History
+ Post cash/check payment
+ | Process credit card payment
+ | Process electronic check (ACH) payment
+
Post credit
+
+
+ <%
+ #get payment history
+ my @history = ();
+
+ #invoices
+ foreach my $cust_bill ($cust_main->cust_bill) {
+ my $pre = ( $cust_bill->owed > 0 )
+ ? 'Open '
+ : '';
+ my $post = ( $cust_bill->owed > 0 ) ? '' : '';
+ my $invnum = $cust_bill->invnum;
+ push @history, {
+ 'date' => $cust_bill->_date,
+ 'desc' => qq!!. $pre.
+ "Invoice #$invnum (Balance \$". $cust_bill->owed. ')'.
+ $post. '',
+ 'charge' => $cust_bill->charged,
+ };
+ }
+
+ #payments (some false laziness w/credits)
+ foreach my $cust_pay ($cust_main->cust_pay) {
+
+ my $payby = $cust_pay->payby;
+ my $payinfo = $payby eq 'CARD'
+ ? $cust_pay->payinfo_masked
+ : $cust_pay->payinfo;
+ my @cust_bill_pay = $cust_pay->cust_bill_pay;
+
+ my $target = "$payby$payinfo";
+ $payby =~ s/^BILL$/Check #/ if $payinfo;
+ $payby =~ s/^BILL$//;
+ $payby =~ s/^(CARD|COMP)$/$1 /;
+ my $info = $payby ? " ($payby$payinfo)" : '';
+
+ my( $pre, $post, $desc, $apply, $ext ) = ( '', '', '', '', '' );
+ if ( scalar(@cust_bill_pay) == 0 ) {
+ #completely unapplied
+ $pre = 'Unapplied ';
+ $post = '';
+ $apply = qq! (apply)';
+ } elsif ( scalar(@cust_bill_pay) == 1 && $cust_pay->unapplied == 0 ) {
+ #applied to one invoice
+ $desc = ' applied to Invoice #'. $cust_bill_pay[0]->invnum;
+ } else {
+ #complicated
+ $desc = '
';
+ foreach my $cust_bill_pay (@cust_bill_pay) {
+ $desc .= ' '.
+ '$'. $cust_bill_pay->amount.
+ ' applied to Invoice #'. $cust_bill_pay->invnum.
+ '
';
+ #' on '. time2str("%D", $cust_bill_pay->_date).
+
+ }
+ if ( $cust_pay->unapplied > 0 ) {
+ $desc .= ' '.
+ '$'.
+ $cust_pay->unapplied. ' unapplied'.
+ qq! (apply)'.
+ '
';
+ }
+ }
+
+ my $delete = '';
+ if ( $cust_pay->closed !~ /^Y/i && $conf->exists('deletepayments') ) {
+ $delete = qq! (delete)!;
+ }
+
+ my $unapply = '';
+ if ( $cust_pay->closed !~ /^Y/i
+ && $conf->exists('unapplypayments')
+ && scalar(@cust_bill_pay) ) {
+ $unapply = qq! (unapply)!;
+ }
+
+ push @history, {
+ 'date' => $cust_pay->_date,
+ 'desc' => $pre. "Payment$post$info$desc".
+ "$apply$delete$unapply",
+ 'payment' => $cust_pay->paid,
+ 'target' => $target,
+ };
+ }
+
+ #credits (some false laziness w/payments)
+ foreach my $cust_credit ($cust_main->cust_credit) {
+
+ 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 = 'Unapplied ';
+ $post = '';
+ $apply = qq! (apply)';
+ } elsif ( scalar(@cust_credit_bill) == 1
+ && scalar(@cust_credit_refund) == 0
+ && $cust_credit->credited == 0 ) {
+ #applied to one invoice
+ $desc = ' applied to Invoice #'. $cust_credit_bill[0]->invnum;
+ } 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 = '
';
+ foreach my $app ( sort { $a->_date <=> $b->_date }
+ ( @cust_credit_bill, @cust_credit_refund ) ) {
+ if ( $app->isa('FS::cust_credit_bill') ) {
+ $desc .= ' '.
+ '$'. $app->amount.
+ ' applied to Invoice #'. $app->invnum.
+ '
';
+ #' on '. time2str("%D", $app->_date).
+ } elsif ( $app->isa('FS::cust_credit_refund') ) {
+ $desc .= ' '.
+ '$'. $app->amount.
+ ' refunded on'. time2str("%D", $app->_date).
+ '
';
+ } else {
+ die "$app is not a FS::cust_credit_bill or a FS::cust_credit_refund";
+ }
+ }
+ if ( $cust_credit->credited > 0 ) {
+ $desc .= ' $'.
+ $cust_credit->credited. ' unapplied'.
+ qq! (apply)'.
+ '
';
+ }
+ }
#
-# major problem: this whole thing is way too sloppy.
-# minor problem: the description lines need better formatting.
-
-@history = (); #needed for mod_perl :)
-
-@bills = qsearch('cust_bill',{'custnum'=>$custnum});
-foreach $bill (@bills) {
- my($bref)=$bill->hashref;
- push @history,
- $bref->{_date} . qq!\tInvoice #! . $bref->{invnum} .
- qq! (Balance \$! . $bill->owed . qq!)\t! .
- $bref->{charged} . qq!\t\t\t!;
-
- my(@payments)=qsearch('cust_pay',{'invnum'=> $bref->{invnum} } );
- my($payment);
- foreach $payment (@payments) {
- my($date,$invnum,$payby,$payinfo,$paid)=($payment->getfield('_date'),
- $payment->getfield('invnum'),
- $payment->getfield('payby'),
- $payment->getfield('payinfo'),
- $payment->getfield('paid'),
- );
- push @history,
- "$date\tPayment, Invoice #$invnum ($payby $payinfo)\t\t$paid\t\t";
+ my $delete = '';
+ if ( $cust_credit->closed !~ /^Y/i && $conf->exists('deletecredits') ) {
+ $delete = qq! (delete)!;
+ }
+
+ my $unapply = '';
+ if ( $cust_credit->closed !~ /^Y/i
+ && $conf->exists('unapplycredits')
+ && scalar(@cust_credit_bill) ) {
+ $unapply = qq! (unapply)!;
+ }
+
+ push @history, {
+ 'date' => $cust_credit->_date,
+ 'desc' => $pre. "Credit$post by ". $cust_credit->otaker.
+ ' ('. $cust_credit->reason. ')'.
+ "$desc$apply$delete$unapply",
+ 'credit' => $cust_credit->amount,
+ };
+
+ }
+
+ #refunds
+ foreach my $cust_refund ($cust_main->cust_refund) {
+
+ my $payby = $cust_refund->payby;
+ my $payinfo = $payby eq 'CARD'
+ ? $cust_refund->payinfo_masked
+ : $cust_refund->payinfo;
+
+ $payby =~ s/^BILL$/Check #/ if $payinfo;
+ $payby =~ s/^(CARD|COMP)$/$1 /;
+
+ push @history, {
+ 'date' => $cust_refund->_date,
+ 'desc' => "Refund ($payby$payinfo) by ". $cust_refund->otaker,
+ 'refund' => $cust_refund->refund,
+ };
+
+ }
+
+ %>
+
+ <%= table() %>
+
+ Date |
+ Description |
+ Charge |
+ Payment |
+ In-house Credit |
+ Refund |
+ Balance |
+
+
+ <%
+ #display payment history
+
+ my %target;
+ my $balance = 0;
+ foreach my $item ( sort { $a->{'date'} <=> $b->{'date'} } @history ) {
+
+ my $charge = exists($item->{'charge'})
+ ? sprintf('$%.2f', $item->{'charge'})
+ : '';
+ my $payment = exists($item->{'payment'})
+ ? sprintf('- $%.2f', $item->{'payment'})
+ : '';
+ my $credit = exists($item->{'credit'})
+ ? sprintf('- $%.2f', $item->{'credit'})
+ : '';
+ my $refund = exists($item->{'refund'})
+ ? sprintf('$%.2f', $item->{'refund'})
+ : '';
+
+ my $target = exists($item->{'target'}) ? $item->{'target'} : '';
+
+ $balance += $item->{'charge'} if exists $item->{'charge'};
+ $balance -= $item->{'payment'} if exists $item->{'payment'};
+ $balance -= $item->{'credit'} if exists $item->{'credit'};
+ $balance += $item->{'refund'} if exists $item->{'refund'};
+ $balance = sprintf("%.2f", $balance);
+ $balance =~ s/^\-0\.00$/0.00/; #yay ieee fp
+ ( my $showbalance = '$'. $balance ) =~ s/^\$\-/- \$/;
+
+ %>
+
+
+
+ <% unless ( !$target || $target{$target}++ ) { %>
+
+ <% } %>
+ <%= time2str("%D",$item->{'date'}) %>
+ <% if ( $target && $target{$target} == 1 ) { %>
+
+ <% } %>
+
+ |
+ <%= $item->{'desc'} %> |
+ <%= $charge %> |
+ <%= $payment %> |
+ <%= $credit %> |
+ <%= $refund %> |
+ <%= $showbalance %> |
+
+
+ <% } %>
+
+
+
+<% } %>
+
+