From 119a850e1bb4c68b524936327104cda7b7d9463d Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Mon, 17 Dec 2018 07:25:25 +0000 Subject: [PATCH] Bug 22393: Update members/manivoice to use add_debit https://bugs.koha-community.org/show_bug.cgi?id=21756 --- members/maninvoice.pl | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/members/maninvoice.pl b/members/maninvoice.pl index 2d22666bd5..52a8791d9d 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 { + my $account = $patron->account; + try { + $account->add_debit( + { + amount => $amount, + item_id => $itemnum, + description => $desc, + type => $type, + note => $note + } + ); if ( C4::Context->preference('AccountAutoReconcile') ) { - $patron->account->reconcile_balance; + $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