3 #script to delete items
7 # Copyright 2000-2002 Katipo Communications
9 # This file is part of Koha.
11 # Koha is free software; you can redistribute it and/or modify it
12 # under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 3 of the License, or
14 # (at your option) any later version.
16 # Koha is distributed in the hope that it will be useful, but
17 # WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License
22 # along with Koha; if not, see <http://www.gnu.org/licenses>.
31 use C4::Suggestions qw( SearchSuggestion );
34 use Koha::Patron::Categories;
38 my ($template, $loggedinuser, $cookie)
39 = get_template_and_user({template_name => "members/deletemem.tt",
42 flagsrequired => {borrowers => 'edit_borrowers'},
46 #print $input->header;
47 my $member = $input->param('member');
49 #Do not delete yourself...
50 if ( $loggedinuser == $member ) {
51 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE_YOURSELF");
52 exit 0; # Exit without error
55 my $logged_in_user = Koha::Patrons->find( $loggedinuser );
56 my $patron = Koha::Patrons->find( $member );
57 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
59 my $debits = $patron->account->outstanding_debits->total_outstanding;
60 my $credits = abs $patron->account->outstanding_credits->total_outstanding;
61 my $countissues = $patron->checkouts->count;
62 my $userenv = C4::Context->userenv;
64 if ($patron->category->category_type eq "S") {
65 unless(C4::Auth::haspermission($userenv->{'id'},{'staffaccess'=>1})) {
66 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE_STAFF");
67 exit 0; # Exit without error
70 unless(C4::Auth::haspermission($userenv->{'id'},{'borrowers'=>'edit_borrowers'})) {
71 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE");
72 exit 0; # Exit without error
76 if (C4::Context->preference("IndependentBranches")) {
77 my $userenv = C4::Context->userenv;
78 if ( !C4::Context->IsSuperLibrarian() && $patron->branchcode){
79 unless ($userenv->{branch} eq $patron->branchcode){
80 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE_OTHERLIBRARY");
81 exit 0; # Exit without error
86 if ( my $anonymous_patron = C4::Context->preference("AnonymousPatron") ) {
87 if ( $patron->id eq $anonymous_patron ) {
88 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE_ANONYMOUS_PATRON");
89 exit 0; # Exit without error
93 my $op = $input->param('op') || 'delete_confirm';
94 my $dbh = C4::Context->dbh;
95 my $is_guarantor = $patron->guarantee_relationships->count;
96 my $countholds = $dbh->selectrow_array("SELECT COUNT(*) FROM reserves WHERE borrowernumber=?", undef, $member);
98 # Add warning if patron has pending suggestions
100 pending_suggestions => scalar @{
101 C4::Suggestions::SearchSuggestion(
102 { suggestedby => $member, STATUS => 'ASKED' }
109 ItemsOnIssues => $countissues,
112 is_guarantor => $is_guarantor,
113 ItemsOnHold => $countholds,
116 if ( $op eq 'delete_confirm' or $countissues > 0 or $debits or $is_guarantor ) {
118 op => 'delete_confirm',
119 csrf_token => Koha::Token->new->generate_csrf({ session_id => scalar $input->cookie('CGISESSID') }),
122 } elsif ( $op eq 'delete_confirmed' ) {
123 output_and_exit( $input, $cookie, $template, 'wrong_csrf_token' )
124 unless Koha::Token->new->check_csrf( {
125 session_id => $input->cookie('CGISESSID'),
126 token => scalar $input->param('csrf_token'),
128 my $patron = Koha::Patrons->find( $member );
129 $patron->move_to_deleted;
131 # TODO Tell the user everything went ok
132 print $input->redirect("/cgi-bin/koha/members/members-home.pl");
133 exit 0; # Exit without error
136 output_html_with_http_headers $input, $cookie, $template->output;