From 9f1847644038cdf5ca947a47cc3d4202eda0ac22 Mon Sep 17 00:00:00 2001 From: Christopher Burger Date: Wed, 7 Nov 2018 11:32:05 -0500 Subject: [PATCH] RT# 74435 - fixed so payment would auto unapply when doing a batch refund --- httemplate/edit/cust_refund.cgi | 9 ++++++++- httemplate/edit/process/cust_refund.cgi | 26 ++++++++++++++++++++------ 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/httemplate/edit/cust_refund.cgi b/httemplate/edit/cust_refund.cgi index f3dec98e1..149213991 100755 --- a/httemplate/edit/cust_refund.cgi +++ b/httemplate/edit/cust_refund.cgi @@ -46,6 +46,7 @@ Method<% $payby %> # <% $paymask %> + % unless ( $paydate || $cust_pay->payby ne 'CARD' ) { # possibly other reasons: i.e. card has since expired @@ -221,7 +222,7 @@ my $batch = $cgi->param('batch'); die "access denied" unless $FS::CurrentUser::CurrentUser->refund_access_right($payby); -my( $paynum, $cust_pay, $batchnum ) = ( '', '', '' ); +my( $paynum, $cust_pay, $batchnum, $cust_payby ) = ( '', '', '', '' ); if ( $cgi->param('paynum') =~ /^(\d+)$/ ) { $paynum = $1; $cust_pay = qsearchs('cust_pay', { paynum=>$paynum } ) @@ -234,6 +235,12 @@ if ( $cgi->param('paynum') =~ /^(\d+)$/ ) { } else { $custnum = $cust_pay->custnum; } + # get custpayby + die "Can not find payby record!" + unless $cust_payby = qsearchs( + 'cust_payby', { paymask => $cust_pay->paymask, custnum => $custnum } + ); + } die "no custnum or paynum specified!" unless $custnum; diff --git a/httemplate/edit/process/cust_refund.cgi b/httemplate/edit/process/cust_refund.cgi index 1f96456e0..9175eb136 100755 --- a/httemplate/edit/process/cust_refund.cgi +++ b/httemplate/edit/process/cust_refund.cgi @@ -55,6 +55,17 @@ if ( $error ) { my( $cust_pay, $cust_payby, $payinfo, $paycvv, $month, $year, $payname ); my $paymask = ''; + +## get cust pay info if paynum exists +if ( $cgi->param('paynum') > 0) { + $cust_pay = qsearchs({ + 'table' => 'cust_pay', + 'hashref' => { 'paynum' => $cgi->param('paynum') }, + 'select' => 'cust_pay.*, cust_pay_batch.payname ', + 'addl_from' => "left join cust_pay_batch on cust_pay_batch.batchnum = cust_pay.batchnum and cust_pay_batch.custnum = $custnum ", + }); +} + if ( (my $custpaybynum = scalar($cgi->param('custpaybynum'))) > 0 ) { ## @@ -75,12 +86,6 @@ if ( (my $custpaybynum = scalar($cgi->param('custpaybynum'))) > 0 ) { } elsif ( $cgi->param('paynum') > 0) { - $cust_pay = qsearchs({ - 'table' => 'cust_pay', - 'hashref' => { 'paynum' => $cgi->param('paynum') }, - 'select' => 'cust_pay.*, cust_pay_batch.payname ', - 'addl_from' => "left join cust_pay_batch on cust_pay_batch.batchnum = cust_pay.batchnum and cust_pay_batch.custnum = $custnum ", - }); $payinfo = $cust_pay->payinfo; $payname = $cust_pay->payname; @@ -228,6 +233,15 @@ if ( (my $custpaybynum = scalar($cgi->param('custpaybynum'))) > 0 ) { $_, scalar($cgi->param($_)) } fields('cust_refund'); + ## unapply payment before creating refund. + while ( $cust_pay && $cust_pay->unapplied < $refund ) { + my @cust_bill_pay = $cust_pay->cust_bill_pay; + last unless @cust_bill_pay; + my $cust_bill_pay = pop @cust_bill_pay; + my $error = $cust_bill_pay->delete; + last if $error; + } + my $new = new FS::cust_refund ( { 'paynum' => $paynum, %hash, } ); -- 2.11.0