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>.
34 use C4::Suggestions qw( SearchSuggestion );
37 use Koha::Patron::Categories;
41 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
42 { template_name => "members/deletemem.tt",
45 flagsrequired => { borrowers => 'delete_borrowers' },
50 #print $input->header;
51 my $member = $input->param('member');
53 #Do not delete yourself...
54 if ( $loggedinuser == $member ) {
55 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE_YOURSELF");
56 exit 0; # Exit without error
59 my $logged_in_user = Koha::Patrons->find( $loggedinuser );
60 my $patron = Koha::Patrons->find( $member );
61 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
63 my $debits = $patron->account->outstanding_debits->total_outstanding;
64 my $credits = abs $patron->account->outstanding_credits->total_outstanding;
65 my $countissues = $patron->checkouts->count;
66 my $userenv = C4::Context->userenv;
68 if ($patron->category->category_type eq "S") {
69 unless(C4::Auth::haspermission($userenv->{'id'},{'staffaccess'=>1})) {
70 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE_STAFF");
71 exit 0; # Exit without error
74 unless(C4::Auth::haspermission($userenv->{'id'},{'borrowers'=>'delete_borrowers'})) {
75 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE");
76 exit 0; # Exit without error
80 if (C4::Context->preference("IndependentBranches")) {
81 my $userenv = C4::Context->userenv;
82 if ( !C4::Context->IsSuperLibrarian() && $patron->branchcode){
83 unless ($userenv->{branch} eq $patron->branchcode){
84 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE_OTHERLIBRARY");
85 exit 0; # Exit without error
90 if ( my $anonymous_patron = C4::Context->preference("AnonymousPatron") ) {
91 if ( $patron->id eq $anonymous_patron ) {
92 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE_ANONYMOUS_PATRON");
93 exit 0; # Exit without error
97 my $op = $input->param('op') || 'delete_confirm';
98 my $dbh = C4::Context->dbh;
99 my $is_guarantor = $patron->guarantee_relationships->count;
100 my $countholds = $dbh->selectrow_array("SELECT COUNT(*) FROM reserves WHERE borrowernumber=?", undef, $member);
102 # Add warning if patron has pending suggestions
104 pending_suggestions => scalar @{
105 C4::Suggestions::SearchSuggestion(
106 { suggestedby => $member, STATUS => 'ASKED' }
113 ItemsOnIssues => $countissues,
116 is_guarantor => $is_guarantor,
117 ItemsOnHold => $countholds,
120 if ( $op eq 'delete_confirm' or $countissues > 0 or $debits or $is_guarantor ) {
122 op => 'delete_confirm',
123 csrf_token => Koha::Token->new->generate_csrf({ session_id => scalar $input->cookie('CGISESSID') }),
126 } elsif ( $op eq 'delete_confirmed' ) {
127 output_and_exit( $input, $cookie, $template, 'wrong_csrf_token' )
128 unless Koha::Token->new->check_csrf( {
129 session_id => $input->cookie('CGISESSID'),
130 token => scalar $input->param('csrf_token'),
132 my $patron = Koha::Patrons->find( $member );
133 $patron->move_to_deleted;
136 print $input->redirect("/cgi-bin/koha/members/members-home.pl");
138 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE_ANONYMOUS_PATRON");
140 # TODO Tell the user everything went ok
141 exit 0; # Exit without error
144 output_html_with_http_headers $input, $cookie, $template->output;