use CGI qw ( -utf8 );
-use Try::Tiny;
+use Try::Tiny qw( catch try );
use C4::Context;
-use C4::Output;
-use C4::Auth;
-use C4::Members;
-use C4::Suggestions qw( SearchSuggestion );
+use C4::Output qw( output_and_exit_if_error output_and_exit output_html_with_http_headers );
+use C4::Auth qw( get_template_and_user );
use Koha::Patrons;
use Koha::Token;
use Koha::Patron::Categories;
+use Koha::Suggestions;
my $input = CGI->new;
query => $input,
type => "intranet",
flagsrequired => { borrowers => 'delete_borrowers' },
- debug => 1,
}
);
# Add warning if patron has pending suggestions
$template->param(
- pending_suggestions => scalar @{
- C4::Suggestions::SearchSuggestion(
- { suggestedby => $member, STATUS => 'ASKED' }
- )
- }
+ pending_suggestions => Koha::Suggestions->search({ suggestedby => $member, STATUS => 'ASKED' })->count,
);
$template->param(
session_id => $input->cookie('CGISESSID'),
token => scalar $input->param('csrf_token'),
});
+
my $patron = Koha::Patrons->find( $member );
- $patron->move_to_deleted;
+
try {
- $patron->delete;
- print $input->redirect("/cgi-bin/koha/members/members-home.pl");
- } catch {
- print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE_ANONYMOUS_PATRON");
+ my $schema = Koha::Database->new->schema;
+ $schema->txn_do(
+ sub {
+ $patron->move_to_deleted;
+ $patron->delete;
+ print $input->redirect( "/cgi-bin/koha/members/members-home.pl" );
+ }
+ );
+ }
+ catch {
+ if ( $_->isa('Koha::Exceptions::Patron::FailedDeleteAnonymousPatron') ) {
+ print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE_ANONYMOUS_PATRON");
+ }
+ else {
+ $_->rethrow;
+ }
};
# TODO Tell the user everything went ok
exit 0; # Exit without error