From 561c7d72fd5b3bfc0c801ba17694007a7df8a7ca Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Mon, 17 Dec 2018 07:25:25 +0000 Subject: [PATCH] Bug 21756: Update members/manivoice to use add_debit https://bugs.koha-community.org/show_bug.cgi?id=21756 --- members/maninvoice.pl | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/members/maninvoice.pl b/members/maninvoice.pl index 2d22666bd5..d5e24497ff 100755 --- a/members/maninvoice.pl +++ b/members/maninvoice.pl @@ -38,6 +38,8 @@ use Koha::Patrons; use Koha::Patron::Categories; +use Try::Tiny; + my $input=new CGI; my $flagsrequired = { borrowers => 'edit_borrowers' }; my ( $template, $loggedinuser, $cookie ) = get_template_and_user( @@ -78,23 +80,33 @@ if ($add){ my $amount=$input->param('amount'); my $type=$input->param('type'); my $note = $input->param('note'); - my $error = manualinvoice( $borrowernumber, $itemnum, $desc, $type, $amount, $note ); - if ($error) { - if ( $error =~ /FOREIGN KEY/ && $error =~ /itemnumber/ ) { - $template->param( 'ITEMNUMBER' => 1 ); - } - $template->param( csrf_token => Koha::Token->new->generate_csrf({ session_id => scalar $input->cookie('CGISESSID') }) ); - $template->param( 'ERROR' => $error ); - output_html_with_http_headers $input, $cookie, $template->output; - } else { + try { + my $account = $patron->account; + my $invoice = $account->add_debit( + { + amount => $amount, + item_id => $itemnum, + description => $desc, + type => $type, + note => $note + } + ); if ( C4::Context->preference('AccountAutoReconcile') ) { $patron->account->reconcile_balance; } - - print $input->redirect("/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber"); - exit; } + catch { + if ( $_->isa('Koha::Exceptions::Object::FKConstraint') $error =~ /FOREIGN KEY/ && $error =~ /itemnumber/ ) { + $template->param( 'ITEMNUMBER' => 1 ) if ($_->broken_fk eq 'itemnumber'); + $template->param( 'ERROR' => $_->message ); + } + $template->param( csrf_token => Koha::Token->new->generate_csrf({ session_id => scalar $input->cookie('CGISESSID') }) ); + output_html_with_http_headers $input, $cookie, $template->output; + }; + + print $input->redirect("/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber"); + exit; } } else { -- 2.20.1